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");
70 if (path == NULL && tmpdir == NULL && abstract == NULL)
72 _dbus_set_bad_address(error, "unix",
73 "path or tmpdir or abstract",
75 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
78 if ((path && tmpdir) ||
82 _dbus_set_bad_address(error, NULL, NULL,
83 "cannot specify two of \"path\" and \"tmpdir\" and \"abstract\" at the same time");
84 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
92 if (!_dbus_string_init (&full_path))
94 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
95 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
98 if (!_dbus_string_init (&filename))
100 _dbus_string_free (&full_path);
101 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
102 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
105 if (!_dbus_string_append (&filename,
107 !_dbus_generate_random_ascii (&filename, 10) ||
108 !_dbus_string_append (&full_path, tmpdir) ||
109 !_dbus_concat_dir_and_file (&full_path, &filename))
111 _dbus_string_free (&full_path);
112 _dbus_string_free (&filename);
113 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
114 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
117 /* Always use abstract namespace if possible with tmpdir */
120 _dbus_server_new_for_domain_socket (_dbus_string_get_const_data (&full_path),
121 #ifdef HAVE_ABSTRACT_SOCKETS
128 _dbus_string_free (&full_path);
129 _dbus_string_free (&filename);
134 *server_p = _dbus_server_new_for_domain_socket (path, FALSE, error);
136 *server_p = _dbus_server_new_for_domain_socket (abstract, TRUE, error);
139 if (*server_p != NULL)
141 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
142 return DBUS_SERVER_LISTEN_OK;
146 _DBUS_ASSERT_ERROR_IS_SET(error);
147 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
150 else if (strcmp (method, "systemd") == 0)
155 n = _dbus_listen_systemd_sockets (&fds, error);
158 _DBUS_ASSERT_ERROR_IS_SET (error);
159 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
162 if (!_dbus_string_init (&address))
165 for (i = 0; i < n; i++)
169 if (!_dbus_string_append (&address, ";"))
172 if (!_dbus_append_address_from_socket (fds[i], &address, error))
176 *server_p = _dbus_server_new_for_socket (fds, n, &address, NULL);
177 if (*server_p == NULL)
182 return DBUS_SERVER_LISTEN_OK;
184 _DBUS_SET_OOM (error);
186 for (i = 0; i < n; i++)
188 _dbus_close_socket (fds[i], NULL);
191 _dbus_string_free (&address);
193 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
195 #ifdef DBUS_ENABLE_LAUNCHD
196 else if (strcmp (method, "launchd") == 0)
198 const char *launchd_env_var = dbus_address_entry_get_value (entry, "env");
199 if (launchd_env_var == NULL)
201 _dbus_set_bad_address (error, "launchd", "env", NULL);
202 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
204 *server_p = _dbus_server_new_for_launchd (launchd_env_var, error);
206 if (*server_p != NULL)
208 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
209 return DBUS_SERVER_LISTEN_OK;
213 _DBUS_ASSERT_ERROR_IS_SET(error);
214 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
220 /* If we don't handle the method, we return NULL with the
223 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
224 return DBUS_SERVER_LISTEN_NOT_HANDLED;
229 * Creates a new server listening on the given Unix domain socket.
231 * @param path the path for the domain socket.
232 * @param abstract #TRUE to use abstract socket namespace
233 * @param error location to store reason for failure.
234 * @returns the new server, or #NULL on failure.
237 _dbus_server_new_for_domain_socket (const char *path,
238 dbus_bool_t abstract,
247 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
249 if (!_dbus_string_init (&address))
251 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
255 _dbus_string_init_const (&path_str, path);
257 !_dbus_string_append (&address, "unix:abstract=")) ||
259 !_dbus_string_append (&address, "unix:path=")) ||
260 !_dbus_address_append_escaped (&address, &path_str))
262 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
272 path_copy = _dbus_strdup (path);
273 if (path_copy == NULL)
275 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
280 listen_fd = _dbus_listen_unix_socket (path, abstract, error);
284 _DBUS_ASSERT_ERROR_IS_SET (error);
288 server = _dbus_server_new_for_socket (&listen_fd, 1, &address, 0);
291 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
295 if (path_copy != NULL)
296 _dbus_server_socket_own_filename(server, path_copy);
298 _dbus_string_free (&address);
303 _dbus_close_socket (listen_fd, NULL);
305 dbus_free (path_copy);
307 _dbus_string_free (&address);