From 938a3629702f4a616bce57c213865be475b22bc8 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Wed, 5 Jun 2013 19:58:22 +0100 Subject: [PATCH 1/2] Export dbus_setenv() as a utility function It's sufficiently portable that GLib has an equivalent, and I really don't want to have to either open-code it in dbus-run-session or link dbus-run-session statically. We have enough statically-linked rubbish already. --- bus/activation-helper.c | 5 +++-- bus/config-parser.c | 9 +++++---- bus/dispatch.c | 3 ++- bus/test-launch-helper.c | 5 +++-- dbus/dbus-bus.c | 5 +++-- dbus/dbus-misc.h | 4 ++++ dbus/dbus-server-launchd.c | 3 ++- dbus/dbus-sysdeps.c | 14 ++++++++++---- dbus/dbus-sysdeps.h | 2 -- test/name-test/test-autolaunch.c | 3 +-- 10 files changed, 33 insertions(+), 20 deletions(-) diff --git a/bus/activation-helper.c b/bus/activation-helper.c index 8d7ae36..e3b3323 100644 --- a/bus/activation-helper.c +++ b/bus/activation-helper.c @@ -40,6 +40,7 @@ #include #include +#include #include #include @@ -156,8 +157,8 @@ clear_environment (DBusError *error) } /* Ensure the bus is set to system */ - _dbus_setenv ("DBUS_STARTER_ADDRESS", DBUS_SYSTEM_BUS_DEFAULT_ADDRESS); - _dbus_setenv ("DBUS_STARTER_BUS_TYPE", "system"); + dbus_setenv ("DBUS_STARTER_ADDRESS", DBUS_SYSTEM_BUS_DEFAULT_ADDRESS); + dbus_setenv ("DBUS_STARTER_BUS_TYPE", "system"); #endif return TRUE; diff --git a/bus/config-parser.c b/bus/config-parser.c index ff73ed0..1df556a 100644 --- a/bus/config-parser.c +++ b/bus/config-parser.c @@ -30,6 +30,7 @@ #include "selinux.h" #include #include +#include #include #include @@ -3417,10 +3418,10 @@ test_default_session_servicedirs (void) } #ifdef DBUS_UNIX - if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare")) + if (!dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare")) _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME"); - if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:")) + if (!dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:")) _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS"); #endif if (!_dbus_get_standard_session_servicedirs (&dirs)) @@ -3550,10 +3551,10 @@ test_default_system_servicedirs (void) } #ifdef DBUS_UNIX - if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare")) + if (!dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare")) _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME"); - if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:")) + if (!dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:")) _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS"); #endif if (!_dbus_get_standard_system_servicedirs (&dirs)) diff --git a/bus/dispatch.c b/bus/dispatch.c index 7a96f9d..34ff46f 100644 --- a/bus/dispatch.c +++ b/bus/dispatch.c @@ -34,6 +34,7 @@ #include "signals.h" #include "test.h" #include +#include #include #ifdef HAVE_UNIX_FD_PASSING @@ -4466,7 +4467,7 @@ setenv_TEST_LAUNCH_HELPER_CONFIG(const DBusString *test_data_dir, _dbus_verbose ("Setting TEST_LAUNCH_HELPER_CONFIG to '%s'\n", _dbus_string_get_const_data (&full)); - _dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full)); + dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", _dbus_string_get_const_data (&full)); _dbus_string_free (&full); diff --git a/bus/test-launch-helper.c b/bus/test-launch-helper.c index ab36b6e..19705ff 100644 --- a/bus/test-launch-helper.c +++ b/bus/test-launch-helper.c @@ -28,6 +28,7 @@ #include #include #include +#include #ifdef DBUS_BUILD_TESTS static void @@ -122,8 +123,8 @@ main (int argc, char **argv) return 1; /* use a config file that will actually work... */ - _dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", - _dbus_string_get_const_data (&config_file)); + dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG", + _dbus_string_get_const_data (&config_file)); _dbus_string_free (&config_file); diff --git a/dbus/dbus-bus.c b/dbus/dbus-bus.c index 6f81c74..462f0ea 100644 --- a/dbus/dbus-bus.c +++ b/dbus/dbus-bus.c @@ -28,6 +28,7 @@ #include "dbus-internals.h" #include "dbus-message.h" #include "dbus-marshal-validate.h" +#include "dbus-misc.h" #include "dbus-threads-internal.h" #include "dbus-connection-internal.h" #include "dbus-string.h" @@ -306,10 +307,10 @@ init_connections_unlocked (void) * the above code will work right */ - if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL)) + if (!dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL)) return FALSE; - if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL)) + if (!dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL)) return FALSE; if (!_dbus_register_shutdown_func (addresses_shutdown_func, diff --git a/dbus/dbus-misc.h b/dbus/dbus-misc.h index 3504bca..6e72d9e 100644 --- a/dbus/dbus-misc.h +++ b/dbus/dbus-misc.h @@ -44,6 +44,10 @@ void dbus_get_version (int *major_version_p, int *minor_version_p, int *micro_version_p); +DBUS_EXPORT +dbus_bool_t dbus_setenv (const char *variable, + const char *value); + /** @} */ DBUS_END_DECLS diff --git a/dbus/dbus-server-launchd.c b/dbus/dbus-server-launchd.c index db4673c..9832875 100644 --- a/dbus/dbus-server-launchd.c +++ b/dbus/dbus-server-launchd.c @@ -40,6 +40,7 @@ #include #include +#include "dbus-misc.h" #include "dbus-server-socket.h" /* put other private launchd functions here */ @@ -176,7 +177,7 @@ _dbus_server_new_for_launchd (const char *launchd_env_var, DBusError * error) else { display = launch_data_get_string(environment_param); - _dbus_setenv ("DISPLAY", display); + dbus_setenv ("DISPLAY", display); } } } diff --git a/dbus/dbus-sysdeps.c b/dbus/dbus-sysdeps.c index 4e14ac3..9d741f7 100644 --- a/dbus/dbus-sysdeps.c +++ b/dbus/dbus-sysdeps.c @@ -29,6 +29,7 @@ #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. @@ -94,6 +95,8 @@ _dbus_abort (void) } /** + * @ingroup DBusMisc + * * Wrapper for setenv(). If the value is #NULL, unsets * the environment variable. * @@ -102,13 +105,16 @@ _dbus_abort (void) * 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 varname name of environment variable - * @param value value of environment variable - * @returns #TRUE on success. + * @param value value of environment variable, or #NULL to unset + * @returns #TRUE on success, #FALSE if not enough memory. */ dbus_bool_t -_dbus_setenv (const char *varname, - const char *value) +dbus_setenv (const char *varname, + const char *value) { _dbus_assert (varname != NULL); diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index d92325c..65c0ec5 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -89,8 +89,6 @@ void _dbus_abort (void) _DBUS_GNUC_NORETURN; dbus_bool_t _dbus_check_setuid (void); 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); diff --git a/test/name-test/test-autolaunch.c b/test/name-test/test-autolaunch.c index adbeb18..732e6dc 100644 --- a/test/name-test/test-autolaunch.c +++ b/test/name-test/test-autolaunch.c @@ -8,7 +8,6 @@ #endif #include -#include "dbus/dbus-sysdeps.h" int main (int argc, char *argv[]) @@ -16,7 +15,7 @@ main (int argc, char *argv[]) DBusConnection *conn = NULL; DBusError error; - _dbus_setenv ("DBUS_SESSION_BUS_ADDRESS", NULL); + dbus_setenv ("DBUS_SESSION_BUS_ADDRESS", NULL); dbus_error_init (&error); -- 1.7.10.4