1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-sysdeps.c Wrappers around system/libc features shared between UNIX and Windows (internal to D-Bus implementation)
*
* Copyright (C) 2002, 2003, 2006 Red Hat, Inc.
* Copyright (C) 2003 CodeFactory AB
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "dbus-internals.h"
#include "dbus-sysdeps.h"
#include "dbus-threads.h"
#include "dbus-protocol.h"
#include "dbus-string.h"
#include "dbus-list.h"
/* NOTE: If you include any unix/windows-specific headers here, you are probably doing something
* wrong and should be putting some code in dbus-sysdeps-unix.c or dbus-sysdeps-win.c.
*
* These are the standard ANSI C headers...
*/
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* This is UNIX-specific (on windows it's just in stdlib.h I believe)
* but OK since the same stuff does exist on Windows in stdlib.h
* and covered by a configure check.
*/
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
_DBUS_DEFINE_GLOBAL_LOCK (win_fds);
_DBUS_DEFINE_GLOBAL_LOCK (sid_atom_cache);
_DBUS_DEFINE_GLOBAL_LOCK (system_users);
/**
* @defgroup DBusSysdeps Internal system-dependent API
* @ingroup DBusInternals
* @brief Internal system-dependent API available on UNIX and Windows
*
* The system-dependent API has a dual purpose. First, it encapsulates
* all usage of operating system APIs for ease of auditing and to
* avoid cluttering the rest of the code with bizarre OS quirks and
* headers. Second, it abstracts different operating system APIs for
* portability.
*
* @{
*/
/**
* Aborts the program with SIGABRT (dumping core).
*/
void
_dbus_abort (void)
{
const char *s;
_dbus_print_backtrace ();
s = _dbus_getenv ("DBUS_BLOCK_ON_ABORT");
if (s && *s)
{
/* don't use _dbus_warn here since it can _dbus_abort() */
fprintf (stderr, " Process %lu sleeping for gdb attach\n", _dbus_pid_for_log ());
_dbus_sleep_milliseconds (1000 * 180);
}
abort ();
_dbus_exit (1); /* in case someone manages to ignore SIGABRT ? */
}
/**
* Wrapper for setenv(). If the value is #NULL, unsets
* the environment variable.
*
* There is an unfixable memleak in that it is unsafe to
* free memory malloced for use with setenv. This is because
* we can not rely on internal implementation details of
* the underlying libc library.
*
* @param varname name of environment variable
* @param value value of environment variable
* @returns #TRUE on success.
*/
dbus_bool_t
_dbus_setenv (const char *varname,
const char *value)
{
_dbus_assert (varname != NULL);
if (value == NULL)
{
#ifdef HAVE_UNSETENV
unsetenv (varname);
return TRUE;
#else
char *putenv_value;
size_t len;
len = strlen (varname);
/* Use system malloc to avoid memleaks that dbus_malloc
* will get upset about.
*/
putenv_value = malloc (len + 2);
if (putenv_value == NULL)
return FALSE;
strcpy (putenv_value, varname);
#if defined(DBUS_WIN)
strcat (putenv_value, "=");
#endif
return (putenv (putenv_value) == 0);
#endif
}
else
{
#ifdef HAVE_SETENV
return (setenv (varname, value, TRUE) == 0);
#else
char *putenv_value;
size_t len;
size_t varname_len;
size_t value_len;
varname_len = strlen (varname);
value_len = strlen (value);
len = varname_len + value_len + 1 /* '=' */ ;
/* Use system malloc to avoid memleaks that dbus_malloc
* will get upset about.
*/
putenv_value = malloc (len + 1);
if (putenv_value == NULL)
return FALSE;
strcpy (putenv_value, varname);
strcpy (putenv_value + varname_len, "=");
strcpy (putenv_value + varname_len + 1, value);
return (putenv (putenv_value) == 0);
#endif
}
}
/**
* Wrapper for getenv().
*
* @param varname name of environment variable
* @returns value of environment variable or #NULL if unset
*/
const char*
_dbus_getenv (const char *varname)
{
return getenv (varname);
}
/**
* Wrapper for clearenv().
*
* @returns #TRUE on success.
*/
dbus_bool_t
_dbus_clearenv (void)
{
return (clearenv () == 0);
}
/*
* init a pipe instance.
*
* @param pipe the pipe
* @param fd the file descriptor to init from
*/
void
_dbus_pipe_init (DBusPipe *pipe,
int fd)
{
pipe->fd_or_handle = fd;
}
/**
* init a pipe with stdout
*
* @param pipe the pipe
*/
void
_dbus_pipe_init_stdout (DBusPipe *pipe)
{
_dbus_pipe_init (pipe, 1);
}
/**
* check if a pipe is valid; pipes can be set invalid, similar to
* a -1 file descriptor.
*
* @param pipe the pipe instance
* @returns #FALSE if pipe is not valid
*/
dbus_bool_t
_dbus_pipe_is_valid(DBusPipe *pipe)
{
return pipe->fd_or_handle >= 0;
}
/**
* Check if a pipe is stdout or stderr.
*
* @param pipe the pipe instance
* @returns #TRUE if pipe is one of the standard out/err channels
*/
dbus_bool_t
_dbus_pipe_is_stdout_or_stderr (DBusPipe *pipe)
{
return pipe->fd_or_handle == 1 || pipe->fd_or_handle == 2;
}
/**
* Initializes a pipe to an invalid value.
* @param pipe the pipe
*/
void
_dbus_pipe_invalidate (DBusPipe *pipe)
{
pipe->fd_or_handle = -1;
}
/**
* Split paths into a list of char strings
*
* @param dirs string with pathes
* @param suffix string concated to each path in dirs
* @param dir_list contains a list of splitted pathes
* return #TRUE is pathes could be splittes,#FALSE in oom case
*/
dbus_bool_t
_dbus_split_paths_and_append (DBusString *dirs,
const char *suffix,
DBusList **dir_list)
{
int start;
int i;
int len;
char *cpath;
DBusString file_suffix;
start = 0;
i = 0;
_dbus_string_init_const (&file_suffix, suffix);
len = _dbus_string_get_length (dirs);
while (_dbus_string_find (dirs, start, _DBUS_PATH_SEPARATOR, &i))
{
DBusString path;
if (!_dbus_string_init (&path))
goto oom;
if (!_dbus_string_copy_len (dirs,
start,
i - start,
&path,
0))
{
_dbus_string_free (&path);
goto oom;
}
_dbus_string_chop_white (&path);
/* check for an empty path */
if (_dbus_string_get_length (&path) == 0)
goto next;
if (!_dbus_concat_dir_and_file (&path,
&file_suffix))
{
_dbus_string_free (&path);
goto oom;
}
if (!_dbus_string_copy_data(&path, &cpath))
{
_dbus_string_free (&path);
goto oom;
}
if (!_dbus_list_append (dir_list, cpath))
{
_dbus_string_free (&path);
dbus_free (cpath);
goto oom;
}
next:
_dbus_string_free (&path);
start = i + 1;
}
if (start != len)
{
DBusString path;
if (!_dbus_string_init (&path))
goto oom;
if (!_dbus_string_copy_len (dirs,
start,
len - start,
&path,
0))
{
_dbus_string_free (&path);
goto oom;
}
if (!_dbus_concat_dir_and_file (&path,
&file_suffix))
{
_dbus_string_free (&path);
goto oom;
}
if (!_dbus_string_copy_data(&path, &cpath))
{
_dbus_string_free (&path);
goto oom;
}
if (!_dbus_list_append (dir_list, cpath))
{
_dbus_string_free (&path);
dbus_free (cpath);
goto oom;
}
_dbus_string_free (&path);
}
return TRUE;
oom:
_dbus_list_foreach (dir_list, (DBusForeachFunction)dbus_free, NULL);
_dbus_list_clear (dir_list);
return FALSE;
}
/** @} */
/**
* @addtogroup DBusString
*
* @{
*/
/**
* Appends an integer to a DBusString.
*
* @param str the string
* @param value the integer value
* @returns #FALSE if not enough memory or other failure.
*/
dbus_bool_t
_dbus_string_append_int (DBusString *str,
long value)
{
/* this calculation is from comp.lang.c faq */
#define MAX_LONG_LEN ((sizeof (long) * 8 + 2) / 3 + 1) /* +1 for '-' */
int orig_len;
int i;
char *buf;
orig_len = _dbus_string_get_length (str);
if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
return FALSE;
buf = _dbus_string_get_data_len (str, orig_len, MAX_LONG_LEN);
snprintf (buf, MAX_LONG_LEN, "%ld", value);
i = 0;
while (*buf)
{
++buf;
++i;
}
_dbus_string_shorten (str, MAX_LONG_LEN - i);
return TRUE;
}
/**
* Appends an unsigned integer to a DBusString.
*
* @param str the string
* @param value the integer value
* @returns #FALSE if not enough memory or other failure.
*/
dbus_bool_t
_dbus_string_append_uint (DBusString *str,
unsigned long value)
{
/* this is wrong, but definitely on the high side. */
#define MAX_ULONG_LEN (MAX_LONG_LEN * 2)
int orig_len;
int i;
char *buf;
orig_len = _dbus_string_get_length (str);
if (!_dbus_string_lengthen (str, MAX_ULONG_LEN))
return FALSE;
buf = _dbus_string_get_data_len (str, orig_len, MAX_ULONG_LEN);
snprintf (buf, MAX_ULONG_LEN, "%lu", value);
i = 0;
while (*buf)
{
++buf;
++i;
}
_dbus_string_shorten (str, MAX_ULONG_LEN - i);
return TRUE;
}
#ifdef DBUS_BUILD_TESTS
/**
* Appends a double to a DBusString.
*
* @param str the string
* @param value the floating point value
* @returns #FALSE if not enough memory or other failure.
*/
dbus_bool_t
_dbus_string_append_double (DBusString *str,
double value)
{
#define MAX_DOUBLE_LEN 64 /* this is completely made up :-/ */
int orig_len;
char *buf;
int i;
orig_len = _dbus_string_get_length (str);
if (!_dbus_string_lengthen (str, MAX_DOUBLE_LEN))
return FALSE;
buf = _dbus_string_get_data_len (str, orig_len, MAX_DOUBLE_LEN);
snprintf (buf, MAX_LONG_LEN, "%g", value);
i = 0;
while (*buf)
{
++buf;
++i;
}
_dbus_string_shorten (str, MAX_DOUBLE_LEN - i);
return TRUE;
}
#endif /* DBUS_BUILD_TESTS */
/**
* Parses an integer contained in a DBusString. Either return parameter
* may be #NULL if you aren't interested in it. The integer is parsed
* and stored in value_return. Return parameters are not initialized
* if the function returns #FALSE.
*
* @param str the string
* @param start the byte index of the start of the integer
* @param value_return return location of the integer value or #NULL
* @param end_return return location of the end of the integer, or #NULL
* @returns #TRUE on success
*/
dbus_bool_t
_dbus_string_parse_int (const DBusString *str,
int start,
long *value_return,
int *end_return)
{
long v;
const char *p;
char *end;
p = _dbus_string_get_const_data_len (str, start,
_dbus_string_get_length (str) - start);
end = NULL;
errno = 0;
v = strtol (p, &end, 0);
if (end == NULL || end == p || errno != 0)
return FALSE;
if (value_return)
*value_return = v;
if (end_return)
*end_return = start + (end - p);
return TRUE;
}
/**
* Parses an unsigned integer contained in a DBusString. Either return
* parameter may be #NULL if you aren't interested in it. The integer
* is parsed and stored in value_return. Return parameters are not
* initialized if the function returns #FALSE.
*
* @param str the string
* @param start the byte index of the start of the integer
* @param value_return return location of the integer value or #NULL
* @param end_return return location of the end of the integer, or #NULL
* @returns #TRUE on success
*/
dbus_bool_t
_dbus_string_parse_uint (const DBusString *str,
int start,
unsigned long *value_return,
int *end_return)
{
unsigned long v;
const char *p;
char *end;
p = _dbus_string_get_const_data_len (str, start,
_dbus_string_get_length (str) - start);
end = NULL;
errno = 0;
v = strtoul (p, &end, 0);
if (end == NULL || end == p || errno != 0)
return FALSE;
if (value_return)
*value_return = v;
if (end_return)
*end_return = start + (end - p);
return TRUE;
}
#ifdef DBUS_BUILD_TESTS
static dbus_bool_t
ascii_isspace (char c)
{
return (c == ' ' ||
c == '\f' ||
c == '\n' ||
c == '\r' ||
c == '\t' ||
c == '\v');
}
#endif /* DBUS_BUILD_TESTS */
#ifdef DBUS_BUILD_TESTS
static dbus_bool_t
ascii_isdigit (char c)
{
return c >= '0' && c <= '9';
}
#endif /* DBUS_BUILD_TESTS */
#ifdef DBUS_BUILD_TESTS
static dbus_bool_t
ascii_isxdigit (char c)
{
return (ascii_isdigit (c) ||
(c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F'));
}
#endif /* DBUS_BUILD_TESTS */
#ifdef DBUS_BUILD_TESTS
/* Calls strtod in a locale-independent fashion, by looking at
* the locale data and patching the decimal comma to a point.
*
* Relicensed from glib.
*/
static double
ascii_strtod (const char *nptr,
char **endptr)
{
/* FIXME: The Win32 C library's strtod() doesn't handle hex.
* Presumably many Unixes don't either.
*/
char *fail_pos;
double val;
struct lconv *locale_data;
const char *decimal_point;
int decimal_point_len;
const char *p, *decimal_point_pos;
const char *end = NULL; /* Silence gcc */
fail_pos = NULL;
locale_data = localeconv ();
decimal_point = locale_data->decimal_point;
decimal_point_len = strlen (decimal_point);
_dbus_assert (decimal_point_len != 0);
decimal_point_pos = NULL;
if (decimal_point[0] != '.' ||
decimal_point[1] != 0)
{
p = nptr;
/* Skip leading space */
while (ascii_isspace (*p))
p++;
/* Skip leading optional sign */
if (*p == '+' || *p == '-')
p++;
if (p[0] == '0' &&
(p[1] == 'x' || p[1] == 'X'))
{
p += 2;
/* HEX - find the (optional) decimal point */
while (ascii_isxdigit (*p))
p++;
if (*p == '.')
{
decimal_point_pos = p++;
while (ascii_isxdigit (*p))
p++;
if (*p == 'p' || *p == 'P')
p++;
if (*p == '+' || *p == '-')
p++;
while (ascii_isdigit (*p))
p++;
end = p;
}
}
else
{
while (ascii_isdigit (*p))
p++;
if (*p == '.')
{
decimal_point_pos = p++;
while (ascii_isdigit (*p))
p++;
if (*p == 'e' || *p == 'E')
p++;
if (*p == '+' || *p == '-')
p++;
while (ascii_isdigit (*p))
p++;
end = p;
}
}
/* For the other cases, we need not convert the decimal point */
}
/* Set errno to zero, so that we can distinguish zero results
and underflows */
errno = 0;
if (decimal_point_pos)
{
char *copy, *c;
/* We need to convert the '.' to the locale specific decimal point */
copy = dbus_malloc (end - nptr + 1 + decimal_point_len);
c = copy;
memcpy (c, nptr, decimal_point_pos - nptr);
c += decimal_point_pos - nptr;
memcpy (c, decimal_point, decimal_point_len);
c += decimal_point_len;
memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
c += end - (decimal_point_pos + 1);
*c = 0;
val = strtod (copy, &fail_pos);
if (fail_pos)
{
if (fail_pos > decimal_point_pos)
fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
else
fail_pos = (char *)nptr + (fail_pos - copy);
}
dbus_free (copy);
}
else
val = strtod (nptr, &fail_pos);
if (endptr)
*endptr = fail_pos;
return val;
}
#endif /* DBUS_BUILD_TESTS */
#ifdef DBUS_BUILD_TESTS
/**
* Parses a floating point number contained in a DBusString. Either
* return parameter may be #NULL if you aren't interested in it. The
* integer is parsed and stored in value_return. Return parameters are
* not initialized if the function returns #FALSE.
*
* @param str the string
* @param start the byte index of the start of the float
* @param value_return return location of the float value or #NULL
* @param end_return return location of the end of the float, or #NULL
* @returns #TRUE on success
*/
dbus_bool_t
_dbus_string_parse_double (const DBusString *str,
int start,
double *value_return,
int *end_return)
{
double v;
const char *p;
char *end;
p = _dbus_string_get_const_data_len (str, start,
_dbus_string_get_length (str) - start);
/* parsing hex works on linux but isn't portable, so intercept it
* here to get uniform behavior.
*/
if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
return FALSE;
end = NULL;
errno = 0;
v = ascii_strtod (p, &end);
if (end == NULL || end == p || errno != 0)
return FALSE;
if (value_return)
*value_return = v;
if (end_return)
*end_return = start + (end - p);
return TRUE;
}
#endif /* DBUS_BUILD_TESTS */
/** @} */ /* DBusString group */
/**
* @addtogroup DBusInternalsUtils
* @{
*/
void
_dbus_generate_pseudorandom_bytes_buffer (char *buffer,
int n_bytes)
{
long tv_usec;
int i;
/* fall back to pseudorandom */
_dbus_verbose ("Falling back to pseudorandom for %d bytes\n",
n_bytes);
_dbus_get_current_time (NULL, &tv_usec);
srand (tv_usec);
i = 0;
while (i < n_bytes)
{
double r;
unsigned int b;
r = rand ();
b = (r / (double) RAND_MAX) * 255.0;
buffer[i] = b;
++i;
}
}
/**
* Fills n_bytes of the given buffer with random bytes.
*
* @param buffer an allocated buffer
* @param n_bytes the number of bytes in buffer to write to
*/
void
_dbus_generate_random_bytes_buffer (char *buffer,
int n_bytes)
{
DBusString str;
if (!_dbus_string_init (&str))
{
_dbus_generate_pseudorandom_bytes_buffer (buffer, n_bytes);
return;
}
if (!_dbus_generate_random_bytes (&str, n_bytes))
{
_dbus_string_free (&str);
_dbus_generate_pseudorandom_bytes_buffer (buffer, n_bytes);
return;
}
_dbus_string_copy_to_buffer (&str, buffer, n_bytes);
_dbus_string_free (&str);
}
/**
* Generates the given number of random bytes, where the bytes are
* chosen from the alphanumeric ASCII subset.
*
* @param str the string
* @param n_bytes the number of random ASCII bytes to append to string
* @returns #TRUE on success, #FALSE if no memory or other failure
*/
dbus_bool_t
_dbus_generate_random_ascii (DBusString *str,
int n_bytes)
{
static const char letters[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
int i;
int len;
if (!_dbus_generate_random_bytes (str, n_bytes))
return FALSE;
len = _dbus_string_get_length (str);
i = len - n_bytes;
while (i < len)
{
_dbus_string_set_byte (str, i,
letters[_dbus_string_get_byte (str, i) %
(sizeof (letters) - 1)]);
++i;
}
_dbus_assert (_dbus_string_validate_ascii (str, len - n_bytes,
n_bytes));
return TRUE;
}
/**
* Converts a UNIX or Windows errno
* into a #DBusError name.
*
* @todo should cover more errnos, specifically those
* from open().
*
* @param error_number the errno.
* @returns an error name
*/
const char*
_dbus_error_from_errno (int error_number)
{
switch (error_number)
{
case 0:
return DBUS_ERROR_FAILED;
#ifdef EPROTONOSUPPORT
case EPROTONOSUPPORT:
return DBUS_ERROR_NOT_SUPPORTED;
#endif
#ifdef EAFNOSUPPORT
case EAFNOSUPPORT:
return DBUS_ERROR_NOT_SUPPORTED;
#endif
#ifdef ENFILE
case ENFILE:
return DBUS_ERROR_LIMITS_EXCEEDED; /* kernel out of memory */
#endif
#ifdef EMFILE
case EMFILE:
return DBUS_ERROR_LIMITS_EXCEEDED;
#endif
#ifdef EACCES
case EACCES:
return DBUS_ERROR_ACCESS_DENIED;
#endif
#ifdef EPERM
case EPERM:
return DBUS_ERROR_ACCESS_DENIED;
#endif
#ifdef ENOBUFS
case ENOBUFS:
return DBUS_ERROR_NO_MEMORY;
#endif
#ifdef ENOMEM
case ENOMEM:
return DBUS_ERROR_NO_MEMORY;
#endif
#ifdef EINVAL
case EINVAL:
return DBUS_ERROR_FAILED;
#endif
#ifdef EBADF
case EBADF:
return DBUS_ERROR_FAILED;
#endif
#ifdef EFAULT
case EFAULT:
return DBUS_ERROR_FAILED;
#endif
#ifdef ENOTSOCK
case ENOTSOCK:
return DBUS_ERROR_FAILED;
#endif
#ifdef EISCONN
case EISCONN:
return DBUS_ERROR_FAILED;
#endif
#ifdef ECONNREFUSED
case ECONNREFUSED:
return DBUS_ERROR_NO_SERVER;
#endif
#ifdef ETIMEDOUT
case ETIMEDOUT:
return DBUS_ERROR_TIMEOUT;
#endif
#ifdef ENETUNREACH
case ENETUNREACH:
return DBUS_ERROR_NO_NETWORK;
#endif
#ifdef EADDRINUSE
case EADDRINUSE:
return DBUS_ERROR_ADDRESS_IN_USE;
#endif
#ifdef EEXIST
case EEXIST:
return DBUS_ERROR_FILE_EXISTS;
#endif
#ifdef ENOENT
case ENOENT:
return DBUS_ERROR_FILE_NOT_FOUND;
#endif
}
return DBUS_ERROR_FAILED;
}
/**
* Assign 0 to the global errno variable
*/
void
_dbus_set_errno_to_zero (void)
{
errno = 0;
}
/**
* See if errno is set
* @returns #TRUE if errno is not 0
*/
dbus_bool_t
_dbus_get_is_errno_nonzero (void)
{
return errno != 0;
}
/**
* See if errno is ENOMEM
* @returns #TRUE if errno == ENOMEM
*/
dbus_bool_t
_dbus_get_is_errno_enomem (void)
{
return errno == ENOMEM;
}
/**
* See if errno is EINTR
* @returns #TRUE if errno == EINTR
*/
dbus_bool_t
_dbus_get_is_errno_eintr (void)
{
return errno == EINTR;
}
/**
* Get error message from errno
* @returns _dbus_strerror(errno)
*/
const char*
_dbus_strerror_from_errno (void)
{
return _dbus_strerror (errno);
}
/** @} end of sysdeps */
/* tests in dbus-sysdeps-util.c */
|