1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-server-unix.c Server implementation for Unix network protocols.
4 * Copyright (C) 2002, 2003, 2004 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "dbus-internals.h"
26 #include "dbus-server-unix.h"
27 #include "dbus-server-socket.h"
28 #include "dbus-server-launchd.h"
29 #include "dbus-transport-unix.h"
30 #include "dbus-connection-internal.h"
31 #include "dbus-sysdeps-unix.h"
32 #include "dbus-string.h"
35 * @defgroup DBusServerUnix DBusServer implementations for UNIX
36 * @ingroup DBusInternals
37 * @brief Implementation details of DBusServer on UNIX
43 * Tries to interpret the address entry in a platform-specific
44 * way, creating a platform-specific server type if appropriate.
45 * Sets error if the result is not OK.
47 * @param entry an address entry
48 * @param server_p location to store a new DBusServer, or #NULL on failure.
49 * @param error location to store rationale for failure on bad address
50 * @returns the outcome
53 DBusServerListenResult
54 _dbus_server_listen_platform_specific (DBusAddressEntry *entry,
55 DBusServer **server_p,
62 method = dbus_address_entry_get_method (entry);
64 if (strcmp (method, "unix") == 0)
66 const char *path = dbus_address_entry_get_value (entry, "path");
67 const char *tmpdir = dbus_address_entry_get_value (entry, "tmpdir");
68 const char *abstract = dbus_address_entry_get_value (entry, "abstract");
69 const char *runtime = dbus_address_entry_get_value (entry, "runtime");
70 int mutually_exclusive_modes = 0;
72 mutually_exclusive_modes = (path != NULL) + (tmpdir != NULL) +
73 (abstract != NULL) + (runtime != NULL);
75 if (mutually_exclusive_modes < 1)
77 _dbus_set_bad_address(error, "unix",
78 "path or tmpdir or abstract or runtime",
80 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
83 if (mutually_exclusive_modes > 1)
85 _dbus_set_bad_address(error, NULL, NULL,
86 "cannot specify two of \"path\", \"tmpdir\", \"abstract\" and \"runtime\" at the same time");
87 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
94 const char *runtimedir;
96 if (strcmp (runtime, "yes") != 0)
98 _dbus_set_bad_address(error, NULL, NULL,
99 "if given, the only value allowed for \"runtime\" is \"yes\"");
100 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
103 runtimedir = _dbus_getenv ("XDG_RUNTIME_DIR");
105 if (runtimedir == NULL)
107 dbus_set_error (error,
108 DBUS_ERROR_NOT_SUPPORTED, "\"XDG_RUNTIME_DIR\" is not set");
109 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
112 _dbus_string_init_const (&filename, "bus");
114 if (!_dbus_string_init (&full_path))
116 _DBUS_SET_OOM (error);
117 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
120 if (!_dbus_string_append (&full_path, runtimedir) ||
121 !_dbus_concat_dir_and_file (&full_path, &filename))
123 _dbus_string_free (&full_path);
124 _DBUS_SET_OOM (error);
125 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
128 /* We can safely use filesystem sockets in the runtime directory,
129 * and they are preferred because they can be bind-mounted between
130 * Linux containers. */
131 *server_p = _dbus_server_new_for_domain_socket (
132 _dbus_string_get_const_data (&full_path),
135 _dbus_string_free (&full_path);
137 else if (tmpdir != NULL)
139 DBusString full_path;
142 if (!_dbus_string_init (&full_path))
144 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
145 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
148 if (!_dbus_string_init (&filename))
150 _dbus_string_free (&full_path);
151 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
152 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
155 if (!_dbus_string_append (&filename, "dbus-"))
157 _dbus_string_free (&full_path);
158 _dbus_string_free (&filename);
159 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
160 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
163 if (!_dbus_generate_random_ascii (&filename, 10, error))
165 _dbus_string_free (&full_path);
166 _dbus_string_free (&filename);
167 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
170 if (!_dbus_string_append (&full_path, tmpdir) ||
171 !_dbus_concat_dir_and_file (&full_path, &filename))
173 _dbus_string_free (&full_path);
174 _dbus_string_free (&filename);
175 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
176 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
179 /* Always use abstract namespace if possible with tmpdir */
182 _dbus_server_new_for_domain_socket (_dbus_string_get_const_data (&full_path),
183 #ifdef HAVE_ABSTRACT_SOCKETS
190 _dbus_string_free (&full_path);
191 _dbus_string_free (&filename);
196 *server_p = _dbus_server_new_for_domain_socket (path, FALSE, error);
198 *server_p = _dbus_server_new_for_domain_socket (abstract, TRUE, error);
201 if (*server_p != NULL)
203 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
204 return DBUS_SERVER_LISTEN_OK;
208 _DBUS_ASSERT_ERROR_IS_SET(error);
209 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
212 else if (strcmp (method, "systemd") == 0)
218 n = _dbus_listen_systemd_sockets (&fds, error);
221 _DBUS_ASSERT_ERROR_IS_SET (error);
222 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
225 if (!_dbus_string_init (&address))
228 for (i = 0; i < n; i++)
232 if (!_dbus_string_append (&address, ";"))
235 if (!_dbus_append_address_from_socket (fds[i], &address, error))
239 *server_p = _dbus_server_new_for_socket (fds, n, &address, NULL, error);
240 if (*server_p == NULL)
245 return DBUS_SERVER_LISTEN_OK;
248 _DBUS_SET_OOM (error);
250 for (i = 0; i < n; i++)
252 _dbus_close_socket (fds[i], NULL);
255 _dbus_string_free (&address);
257 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
259 #ifdef DBUS_ENABLE_LAUNCHD
260 else if (strcmp (method, "launchd") == 0)
262 const char *launchd_env_var = dbus_address_entry_get_value (entry, "env");
263 if (launchd_env_var == NULL)
265 _dbus_set_bad_address (error, "launchd", "env", NULL);
266 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
268 *server_p = _dbus_server_new_for_launchd (launchd_env_var, error);
270 if (*server_p != NULL)
272 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
273 return DBUS_SERVER_LISTEN_OK;
277 _DBUS_ASSERT_ERROR_IS_SET(error);
278 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
284 /* If we don't handle the method, we return NULL with the
287 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
288 return DBUS_SERVER_LISTEN_NOT_HANDLED;
293 * Creates a new server listening on the given Unix domain socket.
295 * @param path the path for the domain socket.
296 * @param abstract #TRUE to use abstract socket namespace
297 * @param error location to store reason for failure.
298 * @returns the new server, or #NULL on failure.
301 _dbus_server_new_for_domain_socket (const char *path,
302 dbus_bool_t abstract,
306 DBusSocket listen_fd;
311 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
313 if (!_dbus_string_init (&address))
315 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
319 _dbus_string_init_const (&path_str, path);
321 !_dbus_string_append (&address, "unix:abstract=")) ||
323 !_dbus_string_append (&address, "unix:path=")) ||
324 !_dbus_address_append_escaped (&address, &path_str))
326 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
336 path_copy = _dbus_strdup (path);
337 if (path_copy == NULL)
339 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
344 listen_fd.fd = _dbus_listen_unix_socket (path, abstract, error);
346 if (listen_fd.fd < 0)
348 _DBUS_ASSERT_ERROR_IS_SET (error);
352 server = _dbus_server_new_for_socket (&listen_fd, 1, &address, 0, error);
358 if (path_copy != NULL)
359 _dbus_server_socket_own_filename(server, path_copy);
361 _dbus_string_free (&address);
366 _dbus_close_socket (listen_fd, NULL);
368 dbus_free (path_copy);
370 _dbus_string_free (&address);