]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
dbus: extract out _dbus_server_new_for_dir()
authorMarc-André Lureau <marcandre.lureau@redhat.com>
Fri, 25 Mar 2022 12:23:38 +0000 (16:23 +0400)
committerSimon McVittie <smcv@collabora.com>
Fri, 15 Jul 2022 15:26:54 +0000 (16:26 +0100)
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
dbus/dbus-server-socket.c
dbus/dbus-server-socket.h

index fc8cee6c5812e0d2809dd327a64852dff5248edf..188d9789adc4dc8bc87024b1b17ed83a33bd1c52 100644 (file)
@@ -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
         {
index 64a51dc26c44eca41dcadba8dbf1ce2b82bc49c4..8324dbfb5fb2f7bcc231eb28cb4bea08f1c165ef 100644 (file)
@@ -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);