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"
32 #include "dbus-misc.h"
34 /* NOTE: If you include any unix/windows-specific headers here, you are probably doing something
35 * wrong and should be putting some code in dbus-sysdeps-unix.c or dbus-sysdeps-win.c.
37 * These are the standard ANSI C headers...
52 #elif (defined __APPLE__)
53 # include <crt_externs.h>
54 # define environ (*_NSGetEnviron())
56 extern char **environ;
60 * @defgroup DBusSysdeps Internal system-dependent API
61 * @ingroup DBusInternals
62 * @brief Internal system-dependent API available on UNIX and Windows
64 * The system-dependent API has a dual purpose. First, it encapsulates
65 * all usage of operating system APIs for ease of auditing and to
66 * avoid cluttering the rest of the code with bizarre OS quirks and
67 * headers. Second, it abstracts different operating system APIs for
74 * Aborts the program with SIGABRT (dumping core).
81 _dbus_print_backtrace ();
83 s = _dbus_getenv ("DBUS_BLOCK_ON_ABORT");
86 /* don't use _dbus_warn here since it can _dbus_abort() */
87 fprintf (stderr, " Process %lu sleeping for gdb attach\n", _dbus_pid_for_log ());
88 _dbus_sleep_milliseconds (1000 * 180);
92 _dbus_exit (1); /* in case someone manages to ignore SIGABRT ? */
98 * Wrapper for setenv(). If the value is #NULL, unsets
99 * the environment variable.
101 * There is an unfixable memleak in that it is unsafe to
102 * free memory malloced for use with setenv. This is because
103 * we can not rely on internal implementation details of
104 * the underlying libc library.
106 * This function is not thread-safe, because altering the environment
107 * in Unix is not thread-safe in general.
109 * @param varname name of environment variable
110 * @param value value of environment variable, or #NULL to unset
111 * @returns #TRUE on success, #FALSE if not enough memory.
114 dbus_setenv (const char *varname,
117 _dbus_assert (varname != NULL);
128 len = strlen (varname);
130 /* Use system malloc to avoid memleaks that dbus_malloc
131 * will get upset about.
134 putenv_value = malloc (len + 2);
135 if (putenv_value == NULL)
138 strcpy (putenv_value, varname);
139 #if defined(DBUS_WIN)
140 strcat (putenv_value, "=");
143 return (putenv (putenv_value) == 0);
149 return (setenv (varname, value, TRUE) == 0);
156 varname_len = strlen (varname);
157 value_len = strlen (value);
159 len = varname_len + value_len + 1 /* '=' */ ;
161 /* Use system malloc to avoid memleaks that dbus_malloc
162 * will get upset about.
165 putenv_value = malloc (len + 1);
166 if (putenv_value == NULL)
169 strcpy (putenv_value, varname);
170 strcpy (putenv_value + varname_len, "=");
171 strcpy (putenv_value + varname_len + 1, value);
173 return (putenv (putenv_value) == 0);
179 * Wrapper for getenv().
181 * @param varname name of environment variable
182 * @returns value of environment variable or #NULL if unset
185 _dbus_getenv (const char *varname)
187 /* Don't respect any environment variables if the current process is
188 * setuid. This is the equivalent of glibc's __secure_getenv().
190 if (_dbus_check_setuid ())
192 return getenv (varname);
196 * Wrapper for clearenv().
198 * @returns #TRUE on success.
201 _dbus_clearenv (void)
203 dbus_bool_t rc = TRUE;
206 if (clearenv () != 0)
218 * Split paths into a list of char strings
220 * @param dirs string with pathes
221 * @param suffix string concated to each path in dirs
222 * @param dir_list contains a list of splitted pathes
223 * return #TRUE is pathes could be splittes,#FALSE in oom case
226 _dbus_split_paths_and_append (DBusString *dirs,
234 DBusString file_suffix;
239 _dbus_string_init_const (&file_suffix, suffix);
241 len = _dbus_string_get_length (dirs);
243 while (_dbus_string_find (dirs, start, _DBUS_PATH_SEPARATOR, &i))
247 if (!_dbus_string_init (&path))
250 if (!_dbus_string_copy_len (dirs,
256 _dbus_string_free (&path);
260 _dbus_string_chop_white (&path);
262 /* check for an empty path */
263 if (_dbus_string_get_length (&path) == 0)
266 if (!_dbus_concat_dir_and_file (&path,
269 _dbus_string_free (&path);
273 if (!_dbus_string_copy_data(&path, &cpath))
275 _dbus_string_free (&path);
279 if (!_dbus_list_append (dir_list, cpath))
281 _dbus_string_free (&path);
287 _dbus_string_free (&path);
295 if (!_dbus_string_init (&path))
298 if (!_dbus_string_copy_len (dirs,
304 _dbus_string_free (&path);
308 if (!_dbus_concat_dir_and_file (&path,
311 _dbus_string_free (&path);
315 if (!_dbus_string_copy_data(&path, &cpath))
317 _dbus_string_free (&path);
321 if (!_dbus_list_append (dir_list, cpath))
323 _dbus_string_free (&path);
328 _dbus_string_free (&path);
334 _dbus_list_foreach (dir_list, (DBusForeachFunction)dbus_free, NULL);
335 _dbus_list_clear (dir_list);
342 * @addtogroup DBusString
347 * Appends an integer to a DBusString.
349 * @param str the string
350 * @param value the integer value
351 * @returns #FALSE if not enough memory or other failure.
354 _dbus_string_append_int (DBusString *str,
357 /* this calculation is from comp.lang.c faq */
358 #define MAX_LONG_LEN ((sizeof (long) * 8 + 2) / 3 + 1) /* +1 for '-' */
363 orig_len = _dbus_string_get_length (str);
365 if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
368 buf = _dbus_string_get_data_len (str, orig_len, MAX_LONG_LEN);
370 snprintf (buf, MAX_LONG_LEN, "%ld", value);
379 _dbus_string_shorten (str, MAX_LONG_LEN - i);
385 * Appends an unsigned integer to a DBusString.
387 * @param str the string
388 * @param value the integer value
389 * @returns #FALSE if not enough memory or other failure.
392 _dbus_string_append_uint (DBusString *str,
395 /* this is wrong, but definitely on the high side. */
396 #define MAX_ULONG_LEN (MAX_LONG_LEN * 2)
401 orig_len = _dbus_string_get_length (str);
403 if (!_dbus_string_lengthen (str, MAX_ULONG_LEN))
406 buf = _dbus_string_get_data_len (str, orig_len, MAX_ULONG_LEN);
408 snprintf (buf, MAX_ULONG_LEN, "%lu", value);
417 _dbus_string_shorten (str, MAX_ULONG_LEN - i);
423 * Parses an integer contained in a DBusString. Either return parameter
424 * may be #NULL if you aren't interested in it. The integer is parsed
425 * and stored in value_return. Return parameters are not initialized
426 * if the function returns #FALSE.
428 * @param str the string
429 * @param start the byte index of the start of the integer
430 * @param value_return return location of the integer value or #NULL
431 * @param end_return return location of the end of the integer, or #NULL
432 * @returns #TRUE on success
435 _dbus_string_parse_int (const DBusString *str,
444 p = _dbus_string_get_const_data_len (str, start,
445 _dbus_string_get_length (str) - start);
448 _dbus_set_errno_to_zero ();
449 v = strtol (p, &end, 0);
450 if (end == NULL || end == p || errno != 0)
456 *end_return = start + (end - p);
462 * Parses an unsigned integer contained in a DBusString. Either return
463 * parameter may be #NULL if you aren't interested in it. The integer
464 * is parsed and stored in value_return. Return parameters are not
465 * initialized if the function returns #FALSE.
467 * @param str the string
468 * @param start the byte index of the start of the integer
469 * @param value_return return location of the integer value or #NULL
470 * @param end_return return location of the end of the integer, or #NULL
471 * @returns #TRUE on success
474 _dbus_string_parse_uint (const DBusString *str,
476 unsigned long *value_return,
483 p = _dbus_string_get_const_data_len (str, start,
484 _dbus_string_get_length (str) - start);
487 _dbus_set_errno_to_zero ();
488 v = strtoul (p, &end, 0);
489 if (end == NULL || end == p || errno != 0)
495 *end_return = start + (end - p);
500 /** @} */ /* DBusString group */
503 * @addtogroup DBusInternalsUtils
508 * Fills n_bytes of the given buffer with random bytes.
510 * @param buffer an allocated buffer
511 * @param n_bytes the number of bytes in buffer to write to
512 * @param error location to store reason for failure
513 * @returns #TRUE on success
516 _dbus_generate_random_bytes_buffer (char *buffer,
522 if (!_dbus_string_init (&str))
524 _DBUS_SET_OOM (error);
528 if (!_dbus_generate_random_bytes (&str, n_bytes, error))
530 _dbus_string_free (&str);
534 _dbus_string_copy_to_buffer (&str, buffer, n_bytes);
536 _dbus_string_free (&str);
541 * Generates the given number of random bytes, where the bytes are
542 * chosen from the alphanumeric ASCII subset.
544 * @param str the string
545 * @param n_bytes the number of random ASCII bytes to append to string
546 * @param error location to store reason for failure
547 * @returns #TRUE on success, #FALSE if no memory or other failure
550 _dbus_generate_random_ascii (DBusString *str,
554 static const char letters[] =
555 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
559 if (!_dbus_generate_random_bytes (str, n_bytes, error))
562 len = _dbus_string_get_length (str);
566 _dbus_string_set_byte (str, i,
567 letters[_dbus_string_get_byte (str, i) %
568 (sizeof (letters) - 1)]);
573 _dbus_assert (_dbus_string_validate_ascii (str, len - n_bytes,
580 * Converts a UNIX errno, or Windows errno or WinSock error value into
583 * @todo should cover more errnos, specifically those
586 * @param error_number the errno.
587 * @returns an error name
590 _dbus_error_from_errno (int error_number)
592 switch (error_number)
595 return DBUS_ERROR_FAILED;
597 #ifdef EPROTONOSUPPORT
598 case EPROTONOSUPPORT:
599 return DBUS_ERROR_NOT_SUPPORTED;
600 #elif defined(WSAEPROTONOSUPPORT)
601 case WSAEPROTONOSUPPORT:
602 return DBUS_ERROR_NOT_SUPPORTED;
606 return DBUS_ERROR_NOT_SUPPORTED;
607 #elif defined(WSAEAFNOSUPPORT)
608 case WSAEAFNOSUPPORT:
609 return DBUS_ERROR_NOT_SUPPORTED;
613 return DBUS_ERROR_LIMITS_EXCEEDED; /* kernel out of memory */
617 return DBUS_ERROR_LIMITS_EXCEEDED;
621 return DBUS_ERROR_ACCESS_DENIED;
625 return DBUS_ERROR_ACCESS_DENIED;
629 return DBUS_ERROR_NO_MEMORY;
633 return DBUS_ERROR_NO_MEMORY;
637 return DBUS_ERROR_NO_SERVER;
638 #elif defined(WSAECONNREFUSED)
639 case WSAECONNREFUSED:
640 return DBUS_ERROR_NO_SERVER;
644 return DBUS_ERROR_TIMEOUT;
645 #elif defined(WSAETIMEDOUT)
647 return DBUS_ERROR_TIMEOUT;
651 return DBUS_ERROR_NO_NETWORK;
652 #elif defined(WSAENETUNREACH)
654 return DBUS_ERROR_NO_NETWORK;
658 return DBUS_ERROR_ADDRESS_IN_USE;
659 #elif defined(WSAEADDRINUSE)
661 return DBUS_ERROR_ADDRESS_IN_USE;
665 return DBUS_ERROR_FILE_EXISTS;
669 return DBUS_ERROR_FILE_NOT_FOUND;
673 return DBUS_ERROR_FAILED;
677 * Converts the current system errno value into a #DBusError name.
679 * @returns an error name
682 _dbus_error_from_system_errno (void)
684 return _dbus_error_from_errno (errno);
688 * Assign 0 to the global errno variable
691 _dbus_set_errno_to_zero (void)
701 * See if errno is ENOMEM
702 * @returns #TRUE if e == ENOMEM
705 _dbus_get_is_errno_enomem (int e)
711 * See if errno is EINTR
712 * @returns #TRUE if e == EINTR
715 _dbus_get_is_errno_eintr (int e)
721 * See if errno is EPIPE
722 * @returns #TRUE if errno == EPIPE
725 _dbus_get_is_errno_epipe (int e)
731 * See if errno is ETOOMANYREFS
732 * @returns #TRUE if errno == ETOOMANYREFS
735 _dbus_get_is_errno_etoomanyrefs (int e)
738 return e == ETOOMANYREFS;
745 * Get error message from errno
746 * @returns _dbus_strerror(errno)
749 _dbus_strerror_from_errno (void)
751 return _dbus_strerror (errno);
754 /** @} end of sysdeps */
756 /* tests in dbus-sysdeps-util.c */