1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23: #include "dbus-internals.h"
24: #include "dbus-protocol.h"
25: #include "dbus-marshal-basic.h"
26: #include "dbus-test.h"
27: #include <stdio.h>
28: #include <stdarg.h>
29: #include <string.h>
30: #include <stdlib.h>
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
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:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192: const char _dbus_no_memory_message[] = "Not enough memory";
193:
194: static dbus_bool_t warn_initted = FALSE;
195: static dbus_bool_t fatal_warnings = FALSE;
196: static dbus_bool_t fatal_warnings_on_check_failed = TRUE;
197:
198: static void
199: init_warnings(void)
200: {
201: if (!warn_initted)
202: {
203: const char *s;
204: s = _dbus_getenv ("DBUS_FATAL_WARNINGS");
205: if (s && *s)
206: {
207: if (*s == '0')
208: {
209: fatal_warnings = FALSE;
210: fatal_warnings_on_check_failed = FALSE;
211: }
212: else if (*s == '1')
213: {
214: fatal_warnings = TRUE;
215: fatal_warnings_on_check_failed = TRUE;
216: }
217: else
218: {
219: fprintf(stderr, "DBUS_FATAL_WARNINGS should be set to 0 or 1 if set, not '%s'",
220: s);
221: }
222: }
223:
224: warn_initted = TRUE;
225: }
226: }
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237: void
238: _dbus_warn (const char *format,
239: ...)
240: {
241: va_list args;
242:
243: if (!warn_initted)
244: init_warnings ();
245:
246: va_start (args, format);
247: vfprintf (stderr, format, args);
248: va_end (args);
249:
250: if (fatal_warnings)
251: {
252: fflush (stderr);
253: _dbus_abort ();
254: }
255: }
256:
257:
258:
259:
260:
261:
262:
263:
264:
265: void
266: _dbus_warn_check_failed(const char *format,
267: ...)
268: {
269: va_list args;
270:
271: if (!warn_initted)
272: init_warnings ();
273:
274: fprintf (stderr, "process %lu: ", _dbus_getpid ());
275:
276: va_start (args, format);
277: vfprintf (stderr, format, args);
278: va_end (args);
279:
280: if (fatal_warnings_on_check_failed)
281: {
282: fflush (stderr);
283: _dbus_abort ();
284: }
285: }
286:
287: #ifdef DBUS_ENABLE_VERBOSE_MODE
288:
289: static dbus_bool_t verbose_initted = FALSE;
290: static dbus_bool_t verbose = TRUE;
291:
292:
293: #define PTHREAD_IN_VERBOSE 0
294: #if PTHREAD_IN_VERBOSE
295: #include <pthread.h>
296: #endif
297:
298: static inline void
299: _dbus_verbose_init (void)
300: {
301: if (!verbose_initted)
302: {
303: const char *p = _dbus_getenv ("DBUS_VERBOSE");
304: verbose = p != NULL && *p == '1';
305: verbose_initted = TRUE;
306: }
307: }
308:
309:
310:
311:
312:
313:
314: dbus_bool_t
315: _dbus_is_verbose_real (void)
316: {
317: _dbus_verbose_init ();
318: return verbose;
319: }
320:
321:
322:
323:
324:
325:
326:
327:
328:
329: void
330: _dbus_verbose_real (const char *format,
331: ...)
332: {
333: va_list args;
334: static dbus_bool_t need_pid = TRUE;
335: int len;
336:
337:
338:
339:
340:
341: if (!_dbus_is_verbose_real())
342: return;
343:
344:
345: if (need_pid)
346: {
347: #if PTHREAD_IN_VERBOSE
348: fprintf (stderr, "%lu: 0x%lx: ", _dbus_getpid (), pthread_self ());
349: #else
350: fprintf (stderr, "%lu: ", _dbus_getpid ());
351: #endif
352: }
353:
354:
355:
356: len = strlen (format);
357: if (format[len-1] == '\n')
358: need_pid = TRUE;
359: else
360: need_pid = FALSE;
361:
362: va_start (args, format);
363: vfprintf (stderr, format, args);
364: va_end (args);
365:
366: fflush (stderr);
367: }
368:
369:
370:
371:
372:
373:
374:
375: void
376: _dbus_verbose_reset_real (void)
377: {
378: verbose_initted = FALSE;
379: }
380:
381: #endif
382:
383:
384:
385:
386:
387:
388:
389:
390:
391: char*
392: _dbus_strdup (const char *str)
393: {
394: size_t len;
395: char *copy;
396:
397: if (str == NULL)
398: return NULL;
399:
400: len = strlen (str);
401:
402: copy = dbus_malloc (len + 1);
403: if (copy == NULL)
404: return NULL;
405:
406: memcpy (copy, str, len + 1);
407:
408: return copy;
409: }
410:
411:
412:
413:
414:
415:
416:
417:
418:
419: void*
420: _dbus_memdup (const void *mem,
421: size_t n_bytes)
422: {
423: void *copy;
424:
425: copy = dbus_malloc (n_bytes);
426: if (copy == NULL)
427: return NULL;
428:
429: memcpy (copy, mem, n_bytes);
430:
431: return copy;
432: }
433:
434:
435:
436:
437:
438:
439:
440:
441:
442: char**
443: _dbus_dup_string_array (const char **array)
444: {
445: int len;
446: int i;
447: char **copy;
448:
449: if (array == NULL)
450: return NULL;
451:
452: for (len = 0; array[len] != NULL; ++len)
453: ;
454:
455: copy = dbus_new0 (char*, len + 1);
456: if (copy == NULL)
457: return NULL;
458:
459: i = 0;
460: while (i < len)
461: {
462: copy[i] = _dbus_strdup (array[i]);
463: if (copy[i] == NULL)
464: {
465: dbus_free_string_array (copy);
466: return NULL;
467: }
468:
469: ++i;
470: }
471:
472: return copy;
473: }
474:
475:
476:
477:
478:
479:
480:
481:
482: dbus_bool_t
483: _dbus_string_array_contains (const char **array,
484: const char *str)
485: {
486: int i;
487:
488: i = 0;
489: while (array[i] != NULL)
490: {
491: if (strcmp (array[i], str) == 0)
492: return TRUE;
493: ++i;
494: }
495:
496: return FALSE;
497: }
498:
499:
500:
501:
502:
503:
504:
505: void
506: _dbus_generate_uuid (DBusGUID *uuid)
507: {
508: long now;
509:
510: _dbus_get_current_time (&now, NULL);
511:
512: uuid->as_uint32s[DBUS_UUID_LENGTH_WORDS - 1] = DBUS_UINT32_TO_BE (now);
513:
514: _dbus_generate_random_bytes_buffer (uuid->as_bytes, DBUS_UUID_LENGTH_BYTES - 4);
515: }
516:
517:
518:
519:
520:
521:
522:
523:
524: dbus_bool_t
525: _dbus_uuid_encode (const DBusGUID *uuid,
526: DBusString *encoded)
527: {
528: DBusString binary;
529: _dbus_string_init_const_len (&binary, uuid->as_bytes, DBUS_UUID_LENGTH_BYTES);
530: return _dbus_string_hex_encode (&binary, 0, encoded, _dbus_string_get_length (encoded));
531: }
532:
533: static dbus_bool_t
534: _dbus_read_uuid_file_without_creating (const DBusString *filename,
535: DBusGUID *uuid,
536: DBusError *error)
537: {
538: DBusString contents;
539: DBusString decoded;
540: int end;
541:
542: _dbus_string_init (&contents);
543: _dbus_string_init (&decoded);
544:
545: if (!_dbus_file_get_contents (&contents, filename, error))
546: goto error;
547:
548: _dbus_string_chop_white (&contents);
549:
550: if (_dbus_string_get_length (&contents) != DBUS_UUID_LENGTH_HEX)
551: {
552: dbus_set_error (error, DBUS_ERROR_INVALID_FILE_CONTENT,
553: "UUID file '%s' should contain a hex string of length %d, not length %d, with no other text",
554: _dbus_string_get_const_data (filename),
555: DBUS_UUID_LENGTH_HEX,
556: _dbus_string_get_length (&contents));
557: goto error;
558: }
559:
560: if (!_dbus_string_hex_decode (&contents, 0, &end, &decoded, 0))
561: {
562: _DBUS_SET_OOM (error);
563: goto error;
564: }
565:
566: if (end == 0)
567: {
568: dbus_set_error (error, DBUS_ERROR_INVALID_FILE_CONTENT,
569: "UUID file '%s' contains invalid hex data",
570: _dbus_string_get_const_data (filename));
571: goto error;
572: }
573:
574: if (_dbus_string_get_length (&decoded) != DBUS_UUID_LENGTH_BYTES)
575: {
576: dbus_set_error (error, DBUS_ERROR_INVALID_FILE_CONTENT,
577: "UUID file '%s' contains %d bytes of hex-encoded data instead of %d",
578: _dbus_string_get_const_data (filename),
579: _dbus_string_get_length (&decoded),
580: DBUS_UUID_LENGTH_BYTES);
581: goto error;
582: }
583:
584: _dbus_string_copy_to_buffer (&decoded, uuid->as_bytes, DBUS_UUID_LENGTH_BYTES);
585:
586: _dbus_string_free (&decoded);
587: _dbus_string_free (&contents);
588:
589: _DBUS_ASSERT_ERROR_IS_CLEAR (error);
590:
591: return TRUE;
592:
593: error:
594: _DBUS_ASSERT_ERROR_IS_SET (error);
595: _dbus_string_free (&contents);
596: _dbus_string_free (&decoded);
597: return FALSE;
598: }
599:
600: static dbus_bool_t
601: _dbus_create_uuid_file_exclusively (const DBusString *filename,
602: DBusGUID *uuid,
603: DBusError *error)
604: {
605: DBusString encoded;
606:
607: _dbus_string_init (&encoded);
608:
609: _dbus_generate_uuid (uuid);
610:
611: if (!_dbus_uuid_encode (uuid, &encoded))
612: {
613: _DBUS_SET_OOM (error);
614: goto error;
615: }
616:
617:
618:
619:
620:
621:
622:
623:
624:
625: if (!_dbus_create_file_exclusively (filename, error))
626: goto error;
627:
628: if (!_dbus_string_append_byte (&encoded, '\n'))
629: {
630: _DBUS_SET_OOM (error);
631: goto error;
632: }
633:
634: if (!_dbus_string_save_to_file (&encoded, filename, error))
635: goto error;
636:
637: if (!_dbus_make_file_world_readable (filename, error))
638: goto error;
639:
640: _dbus_string_free (&encoded);
641:
642: _DBUS_ASSERT_ERROR_IS_CLEAR (error);
643: return TRUE;
644:
645: error:
646: _DBUS_ASSERT_ERROR_IS_SET (error);
647: _dbus_string_free (&encoded);
648: return FALSE;
649: }
650:
651:
652:
653:
654:
655: