1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24: #include "dbus-errors.h"
25: #include "dbus-internals.h"
26: #include "dbus-string.h"
27: #include "dbus-protocol.h"
28: #include <stdarg.h>
29: #include <string.h>
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41: typedef struct
42: {
43: char *name;
44: char *message;
45:
46: unsigned int const_message : 1;
47:
48: unsigned int dummy2 : 1;
49: unsigned int dummy3 : 1;
50: unsigned int dummy4 : 1;
51: unsigned int dummy5 : 1;
52:
53: void *padding1;
54:
55: } DBusRealError;
56:
57:
58:
59:
60:
61:
62:
63:
64:
65: static const char*
66: message_from_error (const char *error)
67: {
68: if (strcmp (error, DBUS_ERROR_FAILED) == 0)
69: return "Unknown error";
70: else if (strcmp (error, DBUS_ERROR_NO_MEMORY) == 0)
71: return "Not enough memory available";
72: else if (strcmp (error, DBUS_ERROR_IO_ERROR) == 0)
73: return "Error reading or writing data";
74: else if (strcmp (error, DBUS_ERROR_BAD_ADDRESS) == 0)
75: return "Could not parse address";
76: else if (strcmp (error, DBUS_ERROR_NOT_SUPPORTED) == 0)
77: return "Feature not supported";
78: else if (strcmp (error, DBUS_ERROR_LIMITS_EXCEEDED) == 0)
79: return "Resource limits exceeded";
80: else if (strcmp (error, DBUS_ERROR_ACCESS_DENIED) == 0)
81: return "Permission denied";
82: else if (strcmp (error, DBUS_ERROR_AUTH_FAILED) == 0)
83: return "Could not authenticate to server";
84: else if (strcmp (error, DBUS_ERROR_NO_SERVER) == 0)
85: return "No server available at address";
86: else if (strcmp (error, DBUS_ERROR_TIMEOUT) == 0)
87: return "Connection timed out";
88: else if (strcmp (error, DBUS_ERROR_NO_NETWORK) == 0)
89: return "Network unavailable";
90: else if (strcmp (error, DBUS_ERROR_ADDRESS_IN_USE) == 0)
91: return "Address already in use";
92: else if (strcmp (error, DBUS_ERROR_DISCONNECTED) == 0)
93: return "Disconnected.";
94: else if (strcmp (error, DBUS_ERROR_INVALID_ARGS) == 0)
95: return "Invalid arguments.";
96: else if (strcmp (error, DBUS_ERROR_NO_REPLY) == 0)
97: return "Did not get a reply message.";
98: else if (strcmp (error, DBUS_ERROR_FILE_NOT_FOUND) == 0)
99: return "File doesn't exist.";
100: else
101: return error;
102: }
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159: void
160: dbus_error_init (DBusError *error)
161: {
162: DBusRealError *real;
163:
164: _dbus_return_if_fail (error != NULL);
165:
166: _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError));
167:
168: real = (DBusRealError *)error;
169:
170: real->name = NULL;
171: real->message = NULL;
172:
173: real->const_message = TRUE;
174: }
175:
176:
177:
178:
179:
180:
181:
182: void
183: dbus_error_free (DBusError *error)
184: {
185: DBusRealError *real;
186:
187: _dbus_return_if_fail (error != NULL);
188:
189: real = (DBusRealError *)error;
190:
191: if (!real->const_message)
192: {
193: dbus_free (real->name);
194: dbus_free (real->message);
195: }
196:
197: dbus_error_init (error);
198: }
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214: void
215: dbus_set_error_const (DBusError *error,
216: const char *name,
217: const char *message)
218: {
219: DBusRealError *real;
220:
221: _dbus_return_if_error_is_set (error);
222: _dbus_return_if_fail (name != NULL);
223:
224: if (error == NULL)
225: return;
226:
227: _dbus_assert (error->name == NULL);
228: _dbus_assert (error->message == NULL);
229:
230: if (message == NULL)
231: message = message_from_error (name);
232:
233: real = (DBusRealError *)error;
234:
235: real->name = (char*) name;
236: real->message = (char *)message;
237: real->const_message = TRUE;
238: }
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250: void
251: dbus_move_error (DBusError *src,
252: DBusError *dest)
253: {
254: _dbus_return_if_error_is_set (dest);
255:
256: if (dest)
257: {
258: dbus_error_free (dest);
259: *dest = *src;
260: dbus_error_init (src);
261: }
262: else
263: dbus_error_free (src);
264: }
265:
266:
267:
268:
269:
270:
271:
272:
273: dbus_bool_t
274: dbus_error_has_name (const DBusError *error,
275: const char *name)
276: {
277: _dbus_return_val_if_fail (error != NULL, FALSE);
278: _dbus_return_val_if_fail (name != NULL, FALSE);
279:
280: _dbus_assert ((error->name != NULL && error->message != NULL) ||
281: (error->name == NULL && error->message == NULL));
282:
283: if (error->name != NULL)
284: {
285: DBusString str1, str2;
286: _dbus_string_init_const (&str1, error->name);
287: _dbus_string_init_const (&str2, name);
288: return _dbus_string_equal (&str1, &str2);
289: }
290: else
291: return FALSE;
292: }
293:
294:
295:
296:
297:
298:
299:
300: dbus_bool_t
301: dbus_error_is_set (const DBusError *error)
302: {
303: _dbus_return_val_if_fail (error != NULL, FALSE);
304: _dbus_assert ((error->name != NULL && error->message != NULL) ||
305: (error->name == NULL && error->message == NULL));
306: return error->name != NULL;
307: }
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325: void
326: dbus_set_error (DBusError *error,
327: const char *name,
328: const char *format,
329: ...)
330: {
331: DBusRealError *real;
332: DBusString str;
333: va_list args;
334:
335: if (error == NULL)
336: return;
337:
338:
339: _dbus_return_if_error_is_set (error);
340: _dbus_return_if_fail (name != NULL);
341:
342: _dbus_assert (error->name == NULL);
343: _dbus_assert (error->message == NULL);
344:
345: if (!_dbus_string_init (&str))
346: goto nomem;
347:
348: if (format == NULL)
349: {
350: if (!_dbus_string_append (&str,
351: message_from_error (name)))
352: {
353: _dbus_string_free (&str);
354: goto nomem;
355: }
356: }
357: else
358: {
359: va_start (args, format);
360: if (!_dbus_string_append_printf_valist (&str, format, args))
361: {
362: _dbus_string_free (&str);
363: goto nomem;
364: }
365: va_end (args);
366: }
367:
368: real = (DBusRealError *)error;
369:
370: if (!_dbus_string_steal_data (&str, &real->message))
371: {
372: _dbus_string_free (&str);
373: goto nomem;
374: }
375: _dbus_string_free (&str);
376:
377: real->name = _dbus_strdup (name);
378: if (real->name == NULL)
379: {
380: dbus_free (real->message);
381: real->message = NULL;
382: goto nomem;
383: }
384: real->const_message = FALSE;
385:
386: return;
387:
388: nomem:
389: _DBUS_SET_OOM (error);
390: }
391:
392: