From 417c41f6c1ca122e1ce72a920bfc8c3ee841bf3c Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Thu, 10 Jul 2008 12:45:36 -0400 Subject: Add new _dbus_get_environment call It's a wrapper around the environ external variable. It will be important in the future when we allow bus clients to modify the environment of future activated clients. Presently, we just always use the bus daemon environment wholesale. --- dbus/dbus-sysdeps.c | 42 ++++++++++++++++++++++++++++++++++++++++++ dbus/dbus-sysdeps.h | 1 + 2 files changed, 43 insertions(+) (limited to 'dbus') diff --git a/dbus/dbus-sysdeps.c b/dbus/dbus-sysdeps.c index 1a736e42..d740f875 100644 --- a/dbus/dbus-sysdeps.c +++ b/dbus/dbus-sysdeps.c @@ -200,6 +200,48 @@ _dbus_clearenv (void) return rc; } +/** + * Gets a #NULL-terminated list of key=value pairs from the + * environment. Use dbus_free_string_array to free it. + * + * @returns the environment or #NULL on OOM + */ +char ** +_dbus_get_environment (void) +{ + int i, length; + extern char **environ; + char **environment; + + _dbus_assert (environ != NULL); + + for (length = 0; environ[length] != NULL; length++); + + /* Add one for NULL */ + length++; + + environment = dbus_new0 (char *, length); + + if (environment == NULL) + return NULL; + + for (i = 0; environ[i] != NULL; i++) + { + environment[i] = _dbus_strdup (environ[i]); + + if (environment[i] == NULL) + break; + } + + if (environ[i] != NULL) + { + dbus_free_string_array (environment); + environment = NULL; + } + + return environment; +} + /* * init a pipe instance. * diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index 5ff13884..80236f05 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -101,6 +101,7 @@ const char* _dbus_getenv (const char *varname); dbus_bool_t _dbus_setenv (const char *varname, const char *value); dbus_bool_t _dbus_clearenv (void); +char ** _dbus_get_environment (void); /** A process ID */ typedef unsigned long dbus_pid_t; -- cgit From 0e3ec9cec0f6740acd39d6e6983f419e20461282 Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Fri, 11 Jul 2008 11:32:30 -0400 Subject: Add new function _dbus_string_split_on_byte It allows you to turn a string like KEY=VALUE into two strings key and value. --- dbus/dbus-string-util.c | 25 +++++++++++++++++++++++++ dbus/dbus-string.c | 42 ++++++++++++++++++++++++++++++++++++++++++ dbus/dbus-string.h | 3 +++ 3 files changed, 70 insertions(+) (limited to 'dbus') diff --git a/dbus/dbus-string-util.c b/dbus/dbus-string-util.c index 492c5289..aed94878 100644 --- a/dbus/dbus-string-util.c +++ b/dbus/dbus-string-util.c @@ -846,6 +846,31 @@ _dbus_string_test (void) _dbus_string_free (&str); } + + { + const char two_strings[] = "one\ttwo"; + + if (!_dbus_string_init (&str)) + _dbus_assert_not_reached ("no memory"); + + if (!_dbus_string_init (&other)) + _dbus_assert_not_reached ("no memory"); + + if (!_dbus_string_append (&str, two_strings)) + _dbus_assert_not_reached ("no memory"); + + if (!_dbus_string_split_on_byte (&str, '\t', &other)) + _dbus_assert_not_reached ("no memory or delimiter not found"); + + if (strcmp (_dbus_string_get_data (&str), "one") != 0) + _dbus_assert_not_reached ("left side after split on tab is wrong"); + + if (strcmp (_dbus_string_get_data (&other), "two") != 0) + _dbus_assert_not_reached ("right side after split on tab is wrong"); + + _dbus_string_free (&str); + _dbus_string_free (&other); + } return TRUE; } diff --git a/dbus/dbus-string.c b/dbus/dbus-string.c index cb108a8d..6b9b2bfe 100644 --- a/dbus/dbus-string.c +++ b/dbus/dbus-string.c @@ -1677,6 +1677,48 @@ _dbus_string_replace_len (const DBusString *source, return TRUE; } +/** + * Looks for the first occurance of a byte, deletes that byte, + * and moves everything after the byte to the beginning of a + * separate string. Both strings must be initialized, valid + * strings. + * + * @param source the source string + * @param byte the byte to remove and split the string at + * @param tail the split off string + * @returns #FALSE if not enough memory or if byte could not be found + * + */ +dbus_bool_t +_dbus_string_split_on_byte (DBusString *source, + unsigned char byte, + DBusString *tail) +{ + int byte_position; + char byte_string[2] = ""; + int head_length; + int tail_length; + + byte_string[0] = (char) byte; + + if (!_dbus_string_find (source, 0, byte_string, &byte_position)) + return FALSE; + + head_length = byte_position; + tail_length = _dbus_string_get_length (source) - head_length - 1; + + if (!_dbus_string_move_len (source, byte_position + 1, tail_length, + tail, 0)) + return FALSE; + + /* remove the trailing delimiter byte from the head now. + */ + if (!_dbus_string_set_length (source, head_length)) + return FALSE; + + return TRUE; +} + /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc * Pennington, and Tom Tromey are the authors and authorized relicense. */ diff --git a/dbus/dbus-string.h b/dbus/dbus-string.h index d88d67ed..374f0a86 100644 --- a/dbus/dbus-string.h +++ b/dbus/dbus-string.h @@ -201,6 +201,9 @@ dbus_bool_t _dbus_string_replace_len (const DBusString *source, DBusString *dest, int replace_at, int replace_len); +dbus_bool_t _dbus_string_split_on_byte (DBusString *source, + unsigned char byte, + DBusString *tail); void _dbus_string_get_unichar (const DBusString *str, int start, dbus_unichar_t *ch_return, -- cgit From 8ec160419231d68c1f6a1e03def8353e02b442a9 Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Thu, 10 Jul 2008 13:12:01 -0400 Subject: When spawning processes, don't ignore the passed in environment Previously, we'd always call execv() and unconditionally use the environment of the parent. Now we call execve() with the passed in environment. For compatibility, we detect if the passed in environment is NULL and for that case, use the environment from the parent instead. --- dbus/dbus-spawn.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'dbus') diff --git a/dbus/dbus-spawn.c b/dbus/dbus-spawn.c index d74b3265..35ccba6c 100644 --- a/dbus/dbus-spawn.c +++ b/dbus/dbus-spawn.c @@ -880,6 +880,7 @@ write_status_and_exit (int fd, int status) static void do_exec (int child_err_report_fd, char **argv, + char **envp, DBusSpawnChildSetupFunc child_setup, void *user_data) { @@ -910,8 +911,17 @@ do_exec (int child_err_report_fd, _dbus_warn ("Fd %d did not have the close-on-exec flag set!\n", i); } #endif + + if (envp == NULL) + { + extern char **environ; + + _dbus_assert (environ != NULL); + + envp = environ; + } - execv (argv[0], argv); + execve (argv[0], argv, envp); /* Exec failed */ write_err_and_exit (child_err_report_fd, @@ -1190,6 +1200,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, { do_exec (child_err_report_pipe[WRITE_END], argv, + env, child_setup, user_data); _dbus_assert_not_reached ("Got to code after exec() - should have exited on error"); } @@ -1218,6 +1229,8 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, else _dbus_babysitter_unref (sitter); + dbus_free_string_array (env); + _DBUS_ASSERT_ERROR_IS_CLEAR (error); return TRUE; -- cgit