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...
49 _DBUS_DEFINE_GLOBAL_LOCK (win_fds);
50 _DBUS_DEFINE_GLOBAL_LOCK (sid_atom_cache);
51 _DBUS_DEFINE_GLOBAL_LOCK (system_users);
55 #elif (defined __APPLE__)
56 # include <crt_externs.h>
57 # define environ (*_NSGetEnviron())
59 extern char **environ;
63 * @defgroup DBusSysdeps Internal system-dependent API
64 * @ingroup DBusInternals
65 * @brief Internal system-dependent API available on UNIX and Windows
67 * The system-dependent API has a dual purpose. First, it encapsulates
68 * all usage of operating system APIs for ease of auditing and to
69 * avoid cluttering the rest of the code with bizarre OS quirks and
70 * headers. Second, it abstracts different operating system APIs for
77 * Aborts the program with SIGABRT (dumping core).
84 _dbus_print_backtrace ();
86 s = _dbus_getenv ("DBUS_BLOCK_ON_ABORT");
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);
95 _dbus_exit (1); /* in case someone manages to ignore SIGABRT ? */
99 * Wrapper for setenv(). If the value is #NULL, unsets
100 * the environment variable.
102 * There is an unfixable memleak in that it is unsafe to
103 * free memory malloced for use with setenv. This is because
104 * we can not rely on internal implementation details of
105 * the underlying libc library.
107 * @param varname name of environment variable
108 * @param value value of environment variable
109 * @returns #TRUE on success.
112 _dbus_setenv (const char *varname,
115 _dbus_assert (varname != NULL);
126 len = strlen (varname);
128 /* Use system malloc to avoid memleaks that dbus_malloc
129 * will get upset about.
132 putenv_value = malloc (len + 2);
133 if (putenv_value == NULL)
136 strcpy (putenv_value, varname);
137 #if defined(DBUS_WIN)
138 strcat (putenv_value, "=");
141 return (putenv (putenv_value) == 0);
147 return (setenv (varname, value, TRUE) == 0);
154 varname_len = strlen (varname);
155 value_len = strlen (value);
157 len = varname_len + value_len + 1 /* '=' */ ;
159 /* Use system malloc to avoid memleaks that dbus_malloc
160 * will get upset about.
163 putenv_value = malloc (len + 1);
164 if (putenv_value == NULL)
167 strcpy (putenv_value, varname);
168 strcpy (putenv_value + varname_len, "=");
169 strcpy (putenv_value + varname_len + 1, value);
171 return (putenv (putenv_value) == 0);
177 * Wrapper for getenv().
179 * @param varname name of environment variable
180 * @returns value of environment variable or #NULL if unset
183 _dbus_getenv (const char *varname)
185 #if defined(HAVE_SECURE_GETENV)
186 return secure_getenv (varname);
187 #elif defined(HAVE___SECURE_GETENV)
188 return __secure_getenv (varname);
190 /* Don't respect any environment variables if the current process is
191 * setuid. This is the equivalent of glibc's __secure_getenv().
193 if (_dbus_check_setuid ())
195 return getenv (varname);
200 * Wrapper for clearenv().
202 * @returns #TRUE on success.
205 _dbus_clearenv (void)
207 dbus_bool_t rc = TRUE;
210 if (clearenv () != 0)
222 * Split paths into a list of char strings
224 * @param dirs string with pathes
225 * @param suffix string concated to each path in dirs
226 * @param dir_list contains a list of splitted pathes
227 * return #TRUE is pathes could be splittes,#FALSE in oom case
230 _dbus_split_paths_and_append (DBusString *dirs,
238 DBusString file_suffix;
243 _dbus_string_init_const (&file_suffix, suffix);
245 len = _dbus_string_get_length (dirs);
247 while (_dbus_string_find (dirs, start, _DBUS_PATH_SEPARATOR, &i))
251 if (!_dbus_string_init (&path))
254 if (!_dbus_string_copy_len (dirs,
260 _dbus_string_free (&path);
264 _dbus_string_chop_white (&path);
266 /* check for an empty path */
267 if (_dbus_string_get_length (&path) == 0)
270 if (!_dbus_concat_dir_and_file (&path,
273 _dbus_string_free (&path);
277 if (!_dbus_string_copy_data(&path, &cpath))
279 _dbus_string_free (&path);
283 if (!_dbus_list_append (dir_list, cpath))
285 _dbus_string_free (&path);
291 _dbus_string_free (&path);
299 if (!_dbus_string_init (&path))
302 if (!_dbus_string_copy_len (dirs,
308 _dbus_string_free (&path);
312 if (!_dbus_concat_dir_and_file (&path,
315 _dbus_string_free (&path);
319 if (!_dbus_string_copy_data(&path, &cpath))
321 _dbus_string_free (&path);
325 if (!_dbus_list_append (dir_list, cpath))
327 _dbus_string_free (&path);
332 _dbus_string_free (&path);
338 _dbus_list_foreach (dir_list, (DBusForeachFunction)dbus_free, NULL);
339 _dbus_list_clear (dir_list);
346 * @addtogroup DBusString
351 * Appends an integer to a DBusString.
353 * @param str the string
354 * @param value the integer value
355 * @returns #FALSE if not enough memory or other failure.
358 _dbus_string_append_int (DBusString *str,
361 /* this calculation is from comp.lang.c faq */
362 #define MAX_LONG_LEN ((sizeof (long) * 8 + 2) / 3 + 1) /* +1 for '-' */
367 orig_len = _dbus_string_get_length (str);
369 if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
372 buf = _dbus_string_get_data_len (str, orig_len, MAX_LONG_LEN);
374 snprintf (buf, MAX_LONG_LEN, "%ld", value);
383 _dbus_string_shorten (str, MAX_LONG_LEN - i);
389 * Appends an unsigned integer to a DBusString.
391 * @param str the string
392 * @param value the integer value
393 * @returns #FALSE if not enough memory or other failure.
396 _dbus_string_append_uint (DBusString *str,
399 /* this is wrong, but definitely on the high side. */
400 #define MAX_ULONG_LEN (MAX_LONG_LEN * 2)
405 orig_len = _dbus_string_get_length (str);
407 if (!_dbus_string_lengthen (str, MAX_ULONG_LEN))
410 buf = _dbus_string_get_data_len (str, orig_len, MAX_ULONG_LEN);
412 snprintf (buf, MAX_ULONG_LEN, "%lu", value);
421 _dbus_string_shorten (str, MAX_ULONG_LEN - i);
427 * Parses an integer contained in a DBusString. Either return parameter
428 * may be #NULL if you aren't interested in it. The integer is parsed
429 * and stored in value_return. Return parameters are not initialized
430 * if the function returns #FALSE.
432 * @param str the string
433 * @param start the byte index of the start of the integer
434 * @param value_return return location of the integer value or #NULL
435 * @param end_return return location of the end of the integer, or #NULL
436 * @returns #TRUE on success
439 _dbus_string_parse_int (const DBusString *str,
448 p = _dbus_string_get_const_data_len (str, start,
449 _dbus_string_get_length (str) - start);
452 _dbus_set_errno_to_zero ();
453 v = strtol (p, &end, 0);
454 if (end == NULL || end == p || errno != 0)
460 *end_return = start + (end - p);
466 * Parses an unsigned integer contained in a DBusString. Either return
467 * parameter may be #NULL if you aren't interested in it. The integer
468 * is parsed and stored in value_return. Return parameters are not
469 * initialized if the function returns #FALSE.
471 * @param str the string
472 * @param start the byte index of the start of the integer
473 * @param value_return return location of the integer value or #NULL
474 * @param end_return return location of the end of the integer, or #NULL
475 * @returns #TRUE on success
478 _dbus_string_parse_uint (const DBusString *str,
480 unsigned long *value_return,
487 p = _dbus_string_get_const_data_len (str, start,
488 _dbus_string_get_length (str) - start);
491 _dbus_set_errno_to_zero ();
492 v = strtoul (p, &end, 0);
493 if (end == NULL || end == p || errno != 0)
499 *end_return = start + (end - p);
504 /** @} */ /* DBusString group */
507 * @addtogroup DBusInternalsUtils
512 _dbus_generate_pseudorandom_bytes_buffer (char *buffer,
518 /* fall back to pseudorandom */
519 _dbus_verbose ("Falling back to pseudorandom for %d bytes\n",
522 _dbus_get_real_time (NULL, &tv_usec);
532 b = (r / (double) RAND_MAX) * 255.0;
541 * Fills n_bytes of the given buffer with random bytes.
543 * @param buffer an allocated buffer
544 * @param n_bytes the number of bytes in buffer to write to
547 _dbus_generate_random_bytes_buffer (char *buffer,
552 if (!_dbus_string_init (&str))
554 _dbus_generate_pseudorandom_bytes_buffer (buffer, n_bytes);
558 if (!_dbus_generate_random_bytes (&str, n_bytes))
560 _dbus_string_free (&str);
561 _dbus_generate_pseudorandom_bytes_buffer (buffer, n_bytes);
565 _dbus_string_copy_to_buffer (&str, buffer, n_bytes);
567 _dbus_string_free (&str);
571 * Generates the given number of random bytes, where the bytes are
572 * chosen from the alphanumeric ASCII subset.
574 * @param str the string
575 * @param n_bytes the number of random ASCII bytes to append to string
576 * @returns #TRUE on success, #FALSE if no memory or other failure
579 _dbus_generate_random_ascii (DBusString *str,
582 static const char letters[] =
583 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
587 if (!_dbus_generate_random_bytes (str, n_bytes))
590 len = _dbus_string_get_length (str);
594 _dbus_string_set_byte (str, i,
595 letters[_dbus_string_get_byte (str, i) %
596 (sizeof (letters) - 1)]);
601 _dbus_assert (_dbus_string_validate_ascii (str, len - n_bytes,
608 * Converts a UNIX errno, or Windows errno or WinSock error value into
611 * @todo should cover more errnos, specifically those
614 * @param error_number the errno.
615 * @returns an error name
618 _dbus_error_from_errno (int error_number)
620 switch (error_number)
623 return DBUS_ERROR_FAILED;
625 #ifdef EPROTONOSUPPORT
626 case EPROTONOSUPPORT:
627 return DBUS_ERROR_NOT_SUPPORTED;
628 #elif defined(WSAEPROTONOSUPPORT)
629 case WSAEPROTONOSUPPORT:
630 return DBUS_ERROR_NOT_SUPPORTED;
634 return DBUS_ERROR_NOT_SUPPORTED;
635 #elif defined(WSAEAFNOSUPPORT)
636 case WSAEAFNOSUPPORT:
637 return DBUS_ERROR_NOT_SUPPORTED;
641 return DBUS_ERROR_LIMITS_EXCEEDED; /* kernel out of memory */
645 return DBUS_ERROR_LIMITS_EXCEEDED;
649 return DBUS_ERROR_ACCESS_DENIED;
653 return DBUS_ERROR_ACCESS_DENIED;
657 return DBUS_ERROR_NO_MEMORY;
661 return DBUS_ERROR_NO_MEMORY;
665 return DBUS_ERROR_NO_SERVER;
666 #elif defined(WSAECONNREFUSED)
667 case WSAECONNREFUSED:
668 return DBUS_ERROR_NO_SERVER;
672 return DBUS_ERROR_TIMEOUT;
673 #elif defined(WSAETIMEDOUT)
675 return DBUS_ERROR_TIMEOUT;
679 return DBUS_ERROR_NO_NETWORK;
680 #elif defined(WSAENETUNREACH)
682 return DBUS_ERROR_NO_NETWORK;
686 return DBUS_ERROR_ADDRESS_IN_USE;
687 #elif defined(WSAEADDRINUSE)
689 return DBUS_ERROR_ADDRESS_IN_USE;
693 return DBUS_ERROR_FILE_EXISTS;
697 return DBUS_ERROR_FILE_NOT_FOUND;
701 return DBUS_ERROR_FAILED;
705 * Converts the current system errno value into a #DBusError name.
707 * @returns an error name
710 _dbus_error_from_system_errno (void)
712 return _dbus_error_from_errno (errno);
716 * Assign 0 to the global errno variable
719 _dbus_set_errno_to_zero (void)
729 * See if errno is set
730 * @returns #TRUE if errno is not 0
733 _dbus_get_is_errno_nonzero (void)
739 * See if errno is ENOMEM
740 * @returns #TRUE if errno == ENOMEM
743 _dbus_get_is_errno_enomem (void)
745 return errno == ENOMEM;
749 * See if errno is EINTR
750 * @returns #TRUE if errno == EINTR
753 _dbus_get_is_errno_eintr (void)
755 return errno == EINTR;
759 * See if errno is EPIPE
760 * @returns #TRUE if errno == EPIPE
763 _dbus_get_is_errno_epipe (void)
765 return errno == EPIPE;
769 * Get error message from errno
770 * @returns _dbus_strerror(errno)
773 _dbus_strerror_from_errno (void)
775 return _dbus_strerror (errno);
778 /** @} end of sysdeps */
780 /* tests in dbus-sysdeps-util.c */