D-Bus  1.5.12
dbus-sysdeps.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #include <config.h>
26 #include "dbus-internals.h"
27 #include "dbus-sysdeps.h"
28 #include "dbus-threads.h"
29 #include "dbus-protocol.h"
30 #include "dbus-string.h"
31 #include "dbus-list.h"
32 
33 /* NOTE: If you include any unix/windows-specific headers here, you are probably doing something
34  * wrong and should be putting some code in dbus-sysdeps-unix.c or dbus-sysdeps-win.c.
35  *
36  * These are the standard ANSI C headers...
37  */
38 #if HAVE_LOCALE_H
39 #include <locale.h>
40 #endif
41 #include <stdlib.h>
42 #include <string.h>
43 #include <stdio.h>
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 _DBUS_DEFINE_GLOBAL_LOCK (system_users);
52 
53 #ifdef DBUS_WIN
54  #include <stdlib.h>
55 #elif (defined __APPLE__)
56 # include <crt_externs.h>
57 # define environ (*_NSGetEnviron())
58 #else
59 extern char **environ;
60 #endif
61 
79 void
81 {
82  const char *s;
83 
85 
86  s = _dbus_getenv ("DBUS_BLOCK_ON_ABORT");
87  if (s && *s)
88  {
89  /* don't use _dbus_warn here since it can _dbus_abort() */
90  fprintf (stderr, " Process %lu sleeping for gdb attach\n", _dbus_pid_for_log ());
91  _dbus_sleep_milliseconds (1000 * 180);
92  }
93 
94  abort ();
95  _dbus_exit (1); /* in case someone manages to ignore SIGABRT ? */
96 }
97 
112 _dbus_setenv (const char *varname,
113  const char *value)
114 {
115  _dbus_assert (varname != NULL);
116 
117  if (value == NULL)
118  {
119 #ifdef HAVE_UNSETENV
120  unsetenv (varname);
121  return TRUE;
122 #else
123  char *putenv_value;
124  size_t len;
125 
126  len = strlen (varname);
127 
128  /* Use system malloc to avoid memleaks that dbus_malloc
129  * will get upset about.
130  */
131 
132  putenv_value = malloc (len + 2);
133  if (putenv_value == NULL)
134  return FALSE;
135 
136  strcpy (putenv_value, varname);
137 #if defined(DBUS_WIN)
138  strcat (putenv_value, "=");
139 #endif
140 
141  return (putenv (putenv_value) == 0);
142 #endif
143  }
144  else
145  {
146 #ifdef HAVE_SETENV
147  return (setenv (varname, value, TRUE) == 0);
148 #else
149  char *putenv_value;
150  size_t len;
151  size_t varname_len;
152  size_t value_len;
153 
154  varname_len = strlen (varname);
155  value_len = strlen (value);
156 
157  len = varname_len + value_len + 1 /* '=' */ ;
158 
159  /* Use system malloc to avoid memleaks that dbus_malloc
160  * will get upset about.
161  */
162 
163  putenv_value = malloc (len + 1);
164  if (putenv_value == NULL)
165  return FALSE;
166 
167  strcpy (putenv_value, varname);
168  strcpy (putenv_value + varname_len, "=");
169  strcpy (putenv_value + varname_len + 1, value);
170 
171  return (putenv (putenv_value) == 0);
172 #endif
173  }
174 }
175 
182 const char*
183 _dbus_getenv (const char *varname)
184 {
185 #ifdef HAVE___SECURE_GETENV
186  return __secure_getenv(varname);
187 #else
188  return getenv(varname);
189 #endif
190 }
191 
199 {
200  dbus_bool_t rc = TRUE;
201 
202 #ifdef HAVE_CLEARENV
203  if (clearenv () != 0)
204  rc = FALSE;
205 #else
206 
207  if (environ != NULL)
208  environ[0] = NULL;
209 #endif
210 
211  return rc;
212 }
213 
224  const char *suffix,
225  DBusList **dir_list)
226 {
227  int start;
228  int i;
229  int len;
230  char *cpath;
231  DBusString file_suffix;
232 
233  start = 0;
234  i = 0;
235 
236  _dbus_string_init_const (&file_suffix, suffix);
237 
238  len = _dbus_string_get_length (dirs);
239 
240  while (_dbus_string_find (dirs, start, _DBUS_PATH_SEPARATOR, &i))
241  {
242  DBusString path;
243 
244  if (!_dbus_string_init (&path))
245  goto oom;
246 
247  if (!_dbus_string_copy_len (dirs,
248  start,
249  i - start,
250  &path,
251  0))
252  {
253  _dbus_string_free (&path);
254  goto oom;
255  }
256 
257  _dbus_string_chop_white (&path);
258 
259  /* check for an empty path */
260  if (_dbus_string_get_length (&path) == 0)
261  goto next;
262 
263  if (!_dbus_concat_dir_and_file (&path,
264  &file_suffix))
265  {
266  _dbus_string_free (&path);
267  goto oom;
268  }
269 
270  if (!_dbus_string_copy_data(&path, &cpath))
271  {
272  _dbus_string_free (&path);
273  goto oom;
274  }
275 
276  if (!_dbus_list_append (dir_list, cpath))
277  {
278  _dbus_string_free (&path);
279  dbus_free (cpath);
280  goto oom;
281  }
282 
283  next:
284  _dbus_string_free (&path);
285  start = i + 1;
286  }
287 
288  if (start != len)
289  {
290  DBusString path;
291 
292  if (!_dbus_string_init (&path))
293  goto oom;
294 
295  if (!_dbus_string_copy_len (dirs,
296  start,
297  len - start,
298  &path,
299  0))
300  {
301  _dbus_string_free (&path);
302  goto oom;
303  }
304 
305  if (!_dbus_concat_dir_and_file (&path,
306  &file_suffix))
307  {
308  _dbus_string_free (&path);
309  goto oom;
310  }
311 
312  if (!_dbus_string_copy_data(&path, &cpath))
313  {
314  _dbus_string_free (&path);
315  goto oom;
316  }
317 
318  if (!_dbus_list_append (dir_list, cpath))
319  {
320  _dbus_string_free (&path);
321  dbus_free (cpath);
322  goto oom;
323  }
324 
325  _dbus_string_free (&path);
326  }
327 
328  return TRUE;
329 
330  oom:
332  _dbus_list_clear (dir_list);
333  return FALSE;
334 }
335 
352  long value)
353 {
354  /* this calculation is from comp.lang.c faq */
355 #define MAX_LONG_LEN ((sizeof (long) * 8 + 2) / 3 + 1) /* +1 for '-' */
356  int orig_len;
357  int i;
358  char *buf;
359 
360  orig_len = _dbus_string_get_length (str);
361 
362  if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
363  return FALSE;
364 
365  buf = _dbus_string_get_data_len (str, orig_len, MAX_LONG_LEN);
366 
367  snprintf (buf, MAX_LONG_LEN, "%ld", value);
368 
369  i = 0;
370  while (*buf)
371  {
372  ++buf;
373  ++i;
374  }
375 
376  _dbus_string_shorten (str, MAX_LONG_LEN - i);
377 
378  return TRUE;
379 }
380 
390  unsigned long value)
391 {
392  /* this is wrong, but definitely on the high side. */
393 #define MAX_ULONG_LEN (MAX_LONG_LEN * 2)
394  int orig_len;
395  int i;
396  char *buf;
397 
398  orig_len = _dbus_string_get_length (str);
399 
400  if (!_dbus_string_lengthen (str, MAX_ULONG_LEN))
401  return FALSE;
402 
403  buf = _dbus_string_get_data_len (str, orig_len, MAX_ULONG_LEN);
404 
405  snprintf (buf, MAX_ULONG_LEN, "%lu", value);
406 
407  i = 0;
408  while (*buf)
409  {
410  ++buf;
411  ++i;
412  }
413 
414  _dbus_string_shorten (str, MAX_ULONG_LEN - i);
415 
416  return TRUE;
417 }
418 
433  int start,
434  long *value_return,
435  int *end_return)
436 {
437  long v;
438  const char *p;
439  char *end;
440 
441  p = _dbus_string_get_const_data_len (str, start,
442  _dbus_string_get_length (str) - start);
443 
444  end = NULL;
446  v = strtol (p, &end, 0);
447  if (end == NULL || end == p || errno != 0)
448  return FALSE;
449 
450  if (value_return)
451  *value_return = v;
452  if (end_return)
453  *end_return = start + (end - p);
454 
455  return TRUE;
456 }
457 
472  int start,
473  unsigned long *value_return,
474  int *end_return)
475 {
476  unsigned long v;
477  const char *p;
478  char *end;
479 
480  p = _dbus_string_get_const_data_len (str, start,
481  _dbus_string_get_length (str) - start);
482 
483  end = NULL;
485  v = strtoul (p, &end, 0);
486  if (end == NULL || end == p || errno != 0)
487  return FALSE;
488 
489  if (value_return)
490  *value_return = v;
491  if (end_return)
492  *end_return = start + (end - p);
493 
494  return TRUE;
495 }
496  /* DBusString group */
498 
504 void
506  int n_bytes)
507 {
508  long tv_usec;
509  int i;
510 
511  /* fall back to pseudorandom */
512  _dbus_verbose ("Falling back to pseudorandom for %d bytes\n",
513  n_bytes);
514 
515  _dbus_get_current_time (NULL, &tv_usec);
516  srand (tv_usec);
517 
518  i = 0;
519  while (i < n_bytes)
520  {
521  double r;
522  unsigned int b;
523 
524  r = rand ();
525  b = (r / (double) RAND_MAX) * 255.0;
526 
527  buffer[i] = b;
528 
529  ++i;
530  }
531 }
532 
539 void
541  int n_bytes)
542 {
543  DBusString str;
544 
545  if (!_dbus_string_init (&str))
546  {
548  return;
549  }
550 
551  if (!_dbus_generate_random_bytes (&str, n_bytes))
552  {
553  _dbus_string_free (&str);
555  return;
556  }
557 
558  _dbus_string_copy_to_buffer (&str, buffer, n_bytes);
559 
560  _dbus_string_free (&str);
561 }
562 
573  int n_bytes)
574 {
575  static const char letters[] =
576  "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
577  int i;
578  int len;
579 
580  if (!_dbus_generate_random_bytes (str, n_bytes))
581  return FALSE;
582 
583  len = _dbus_string_get_length (str);
584  i = len - n_bytes;
585  while (i < len)
586  {
587  _dbus_string_set_byte (str, i,
588  letters[_dbus_string_get_byte (str, i) %
589  (sizeof (letters) - 1)]);
590 
591  ++i;
592  }
593 
594  _dbus_assert (_dbus_string_validate_ascii (str, len - n_bytes,
595  n_bytes));
596 
597  return TRUE;
598 }
599 
610 const char*
611 _dbus_error_from_errno (int error_number)
612 {
613  switch (error_number)
614  {
615  case 0:
616  return DBUS_ERROR_FAILED;
617 
618 #ifdef EPROTONOSUPPORT
619  case EPROTONOSUPPORT:
621 #elif defined(WSAEPROTONOSUPPORT)
622  case WSAEPROTONOSUPPORT:
624 #endif
625 #ifdef EAFNOSUPPORT
626  case EAFNOSUPPORT:
628 #elif defined(WSAEAFNOSUPPORT)
629  case WSAEAFNOSUPPORT:
631 #endif
632 #ifdef ENFILE
633  case ENFILE:
634  return DBUS_ERROR_LIMITS_EXCEEDED; /* kernel out of memory */
635 #endif
636 #ifdef EMFILE
637  case EMFILE:
639 #endif
640 #ifdef EACCES
641  case EACCES:
643 #endif
644 #ifdef EPERM
645  case EPERM:
647 #endif
648 #ifdef ENOBUFS
649  case ENOBUFS:
650  return DBUS_ERROR_NO_MEMORY;
651 #endif
652 #ifdef ENOMEM
653  case ENOMEM:
654  return DBUS_ERROR_NO_MEMORY;
655 #endif
656 #ifdef ECONNREFUSED
657  case ECONNREFUSED:
658  return DBUS_ERROR_NO_SERVER;
659 #elif defined(WSAECONNREFUSED)
660  case WSAECONNREFUSED:
661  return DBUS_ERROR_NO_SERVER;
662 #endif
663 #ifdef ETIMEDOUT
664  case ETIMEDOUT:
665  return DBUS_ERROR_TIMEOUT;
666 #elif defined(WSAETIMEDOUT)
667  case WSAETIMEDOUT:
668  return DBUS_ERROR_TIMEOUT;
669 #endif
670 #ifdef ENETUNREACH
671  case ENETUNREACH:
672  return DBUS_ERROR_NO_NETWORK;
673 #elif defined(WSAENETUNREACH)
674  case WSAENETUNREACH:
675  return DBUS_ERROR_NO_NETWORK;
676 #endif
677 #ifdef EADDRINUSE
678  case EADDRINUSE:
680 #elif defined(WSAEADDRINUSE)
681  case WSAEADDRINUSE:
683 #endif
684 #ifdef EEXIST
685  case EEXIST:
686  return DBUS_ERROR_FILE_EXISTS;
687 #endif
688 #ifdef ENOENT
689  case ENOENT:
691 #endif
692  }
693 
694  return DBUS_ERROR_FAILED;
695 }
696 
702 const char*
704 {
705  return _dbus_error_from_errno (errno);
706 }
707 
711 void
713 {
714 #ifdef DBUS_WINCE
715  SetLastError (0);
716 #else
717  errno = 0;
718 #endif
719 }
720 
727 {
728  return errno != 0;
729 }
730 
737 {
738  return errno == ENOMEM;
739 }
740 
747 {
748  return errno == EINTR;
749 }
750 
757 {
758  return errno == EPIPE;
759 }
760 
765 const char*
767 {
768  return _dbus_strerror (errno);
769 }
770 
773 /* tests in dbus-sysdeps-util.c */