2006-09-10 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-sysdeps.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-sysdeps.c Wrappers around system/libc features shared between UNIX and Windows (internal to D-Bus implementation)
3  * 
4  * Copyright (C) 2002, 2003, 2006  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24
25 #include "dbus-internals.h"
26 #include "dbus-sysdeps.h"
27 #include "dbus-threads.h"
28 #include "dbus-protocol.h"
29 #include "dbus-string.h"
30
31 /* NOTE: If you include any unix/windows-specific headers here, you are probably doing something
32  * wrong and should be putting some code in dbus-sysdeps-unix.c or dbus-sysdeps-win.c.
33  *
34  * These are the standard ANSI C headers...
35  */
36 #include <locale.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40
41 /* This is UNIX-specific (on windows it's just in stdlib.h I believe)
42  * but OK since the same stuff does exist on Windows in stdlib.h
43  * and covered by a configure check.
44  */
45 #ifdef HAVE_ERRNO_H
46 #include <errno.h>
47 #endif
48
49 _DBUS_DEFINE_GLOBAL_LOCK (win_fds);
50 _DBUS_DEFINE_GLOBAL_LOCK (sid_atom_cache);
51
52 /**
53  * @addtogroup DBusInternalsUtils
54  * @{
55  */
56 #ifndef DBUS_DISABLE_ASSERT
57 /**
58  * Aborts the program with SIGABRT (dumping core).
59  */
60 void
61 _dbus_abort (void)
62 {
63 #ifdef DBUS_ENABLE_VERBOSE_MODE
64   const char *s;
65   s = _dbus_getenv ("DBUS_PRINT_BACKTRACE");
66   if (s && *s)
67     _dbus_print_backtrace ();
68 #endif
69   abort ();
70   _dbus_exit (1); /* in case someone manages to ignore SIGABRT */
71 }
72 #endif
73
74 /**
75  * Wrapper for setenv(). If the value is #NULL, unsets
76  * the environment variable.
77  *
78  * @todo 1.0 if someone can verify it's safe, we could avoid the
79  * memleak when doing an unset.
80  *
81  * @param varname name of environment variable
82  * @param value value of environment variable
83  * @returns #TRUE on success.
84  */
85 dbus_bool_t
86 _dbus_setenv (const char *varname,
87               const char *value)
88 {
89   _dbus_assert (varname != NULL);
90   
91   if (value == NULL)
92     {
93 #ifdef HAVE_UNSETENV
94       unsetenv (varname);
95       return TRUE;
96 #else
97       char *putenv_value;
98       size_t len;
99
100       len = strlen (varname);
101
102       /* Use system malloc to avoid memleaks that dbus_malloc
103        * will get upset about.
104        */
105       
106       putenv_value = malloc (len + 1);
107       if (putenv_value == NULL)
108         return FALSE;
109
110       strcpy (putenv_value, varname);
111       
112       return (putenv (putenv_value) == 0);
113 #endif
114     }
115   else
116     {
117 #ifdef HAVE_SETENV
118       return (setenv (varname, value, TRUE) == 0);
119 #else
120       char *putenv_value;
121       size_t len;
122       size_t varname_len;
123       size_t value_len;
124
125       varname_len = strlen (varname);
126       value_len = strlen (value);
127       
128       len = varname_len + value_len + 1 /* '=' */ ;
129
130       /* Use system malloc to avoid memleaks that dbus_malloc
131        * will get upset about.
132        */
133       
134       putenv_value = malloc (len + 1);
135       if (putenv_value == NULL)
136         return FALSE;
137
138       strcpy (putenv_value, varname);
139       strcpy (putenv_value + varname_len, "=");
140       strcpy (putenv_value + varname_len + 1, value);
141       
142       return (putenv (putenv_value) == 0);
143 #endif
144     }
145 }
146
147 /**
148  * Wrapper for getenv().
149  *
150  * @param varname name of environment variable
151  * @returns value of environment variable or #NULL if unset
152  */
153 const char*
154 _dbus_getenv (const char *varname)
155 {  
156   return getenv (varname);
157 }
158
159 /** @} */
160
161 /**
162  * @addtogroup DBusString
163  *
164  * @{
165  */
166 /**
167  * Appends an integer to a DBusString.
168  * 
169  * @param str the string
170  * @param value the integer value
171  * @returns #FALSE if not enough memory or other failure.
172  */
173 dbus_bool_t
174 _dbus_string_append_int (DBusString *str,
175                          long        value)
176 {
177   /* this calculation is from comp.lang.c faq */
178 #define MAX_LONG_LEN ((sizeof (long) * 8 + 2) / 3 + 1)  /* +1 for '-' */
179   int orig_len;
180   int i;
181   char *buf;
182   
183   orig_len = _dbus_string_get_length (str);
184
185   if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
186     return FALSE;
187
188   buf = _dbus_string_get_data_len (str, orig_len, MAX_LONG_LEN);
189
190   snprintf (buf, MAX_LONG_LEN, "%ld", value);
191
192   i = 0;
193   while (*buf)
194     {
195       ++buf;
196       ++i;
197     }
198   
199   _dbus_string_shorten (str, MAX_LONG_LEN - i);
200   
201   return TRUE;
202 }
203
204 /**
205  * Appends an unsigned integer to a DBusString.
206  * 
207  * @param str the string
208  * @param value the integer value
209  * @returns #FALSE if not enough memory or other failure.
210  */
211 dbus_bool_t
212 _dbus_string_append_uint (DBusString    *str,
213                           unsigned long  value)
214 {
215   /* this is wrong, but definitely on the high side. */
216 #define MAX_ULONG_LEN (MAX_LONG_LEN * 2)
217   int orig_len;
218   int i;
219   char *buf;
220   
221   orig_len = _dbus_string_get_length (str);
222
223   if (!_dbus_string_lengthen (str, MAX_ULONG_LEN))
224     return FALSE;
225
226   buf = _dbus_string_get_data_len (str, orig_len, MAX_ULONG_LEN);
227
228   snprintf (buf, MAX_ULONG_LEN, "%lu", value);
229
230   i = 0;
231   while (*buf)
232     {
233       ++buf;
234       ++i;
235     }
236   
237   _dbus_string_shorten (str, MAX_ULONG_LEN - i);
238   
239   return TRUE;
240 }
241
242 #ifdef DBUS_BUILD_TESTS
243 /**
244  * Appends a double to a DBusString.
245  * 
246  * @param str the string
247  * @param value the floating point value
248  * @returns #FALSE if not enough memory or other failure.
249  */
250 dbus_bool_t
251 _dbus_string_append_double (DBusString *str,
252                             double      value)
253 {
254 #define MAX_DOUBLE_LEN 64 /* this is completely made up :-/ */
255   int orig_len;
256   char *buf;
257   int i;
258   
259   orig_len = _dbus_string_get_length (str);
260
261   if (!_dbus_string_lengthen (str, MAX_DOUBLE_LEN))
262     return FALSE;
263
264   buf = _dbus_string_get_data_len (str, orig_len, MAX_DOUBLE_LEN);
265
266   snprintf (buf, MAX_LONG_LEN, "%g", value);
267
268   i = 0;
269   while (*buf)
270     {
271       ++buf;
272       ++i;
273     }
274   
275   _dbus_string_shorten (str, MAX_DOUBLE_LEN - i);
276   
277   return TRUE;
278 }
279 #endif /* DBUS_BUILD_TESTS */
280
281 /**
282  * Parses an integer contained in a DBusString. Either return parameter
283  * may be #NULL if you aren't interested in it. The integer is parsed
284  * and stored in value_return. Return parameters are not initialized
285  * if the function returns #FALSE.
286  *
287  * @param str the string
288  * @param start the byte index of the start of the integer
289  * @param value_return return location of the integer value or #NULL
290  * @param end_return return location of the end of the integer, or #NULL
291  * @returns #TRUE on success
292  */
293 dbus_bool_t
294 _dbus_string_parse_int (const DBusString *str,
295                         int               start,
296                         long             *value_return,
297                         int              *end_return)
298 {
299   long v;
300   const char *p;
301   char *end;
302
303   p = _dbus_string_get_const_data_len (str, start,
304                                        _dbus_string_get_length (str) - start);
305
306   end = NULL;
307   errno = 0;
308   v = strtol (p, &end, 0);
309   if (end == NULL || end == p || errno != 0)
310     return FALSE;
311
312   if (value_return)
313     *value_return = v;
314   if (end_return)
315     *end_return = start + (end - p);
316
317   return TRUE;
318 }
319
320 /**
321  * Parses an unsigned integer contained in a DBusString. Either return
322  * parameter may be #NULL if you aren't interested in it. The integer
323  * is parsed and stored in value_return. Return parameters are not
324  * initialized if the function returns #FALSE.
325  *
326  * @param str the string
327  * @param start the byte index of the start of the integer
328  * @param value_return return location of the integer value or #NULL
329  * @param end_return return location of the end of the integer, or #NULL
330  * @returns #TRUE on success
331  */
332 dbus_bool_t
333 _dbus_string_parse_uint (const DBusString *str,
334                          int               start,
335                          unsigned long    *value_return,
336                          int              *end_return)
337 {
338   unsigned long v;
339   const char *p;
340   char *end;
341
342   p = _dbus_string_get_const_data_len (str, start,
343                                        _dbus_string_get_length (str) - start);
344
345   end = NULL;
346   errno = 0;
347   v = strtoul (p, &end, 0);
348   if (end == NULL || end == p || errno != 0)
349     return FALSE;
350
351   if (value_return)
352     *value_return = v;
353   if (end_return)
354     *end_return = start + (end - p);
355
356   return TRUE;
357 }
358
359 #ifdef DBUS_BUILD_TESTS
360 static dbus_bool_t
361 ascii_isspace (char c)
362 {
363   return (c == ' ' ||
364           c == '\f' ||
365           c == '\n' ||
366           c == '\r' ||
367           c == '\t' ||
368           c == '\v');
369 }
370 #endif /* DBUS_BUILD_TESTS */
371
372 #ifdef DBUS_BUILD_TESTS
373 static dbus_bool_t
374 ascii_isdigit (char c)
375 {
376   return c >= '0' && c <= '9';
377 }
378 #endif /* DBUS_BUILD_TESTS */
379
380 #ifdef DBUS_BUILD_TESTS
381 static dbus_bool_t
382 ascii_isxdigit (char c)
383 {
384   return (ascii_isdigit (c) ||
385           (c >= 'a' && c <= 'f') ||
386           (c >= 'A' && c <= 'F'));
387 }
388 #endif /* DBUS_BUILD_TESTS */
389
390 #ifdef DBUS_BUILD_TESTS
391 /* Calls strtod in a locale-independent fashion, by looking at
392  * the locale data and patching the decimal comma to a point.
393  *
394  * Relicensed from glib.
395  */
396 static double
397 ascii_strtod (const char *nptr,
398               char      **endptr)
399 {
400   char *fail_pos;
401   double val;
402   struct lconv *locale_data;
403   const char *decimal_point;
404   int decimal_point_len;
405   const char *p, *decimal_point_pos;
406   const char *end = NULL; /* Silence gcc */
407
408   fail_pos = NULL;
409
410   locale_data = localeconv ();
411   decimal_point = locale_data->decimal_point;
412   decimal_point_len = strlen (decimal_point);
413
414   _dbus_assert (decimal_point_len != 0);
415   
416   decimal_point_pos = NULL;
417   if (decimal_point[0] != '.' ||
418       decimal_point[1] != 0)
419     {
420       p = nptr;
421       /* Skip leading space */
422       while (ascii_isspace (*p))
423         p++;
424       
425       /* Skip leading optional sign */
426       if (*p == '+' || *p == '-')
427         p++;
428       
429       if (p[0] == '0' &&
430           (p[1] == 'x' || p[1] == 'X'))
431         {
432           p += 2;
433           /* HEX - find the (optional) decimal point */
434           
435           while (ascii_isxdigit (*p))
436             p++;
437           
438           if (*p == '.')
439             {
440               decimal_point_pos = p++;
441               
442               while (ascii_isxdigit (*p))
443                 p++;
444               
445               if (*p == 'p' || *p == 'P')
446                 p++;
447               if (*p == '+' || *p == '-')
448                 p++;
449               while (ascii_isdigit (*p))
450                 p++;
451               end = p;
452             }
453         }
454       else
455         {
456           while (ascii_isdigit (*p))
457             p++;
458           
459           if (*p == '.')
460             {
461               decimal_point_pos = p++;
462               
463               while (ascii_isdigit (*p))
464                 p++;
465               
466               if (*p == 'e' || *p == 'E')
467                 p++;
468               if (*p == '+' || *p == '-')
469                 p++;
470               while (ascii_isdigit (*p))
471                 p++;
472               end = p;
473             }
474         }
475       /* For the other cases, we need not convert the decimal point */
476     }
477
478   /* Set errno to zero, so that we can distinguish zero results
479      and underflows */
480   errno = 0;
481   
482   if (decimal_point_pos)
483     {
484       char *copy, *c;
485
486       /* We need to convert the '.' to the locale specific decimal point */
487       copy = dbus_malloc (end - nptr + 1 + decimal_point_len);
488       
489       c = copy;
490       memcpy (c, nptr, decimal_point_pos - nptr);
491       c += decimal_point_pos - nptr;
492       memcpy (c, decimal_point, decimal_point_len);
493       c += decimal_point_len;
494       memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
495       c += end - (decimal_point_pos + 1);
496       *c = 0;
497
498       val = strtod (copy, &fail_pos);
499
500       if (fail_pos)
501         {
502           if (fail_pos > decimal_point_pos)
503             fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
504           else
505             fail_pos = (char *)nptr + (fail_pos - copy);
506         }
507       
508       dbus_free (copy);
509           
510     }
511   else
512     val = strtod (nptr, &fail_pos);
513
514   if (endptr)
515     *endptr = fail_pos;
516   
517   return val;
518 }
519 #endif /* DBUS_BUILD_TESTS */
520
521 #ifdef DBUS_BUILD_TESTS
522 /**
523  * Parses a floating point number contained in a DBusString. Either
524  * return parameter may be #NULL if you aren't interested in it. The
525  * integer is parsed and stored in value_return. Return parameters are
526  * not initialized if the function returns #FALSE.
527  *
528  * @param str the string
529  * @param start the byte index of the start of the float
530  * @param value_return return location of the float value or #NULL
531  * @param end_return return location of the end of the float, or #NULL
532  * @returns #TRUE on success
533  */
534 dbus_bool_t
535 _dbus_string_parse_double (const DBusString *str,
536                            int               start,
537                            double           *value_return,
538                            int              *end_return)
539 {
540   double v;
541   const char *p;
542   char *end;
543
544   p = _dbus_string_get_const_data_len (str, start,
545                                        _dbus_string_get_length (str) - start);
546
547   end = NULL;
548   errno = 0;
549   v = ascii_strtod (p, &end);
550   if (end == NULL || end == p || errno != 0)
551     return FALSE;
552
553   if (value_return)
554     *value_return = v;
555   if (end_return)
556     *end_return = start + (end - p);
557
558   return TRUE;
559 }
560 #endif /* DBUS_BUILD_TESTS */
561
562 /** @} */ /* DBusString group */
563
564 /**
565  * @addtogroup DBusInternalsUtils
566  * @{
567  */
568
569 /**
570  * Frees the members of info
571  * (but not info itself)
572  * @param info the user info struct
573  */
574 void
575 _dbus_user_info_free (DBusUserInfo *info)
576 {
577   dbus_free (info->group_ids);
578   dbus_free (info->username);
579   dbus_free (info->homedir);
580 }
581
582 /**
583  * Frees the members of info (but not info itself).
584  *
585  * @param info the group info
586  */
587 void
588 _dbus_group_info_free (DBusGroupInfo    *info)
589 {
590   dbus_free (info->groupname);
591 }
592
593 /**
594  * Sets fields in DBusCredentials to DBUS_PID_UNSET,
595  * DBUS_UID_UNSET, DBUS_GID_UNSET.
596  *
597  * @param credentials the credentials object to fill in
598  */
599 void
600 _dbus_credentials_clear (DBusCredentials *credentials)
601 {
602   credentials->pid = DBUS_PID_UNSET;
603   credentials->uid = DBUS_UID_UNSET;
604   credentials->gid = DBUS_GID_UNSET;
605 }
606
607 /**
608  * Checks whether the provided_credentials are allowed to log in
609  * as the expected_credentials.
610  *
611  * @param expected_credentials credentials we're trying to log in as
612  * @param provided_credentials credentials we have
613  * @returns #TRUE if we can log in
614  */
615 dbus_bool_t
616 _dbus_credentials_match (const DBusCredentials *expected_credentials,
617                          const DBusCredentials *provided_credentials)
618 {
619   if (provided_credentials->uid == DBUS_UID_UNSET)
620     return FALSE;
621   else if (expected_credentials->uid == DBUS_UID_UNSET)
622     return FALSE;
623   else if (provided_credentials->uid == 0)
624     return TRUE;
625   else if (provided_credentials->uid == expected_credentials->uid)
626     return TRUE;
627   else
628     return FALSE;
629 }
630
631 _DBUS_DEFINE_GLOBAL_LOCK (atomic);
632
633 #ifdef DBUS_USE_ATOMIC_INT_486
634 /* Taken from CVS version 1.7 of glibc's sysdeps/i386/i486/atomicity.h */
635 /* Since the asm stuff here is gcc-specific we go ahead and use "inline" also */
636 static inline dbus_int32_t
637 atomic_exchange_and_add (DBusAtomic            *atomic,
638                          volatile dbus_int32_t  val)
639 {
640   register dbus_int32_t result;
641
642   __asm__ __volatile__ ("lock; xaddl %0,%1"
643                         : "=r" (result), "=m" (atomic->value)
644                         : "0" (val), "m" (atomic->value));
645   return result;
646 }
647 #endif
648
649 /**
650  * Atomically increments an integer
651  *
652  * @param atomic pointer to the integer to increment
653  * @returns the value before incrementing
654  *
655  * @todo implement arch-specific faster atomic ops
656  */
657 dbus_int32_t
658 _dbus_atomic_inc (DBusAtomic *atomic)
659 {
660 #ifdef DBUS_USE_ATOMIC_INT_486
661   return atomic_exchange_and_add (atomic, 1);
662 #else
663   dbus_int32_t res;
664   _DBUS_LOCK (atomic);
665   res = atomic->value;
666   atomic->value += 1;
667   _DBUS_UNLOCK (atomic);
668   return res;
669 #endif
670 }
671
672 /**
673  * Atomically decrement an integer
674  *
675  * @param atomic pointer to the integer to decrement
676  * @returns the value before decrementing
677  *
678  * @todo implement arch-specific faster atomic ops
679  */
680 dbus_int32_t
681 _dbus_atomic_dec (DBusAtomic *atomic)
682 {
683 #ifdef DBUS_USE_ATOMIC_INT_486
684   return atomic_exchange_and_add (atomic, -1);
685 #else
686   dbus_int32_t res;
687   
688   _DBUS_LOCK (atomic);
689   res = atomic->value;
690   atomic->value -= 1;
691   _DBUS_UNLOCK (atomic);
692   return res;
693 #endif
694 }
695
696 void
697 _dbus_generate_pseudorandom_bytes_buffer (char *buffer,
698                                           int   n_bytes)
699 {
700   long tv_usec;
701   int i;
702   
703   /* fall back to pseudorandom */
704   _dbus_verbose ("Falling back to pseudorandom for %d bytes\n",
705                  n_bytes);
706   
707   _dbus_get_current_time (NULL, &tv_usec);
708   srand (tv_usec);
709   
710   i = 0;
711   while (i < n_bytes)
712     {
713       double r;
714       unsigned int b;
715           
716       r = rand ();
717       b = (r / (double) RAND_MAX) * 255.0;
718
719       buffer[i] = b;
720
721       ++i;
722     }
723 }
724
725 /**
726  * Fills n_bytes of the given buffer with random bytes.
727  *
728  * @param buffer an allocated buffer
729  * @param n_bytes the number of bytes in buffer to write to
730  */
731 void
732 _dbus_generate_random_bytes_buffer (char *buffer,
733                                     int   n_bytes)
734 {
735   DBusString str;
736
737   if (!_dbus_string_init (&str))
738     {
739       _dbus_generate_pseudorandom_bytes_buffer (buffer, n_bytes);
740       return;
741     }
742
743   if (!_dbus_generate_random_bytes (&str, n_bytes))
744     {
745       _dbus_string_free (&str);
746       _dbus_generate_pseudorandom_bytes_buffer (buffer, n_bytes);
747       return;
748     }
749
750   _dbus_string_copy_to_buffer (&str, buffer, n_bytes);
751
752   _dbus_string_free (&str);
753 }
754
755 /**
756  * Generates the given number of random bytes, where the bytes are
757  * chosen from the alphanumeric ASCII subset.
758  *
759  * @param str the string
760  * @param n_bytes the number of random ASCII bytes to append to string
761  * @returns #TRUE on success, #FALSE if no memory or other failure
762  */
763 dbus_bool_t
764 _dbus_generate_random_ascii (DBusString *str,
765                              int         n_bytes)
766 {
767   static const char letters[] =
768     "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
769   int i;
770   int len;
771   
772   if (!_dbus_generate_random_bytes (str, n_bytes))
773     return FALSE;
774   
775   len = _dbus_string_get_length (str);
776   i = len - n_bytes;
777   while (i < len)
778     {
779       _dbus_string_set_byte (str, i,
780                              letters[_dbus_string_get_byte (str, i) %
781                                      (sizeof (letters) - 1)]);
782
783       ++i;
784     }
785
786   _dbus_assert (_dbus_string_validate_ascii (str, len - n_bytes,
787                                              n_bytes));
788
789   return TRUE;
790 }
791
792 /**
793  * Gets a UID from a UID string.
794  *
795  * @param uid_str the UID in string form
796  * @param uid UID to fill in
797  * @returns #TRUE if successfully filled in UID
798  */
799 dbus_bool_t
800 _dbus_parse_uid (const DBusString      *uid_str,
801                  dbus_uid_t            *uid)
802 {
803   int end;
804   long val;
805   
806   if (_dbus_string_get_length (uid_str) == 0)
807     {
808       _dbus_verbose ("UID string was zero length\n");
809       return FALSE;
810     }
811
812   val = -1;
813   end = 0;
814   if (!_dbus_string_parse_int (uid_str, 0, &val,
815                                &end))
816     {
817       _dbus_verbose ("could not parse string as a UID\n");
818       return FALSE;
819     }
820   
821   if (end != _dbus_string_get_length (uid_str))
822     {
823       _dbus_verbose ("string contained trailing stuff after UID\n");
824       return FALSE;
825     }
826
827   *uid = val;
828
829   return TRUE;
830 }
831
832 /**
833  * Converts a UNIX or Windows errno
834  * into a #DBusError name.
835  *
836  * @todo should cover more errnos, specifically those
837  * from open().
838  * 
839  * @param error_number the errno.
840  * @returns an error name
841  */
842 const char*
843 _dbus_error_from_errno (int error_number)
844 {
845   switch (error_number)
846     {
847     case 0:
848       return DBUS_ERROR_FAILED;
849       
850 #ifdef EPROTONOSUPPORT
851     case EPROTONOSUPPORT:
852       return DBUS_ERROR_NOT_SUPPORTED;
853 #endif
854 #ifdef EAFNOSUPPORT
855     case EAFNOSUPPORT:
856       return DBUS_ERROR_NOT_SUPPORTED;
857 #endif
858 #ifdef ENFILE
859     case ENFILE:
860       return DBUS_ERROR_LIMITS_EXCEEDED; /* kernel out of memory */
861 #endif
862 #ifdef EMFILE
863     case EMFILE:
864       return DBUS_ERROR_LIMITS_EXCEEDED;
865 #endif
866 #ifdef EACCES
867     case EACCES:
868       return DBUS_ERROR_ACCESS_DENIED;
869 #endif
870 #ifdef EPERM
871     case EPERM:
872       return DBUS_ERROR_ACCESS_DENIED;
873 #endif
874 #ifdef ENOBUFS
875     case ENOBUFS:
876       return DBUS_ERROR_NO_MEMORY;
877 #endif
878 #ifdef ENOMEM
879     case ENOMEM:
880       return DBUS_ERROR_NO_MEMORY;
881 #endif
882 #ifdef EINVAL
883     case EINVAL:
884       return DBUS_ERROR_FAILED;
885 #endif
886 #ifdef EBADF
887     case EBADF:
888       return DBUS_ERROR_FAILED;
889 #endif
890 #ifdef EFAULT
891     case EFAULT:
892       return DBUS_ERROR_FAILED;
893 #endif
894 #ifdef ENOTSOCK
895     case ENOTSOCK:
896       return DBUS_ERROR_FAILED;
897 #endif
898 #ifdef EISCONN
899     case EISCONN:
900       return DBUS_ERROR_FAILED;
901 #endif
902 #ifdef ECONNREFUSED
903     case ECONNREFUSED:
904       return DBUS_ERROR_NO_SERVER;
905 #endif
906 #ifdef ETIMEDOUT
907     case ETIMEDOUT:
908       return DBUS_ERROR_TIMEOUT;
909 #endif
910 #ifdef ENETUNREACH
911     case ENETUNREACH:
912       return DBUS_ERROR_NO_NETWORK;
913 #endif
914 #ifdef EADDRINUSE
915     case EADDRINUSE:
916       return DBUS_ERROR_ADDRESS_IN_USE;
917 #endif
918 #ifdef EEXIST
919     case EEXIST:
920       return DBUS_ERROR_FILE_NOT_FOUND;
921 #endif
922 #ifdef ENOENT
923     case ENOENT:
924       return DBUS_ERROR_FILE_NOT_FOUND;
925 #endif
926     }
927
928   return DBUS_ERROR_FAILED;
929 }
930
931 /** @} end of sysdeps */
932
933 /* tests in dbus-sysdeps-util.c */