From c5e1bcd2e323c5ed7aa3ea4434f304c886d6ab28 Mon Sep 17 00:00:00 2001 From: lovelace Date: Wed, 22 Jul 2009 17:17:22 -0400 Subject: [PATCH 5/7] Use DBusString for launchd socket lookup --- dbus/dbus-sysdeps-unix.c | 57 ++++++++++++++++++++++++++----------------- dbus/dbus-sysdeps-unix.h | 3 +- dbus/dbus-transport-unix.c | 28 +++++++++++++++++---- 3 files changed, 58 insertions(+), 30 deletions(-) diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 7bd96df..d2559f2 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -3573,23 +3573,17 @@ _dbus_read_local_machine_uuid (DBusGUID *machine_id, * @param error a DBusError to store the error in case of failure * @return the value of the env var */ -const char * -_dbus_lookup_launchd_socket (const char *launchd_env_var, +dbus_bool_t +_dbus_lookup_launchd_socket (DBusString *socket_path, + const char *launchd_env_var, DBusError *error) { #ifdef DBUS_ENABLE_LAUNCHD char *argv[4]; int i; - DBusString socket_path; _DBUS_ASSERT_ERROR_IS_CLEAR (error); - if (!_dbus_string_init (&socket_path)) - { - _DBUS_SET_OOM (error); - return FALSE; - } - i = 0; argv[i] = "launchctl"; ++i; @@ -3602,53 +3596,70 @@ _dbus_lookup_launchd_socket (const char *launchd_env_var, _dbus_assert (i == _DBUS_N_ELEMENTS (argv)); - if (!_read_subprocess_line_argv(argv[0], TRUE, argv, &socket_path, error)) + if (!_read_subprocess_line_argv(argv[0], TRUE, argv, socket_path, error)) { - _dbus_string_free(&socket_path); - return NULL; + return FALSE; } /* no error, but no result either */ - if (_dbus_string_get_length(&socket_path) == 0) + if (_dbus_string_get_length(socket_path) == 0) { - _dbus_string_free(&socket_path); - return NULL; + return FALSE; } /* strip the carriage-return */ - _dbus_string_shorten(&socket_path, 1); - return _dbus_string_get_const_data(&socket_path); + _dbus_string_shorten(socket_path, 1); + return TRUE; #else /* DBUS_ENABLE_LAUNCHD */ dbus_set_error(error, DBUS_ERROR_NOT_SUPPORTED, "can't lookup socket from launchd; launchd support not compiled in"); - return NULL; + return FALSE; #endif } static dbus_bool_t _dbus_lookup_session_address_launchd (DBusString *address, DBusError *error) { - const char *launchd_socket = _dbus_lookup_launchd_socket ("DBUS_LAUNCHD_SESSION_BUS_SOCKET", error); - if (dbus_error_is_set(error)) - return FALSE; - if (launchd_socket == NULL) + dbus_bool_t valid_socket; + DBusString *socket_path; + + if (!_dbus_string_init (socket_path)) + { + _DBUS_SET_OOM (error); + return FALSE; + } + + valid_socket = _dbus_lookup_launchd_socket (socket_path, "DBUS_LAUNCHD_SESSION_BUS_SOCKET", error); + + if (dbus_error_is_set(error)) + { + _dbus_string_free(socket_path); + return FALSE; + } + + if (!valid_socket) { dbus_set_error(error, "no socket path", "launchd did not provide a socket path, " "verify that org.freedesktop.dbus-session.plist is loaded!"); + _dbus_string_free(socket_path); return FALSE; } if (!_dbus_string_append (address, "unix:path=")) { _DBUS_SET_OOM (error); + _dbus_string_free(socket_path); return FALSE; } - if (!_dbus_string_append (address, launchd_socket)) + if (!_dbus_string_copy (socket_path, 0, address, + _dbus_string_get_length (address))) { _DBUS_SET_OOM (error); + _dbus_string_free(socket_path); return FALSE; } + _dbus_string_free(socket_path); return TRUE; } diff --git a/dbus/dbus-sysdeps-unix.h b/dbus/dbus-sysdeps-unix.h index e06688e..8eec96b 100644 --- a/dbus/dbus-sysdeps-unix.h +++ b/dbus/dbus-sysdeps-unix.h @@ -79,7 +79,8 @@ dbus_bool_t _dbus_read_credentials (int client_fd, dbus_bool_t _dbus_send_credentials (int server_fd, DBusError *error); -const char *_dbus_lookup_launchd_socket (const char *launchd_env_var, +dbus_bool_t _dbus_lookup_launchd_socket (DBusString *socket_path, + const char *launchd_env_var, DBusError *error); /** Information about a UNIX user */ diff --git a/dbus/dbus-transport-unix.c b/dbus/dbus-transport-unix.c index 16cf80f..ce77c57 100644 --- a/dbus/dbus-transport-unix.c +++ b/dbus/dbus-transport-unix.c @@ -171,28 +171,44 @@ _dbus_transport_open_platform_specific (DBusAddressEntry *entry, } else if (strcmp (method, "launchd") == 0) { - const char *launchd_env_var = dbus_address_entry_get_value (entry, "env"); DBusError tmp_error = DBUS_ERROR_INIT; + const char *launchd_env_var = dbus_address_entry_get_value (entry, "env"); + const char *launchd_socket; + DBusString socket_path; + dbus_bool_t valid_socket; + + if (!_dbus_string_init (&socket_path)) + { + _DBUS_SET_OOM (error); + return FALSE; + } + if (launchd_env_var == NULL) { _dbus_set_bad_address (error, "launchd", "env", NULL); return DBUS_TRANSPORT_OPEN_BAD_ADDRESS; } - const char *launchd_socket = _dbus_lookup_launchd_socket (launchd_env_var, error); - if (launchd_socket) + valid_socket = _dbus_lookup_launchd_socket (&socket_path, launchd_env_var, error); + + if (dbus_error_is_set(error)) { - *transport_p = _dbus_transport_new_for_domain_socket (launchd_socket, FALSE, - error); + _dbus_string_free(&socket_path); + return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT; } - else + + if (!valid_socket) { dbus_set_error(&tmp_error, DBUS_ERROR_BAD_ADDRESS, "launchd's env var %s does not exist", launchd_env_var); dbus_error_free(error); dbus_move_error(&tmp_error, error); + return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT; } + launchd_socket = _dbus_string_get_const_data(&socket_path); + *transport_p = _dbus_transport_new_for_domain_socket (launchd_socket, FALSE, error); + if (*transport_p == NULL) { _DBUS_ASSERT_ERROR_IS_SET (error); -- 1.6.3.2