-/* -*- mode: C; c-file-style: "gnu" -*- */
-/* dbus-sysdeps.c Wrappers around system/libc features (internal to D-BUS implementation)
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/* dbus-sysdeps.c Wrappers around system/libc features shared between UNIX and Windows (internal to D-Bus implementation)
*
- * Copyright (C) 2002 Red Hat, Inc.
+ * Copyright (C) 2002, 2003, 2006 Red Hat, Inc.
+ * Copyright (C) 2003 CodeFactory AB
*
- * Licensed under the Academic Free License version 1.2
+ * Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
+#include <config.h>
#include "dbus-internals.h"
#include "dbus-sysdeps.h"
#include "dbus-threads.h"
-#include <sys/types.h>
+#include "dbus-protocol.h"
+#include "dbus-string.h"
+#include "dbus-list.h"
+#include "dbus-misc.h"
+
+/* NOTE: If you include any unix/windows-specific headers here, you are probably doing something
+ * wrong and should be putting some code in dbus-sysdeps-unix.c or dbus-sysdeps-win.c.
+ *
+ * These are the standard ANSI C headers...
+ */
+#if HAVE_LOCALE_H
+#include <locale.h>
+#endif
#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
#include <stdio.h>
+
+#ifdef HAVE_ERRNO_H
#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/socket.h>
-#include <dirent.h>
-#include <sys/un.h>
-#include <pwd.h>
-#include <time.h>
-#include <sys/time.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-
-#ifdef HAVE_WRITEV
-#include <sys/uio.h>
-#endif
-#ifdef HAVE_POLL
-#include <sys/poll.h>
#endif
-#ifndef O_BINARY
-#define O_BINARY 0
+#ifdef DBUS_WIN
+ #include <stdlib.h>
+#elif (defined __APPLE__)
+# include <crt_externs.h>
+# define environ (*_NSGetEnviron())
+#else
+extern char **environ;
#endif
/**
- * @addtogroup DBusInternalsUtils
+ * @defgroup DBusSysdeps Internal system-dependent API
+ * @ingroup DBusInternals
+ * @brief Internal system-dependent API available on UNIX and Windows
+ *
+ * The system-dependent API has a dual purpose. First, it encapsulates
+ * all usage of operating system APIs for ease of auditing and to
+ * avoid cluttering the rest of the code with bizarre OS quirks and
+ * headers. Second, it abstracts different operating system APIs for
+ * portability.
+ *
* @{
*/
+
/**
* Aborts the program with SIGABRT (dumping core).
*/
void
_dbus_abort (void)
{
+ const char *s;
+
+ _dbus_print_backtrace ();
+
+ s = _dbus_getenv ("DBUS_BLOCK_ON_ABORT");
+ if (s && *s)
+ {
+ /* don't use _dbus_warn here since it can _dbus_abort() */
+ fprintf (stderr, " Process %lu sleeping for gdb attach\n", _dbus_pid_for_log ());
+ _dbus_sleep_milliseconds (1000 * 180);
+ }
+
abort ();
- _exit (1); /* in case someone manages to ignore SIGABRT */
+ _dbus_exit (1); /* in case someone manages to ignore SIGABRT ? */
}
/**
- * Wrapper for getenv().
+ * @ingroup DBusMisc
*
- * @param varname name of environment variable
- * @returns value of environment variable or #NULL if unset
- */
-const char*
-_dbus_getenv (const char *varname)
-{
- return getenv (varname);
-}
-
-/**
- * Thin wrapper around the read() system call that appends
- * the data it reads to the DBusString buffer. It appends
- * up to the given count, and returns the same value
- * and same errno as read(). The only exception is that
- * _dbus_read() handles EINTR for you.
+ * Wrapper for setenv(). If the value is #NULL, unsets
+ * the environment variable.
+ *
+ * There is an unfixable memleak in that it is unsafe to
+ * free memory malloced for use with setenv. This is because
+ * we can not rely on internal implementation details of
+ * the underlying libc library.
+ *
+ * This function is not thread-safe, because altering the environment
+ * in Unix is not thread-safe in general.
*
- * @param fd the file descriptor to read from
- * @param buffer the buffer to append data to
- * @param count the amount of data to read
- * @returns the number of bytes read or -1
+ * @param varname name of environment variable
+ * @param value value of environment variable, or #NULL to unset
+ * @returns #TRUE on success, #FALSE if not enough memory.
*/
-int
-_dbus_read (int fd,
- DBusString *buffer,
- int count)
+dbus_bool_t
+dbus_setenv (const char *varname,
+ const char *value)
{
- int bytes_read;
- int start;
- char *data;
-
- _dbus_assert (count >= 0);
+ _dbus_assert (varname != NULL);
- start = _dbus_string_get_length (buffer);
-
- if (!_dbus_string_lengthen (buffer, count))
+ if (value == NULL)
{
- errno = ENOMEM;
- return -1;
- }
+#ifdef HAVE_UNSETENV
+ unsetenv (varname);
+ return TRUE;
+#else
+ char *putenv_value;
+ size_t len;
- _dbus_string_get_data_len (buffer, &data, start, count);
+ len = strlen (varname);
- again:
-
- bytes_read = read (fd, data, count);
+ /* Use system malloc to avoid memleaks that dbus_malloc
+ * will get upset about.
+ */
+
+ putenv_value = malloc (len + 2);
+ if (putenv_value == NULL)
+ return FALSE;
- if (bytes_read < 0)
- {
- if (errno == EINTR)
- goto again;
- else
- {
- /* put length back (note that this doesn't actually realloc anything) */
- _dbus_string_set_length (buffer, start);
- return -1;
- }
+ strcpy (putenv_value, varname);
+#if defined(DBUS_WIN)
+ strcat (putenv_value, "=");
+#endif
+
+ return (putenv (putenv_value) == 0);
+#endif
}
else
{
- /* put length back (doesn't actually realloc) */
- _dbus_string_set_length (buffer, start + bytes_read);
+#ifdef HAVE_SETENV
+ return (setenv (varname, value, TRUE) == 0);
+#else
+ char *putenv_value;
+ size_t len;
+ size_t varname_len;
+ size_t value_len;
-#if 0
- if (bytes_read > 0)
- _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
-#endif
+ varname_len = strlen (varname);
+ value_len = strlen (value);
- return bytes_read;
- }
-}
-
-/**
- * Thin wrapper around the write() system call that writes a part of a
- * DBusString and handles EINTR for you.
- *
- * @param fd the file descriptor to write
- * @param buffer the buffer to write data from
- * @param start the first byte in the buffer to write
- * @param len the number of bytes to try to write
- * @returns the number of bytes written or -1 on error
- */
-int
-_dbus_write (int fd,
- const DBusString *buffer,
- int start,
- int len)
-{
- const char *data;
- int bytes_written;
-
- _dbus_string_get_const_data_len (buffer, &data, start, len);
-
- again:
+ len = varname_len + value_len + 1 /* '=' */ ;
- bytes_written = write (fd, data, len);
-
- if (bytes_written < 0 && errno == EINTR)
- goto again;
+ /* Use system malloc to avoid memleaks that dbus_malloc
+ * will get upset about.
+ */
+
+ putenv_value = malloc (len + 1);
+ if (putenv_value == NULL)
+ return FALSE;
-#if 0
- if (bytes_written > 0)
- _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
+ strcpy (putenv_value, varname);
+ strcpy (putenv_value + varname_len, "=");
+ strcpy (putenv_value + varname_len + 1, value);
+
+ return (putenv (putenv_value) == 0);
#endif
-
- return bytes_written;
+ }
}
/**
- * Like _dbus_write() but will use writev() if possible
- * to write both buffers in sequence. The return value
- * is the number of bytes written in the first buffer,
- * plus the number written in the second. If the first
- * buffer is written successfully and an error occurs
- * writing the second, the number of bytes in the first
- * is returned (i.e. the error is ignored), on systems that
- * don't have writev. Handles EINTR for you.
- * The second buffer may be #NULL.
+ * Wrapper for getenv().
*
- * @param fd the file descriptor
- * @param buffer1 first buffer
- * @param start1 first byte to write in first buffer
- * @param len1 number of bytes to write from first buffer
- * @param buffer2 second buffer, or #NULL
- * @param start2 first byte to write in second buffer
- * @param len2 number of bytes to write in second buffer
- * @returns total bytes written from both buffers, or -1 on error
+ * @param varname name of environment variable
+ * @returns value of environment variable or #NULL if unset
*/
-int
-_dbus_write_two (int fd,
- const DBusString *buffer1,
- int start1,
- int len1,
- const DBusString *buffer2,
- int start2,
- int len2)
-{
- _dbus_assert (buffer1 != NULL);
- _dbus_assert (start1 >= 0);
- _dbus_assert (start2 >= 0);
- _dbus_assert (len1 >= 0);
- _dbus_assert (len2 >= 0);
-
-#ifdef HAVE_WRITEV
- {
- struct iovec vectors[2];
- const char *data1;
- const char *data2;
- int bytes_written;
-
- _dbus_string_get_const_data_len (buffer1, &data1, start1, len1);
-
- if (buffer2 != NULL)
- _dbus_string_get_const_data_len (buffer2, &data2, start2, len2);
- else
- {
- data2 = NULL;
- start2 = 0;
- len2 = 0;
- }
-
- vectors[0].iov_base = (char*) data1;
- vectors[0].iov_len = len1;
- vectors[1].iov_base = (char*) data2;
- vectors[1].iov_len = len2;
-
- again:
-
- bytes_written = writev (fd,
- vectors,
- data2 ? 2 : 1);
-
- if (bytes_written < 0 && errno == EINTR)
- goto again;
-
- return bytes_written;
- }
-#else /* HAVE_WRITEV */
- {
- int ret1;
-
- ret1 = _dbus_write (fd, buffer1, start1, len1);
- if (ret1 == len1 && buffer2 != NULL)
- {
- ret2 = _dbus_write (fd, buffer2, start2, len2);
- if (ret2 < 0)
- ret2 = 0; /* we can't report an error as the first write was OK */
-
- return ret1 + ret2;
- }
- else
- return ret1;
- }
-#endif /* !HAVE_WRITEV */
+const char*
+_dbus_getenv (const char *varname)
+{
+ /* Don't respect any environment variables if the current process is
+ * setuid. This is the equivalent of glibc's __secure_getenv().
+ */
+ if (_dbus_check_setuid ())
+ return NULL;
+ return getenv (varname);
}
/**
- * Creates a socket and connects it to the UNIX domain socket at the
- * given path. The connection fd is returned, and is set up as
- * nonblocking.
+ * Wrapper for clearenv().
*
- * @param path the path to UNIX domain socket
- * @param result return location for error code
- * @returns connection file descriptor or -1 on error
+ * @returns #TRUE on success.
*/
-int
-_dbus_connect_unix_socket (const char *path,
- DBusResultCode *result)
+dbus_bool_t
+_dbus_clearenv (void)
{
- int fd;
- struct sockaddr_un addr;
-
- fd = socket (AF_LOCAL, SOCK_STREAM, 0);
-
- if (fd < 0)
- {
- dbus_set_result (result,
- _dbus_result_from_errno (errno));
-
- _dbus_verbose ("Failed to create socket: %s\n",
- _dbus_strerror (errno));
-
- return -1;
- }
-
- _DBUS_ZERO (addr);
- addr.sun_family = AF_LOCAL;
- strncpy (addr.sun_path, path, _DBUS_MAX_SUN_PATH_LENGTH);
- addr.sun_path[_DBUS_MAX_SUN_PATH_LENGTH] = '\0';
-
- if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
- {
- dbus_set_result (result,
- _dbus_result_from_errno (errno));
+ dbus_bool_t rc = TRUE;
- _dbus_verbose ("Failed to connect to socket %s: %s\n",
- path, _dbus_strerror (errno));
+#ifdef HAVE_CLEARENV
+ if (clearenv () != 0)
+ rc = FALSE;
+#else
- close (fd);
- fd = -1;
-
- return -1;
- }
-
- if (!_dbus_set_fd_nonblocking (fd, result))
- {
- close (fd);
- fd = -1;
-
- return -1;
- }
+ if (environ != NULL)
+ environ[0] = NULL;
+#endif
- return fd;
+ return rc;
}
/**
- * Creates a socket and binds it to the given path,
- * then listens on the socket. The socket is
- * set to be nonblocking.
- *
- * @param path the socket name
- * @param result return location for errors
- * @returns the listening file descriptor or -1 on error
- */
-int
-_dbus_listen_unix_socket (const char *path,
- DBusResultCode *result)
-{
- int listen_fd;
- struct sockaddr_un addr;
-
- listen_fd = socket (AF_LOCAL, SOCK_STREAM, 0);
-
- if (listen_fd < 0)
- {
- dbus_set_result (result, _dbus_result_from_errno (errno));
- _dbus_verbose ("Failed to create socket \"%s\": %s\n",
- path, _dbus_strerror (errno));
- return -1;
- }
-
- _DBUS_ZERO (addr);
- addr.sun_family = AF_LOCAL;
- strncpy (addr.sun_path, path, _DBUS_MAX_SUN_PATH_LENGTH);
- addr.sun_path[_DBUS_MAX_SUN_PATH_LENGTH] = '\0';
-
- if (bind (listen_fd, (struct sockaddr*) &addr, SUN_LEN (&addr)) < 0)
- {
- dbus_set_result (result, _dbus_result_from_errno (errno));
- _dbus_verbose ("Failed to bind socket \"%s\": %s\n",
- path, _dbus_strerror (errno));
- close (listen_fd);
- return -1;
- }
-
- if (listen (listen_fd, 30 /* backlog */) < 0)
- {
- dbus_set_result (result, _dbus_result_from_errno (errno));
- _dbus_verbose ("Failed to listen on socket \"%s\": %s\n",
- path, _dbus_strerror (errno));
- close (listen_fd);
- return -1;
- }
-
- if (!_dbus_set_fd_nonblocking (listen_fd, result))
- {
- close (listen_fd);
- return -1;
- }
-
- return listen_fd;
-}
-
-/* try to read a single byte and return #TRUE if we read it
- * and it's equal to nul.
+ * Split paths into a list of char strings
+ *
+ * @param dirs string with pathes
+ * @param suffix string concated to each path in dirs
+ * @param dir_list contains a list of splitted pathes
+ * return #TRUE is pathes could be splittes,#FALSE in oom case
*/
-static dbus_bool_t
-read_credentials_byte (int client_fd,
- DBusResultCode *result)
-{
- char buf[1];
- int bytes_read;
-
- again:
- bytes_read = read (client_fd, buf, 1);
- if (bytes_read < 0)
- {
- if (errno == EINTR)
- goto again;
- else
+dbus_bool_t
+_dbus_split_paths_and_append (DBusString *dirs,
+ const char *suffix,
+ DBusList **dir_list)
+{
+ int start;
+ int i;
+ int len;
+ char *cpath;
+ DBusString file_suffix;
+
+ start = 0;
+ i = 0;
+
+ _dbus_string_init_const (&file_suffix, suffix);
+
+ len = _dbus_string_get_length (dirs);
+
+ while (_dbus_string_find (dirs, start, _DBUS_PATH_SEPARATOR, &i))
+ {
+ DBusString path;
+
+ if (!_dbus_string_init (&path))
+ goto oom;
+
+ if (!_dbus_string_copy_len (dirs,
+ start,
+ i - start,
+ &path,
+ 0))
+ {
+ _dbus_string_free (&path);
+ goto oom;
+ }
+
+ _dbus_string_chop_white (&path);
+
+ /* check for an empty path */
+ if (_dbus_string_get_length (&path) == 0)
+ goto next;
+
+ if (!_dbus_concat_dir_and_file (&path,
+ &file_suffix))
+ {
+ _dbus_string_free (&path);
+ goto oom;
+ }
+
+ if (!_dbus_string_copy_data(&path, &cpath))
+ {
+ _dbus_string_free (&path);
+ goto oom;
+ }
+
+ if (!_dbus_list_append (dir_list, cpath))
+ {
+ _dbus_string_free (&path);
+ dbus_free (cpath);
+ goto oom;
+ }
+
+ next:
+ _dbus_string_free (&path);
+ start = i + 1;
+ }
+
+ if (start != len)
+ {
+ DBusString path;
+
+ if (!_dbus_string_init (&path))
+ goto oom;
+
+ if (!_dbus_string_copy_len (dirs,
+ start,
+ len - start,
+ &path,
+ 0))
{
- dbus_set_result (result, _dbus_result_from_errno (errno));
- _dbus_verbose ("Failed to read credentials byte: %s\n",
- _dbus_strerror (errno));
- return FALSE;
+ _dbus_string_free (&path);
+ goto oom;
}
- }
- else if (bytes_read == 0)
- {
- dbus_set_result (result, DBUS_RESULT_IO_ERROR);
- _dbus_verbose ("EOF reading credentials byte\n");
- return FALSE;
- }
- else
- {
- _dbus_assert (bytes_read == 1);
- if (buf[0] != '\0')
+ if (!_dbus_concat_dir_and_file (&path,
+ &file_suffix))
{
- dbus_set_result (result, DBUS_RESULT_FAILED);
- _dbus_verbose ("Credentials byte was not nul\n");
- return FALSE;
+ _dbus_string_free (&path);
+ goto oom;
}
- _dbus_verbose ("read credentials byte\n");
-
- return TRUE;
- }
-}
-
-static dbus_bool_t
-write_credentials_byte (int server_fd,
- DBusResultCode *result)
-{
- int bytes_written;
- char buf[1] = { '\0' };
-
- again:
-
- bytes_written = write (server_fd, buf, 1);
-
- if (bytes_written < 0 && errno == EINTR)
- goto again;
-
- if (bytes_written < 0)
- {
- dbus_set_result (result, _dbus_result_from_errno (errno));
- _dbus_verbose ("Failed to write credentials byte: %s\n",
- _dbus_strerror (errno));
- return FALSE;
- }
- else if (bytes_written == 0)
- {
- dbus_set_result (result, DBUS_RESULT_IO_ERROR);
- _dbus_verbose ("wrote zero bytes writing credentials byte\n");
- return FALSE;
- }
- else
- {
- _dbus_assert (bytes_written == 1);
- _dbus_verbose ("wrote credentials byte\n");
- return TRUE;
- }
-}
-
-/**
- * Reads a single byte which must be nul (an error occurs otherwise),
- * and reads unix credentials if available. Fills in pid/uid/gid with
- * -1 if no credentials are available. Return value indicates whether
- * a byte was read, not whether we got valid credentials. On some
- * systems, such as Linux, reading/writing the byte isn't actually
- * required, but we do it anyway just to avoid multiple codepaths.
- *
- * Fails if no byte is available, so you must select() first.
- *
- * The point of the byte is that on some systems we have to
- * use sendmsg()/recvmsg() to transmit credentials.
- *
- * @param client_fd the client file descriptor
- * @param credentials struct to fill with credentials of client
- * @param result location to store result code
- * @returns #TRUE on success
- */
-dbus_bool_t
-_dbus_read_credentials_unix_socket (int client_fd,
- DBusCredentials *credentials,
- DBusResultCode *result)
-{
- credentials->pid = -1;
- credentials->uid = -1;
- credentials->gid = -1;
-
-#ifdef SO_PEERCRED
- if (read_credentials_byte (client_fd, result))
- {
- struct ucred cr;
- int cr_len = sizeof (cr);
-
- if (getsockopt (client_fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) == 0 &&
- cr_len == sizeof (cr))
+ if (!_dbus_string_copy_data(&path, &cpath))
{
- credentials->pid = cr.pid;
- credentials->uid = cr.uid;
- credentials->gid = cr.gid;
- _dbus_verbose ("Got credentials pid %d uid %d gid %d\n",
- credentials->pid,
- credentials->uid,
- credentials->gid);
+ _dbus_string_free (&path);
+ goto oom;
}
- else
+
+ if (!_dbus_list_append (dir_list, cpath))
{
- _dbus_verbose ("Failed to getsockopt() credentials, returned len %d/%d: %s\n",
- cr_len, (int) sizeof (cr), _dbus_strerror (errno));
+ _dbus_string_free (&path);
+ dbus_free (cpath);
+ goto oom;
}
- return TRUE;
+ _dbus_string_free (&path);
}
- else
- return FALSE;
-#else /* !SO_PEERCRED */
- _dbus_verbose ("Socket credentials not supported on this OS\n");
- return TRUE;
-#endif
-}
-/**
- * Sends a single nul byte with our UNIX credentials as ancillary
- * data. Returns #TRUE if the data was successfully written. On
- * systems that don't support sending credentials, just writes a byte,
- * doesn't send any credentials. On some systems, such as Linux,
- * reading/writing the byte isn't actually required, but we do it
- * anyway just to avoid multiple codepaths.
- *
- * Fails if no byte can be written, so you must select() first.
- *
- * The point of the byte is that on some systems we have to
- * use sendmsg()/recvmsg() to transmit credentials.
- *
- * @param server_fd file descriptor for connection to server
- * @param result return location for error code
- * @returns #TRUE if the byte was sent
- */
-dbus_bool_t
-_dbus_send_credentials_unix_socket (int server_fd,
- DBusResultCode *result)
-{
- if (write_credentials_byte (server_fd, result))
- return TRUE;
- else
- return FALSE;
-}
+ return TRUE;
-/**
- * Accepts a connection on a listening socket.
- * Handles EINTR for you.
- *
- * @param listen_fd the listen file descriptor
- * @returns the connection fd of the client, or -1 on error
- */
-int
-_dbus_accept (int listen_fd)
-{
- int client_fd;
-
- retry:
- client_fd = accept (listen_fd, NULL, NULL);
-
- if (client_fd < 0)
- {
- if (errno == EINTR)
- goto retry;
- }
-
- return client_fd;
+ oom:
+ _dbus_list_foreach (dir_list, (DBusForeachFunction)dbus_free, NULL);
+ _dbus_list_clear (dir_list);
+ return FALSE;
}
/** @} */
if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
return FALSE;
- _dbus_string_get_data_len (str, &buf, orig_len, MAX_LONG_LEN);
+ buf = _dbus_string_get_data_len (str, orig_len, MAX_LONG_LEN);
snprintf (buf, MAX_LONG_LEN, "%ld", value);
if (!_dbus_string_lengthen (str, MAX_ULONG_LEN))
return FALSE;
- _dbus_string_get_data_len (str, &buf, orig_len, MAX_ULONG_LEN);
+ buf = _dbus_string_get_data_len (str, orig_len, MAX_ULONG_LEN);
snprintf (buf, MAX_ULONG_LEN, "%lu", value);
}
/**
- * Appends a double to a DBusString.
- *
- * @param str the string
- * @param value the floating point value
- * @returns #FALSE if not enough memory or other failure.
- */
-dbus_bool_t
-_dbus_string_append_double (DBusString *str,
- double value)
-{
-#define MAX_DOUBLE_LEN 64 /* this is completely made up :-/ */
- int orig_len;
- char *buf;
- int i;
-
- orig_len = _dbus_string_get_length (str);
-
- if (!_dbus_string_lengthen (str, MAX_DOUBLE_LEN))
- return FALSE;
-
- _dbus_string_get_data_len (str, &buf, orig_len, MAX_DOUBLE_LEN);
-
- snprintf (buf, MAX_LONG_LEN, "%g", value);
-
- i = 0;
- while (*buf)
- {
- ++buf;
- ++i;
- }
-
- _dbus_string_shorten (str, MAX_DOUBLE_LEN - i);
-
- return TRUE;
-}
-
-/**
* Parses an integer contained in a DBusString. Either return parameter
* may be #NULL if you aren't interested in it. The integer is parsed
* and stored in value_return. Return parameters are not initialized
const char *p;
char *end;
- _dbus_string_get_const_data_len (str, &p, start,
- _dbus_string_get_length (str) - start);
+ p = _dbus_string_get_const_data_len (str, start,
+ _dbus_string_get_length (str) - start);
end = NULL;
- errno = 0;
+ _dbus_set_errno_to_zero ();
v = strtol (p, &end, 0);
if (end == NULL || end == p || errno != 0)
return FALSE;
if (value_return)
*value_return = v;
if (end_return)
- *end_return = (end - p);
+ *end_return = start + (end - p);
return TRUE;
}
/**
- * Parses a floating point number contained in a DBusString. Either
- * return parameter may be #NULL if you aren't interested in it. The
- * integer is parsed and stored in value_return. Return parameters are
- * not initialized if the function returns #FALSE.
- *
- * @todo this function is currently locale-dependent. Should
- * ask alexl to relicense g_ascii_strtod() code and put that in
- * here instead, so it's locale-independent.
+ * Parses an unsigned integer contained in a DBusString. Either return
+ * parameter may be #NULL if you aren't interested in it. The integer
+ * is parsed and stored in value_return. Return parameters are not
+ * initialized if the function returns #FALSE.
*
* @param str the string
- * @param start the byte index of the start of the float
- * @param value_return return location of the float value or #NULL
- * @param end_return return location of the end of the float, or #NULL
+ * @param start the byte index of the start of the integer
+ * @param value_return return location of the integer value or #NULL
+ * @param end_return return location of the end of the integer, or #NULL
* @returns #TRUE on success
*/
dbus_bool_t
-_dbus_string_parse_double (const DBusString *str,
- int start,
- double *value_return,
- int *end_return)
+_dbus_string_parse_uint (const DBusString *str,
+ int start,
+ unsigned long *value_return,
+ int *end_return)
{
- double v;
+ unsigned long v;
const char *p;
char *end;
- _dbus_warn ("_dbus_string_parse_double() needs to be made locale-independent\n");
-
- _dbus_string_get_const_data_len (str, &p, start,
- _dbus_string_get_length (str) - start);
+ p = _dbus_string_get_const_data_len (str, start,
+ _dbus_string_get_length (str) - start);
end = NULL;
- errno = 0;
- v = strtod (p, &end);
+ _dbus_set_errno_to_zero ();
+ v = strtoul (p, &end, 0);
if (end == NULL || end == p || errno != 0)
return FALSE;
if (value_return)
*value_return = v;
if (end_return)
- *end_return = (end - p);
+ *end_return = start + (end - p);
return TRUE;
}
+/** @} */ /* DBusString group */
+
/**
- * Gets the credentials corresponding to the given username.
- *
- * @param username the username
- * @param credentials credentials to fill in
- * @returns #TRUE if the username existed and we got some credentials
+ * @addtogroup DBusInternalsUtils
+ * @{
*/
-dbus_bool_t
-_dbus_credentials_from_username (const DBusString *username,
- DBusCredentials *credentials)
+
+void
+_dbus_generate_pseudorandom_bytes_buffer (char *buffer,
+ int n_bytes)
{
- const char *username_c_str;
+ long tv_usec;
+ int i;
- credentials->pid = -1;
- credentials->uid = -1;
- credentials->gid = -1;
-
- _dbus_string_get_const_data (username, &username_c_str);
+ /* fall back to pseudorandom */
+ _dbus_verbose ("Falling back to pseudorandom for %d bytes\n",
+ n_bytes);
-#ifdef HAVE_GETPWNAM_R
- {
- struct passwd *p;
- int result;
- char buf[1024];
- struct passwd p_str;
-
- p = NULL;
- result = getpwnam_r (username_c_str, &p_str, buf, sizeof (buf),
- &p);
-
- if (result == 0 && p == &p_str)
- {
- credentials->uid = p->pw_uid;
- credentials->gid = p->pw_gid;
-
- _dbus_verbose ("Username %s has uid %d gid %d\n",
- username_c_str, credentials->uid, credentials->gid);
- return TRUE;
- }
- else
- {
- _dbus_verbose ("User %s unknown\n", username_c_str);
- return FALSE;
- }
- }
-#else /* ! HAVE_GETPWNAM_R */
- {
- /* I guess we're screwed on thread safety here */
- struct passwd *p;
-
- p = getpwnam (username_c_str);
-
- if (p != NULL)
- {
- credentials->uid = p->pw_uid;
- credentials->gid = p->pw_gid;
-
- _dbus_verbose ("Username %s has uid %d gid %d\n",
- username_c_str, credentials->uid, credentials->gid);
- return TRUE;
- }
- else
- {
- _dbus_verbose ("User %s unknown\n", username_c_str);
- return FALSE;
- }
- }
-#endif
+ _dbus_get_real_time (NULL, &tv_usec);
+ srand (tv_usec);
+
+ i = 0;
+ while (i < n_bytes)
+ {
+ double r;
+ unsigned int b;
+
+ r = rand ();
+ b = (r / (double) RAND_MAX) * 255.0;
+
+ buffer[i] = b;
+
+ ++i;
+ }
}
/**
- * Gets credentials from a UID string. (Parses a string to a UID
- * and converts to a DBusCredentials.)
+ * Fills n_bytes of the given buffer with random bytes.
*
- * @param uid_str the UID in string form
- * @param credentials credentials to fill in
- * @returns #TRUE if successfully filled in some credentials
+ * @param buffer an allocated buffer
+ * @param n_bytes the number of bytes in buffer to write to
*/
-dbus_bool_t
-_dbus_credentials_from_uid_string (const DBusString *uid_str,
- DBusCredentials *credentials)
+void
+_dbus_generate_random_bytes_buffer (char *buffer,
+ int n_bytes)
{
- int end;
- long uid;
+ DBusString str;
- credentials->pid = -1;
- credentials->uid = -1;
- credentials->gid = -1;
-
- if (_dbus_string_get_length (uid_str) == 0)
+ if (!_dbus_string_init (&str))
{
- _dbus_verbose ("UID string was zero length\n");
- return FALSE;
+ _dbus_generate_pseudorandom_bytes_buffer (buffer, n_bytes);
+ return;
}
- uid = -1;
- end = 0;
- if (!_dbus_string_parse_int (uid_str, 0, &uid,
- &end))
- {
- _dbus_verbose ("could not parse string as a UID\n");
- return FALSE;
- }
-
- if (end != _dbus_string_get_length (uid_str))
+ if (!_dbus_generate_random_bytes (&str, n_bytes))
{
- _dbus_verbose ("string contained trailing stuff after UID\n");
- return FALSE;
+ _dbus_string_free (&str);
+ _dbus_generate_pseudorandom_bytes_buffer (buffer, n_bytes);
+ return;
}
- credentials->uid = uid;
+ _dbus_string_copy_to_buffer (&str, buffer, n_bytes);
- return TRUE;
+ _dbus_string_free (&str);
}
/**
- * Gets the credentials of the current process.
+ * Generates the given number of random bytes, where the bytes are
+ * chosen from the alphanumeric ASCII subset.
*
- * @param credentials credentials to fill in.
+ * @param str the string
+ * @param n_bytes the number of random ASCII bytes to append to string
+ * @returns #TRUE on success, #FALSE if no memory or other failure
*/
-void
-_dbus_credentials_from_current_process (DBusCredentials *credentials)
+dbus_bool_t
+_dbus_generate_random_ascii (DBusString *str,
+ int n_bytes)
{
- credentials->pid = getpid ();
- credentials->uid = getuid ();
- credentials->gid = getgid ();
+ static const char letters[] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
+ int i;
+ int len;
+
+ if (!_dbus_generate_random_bytes (str, n_bytes))
+ return FALSE;
+
+ len = _dbus_string_get_length (str);
+ i = len - n_bytes;
+ while (i < len)
+ {
+ _dbus_string_set_byte (str, i,
+ letters[_dbus_string_get_byte (str, i) %
+ (sizeof (letters) - 1)]);
+
+ ++i;
+ }
+
+ _dbus_assert (_dbus_string_validate_ascii (str, len - n_bytes,
+ n_bytes));
+
+ return TRUE;
}
/**
- * Checks whether the provided_credentials are allowed to log in
- * as the expected_credentials.
+ * Converts a UNIX errno, or Windows errno or WinSock error value into
+ * a #DBusError name.
*
- * @param expected_credentials credentials we're trying to log in as
- * @param provided_credentials credentials we have
- * @returns #TRUE if we can log in
+ * @todo should cover more errnos, specifically those
+ * from open().
+ *
+ * @param error_number the errno.
+ * @returns an error name
*/
-dbus_bool_t
-_dbus_credentials_match (const DBusCredentials *expected_credentials,
- const DBusCredentials *provided_credentials)
+const char*
+_dbus_error_from_errno (int error_number)
{
- if (provided_credentials->uid < 0)
- return FALSE;
- else if (expected_credentials->uid < 0)
- return FALSE;
- else if (provided_credentials->uid == 0)
- return TRUE;
- else if (provided_credentials->uid == expected_credentials->uid)
- return TRUE;
- else
- return FALSE;
-}
-
-/**
- * Appends the uid of the current process to the given string.
- *
- * @param str the string to append to
- * @returns #TRUE on success
- */
-dbus_bool_t
-_dbus_string_append_our_uid (DBusString *str)
-{
- return _dbus_string_append_int (str, getuid ());
-}
-
-
-static DBusMutex *atomic_lock = NULL;
-DBusMutex *_dbus_atomic_init_lock (void);
-DBusMutex *
-_dbus_atomic_init_lock (void)
-{
- atomic_lock = dbus_mutex_new ();
- return atomic_lock;
-}
-
-/**
- * Atomically increments an integer
- *
- * @param atomic pointer to the integer to increment
- * @returns the value after incrementing
- *
- * @todo implement arch-specific faster atomic ops
- */
-dbus_atomic_t
-_dbus_atomic_inc (dbus_atomic_t *atomic)
-{
- dbus_atomic_t res;
-
- dbus_mutex_lock (atomic_lock);
- *atomic += 1;
- res = *atomic;
- dbus_mutex_unlock (atomic_lock);
- return res;
-}
-
-/**
- * Atomically decrement an integer
- *
- * @param atomic pointer to the integer to decrement
- * @returns the value after decrementing
- *
- * @todo implement arch-specific faster atomic ops
- */
-dbus_atomic_t
-_dbus_atomic_dec (dbus_atomic_t *atomic)
-{
- dbus_atomic_t res;
-
- dbus_mutex_lock (atomic_lock);
- *atomic -= 1;
- res = *atomic;
- dbus_mutex_unlock (atomic_lock);
- return res;
-}
-
-/**
- * Wrapper for poll().
- *
- * @todo need a fallback implementation using select()
- *
- * @param fds the file descriptors to poll
- * @param n_fds number of descriptors in the array
- * @param timeout_milliseconds timeout or -1 for infinite
- * @returns numbers of fds with revents, or <0 on error
- */
-int
-_dbus_poll (DBusPollFD *fds,
- int n_fds,
- int timeout_milliseconds)
-{
-#ifdef HAVE_POLL
- /* This big thing is a constant expression and should get optimized
- * out of existence. So it's more robust than a configure check at
- * no cost.
- */
- if (_DBUS_POLLIN == POLLIN &&
- _DBUS_POLLPRI == POLLPRI &&
- _DBUS_POLLOUT == POLLOUT &&
- _DBUS_POLLERR == POLLERR &&
- _DBUS_POLLHUP == POLLHUP &&
- _DBUS_POLLNVAL == POLLNVAL &&
- sizeof (DBusPollFD) == sizeof (struct pollfd) &&
- _DBUS_STRUCT_OFFSET (DBusPollFD, fd) ==
- _DBUS_STRUCT_OFFSET (struct pollfd, fd) &&
- _DBUS_STRUCT_OFFSET (DBusPollFD, events) ==
- _DBUS_STRUCT_OFFSET (struct pollfd, events) &&
- _DBUS_STRUCT_OFFSET (DBusPollFD, revents) ==
- _DBUS_STRUCT_OFFSET (struct pollfd, revents))
+ switch (error_number)
{
- return poll ((struct pollfd*) fds,
- n_fds,
- timeout_milliseconds);
- }
- else
- {
- /* We have to convert the DBusPollFD to an array of
- * struct pollfd, poll, and convert back.
- */
- _dbus_warn ("didn't implement poll() properly for this system yet\n");
- return -1;
- }
-#else /* ! HAVE_POLL */
- _dbus_warn ("need to implement select() fallback for systems with no poll()\n");
- return -1;
+ case 0:
+ return DBUS_ERROR_FAILED;
+
+#ifdef EPROTONOSUPPORT
+ case EPROTONOSUPPORT:
+ return DBUS_ERROR_NOT_SUPPORTED;
+#elif defined(WSAEPROTONOSUPPORT)
+ case WSAEPROTONOSUPPORT:
+ return DBUS_ERROR_NOT_SUPPORTED;
#endif
-}
-
-/** nanoseconds in a second */
-#define NANOSECONDS_PER_SECOND 1000000000
-/** microseconds in a second */
-#define MICROSECONDS_PER_SECOND 1000000
-/** milliseconds in a second */
-#define MILLISECONDS_PER_SECOND 1000
-/** nanoseconds in a millisecond */
-#define NANOSECONDS_PER_MILLISECOND 1000000
-/** microseconds in a millisecond */
-#define MICROSECONDS_PER_MILLISECOND 1000
-
-/**
- * Sleeps the given number of milliseconds.
- * @param milliseconds number of milliseconds
- */
-void
-_dbus_sleep_milliseconds (int milliseconds)
-{
-#ifdef HAVE_NANOSLEEP
- struct timespec req;
- struct timespec rem;
-
- req.tv_sec = milliseconds / MILLISECONDS_PER_SECOND;
- req.tv_nsec = (milliseconds % MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND;
- rem.tv_sec = 0;
- rem.tv_nsec = 0;
-
- while (nanosleep (&req, &rem) < 0 && errno == EINTR)
- req = rem;
-#elif defined (HAVE_USLEEP)
- usleep (milliseconds * MICROSECONDS_PER_MILLISECOND);
-#else /* ! HAVE_USLEEP */
- sleep (MAX (milliseconds / 1000, 1));
+#ifdef EAFNOSUPPORT
+ case EAFNOSUPPORT:
+ return DBUS_ERROR_NOT_SUPPORTED;
+#elif defined(WSAEAFNOSUPPORT)
+ case WSAEAFNOSUPPORT:
+ return DBUS_ERROR_NOT_SUPPORTED;
+#endif
+#ifdef ENFILE
+ case ENFILE:
+ return DBUS_ERROR_LIMITS_EXCEEDED; /* kernel out of memory */
+#endif
+#ifdef EMFILE
+ case EMFILE:
+ return DBUS_ERROR_LIMITS_EXCEEDED;
+#endif
+#ifdef EACCES
+ case EACCES:
+ return DBUS_ERROR_ACCESS_DENIED;
+#endif
+#ifdef EPERM
+ case EPERM:
+ return DBUS_ERROR_ACCESS_DENIED;
+#endif
+#ifdef ENOBUFS
+ case ENOBUFS:
+ return DBUS_ERROR_NO_MEMORY;
+#endif
+#ifdef ENOMEM
+ case ENOMEM:
+ return DBUS_ERROR_NO_MEMORY;
+#endif
+#ifdef ECONNREFUSED
+ case ECONNREFUSED:
+ return DBUS_ERROR_NO_SERVER;
+#elif defined(WSAECONNREFUSED)
+ case WSAECONNREFUSED:
+ return DBUS_ERROR_NO_SERVER;
+#endif
+#ifdef ETIMEDOUT
+ case ETIMEDOUT:
+ return DBUS_ERROR_TIMEOUT;
+#elif defined(WSAETIMEDOUT)
+ case WSAETIMEDOUT:
+ return DBUS_ERROR_TIMEOUT;
+#endif
+#ifdef ENETUNREACH
+ case ENETUNREACH:
+ return DBUS_ERROR_NO_NETWORK;
+#elif defined(WSAENETUNREACH)
+ case WSAENETUNREACH:
+ return DBUS_ERROR_NO_NETWORK;
+#endif
+#ifdef EADDRINUSE
+ case EADDRINUSE:
+ return DBUS_ERROR_ADDRESS_IN_USE;
+#elif defined(WSAEADDRINUSE)
+ case WSAEADDRINUSE:
+ return DBUS_ERROR_ADDRESS_IN_USE;
+#endif
+#ifdef EEXIST
+ case EEXIST:
+ return DBUS_ERROR_FILE_EXISTS;
+#endif
+#ifdef ENOENT
+ case ENOENT:
+ return DBUS_ERROR_FILE_NOT_FOUND;
#endif
-}
-
-/**
- * Get current time, as in gettimeofday().
- *
- * @param tv_sec return location for number of seconds
- * @param tv_usec return location for number of microseconds (thousandths)
- */
-void
-_dbus_get_current_time (long *tv_sec,
- long *tv_usec)
-{
- struct timeval t;
-
- gettimeofday (&t, NULL);
-
- if (tv_sec)
- *tv_sec = t.tv_sec;
- if (tv_usec)
- *tv_usec = t.tv_usec;
-}
-
-/**
- * Appends the contents of the given file to the string,
- * returning result code. At the moment, won't open a file
- * more than a megabyte in size.
- *
- * @param str the string to append to
- * @param filename filename to load
- * @returns result
- */
-DBusResultCode
-_dbus_file_get_contents (DBusString *str,
- const DBusString *filename)
-{
- int fd;
- struct stat sb;
- int orig_len;
- int total;
- const char *filename_c;
-
- _dbus_string_get_const_data (filename, &filename_c);
-
- /* O_BINARY useful on Cygwin */
- fd = open (filename_c, O_RDONLY | O_BINARY);
- if (fd < 0)
- return _dbus_result_from_errno (errno);
-
- if (fstat (fd, &sb) < 0)
- {
- DBusResultCode result;
-
- result = _dbus_result_from_errno (errno); /* prior to close() */
-
- _dbus_verbose ("fstat() failed: %s",
- _dbus_strerror (errno));
-
- close (fd);
-
- return result;
- }
-
- if (sb.st_size > _DBUS_ONE_MEGABYTE)
- {
- _dbus_verbose ("File size %lu is too large.\n",
- (unsigned long) sb.st_size);
- close (fd);
- return DBUS_RESULT_FAILED;
}
-
- total = 0;
- orig_len = _dbus_string_get_length (str);
- if (sb.st_size > 0 && S_ISREG (sb.st_mode))
- {
- int bytes_read;
- while (total < (int) sb.st_size)
- {
- bytes_read = _dbus_read (fd, str,
- sb.st_size - total);
- if (bytes_read <= 0)
- {
- DBusResultCode result;
-
- result = _dbus_result_from_errno (errno); /* prior to close() */
-
- _dbus_verbose ("read() failed: %s",
- _dbus_strerror (errno));
-
- close (fd);
- _dbus_string_set_length (str, orig_len);
- return result;
- }
- else
- total += bytes_read;
- }
-
- close (fd);
- return DBUS_RESULT_SUCCESS;
- }
- else if (sb.st_size != 0)
- {
- _dbus_verbose ("Can only open regular files at the moment.\n");
- close (fd);
- return DBUS_RESULT_FAILED;
- }
- else
- {
- close (fd);
- return DBUS_RESULT_SUCCESS;
- }
+ return DBUS_ERROR_FAILED;
}
/**
- * Writes a string out to a file.
+ * Converts the current system errno value into a #DBusError name.
*
- * @param str the string to write out
- * @param filename the file to save string to
- * @returns result code
+ * @returns an error name
*/
-DBusResultCode
-_dbus_string_save_to_file (const DBusString *str,
- const DBusString *filename)
+const char*
+_dbus_error_from_system_errno (void)
{
- int fd;
- int bytes_to_write;
- const char *filename_c;
- int total;
-
- _dbus_string_get_const_data (filename, &filename_c);
-
- fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
- 0600);
- if (fd < 0)
- return _dbus_result_from_errno (errno);
-
- total = 0;
- bytes_to_write = _dbus_string_get_length (str);
-
- while (total < bytes_to_write)
- {
- int bytes_written;
-
- bytes_written = _dbus_write (fd, str, total,
- bytes_to_write - total);
-
- if (bytes_written <= 0)
- {
- DBusResultCode result;
-
- result = _dbus_result_from_errno (errno); /* prior to close() */
-
- _dbus_verbose ("write() failed: %s",
- _dbus_strerror (errno));
-
- close (fd);
- return result;
- }
-
- total += bytes_written;
- }
-
- close (fd);
- return DBUS_RESULT_SUCCESS;
+ return _dbus_error_from_errno (errno);
}
/**
- * Appends the given filename to the given directory.
- *
- * @param dir the directory name
- * @param next_component the filename
- * @returns #TRUE on success
+ * Assign 0 to the global errno variable
*/
-dbus_bool_t
-_dbus_concat_dir_and_file (DBusString *dir,
- const DBusString *next_component)
+void
+_dbus_set_errno_to_zero (void)
{
- dbus_bool_t dir_ends_in_slash;
- dbus_bool_t file_starts_with_slash;
-
- if (_dbus_string_get_length (dir) == 0 ||
- _dbus_string_get_length (next_component) == 0)
- return TRUE;
-
- dir_ends_in_slash = '/' == _dbus_string_get_byte (dir,
- _dbus_string_get_length (dir) - 1);
-
- file_starts_with_slash = '/' == _dbus_string_get_byte (next_component, 0);
-
- if (dir_ends_in_slash && file_starts_with_slash)
- {
- _dbus_string_shorten (dir, 1);
- }
- else if (!(dir_ends_in_slash || file_starts_with_slash))
- {
- if (!_dbus_string_append_byte (dir, '/'))
- return FALSE;
- }
-
- return _dbus_string_copy (next_component, 0, dir,
- _dbus_string_get_length (dir));
+#ifdef DBUS_WINCE
+ SetLastError (0);
+#else
+ errno = 0;
+#endif
}
-struct DBusDirIter
-{
- DIR *d;
-
-};
-
/**
- * Open a directory to iterate over.
- *
- * @param filename the directory name
- * @param result return location for error code if #NULL returned
- * @returns new iterator, or #NULL on error
+ * See if errno is set
+ * @returns #TRUE if errno is not 0
*/
-DBusDirIter*
-_dbus_directory_open (const DBusString *filename,
- DBusResultCode *result)
+dbus_bool_t
+_dbus_get_is_errno_nonzero (void)
{
- DIR *d;
- DBusDirIter *iter;
- const char *filename_c;
-
- _dbus_string_get_const_data (filename, &filename_c);
-
- d = opendir (filename_c);
- if (d == NULL)
- {
- dbus_set_result (result, _dbus_result_from_errno (errno));
- return NULL;
- }
-
- iter = dbus_new0 (DBusDirIter, 1);
- if (iter == NULL)
- {
- closedir (d);
- dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
- return NULL;
- }
-
- iter->d = d;
-
- return iter;
+ return errno != 0;
}
/**
- * Get next file in the directory. Will not return "." or ".."
- * on UNIX. If an error occurs, the contents of "filename"
- * are undefined. #DBUS_RESULT_SUCCESS is always returned
- * in result if no error occurs.
- *
- * @todo for thread safety, I think we have to use
- * readdir_r(). (GLib has the same issue, should file a bug.)
- *
- * @param iter the iterator
- * @param filename string to be set to the next file in the dir
- * @param result return location for error, or #DBUS_RESULT_SUCCESS
- * @returns #TRUE if filename was filled in with a new filename
+ * See if errno is ENOMEM
+ * @returns #TRUE if errno == ENOMEM
*/
dbus_bool_t
-_dbus_directory_get_next_file (DBusDirIter *iter,
- DBusString *filename,
- DBusResultCode *result)
+_dbus_get_is_errno_enomem (void)
{
- /* we always have to put something in result, since return
- * value means whether there's a filename and doesn't
- * reliably indicate whether an error was set.
- */
- struct dirent *ent;
-
- dbus_set_result (result, DBUS_RESULT_SUCCESS);
-
- again:
- errno = 0;
- ent = readdir (iter->d);
- if (ent == NULL)
- {
- dbus_set_result (result,
- _dbus_result_from_errno (errno));
- return FALSE;
- }
- else if (ent->d_name[0] == '.' &&
- (ent->d_name[1] == '\0' ||
- (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
- goto again;
- else
- {
- _dbus_string_set_length (filename, 0);
- if (!_dbus_string_append (filename, ent->d_name))
- {
- dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
- return FALSE;
- }
- else
- return TRUE;
- }
+ return errno == ENOMEM;
}
/**
- * Closes a directory iteration.
+ * See if errno is EINTR
+ * @returns #TRUE if errno == EINTR
*/
-void
-_dbus_directory_close (DBusDirIter *iter)
+dbus_bool_t
+_dbus_get_is_errno_eintr (void)
{
- closedir (iter->d);
- dbus_free (iter);
+ return errno == EINTR;
}
/**
- * Generates the given number of random bytes,
- * using the best mechanism we can come up with.
- *
- * @param str the string
- * @param n_bytes the number of random bytes to append to string
- * @returns #TRUE on success, #FALSE if no memory or other failure
+ * See if errno is EPIPE
+ * @returns #TRUE if errno == EPIPE
*/
dbus_bool_t
-_dbus_generate_random_bytes (DBusString *str,
- int n_bytes)
-{
- int old_len;
- int fd;
-
- old_len = _dbus_string_get_length (str);
- fd = -1;
-
- /* note, urandom on linux will fall back to pseudorandom */
- fd = open ("/dev/urandom", O_RDONLY);
- if (fd < 0)
- {
- unsigned long tv_usec;
- int i;
-
- /* fall back to pseudorandom */
-
- _dbus_get_current_time (NULL, &tv_usec);
- srand (tv_usec);
-
- i = 0;
- while (i < n_bytes)
- {
- double r;
- int b;
-
- r = rand ();
- b = (r / (double) RAND_MAX) * 255.0;
-
- if (!_dbus_string_append_byte (str, b))
- goto failed;
-
- ++i;
- }
-
- return TRUE;
- }
- else
- {
- if (_dbus_read (fd, str, n_bytes) != n_bytes)
- goto failed;
-
- close (fd);
-
- return TRUE;
- }
-
- failed:
- _dbus_string_set_length (str, old_len);
- if (fd >= 0)
- close (fd);
- return FALSE;
-}
-
-const char *
-_dbus_errno_to_string (int errnum)
+_dbus_get_is_errno_epipe (void)
{
- return strerror (errnum);
+ return errno == EPIPE;
}
-/* Avoids a danger in threaded situations (calling close()
- * on a file descriptor twice, and another thread has
- * re-opened it since the first close)
+/**
+ * Get error message from errno
+ * @returns _dbus_strerror(errno)
*/
-static int
-close_and_invalidate (int *fd)
-{
- int ret;
-
- if (*fd < 0)
- return -1;
- else
- {
- ret = close (*fd);
- *fd = -1;
- }
-
- return ret;
-}
-
-static dbus_bool_t
-make_pipe (int p[2],
- DBusError *error)
-{
- if (pipe (p) < 0)
- {
- dbus_set_error (error,
- DBUS_ERROR_SPAWN_FAILED,
- "Failed to create pipe for communicating with child process (%s)",
- _dbus_errno_to_string (errno));
- return FALSE;
- }
- else
- return TRUE;
-}
-
-enum
-{
- CHILD_CHDIR_FAILED,
- CHILD_EXEC_FAILED,
- CHILD_DUP2_FAILED,
- CHILD_FORK_FAILED
-};
-
-static void
-write_err_and_exit (int fd, int msg)
-{
- int en = errno;
-
- write (fd, &msg, sizeof(msg));
- write (fd, &en, sizeof(en));
-
- _exit (1);
-}
-
-static dbus_bool_t
-read_ints (int fd,
- int *buf,
- int n_ints_in_buf,
- int *n_ints_read,
- DBusError *error)
-{
- size_t bytes = 0;
-
- while (TRUE)
- {
- size_t chunk;
-
- if (bytes >= sizeof(int)*2)
- break; /* give up, who knows what happened, should not be
- * possible.
- */
-
- again:
- chunk = read (fd,
- ((char*)buf) + bytes,
- sizeof(int) * n_ints_in_buf - bytes);
- if (chunk < 0 && errno == EINTR)
- goto again;
-
- if (chunk < 0)
- {
- /* Some weird shit happened, bail out */
-
- dbus_set_error (error,
- DBUS_ERROR_SPAWN_FAILED,
- "Failed to read from child pipe (%s)",
- _dbus_errno_to_string (errno));
-
- return FALSE;
- }
- else if (chunk == 0)
- break; /* EOF */
- else /* chunk > 0 */
- bytes += chunk;
- }
-
- *n_ints_read = (int)(bytes / sizeof(int));
-
- return TRUE;
-}
-
-static void
-do_exec (int child_err_report_fd,
- char **argv)
-{
- execvp (argv[0], argv);
-
- /* Exec failed */
- write_err_and_exit (child_err_report_fd,
- CHILD_EXEC_FAILED);
-
-}
-
-dbus_bool_t
-_dbus_spawn_async (char **argv,
- DBusError *error)
+const char*
+_dbus_strerror_from_errno (void)
{
- int pid = -1, grandchild_pid;
- int child_err_report_pipe[2] = { -1, -1 };
- int child_pid_report_pipe[2] = { -1, -1 };
- int status;
-
- printf ("spawning application: %s\n", argv[0]);
-
- if (!make_pipe (child_err_report_pipe, error))
- return FALSE;
-
- if (!make_pipe (child_pid_report_pipe, error))
- goto cleanup_and_fail;
-
- pid = fork ();
-
- if (pid < 0)
- {
- dbus_set_error (error,
- DBUS_ERROR_SPAWN_FORK_FAILED,
- "Failed to fork (%s)",
- _dbus_errno_to_string (errno));
- return FALSE;
- }
- else if (pid == 0)
- {
- /* Immediate child. */
-
- /* Be sure we crash if the parent exits
- * and we write to the err_report_pipe
- */
- signal (SIGPIPE, SIG_DFL);
-
- /* Close the parent's end of the pipes;
- * not needed in the close_descriptors case,
- * though
- */
- close_and_invalidate (&child_err_report_pipe[0]);
- close_and_invalidate (&child_pid_report_pipe[0]);
-
- /* We need to fork an intermediate child that launches the
- * final child. The purpose of the intermediate child
- * is to exit, so we can waitpid() it immediately.
- * Then the grandchild will not become a zombie.
- */
- grandchild_pid = fork ();
-
- if (grandchild_pid < 0)
- {
- /* report -1 as child PID */
- write (child_pid_report_pipe[1], &grandchild_pid,
- sizeof(grandchild_pid));
-
- write_err_and_exit (child_err_report_pipe[1],
- CHILD_FORK_FAILED);
- }
- else if (grandchild_pid == 0)
- {
- do_exec (child_err_report_pipe[1],
- argv);
- }
- else
- {
- write (child_pid_report_pipe[1], &grandchild_pid, sizeof(grandchild_pid));
- close_and_invalidate (&child_pid_report_pipe[1]);
-
- _exit (0);
- }
- }
- else
- {
- /* Parent */
-
- int buf[2];
- int n_ints = 0;
-
- /* Close the uncared-about ends of the pipes */
- close_and_invalidate (&child_err_report_pipe[1]);
- close_and_invalidate (&child_pid_report_pipe[1]);
-
- wait_again:
- if (waitpid (pid, &status, 0) < 0)
- {
- if (errno == EINTR)
- goto wait_again;
- else if (errno == ECHILD)
- ; /* do nothing, child already reaped */
- else
- _dbus_warn ("waitpid() should not fail in "
- "'_dbus_spawn_async'");
- }
-
- if (!read_ints (child_err_report_pipe[0],
- buf, 2, &n_ints,
- error))
- goto cleanup_and_fail;
-
- if (n_ints >= 2)
- {
- /* Error from the child. */
- switch (buf[0])
- {
- default:
- dbus_set_error (error,
- DBUS_ERROR_SPAWN_FAILED,
- "Unknown error executing child process \"%s\"",
- argv[0]);
- break;
- }
-
- goto cleanup_and_fail;
- }
-
-
- /* Success against all odds! return the information */
- close_and_invalidate (&child_err_report_pipe[0]);
-
- return TRUE;
- }
-
- cleanup_and_fail:
-
- /* There was an error from the Child, reap the child to avoid it being
- a zombie.
- */
- if (pid > 0)
- {
- wait_failed:
- if (waitpid (pid, NULL, 0) < 0)
- {
- if (errno == EINTR)
- goto wait_failed;
- else if (errno == ECHILD)
- ; /* do nothing, child already reaped */
- else
- _dbus_warn ("waitpid() should not fail in "
- "'_dbus_spawn_async'");
- }
- }
-
- close_and_invalidate (&child_err_report_pipe[0]);
- close_and_invalidate (&child_err_report_pipe[1]);
- close_and_invalidate (&child_pid_report_pipe[0]);
- close_and_invalidate (&child_pid_report_pipe[1]);
-
- return FALSE;
+ return _dbus_strerror (errno);
}
/** @} end of sysdeps */
+
+/* tests in dbus-sysdeps-util.c */