From df6ab5924cd12b68764f76c990094d1791a974cf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marc-Andr=C3=A9=20Lureau?= Date: Fri, 25 Mar 2022 16:23:38 +0400 Subject: [PATCH] dbus: extract out _dbus_server_new_for_dir() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Marc-André Lureau --- dbus/dbus-server-socket.c | 113 ++++++++++++++++++++++---------------- dbus/dbus-server-socket.h | 3 + 2 files changed, 70 insertions(+), 46 deletions(-) diff --git a/dbus/dbus-server-socket.c b/dbus/dbus-server-socket.c index fc8cee6c5..188d9789a 100644 --- a/dbus/dbus-server-socket.c +++ b/dbus/dbus-server-socket.c @@ -676,6 +676,72 @@ _dbus_server_new_for_domain_socket (const char *path, return NULL; } +/** + * Creates a new Unix domain socket server listening under the given directory. + * This function is used for "unix:dir/tmpdir" kind of addresses. + * + * @param dir the path to a directory. + * @param abstract #TRUE to use abstract socket namespace + * @param error location to store reason for failure. + * @returns the new server, or #NULL on failure. + */ +DBusServer * +_dbus_server_new_for_dir (const char *dir, + dbus_bool_t use_abstract, + DBusError *error) +{ + DBusServer *server; + DBusString full_path; + DBusString filename; + + if (!_dbus_string_init (&full_path)) + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + return NULL; + } + + if (!_dbus_string_init (&filename)) + { + _dbus_string_free (&full_path); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + return NULL; + } + + if (!_dbus_string_append (&filename, "dbus-")) + { + _dbus_string_free (&full_path); + _dbus_string_free (&filename); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + return NULL; + } + + if (!_dbus_generate_random_ascii (&filename, 10, error)) + { + _dbus_string_free (&full_path); + _dbus_string_free (&filename); + return NULL; + } + + if (!_dbus_string_append (&full_path, dir) || + !_dbus_concat_dir_and_file (&full_path, &filename)) + { + _dbus_string_free (&full_path); + _dbus_string_free (&filename); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + return NULL; + } + + server = + _dbus_server_new_for_domain_socket (_dbus_string_get_const_data (&full_path), + use_abstract, + error); + + _dbus_string_free (&full_path); + _dbus_string_free (&filename); + + return server; +} + /** * Tries to interpret the address entry for UNIX socket * addresses. @@ -775,8 +841,6 @@ _dbus_server_listen_unix_socket (DBusAddressEntry *entry, } else if (tmpdir != NULL || dir != NULL) { - DBusString full_path; - DBusString filename; dbus_bool_t use_abstract = FALSE; if (tmpdir != NULL) @@ -791,50 +855,7 @@ _dbus_server_listen_unix_socket (DBusAddressEntry *entry, #endif } - if (!_dbus_string_init (&full_path)) - { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); - return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; - } - - if (!_dbus_string_init (&filename)) - { - _dbus_string_free (&full_path); - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); - return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; - } - - if (!_dbus_string_append (&filename, "dbus-")) - { - _dbus_string_free (&full_path); - _dbus_string_free (&filename); - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); - return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; - } - - if (!_dbus_generate_random_ascii (&filename, 10, error)) - { - _dbus_string_free (&full_path); - _dbus_string_free (&filename); - return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; - } - - if (!_dbus_string_append (&full_path, dir) || - !_dbus_concat_dir_and_file (&full_path, &filename)) - { - _dbus_string_free (&full_path); - _dbus_string_free (&filename); - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); - return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; - } - - *server_p = - _dbus_server_new_for_domain_socket (_dbus_string_get_const_data (&full_path), - use_abstract, - error); - - _dbus_string_free (&full_path); - _dbus_string_free (&filename); + *server_p = _dbus_server_new_for_dir (dir, use_abstract, error); } else { diff --git a/dbus/dbus-server-socket.h b/dbus/dbus-server-socket.h index 64a51dc26..8324dbfb5 100644 --- a/dbus/dbus-server-socket.h +++ b/dbus/dbus-server-socket.h @@ -43,6 +43,9 @@ DBusServer* _dbus_server_new_for_tcp_socket (const char *host, const char *family, DBusError *error, dbus_bool_t use_nonce); +DBusServer* _dbus_server_new_for_dir (const char *dir, + dbus_bool_t use_abstract, + DBusError *error); DBusServerListenResult _dbus_server_listen_socket (DBusAddressEntry *entry, DBusServer **server_p, DBusError *error); -- 2.47.3