D-Bus 1.13.18

dbus-errors.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-errors.c Error reporting
3 *
4 * Copyright (C) 2002, 2004 Red Hat Inc.
5 * Copyright (C) 2003 CodeFactory AB
6 *
7 * Licensed under the Academic Free License version 2.1
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25 #include <config.h>
26 #include "dbus-errors.h"
27 #include "dbus-internals.h"
28 #include "dbus-string.h"
29 #include "dbus-protocol.h"
30 #include <stdarg.h>
31 #include <string.h>
32
65 typedef struct
66 {
67 char *name;
68 char *message;
70 unsigned int const_message : 1;
72 unsigned int dummy2 : 1;
73 unsigned int dummy3 : 1;
74 unsigned int dummy4 : 1;
75 unsigned int dummy5 : 1;
77 void *padding1;
79 } DBusRealError;
80
81 _DBUS_STATIC_ASSERT (sizeof (DBusRealError) == sizeof (DBusError));
82
91 static const char*
92 message_from_error (const char *error)
93 {
94 if (strcmp (error, DBUS_ERROR_FAILED) == 0)
95 return "Unknown error";
96 else if (strcmp (error, DBUS_ERROR_NO_MEMORY) == 0)
97 return "Not enough memory available";
98 else if (strcmp (error, DBUS_ERROR_IO_ERROR) == 0)
99 return "Error reading or writing data";
100 else if (strcmp (error, DBUS_ERROR_BAD_ADDRESS) == 0)
101 return "Could not parse address";
102 else if (strcmp (error, DBUS_ERROR_NOT_SUPPORTED) == 0)
103 return "Feature not supported";
104 else if (strcmp (error, DBUS_ERROR_LIMITS_EXCEEDED) == 0)
105 return "Resource limits exceeded";
106 else if (strcmp (error, DBUS_ERROR_ACCESS_DENIED) == 0)
107 return "Permission denied";
108 else if (strcmp (error, DBUS_ERROR_AUTH_FAILED) == 0)
109 return "Could not authenticate to server";
110 else if (strcmp (error, DBUS_ERROR_NO_SERVER) == 0)
111 return "No server available at address";
112 else if (strcmp (error, DBUS_ERROR_TIMEOUT) == 0)
113 return "Connection timed out";
114 else if (strcmp (error, DBUS_ERROR_NO_NETWORK) == 0)
115 return "Network unavailable";
116 else if (strcmp (error, DBUS_ERROR_ADDRESS_IN_USE) == 0)
117 return "Address already in use";
118 else if (strcmp (error, DBUS_ERROR_DISCONNECTED) == 0)
119 return "Disconnected.";
120 else if (strcmp (error, DBUS_ERROR_INVALID_ARGS) == 0)
121 return "Invalid arguments.";
122 else if (strcmp (error, DBUS_ERROR_NO_REPLY) == 0)
123 return "Did not get a reply message.";
124 else if (strcmp (error, DBUS_ERROR_FILE_NOT_FOUND) == 0)
125 return "File doesn't exist.";
126 else if (strcmp (error, DBUS_ERROR_OBJECT_PATH_IN_USE) == 0)
127 return "Object path already in use";
128 else
129 return error;
130 }
131 /* End of internals */
133
187 void
188 dbus_error_init (DBusError *error)
189 {
190 DBusRealError *real;
191
192 _DBUS_STATIC_ASSERT (sizeof (DBusError) == sizeof (DBusRealError));
193
194 _dbus_return_if_fail (error != NULL);
195
196 real = (DBusRealError *)error;
197
198 real->name = NULL;
199 real->message = NULL;
200
201 real->const_message = TRUE;
202 }
203
210 void
211 dbus_error_free (DBusError *error)
212 {
213 DBusRealError *real;
214
215 _dbus_return_if_fail (error != NULL);
216
217 real = (DBusRealError *)error;
218
219 if (!real->const_message)
220 {
221 dbus_free (real->name);
222 dbus_free (real->message);
223 }
224
225 dbus_error_init (error);
226 }
227
242 void
243 dbus_set_error_const (DBusError *error,
244 const char *name,
245 const char *message)
246 {
247 DBusRealError *real;
248
249 _dbus_return_if_error_is_set (error);
250 _dbus_return_if_fail (name != NULL);
251
252 if (error == NULL)
253 return;
254
255 _dbus_assert (error->name == NULL);
256 _dbus_assert (error->message == NULL);
257
258 if (message == NULL)
259 message = message_from_error (name);
260
261 real = (DBusRealError *)error;
262
263 real->name = (char*) name;
264 real->message = (char *)message;
265 real->const_message = TRUE;
266 }
267
278 void
279 dbus_move_error (DBusError *src,
280 DBusError *dest)
281 {
282 _dbus_return_if_error_is_set (dest);
283
284 if (dest)
285 {
286 dbus_error_free (dest);
287 *dest = *src;
288 dbus_error_init (src);
289 }
290 else
291 dbus_error_free (src);
292 }
293
301 dbus_bool_t
302 dbus_error_has_name (const DBusError *error,
303 const char *name)
304 {
305 _dbus_return_val_if_fail (error != NULL, FALSE);
306 _dbus_return_val_if_fail (name != NULL, FALSE);
307
308 _dbus_assert ((error->name != NULL && error->message != NULL) ||
309 (error->name == NULL && error->message == NULL));
310
311 if (error->name != NULL)
312 {
313 DBusString str1, str2;
314 _dbus_string_init_const (&str1, error->name);
315 _dbus_string_init_const (&str2, name);
316 return _dbus_string_equal (&str1, &str2);
317 }
318 else
319 return FALSE;
320 }
321
328 dbus_bool_t
329 dbus_error_is_set (const DBusError *error)
330 {
331 _dbus_return_val_if_fail (error != NULL, FALSE);
332 _dbus_assert ((error->name != NULL && error->message != NULL) ||
333 (error->name == NULL && error->message == NULL));
334 return error->name != NULL;
335 }
336
353 void
354 dbus_set_error (DBusError *error,
355 const char *name,
356 const char *format,
357 ...)
358 {
359 va_list args;
360
361 if (error == NULL)
362 return;
363
364 /* it's a bug to pile up errors */
365 _dbus_return_if_error_is_set (error);
366 _dbus_return_if_fail (name != NULL);
367
368 va_start (args, format);
369 _dbus_set_error_valist (error, name, format, args);
370 va_end (args);
371 }
372
373 void
374 _dbus_set_error_valist (DBusError *error,
375 const char *name,
376 const char *format,
377 va_list args)
378 {
379 DBusRealError *real;
380 DBusString str;
381
382 _dbus_assert (name != NULL);
383
384 if (error == NULL)
385 return;
386
387 _dbus_assert (error->name == NULL);
388 _dbus_assert (error->message == NULL);
389
390 if (!_dbus_string_init (&str))
391 goto nomem;
392
393 if (format == NULL)
394 {
395 if (!_dbus_string_append (&str,
396 message_from_error (name)))
397 {
398 _dbus_string_free (&str);
399 goto nomem;
400 }
401 }
402 else
403 {
404 if (!_dbus_string_append_printf_valist (&str, format, args))
405 {
406 _dbus_string_free (&str);
407 goto nomem;
408 }
409 }
410
411 real = (DBusRealError *)error;
412
413 if (!_dbus_string_steal_data (&str, &real->message))
414 {
415 _dbus_string_free (&str);
416 goto nomem;
417 }
418 _dbus_string_free (&str);
419
420 real->name = _dbus_strdup (name);
421 if (real->name == NULL)
422 {
423 dbus_free (real->message);
424 real->message = NULL;
425 goto nomem;
426 }
427 real->const_message = FALSE;
428
429 return;
430
431 nomem:
432 _DBUS_SET_OOM (error);
433 }
434 /* End public API */
DBusRealError::name
char * name
error name
Definition: dbus-errors.c:67
DBusRealError
Internals of DBusError.
Definition: dbus-errors.c:66
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:271
dbus_set_error_const
void dbus_set_error_const(DBusError *error, const char *name, const char *message)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:243
DBusRealError::dummy4
unsigned int dummy4
placeholder
Definition: dbus-errors.c:74
DBUS_ERROR_LIMITS_EXCEEDED
#define DBUS_ERROR_LIMITS_EXCEEDED
Some limited resource is exhausted.
Definition: dbus-protocol.h:375
DBUS_ERROR_ADDRESS_IN_USE
#define DBUS_ERROR_ADDRESS_IN_USE
Can't bind a socket since its address is in use (i.e.
Definition: dbus-protocol.h:391
DBusRealError::dummy3
unsigned int dummy3
placeholder
Definition: dbus-errors.c:73
dbus_error_init
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
Definition: dbus-errors.c:188
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:182
_dbus_string_append_printf_valist
dbus_bool_t _dbus_string_append_printf_valist(DBusString *str, const char *format, va_list args)
Appends a printf-style formatted string to the DBusString.
Definition: dbus-string.c:1096
TRUE
#define TRUE
DBUS_ERROR_FILE_NOT_FOUND
#define DBUS_ERROR_FILE_NOT_FOUND
Missing file.
Definition: dbus-protocol.h:397
DBUS_ERROR_AUTH_FAILED
#define DBUS_ERROR_AUTH_FAILED
Authentication didn't work.
Definition: dbus-protocol.h:379
DBUS_ERROR_FAILED
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
Definition: dbus-protocol.h:359
dbus_error_has_name
dbus_bool_t dbus_error_has_name(const DBusError *error, const char *name)
Checks whether the error is set and has the given name.
Definition: dbus-errors.c:302
DBusRealError::dummy5
unsigned int dummy5
placeholder
Definition: dbus-errors.c:75
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:704
DBUS_ERROR_NO_REPLY
#define DBUS_ERROR_NO_REPLY
No reply to a message expecting one, usually means a timeout occurred.
Definition: dbus-protocol.h:367
DBUS_ERROR_BAD_ADDRESS
#define DBUS_ERROR_BAD_ADDRESS
A D-Bus bus address was malformed.
Definition: dbus-protocol.h:371
DBUS_ERROR_DISCONNECTED
#define DBUS_ERROR_DISCONNECTED
The connection is disconnected and you're trying to use it.
Definition: dbus-protocol.h:393
DBusString
Definition: dbus-string.h:43
DBUS_ERROR_OBJECT_PATH_IN_USE
#define DBUS_ERROR_OBJECT_PATH_IN_USE
There's already an object with the requested object path.
Definition: dbus-protocol.h:454
DBusRealError::dummy2
unsigned int dummy2
placeholder
Definition: dbus-errors.c:72
DBusError::name
const char * name
public error name field
Definition: dbus-errors.h:50
FALSE
#define FALSE
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition: dbus-protocol.h:361
dbus_error_is_set
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329
DBUS_ERROR_INVALID_ARGS
#define DBUS_ERROR_INVALID_ARGS
Invalid arguments passed to a method call.
Definition: dbus-protocol.h:395
_dbus_assert
#define _dbus_assert(condition)
Definition: dbus-internals.h:153
dbus_error_free
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
_dbus_strdup
char * _dbus_strdup(const char *str)
Duplicates a string.
Definition: dbus-internals.c:589
DBusError
Object representing an exception.
Definition: dbus-errors.h:49
DBusRealError::const_message
unsigned int const_message
Message is not owned by DBusError.
Definition: dbus-errors.c:70
dbus_set_error
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
_dbus_string_steal_data
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
Definition: dbus-string.c:665
DBUS_ERROR_ACCESS_DENIED
#define DBUS_ERROR_ACCESS_DENIED
Security restrictions don't allow doing what you're trying to do.
Definition: dbus-protocol.h:377
DBUS_ERROR_NO_SERVER
#define DBUS_ERROR_NO_SERVER
Unable to connect to server (probably caused by ECONNREFUSED on a socket).
Definition: dbus-protocol.h:381
DBusError::message
const char * message
public error message field
Definition: dbus-errors.h:51
_dbus_string_init_const
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:197
DBusRealError::padding1
void * padding1
placeholder
Definition: dbus-errors.c:77
DBUS_ERROR_NOT_SUPPORTED
#define DBUS_ERROR_NOT_SUPPORTED
Requested operation isn't supported (like ENOSYS on UNIX).
Definition: dbus-protocol.h:373
_dbus_string_append
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:959
DBusRealError::message
char * message
error message
Definition: dbus-errors.c:68
DBUS_ERROR_NO_NETWORK
#define DBUS_ERROR_NO_NETWORK
No network access (probably ENETUNREACH on a socket).
Definition: dbus-protocol.h:389
DBUS_ERROR_IO_ERROR
#define DBUS_ERROR_IO_ERROR
Something went wrong reading or writing to a socket, for example.
Definition: dbus-protocol.h:369
dbus_move_error
void dbus_move_error(DBusError *src, DBusError *dest)
Moves an error src into dest, freeing src and overwriting dest.
Definition: dbus-errors.c:279
DBUS_ERROR_TIMEOUT
#define DBUS_ERROR_TIMEOUT
Certain timeout errors, possibly ETIMEDOUT on a socket.
Definition: dbus-protocol.h:387
dbus_bool_t
dbus_uint32_t dbus_bool_t
Definition: dbus-types.h:35
NULL
#define NULL
_dbus_string_equal
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
Definition: dbus-string.c:2037