1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-server-unix.h"
26 #include "dbus-server-socket.h"
27 #include "dbus-transport-unix.h"
28 #include "dbus-connection-internal.h"
29 #include "dbus-sysdeps-unix.h"
30 #include "dbus-string.h"
33 * @defgroup DBusServerUnix DBusServer implementations for UNIX
34 * @ingroup DBusInternals
35 * @brief Implementation details of DBusServer on UNIX
41 * Tries to interpret the address entry in a platform-specific
42 * way, creating a platform-specific server type if appropriate.
43 * Sets error if the result is not OK.
45 * @param entry an address entry
46 * @param a new DBusServer, or #NULL on failure.
47 * @param error location to store rationale for failure on bad address
48 * @returns the outcome
51 DBusServerListenResult
52 _dbus_server_listen_platform_specific (DBusAddressEntry *entry,
53 DBusServer **server_p,
60 method = dbus_address_entry_get_method (entry);
62 if (strcmp (method, "unix") == 0)
64 const char *path = dbus_address_entry_get_value (entry, "path");
65 const char *tmpdir = dbus_address_entry_get_value (entry, "tmpdir");
66 const char *abstract = dbus_address_entry_get_value (entry, "abstract");
68 if (path == NULL && tmpdir == NULL && abstract == NULL)
70 _dbus_server_set_bad_address(error, "unix",
71 "path or tmpdir or abstract",
73 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
76 if ((path && tmpdir) ||
80 _dbus_server_set_bad_address(error, NULL, NULL,
81 "cannot specify two of \"path\" and \"tmpdir\" and \"abstract\" at the same time");
82 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
90 if (!_dbus_string_init (&full_path))
92 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
93 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
96 if (!_dbus_string_init (&filename))
98 _dbus_string_free (&full_path);
99 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
100 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
103 if (!_dbus_string_append (&filename,
105 !_dbus_generate_random_ascii (&filename, 10) ||
106 !_dbus_string_append (&full_path, tmpdir) ||
107 !_dbus_concat_dir_and_file (&full_path, &filename))
109 _dbus_string_free (&full_path);
110 _dbus_string_free (&filename);
111 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
112 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
115 /* Always use abstract namespace if possible with tmpdir */
118 _dbus_server_new_for_domain_socket (_dbus_string_get_const_data (&full_path),
119 #ifdef HAVE_ABSTRACT_SOCKETS
126 _dbus_string_free (&full_path);
127 _dbus_string_free (&filename);
132 *server_p = _dbus_server_new_for_domain_socket (path, FALSE, error);
134 *server_p = _dbus_server_new_for_domain_socket (abstract, TRUE, error);
137 if (*server_p != NULL)
139 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
140 return DBUS_SERVER_LISTEN_OK;
144 _DBUS_ASSERT_ERROR_IS_SET(error);
145 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
150 /* If we don't handle the method, we return NULL with the
153 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
154 return DBUS_SERVER_LISTEN_NOT_HANDLED;
159 * Creates a new server listening on the given Unix domain socket.
161 * @param path the path for the domain socket.
162 * @param abstract #TRUE to use abstract socket namespace
163 * @param error location to store reason for failure.
164 * @returns the new server, or #NULL on failure.
167 _dbus_server_new_for_domain_socket (const char *path,
168 dbus_bool_t abstract,
177 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
179 if (!_dbus_string_init (&address))
181 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
185 _dbus_string_init_const (&path_str, path);
187 !_dbus_string_append (&address, "unix:abstract=")) ||
189 !_dbus_string_append (&address, "unix:path=")) ||
190 !_dbus_address_append_escaped (&address, &path_str))
192 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
196 path_copy = _dbus_strdup (path);
197 if (path_copy == NULL)
199 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
203 listen_fd = _dbus_listen_unix_socket (path, abstract, error);
204 _dbus_fd_set_close_on_exec (listen_fd);
208 _DBUS_ASSERT_ERROR_IS_SET (error);
212 server = _dbus_server_new_for_socket (listen_fd, &address);
215 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
219 _dbus_server_socket_own_filename(server, path_copy);
221 _dbus_string_free (&address);
226 _dbus_close_socket (listen_fd, NULL);
228 dbus_free (path_copy);
230 _dbus_string_free (&address);