From 5c0fcbc41e0ddfb2e319c413e15a5852e6f8e2c6 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 19 Feb 2013 15:46:56 +0000 Subject: [PATCH 5/8] DBusServer: allow "owning" the directory containing the socket --- dbus/dbus-server-socket.c | 31 +++++++++++++++++++++++++++---- dbus/dbus-server-socket.h | 3 ++- dbus/dbus-server-unix.c | 36 +++++++++++++++++++++++++++++++++--- dbus/dbus-server-unix.h | 1 + 4 files changed, 63 insertions(+), 8 deletions(-) diff --git a/dbus/dbus-server-socket.c b/dbus/dbus-server-socket.c index ae4b602..1234cfc 100644 --- a/dbus/dbus-server-socket.c +++ b/dbus/dbus-server-socket.c @@ -54,6 +54,7 @@ struct DBusServerSocket int *fds; /**< File descriptor or -1 if disconnected. */ DBusWatch **watch; /**< File descriptor watch. */ char *socket_name; /**< Name of domain socket, to unlink if appropriate */ + dbus_bool_t own_socket_name_parent; /**< Unlink parent of socket_name? */ DBusNonceFile *noncefile; /**< Nonce file used to authenticate clients */ }; @@ -245,9 +246,29 @@ socket_disconnect (DBusServer *server) if (socket_server->socket_name != NULL) { - DBusString tmp; - _dbus_string_init_const (&tmp, socket_server->socket_name); - _dbus_delete_file (&tmp, NULL); + DBusString name; + DBusString dir; + + _dbus_string_init_const (&name, socket_server->socket_name); + _dbus_delete_file (&name, NULL); + + if (socket_server->own_socket_name_parent) + { + if (!_dbus_string_init (&dir)) + { + /* never mind, we just won't delete it */ + } + else if (!_dbus_string_get_dirname (&name, &dir)) + { + /* again, never mind, we just won't delete it */ + _dbus_string_free (&dir); + } + else + { + _dbus_delete_directory (&dir, NULL); + _dbus_string_free (&dir); + } + } } if (server->published_address) @@ -574,11 +595,13 @@ _dbus_server_listen_socket (DBusAddressEntry *entry, */ void _dbus_server_socket_own_filename (DBusServer *server, - char *filename) + char *filename, + dbus_bool_t own_parent) { DBusServerSocket *socket_server = (DBusServerSocket*) server; socket_server->socket_name = filename; + socket_server->own_socket_name_parent = own_parent; } diff --git a/dbus/dbus-server-socket.h b/dbus/dbus-server-socket.h index 3aec712..4472ca8 100644 --- a/dbus/dbus-server-socket.h +++ b/dbus/dbus-server-socket.h @@ -47,7 +47,8 @@ DBusServerListenResult _dbus_server_listen_socket (DBusAddressEntry *entry, void _dbus_server_socket_own_filename (DBusServer *server, - char *filename); + char *filename, + dbus_bool_t own_parent); DBUS_END_DECLS diff --git a/dbus/dbus-server-unix.c b/dbus/dbus-server-unix.c index d995240..dd20ed8 100644 --- a/dbus/dbus-server-unix.c +++ b/dbus/dbus-server-unix.c @@ -123,6 +123,7 @@ _dbus_server_listen_platform_specific (DBusAddressEntry *entry, #else FALSE, #endif + FALSE, error); _dbus_string_free (&full_path); @@ -131,9 +132,9 @@ _dbus_server_listen_platform_specific (DBusAddressEntry *entry, else { if (path) - *server_p = _dbus_server_new_for_domain_socket (path, FALSE, error); + *server_p = _dbus_server_new_for_domain_socket (path, FALSE, FALSE, error); else - *server_p = _dbus_server_new_for_domain_socket (abstract, TRUE, error); + *server_p = _dbus_server_new_for_domain_socket (abstract, TRUE, FALSE, error); } if (*server_p != NULL) @@ -230,12 +231,14 @@ _dbus_server_listen_platform_specific (DBusAddressEntry *entry, * * @param path the path for the domain socket. * @param abstract #TRUE to use abstract socket namespace + * @param own_parent if #TRUE, create the directory containing @path and try to unlink it on disconnection * @param error location to store reason for failure. * @returns the new server, or #NULL on failure. */ DBusServer* _dbus_server_new_for_domain_socket (const char *path, dbus_bool_t abstract, + dbus_bool_t own_parent, DBusError *error) { DBusServer *server; @@ -275,6 +278,33 @@ _dbus_server_new_for_domain_socket (const char *path, dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto failed_0; } + + if (own_parent) + { + DBusString dir; + + if (!_dbus_string_init (&dir)) + { + _DBUS_SET_OOM (error); + goto failed_1; + } + + if (!_dbus_string_get_dirname (&path_str, &dir)) + { + _DBUS_SET_OOM (error); + _dbus_string_free (&dir); + goto failed_1; + } + + if (!_dbus_create_directory (&dir, error) || + !_dbus_check_dir_is_private_to_user (&dir, error)) + { + _dbus_string_free (&dir); + goto failed_1; + } + + _dbus_string_free (&dir); + } } listen_fd = _dbus_listen_unix_socket (path, abstract, error); @@ -293,7 +323,7 @@ _dbus_server_new_for_domain_socket (const char *path, } if (path_copy != NULL) - _dbus_server_socket_own_filename(server, path_copy); + _dbus_server_socket_own_filename (server, path_copy, own_parent); _dbus_string_free (&address); diff --git a/dbus/dbus-server-unix.h b/dbus/dbus-server-unix.h index 92b996c..8c119e4 100644 --- a/dbus/dbus-server-unix.h +++ b/dbus/dbus-server-unix.h @@ -30,6 +30,7 @@ DBUS_BEGIN_DECLS DBusServer* _dbus_server_new_for_domain_socket (const char *path, dbus_bool_t abstract, + dbus_bool_t own_parent, DBusError *error); DBUS_END_DECLS -- 1.7.10.4