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)
4 * Copyright (C) 2002, 2003, 2006 Red Hat, Inc.
5 * Copyright (C) 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 2.1
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.
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.
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
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"
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.
36 * These are the standard ANSI C headers...
51 #elif (defined __APPLE__)
52 # include <crt_externs.h>
53 # define environ (*_NSGetEnviron())
55 extern char **environ;
59 * @defgroup DBusSysdeps Internal system-dependent API
60 * @ingroup DBusInternals
61 * @brief Internal system-dependent API available on UNIX and Windows
63 * The system-dependent API has a dual purpose. First, it encapsulates
64 * all usage of operating system APIs for ease of auditing and to
65 * avoid cluttering the rest of the code with bizarre OS quirks and
66 * headers. Second, it abstracts different operating system APIs for
73 * Aborts the program with SIGABRT (dumping core).
80 _dbus_print_backtrace ();
82 s = _dbus_getenv ("DBUS_BLOCK_ON_ABORT");
85 /* don't use _dbus_warn here since it can _dbus_abort() */
86 fprintf (stderr, " Process %lu sleeping for gdb attach\n", _dbus_pid_for_log ());
87 _dbus_sleep_milliseconds (1000 * 180);
91 _dbus_exit (1); /* in case someone manages to ignore SIGABRT ? */
95 * Wrapper for setenv(). If the value is #NULL, unsets
96 * the environment variable.
98 * There is an unfixable memleak in that it is unsafe to
99 * free memory malloced for use with setenv. This is because
100 * we can not rely on internal implementation details of
101 * the underlying libc library.
103 * @param varname name of environment variable
104 * @param value value of environment variable
105 * @returns #TRUE on success.
108 _dbus_setenv (const char *varname,
111 _dbus_assert (varname != NULL);
122 len = strlen (varname);
124 /* Use system malloc to avoid memleaks that dbus_malloc
125 * will get upset about.
128 putenv_value = malloc (len + 2);
129 if (putenv_value == NULL)
132 strcpy (putenv_value, varname);
133 #if defined(DBUS_WIN)
134 strcat (putenv_value, "=");
137 return (putenv (putenv_value) == 0);
143 return (setenv (varname, value, TRUE) == 0);
150 varname_len = strlen (varname);
151 value_len = strlen (value);
153 len = varname_len + value_len + 1 /* '=' */ ;
155 /* Use system malloc to avoid memleaks that dbus_malloc
156 * will get upset about.
159 putenv_value = malloc (len + 1);
160 if (putenv_value == NULL)
163 strcpy (putenv_value, varname);
164 strcpy (putenv_value + varname_len, "=");
165 strcpy (putenv_value + varname_len + 1, value);
167 return (putenv (putenv_value) == 0);
173 * Wrapper for getenv().
175 * @param varname name of environment variable
176 * @returns value of environment variable or #NULL if unset
179 _dbus_getenv (const char *varname)
181 /* Don't respect any environment variables if the current process is
182 * setuid. This is the equivalent of glibc's __secure_getenv().
184 if (_dbus_check_setuid ())
186 return getenv (varname);
190 * Wrapper for clearenv().
192 * @returns #TRUE on success.
195 _dbus_clearenv (void)
197 dbus_bool_t rc = TRUE;
200 if (clearenv () != 0)
212 * Split paths into a list of char strings
214 * @param dirs string with pathes
215 * @param suffix string concated to each path in dirs
216 * @param dir_list contains a list of splitted pathes
217 * return #TRUE is pathes could be splittes,#FALSE in oom case
220 _dbus_split_paths_and_append (DBusString *dirs,
228 DBusString file_suffix;
233 _dbus_string_init_const (&file_suffix, suffix);
235 len = _dbus_string_get_length (dirs);
237 while (_dbus_string_find (dirs, start, _DBUS_PATH_SEPARATOR, &i))
241 if (!_dbus_string_init (&path))
244 if (!_dbus_string_copy_len (dirs,
250 _dbus_string_free (&path);
254 _dbus_string_chop_white (&path);
256 /* check for an empty path */
257 if (_dbus_string_get_length (&path) == 0)
260 if (!_dbus_concat_dir_and_file (&path,
263 _dbus_string_free (&path);
267 if (!_dbus_string_copy_data(&path, &cpath))
269 _dbus_string_free (&path);
273 if (!_dbus_list_append (dir_list, cpath))
275 _dbus_string_free (&path);
281 _dbus_string_free (&path);
289 if (!_dbus_string_init (&path))
292 if (!_dbus_string_copy_len (dirs,
298 _dbus_string_free (&path);
302 if (!_dbus_concat_dir_and_file (&path,
305 _dbus_string_free (&path);
309 if (!_dbus_string_copy_data(&path, &cpath))
311 _dbus_string_free (&path);
315 if (!_dbus_list_append (dir_list, cpath))
317 _dbus_string_free (&path);
322 _dbus_string_free (&path);
328 _dbus_list_foreach (dir_list, (DBusForeachFunction)dbus_free, NULL);
329 _dbus_list_clear (dir_list);
336 * @addtogroup DBusString
341 * Appends an integer to a DBusString.
343 * @param str the string
344 * @param value the integer value
345 * @returns #FALSE if not enough memory or other failure.
348 _dbus_string_append_int (DBusString *str,
351 /* this calculation is from comp.lang.c faq */
352 #define MAX_LONG_LEN ((sizeof (long) * 8 + 2) / 3 + 1) /* +1 for '-' */
357 orig_len = _dbus_string_get_length (str);
359 if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
362 buf = _dbus_string_get_data_len (str, orig_len, MAX_LONG_LEN);
364 snprintf (buf, MAX_LONG_LEN, "%ld", value);
373 _dbus_string_shorten (str, MAX_LONG_LEN - i);
379 * Appends an unsigned integer to a DBusString.
381 * @param str the string
382 * @param value the integer value
383 * @returns #FALSE if not enough memory or other failure.
386 _dbus_string_append_uint (DBusString *str,
389 /* this is wrong, but definitely on the high side. */
390 #define MAX_ULONG_LEN (MAX_LONG_LEN * 2)
395 orig_len = _dbus_string_get_length (str);
397 if (!_dbus_string_lengthen (str, MAX_ULONG_LEN))
400 buf = _dbus_string_get_data_len (str, orig_len, MAX_ULONG_LEN);
402 snprintf (buf, MAX_ULONG_LEN, "%lu", value);
411 _dbus_string_shorten (str, MAX_ULONG_LEN - i);
417 * Parses an integer contained in a DBusString. Either return parameter
418 * may be #NULL if you aren't interested in it. The integer is parsed
419 * and stored in value_return. Return parameters are not initialized
420 * if the function returns #FALSE.
422 * @param str the string
423 * @param start the byte index of the start of the integer
424 * @param value_return return location of the integer value or #NULL
425 * @param end_return return location of the end of the integer, or #NULL
426 * @returns #TRUE on success
429 _dbus_string_parse_int (const DBusString *str,
438 p = _dbus_string_get_const_data_len (str, start,
439 _dbus_string_get_length (str) - start);
442 _dbus_set_errno_to_zero ();
443 v = strtol (p, &end, 0);
444 if (end == NULL || end == p || errno != 0)
450 *end_return = start + (end - p);
456 * Parses an unsigned integer contained in a DBusString. Either return
457 * parameter may be #NULL if you aren't interested in it. The integer
458 * is parsed and stored in value_return. Return parameters are not
459 * initialized if the function returns #FALSE.
461 * @param str the string
462 * @param start the byte index of the start of the integer
463 * @param value_return return location of the integer value or #NULL
464 * @param end_return return location of the end of the integer, or #NULL
465 * @returns #TRUE on success
468 _dbus_string_parse_uint (const DBusString *str,
470 unsigned long *value_return,
477 p = _dbus_string_get_const_data_len (str, start,
478 _dbus_string_get_length (str) - start);
481 _dbus_set_errno_to_zero ();
482 v = strtoul (p, &end, 0);
483 if (end == NULL || end == p || errno != 0)
489 *end_return = start + (end - p);
494 /** @} */ /* DBusString group */
497 * @addtogroup DBusInternalsUtils
502 _dbus_generate_pseudorandom_bytes_buffer (char *buffer,
508 /* fall back to pseudorandom */
509 _dbus_verbose ("Falling back to pseudorandom for %d bytes\n",
512 _dbus_get_real_time (NULL, &tv_usec);
522 b = (r / (double) RAND_MAX) * 255.0;
531 * Fills n_bytes of the given buffer with random bytes.
533 * @param buffer an allocated buffer
534 * @param n_bytes the number of bytes in buffer to write to
537 _dbus_generate_random_bytes_buffer (char *buffer,
542 if (!_dbus_string_init (&str))
544 _dbus_generate_pseudorandom_bytes_buffer (buffer, n_bytes);
548 if (!_dbus_generate_random_bytes (&str, n_bytes))
550 _dbus_string_free (&str);
551 _dbus_generate_pseudorandom_bytes_buffer (buffer, n_bytes);
555 _dbus_string_copy_to_buffer (&str, buffer, n_bytes);
557 _dbus_string_free (&str);
561 * Generates the given number of random bytes, where the bytes are
562 * chosen from the alphanumeric ASCII subset.
564 * @param str the string
565 * @param n_bytes the number of random ASCII bytes to append to string
566 * @returns #TRUE on success, #FALSE if no memory or other failure
569 _dbus_generate_random_ascii (DBusString *str,
572 static const char letters[] =
573 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
577 if (!_dbus_generate_random_bytes (str, n_bytes))
580 len = _dbus_string_get_length (str);
584 _dbus_string_set_byte (str, i,
585 letters[_dbus_string_get_byte (str, i) %
586 (sizeof (letters) - 1)]);
591 _dbus_assert (_dbus_string_validate_ascii (str, len - n_bytes,
598 * Converts a UNIX errno, or Windows errno or WinSock error value into
601 * @todo should cover more errnos, specifically those
604 * @param error_number the errno.
605 * @returns an error name
608 _dbus_error_from_errno (int error_number)
610 switch (error_number)
613 return DBUS_ERROR_FAILED;
615 #ifdef EPROTONOSUPPORT
616 case EPROTONOSUPPORT:
617 return DBUS_ERROR_NOT_SUPPORTED;
618 #elif defined(WSAEPROTONOSUPPORT)
619 case WSAEPROTONOSUPPORT:
620 return DBUS_ERROR_NOT_SUPPORTED;
624 return DBUS_ERROR_NOT_SUPPORTED;
625 #elif defined(WSAEAFNOSUPPORT)
626 case WSAEAFNOSUPPORT:
627 return DBUS_ERROR_NOT_SUPPORTED;
631 return DBUS_ERROR_LIMITS_EXCEEDED; /* kernel out of memory */
635 return DBUS_ERROR_LIMITS_EXCEEDED;
639 return DBUS_ERROR_ACCESS_DENIED;
643 return DBUS_ERROR_ACCESS_DENIED;
647 return DBUS_ERROR_NO_MEMORY;
651 return DBUS_ERROR_NO_MEMORY;
655 return DBUS_ERROR_NO_SERVER;
656 #elif defined(WSAECONNREFUSED)
657 case WSAECONNREFUSED:
658 return DBUS_ERROR_NO_SERVER;
662 return DBUS_ERROR_TIMEOUT;
663 #elif defined(WSAETIMEDOUT)
665 return DBUS_ERROR_TIMEOUT;
669 return DBUS_ERROR_NO_NETWORK;
670 #elif defined(WSAENETUNREACH)
672 return DBUS_ERROR_NO_NETWORK;
676 return DBUS_ERROR_ADDRESS_IN_USE;
677 #elif defined(WSAEADDRINUSE)
679 return DBUS_ERROR_ADDRESS_IN_USE;
683 return DBUS_ERROR_FILE_EXISTS;
687 return DBUS_ERROR_FILE_NOT_FOUND;
691 return DBUS_ERROR_FAILED;
695 * Converts the current system errno value into a #DBusError name.
697 * @returns an error name
700 _dbus_error_from_system_errno (void)
702 return _dbus_error_from_errno (errno);
706 * Assign 0 to the global errno variable
709 _dbus_set_errno_to_zero (void)
719 * See if errno is set
720 * @returns #TRUE if errno is not 0
723 _dbus_get_is_errno_nonzero (void)
729 * See if errno is ENOMEM
730 * @returns #TRUE if errno == ENOMEM
733 _dbus_get_is_errno_enomem (void)
735 return errno == ENOMEM;
739 * See if errno is EINTR
740 * @returns #TRUE if errno == EINTR
743 _dbus_get_is_errno_eintr (void)
745 return errno == EINTR;
749 * See if errno is EPIPE
750 * @returns #TRUE if errno == EPIPE
753 _dbus_get_is_errno_epipe (void)
755 return errno == EPIPE;
759 * Get error message from errno
760 * @returns _dbus_strerror(errno)
763 _dbus_strerror_from_errno (void)
765 return _dbus_strerror (errno);
768 /** @} end of sysdeps */
770 /* tests in dbus-sysdeps-util.c */