D-Bus  1.6.12
dbus-server-unix.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-server-unix.c Server implementation for Unix network protocols.
3  *
4  * Copyright (C) 2002, 2003, 2004 Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23 
24 #include <config.h>
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"
33 
53 DBusServerListenResult
55  DBusServer **server_p,
56  DBusError *error)
57 {
58  const char *method;
59 
60  *server_p = NULL;
61 
62  method = dbus_address_entry_get_method (entry);
63 
64  if (strcmp (method, "unix") == 0)
65  {
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 
70  if (path == NULL && tmpdir == NULL && abstract == NULL)
71  {
72  _dbus_set_bad_address(error, "unix",
73  "path or tmpdir or abstract",
74  NULL);
75  return DBUS_SERVER_LISTEN_BAD_ADDRESS;
76  }
77 
78  if ((path && tmpdir) ||
79  (path && abstract) ||
80  (tmpdir && abstract))
81  {
83  "cannot specify two of \"path\" and \"tmpdir\" and \"abstract\" at the same time");
84  return DBUS_SERVER_LISTEN_BAD_ADDRESS;
85  }
86 
87  if (tmpdir != NULL)
88  {
89  DBusString full_path;
90  DBusString filename;
91 
92  if (!_dbus_string_init (&full_path))
93  {
95  return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
96  }
97 
98  if (!_dbus_string_init (&filename))
99  {
100  _dbus_string_free (&full_path);
102  return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
103  }
104 
105  if (!_dbus_string_append (&filename,
106  "dbus-") ||
107  !_dbus_generate_random_ascii (&filename, 10) ||
108  !_dbus_string_append (&full_path, tmpdir) ||
109  !_dbus_concat_dir_and_file (&full_path, &filename))
110  {
111  _dbus_string_free (&full_path);
112  _dbus_string_free (&filename);
114  return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
115  }
116 
117  /* Always use abstract namespace if possible with tmpdir */
118 
119  *server_p =
121 #ifdef HAVE_ABSTRACT_SOCKETS
122  TRUE,
123 #else
124  FALSE,
125 #endif
126  error);
127 
128  _dbus_string_free (&full_path);
129  _dbus_string_free (&filename);
130  }
131  else
132  {
133  if (path)
134  *server_p = _dbus_server_new_for_domain_socket (path, FALSE, error);
135  else
136  *server_p = _dbus_server_new_for_domain_socket (abstract, TRUE, error);
137  }
138 
139  if (*server_p != NULL)
140  {
141  _DBUS_ASSERT_ERROR_IS_CLEAR(error);
142  return DBUS_SERVER_LISTEN_OK;
143  }
144  else
145  {
146  _DBUS_ASSERT_ERROR_IS_SET(error);
147  return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
148  }
149  }
150  else if (strcmp (method, "systemd") == 0)
151  {
152  int n, *fds;
153  DBusString address;
154 
155  n = _dbus_listen_systemd_sockets (&fds, error);
156  if (n < 0)
157  {
158  _DBUS_ASSERT_ERROR_IS_SET (error);
159  return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
160  }
161 
162  _dbus_string_init_const (&address, "systemd:");
163 
164  *server_p = _dbus_server_new_for_socket (fds, n, &address, NULL);
165  if (*server_p == NULL)
166  {
167  int i;
168 
169  for (i = 0; i < n; i++)
170  {
171  _dbus_close_socket (fds[i], NULL);
172  }
173  dbus_free (fds);
174 
176  return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
177  }
178 
179  dbus_free (fds);
180 
181  return DBUS_SERVER_LISTEN_OK;
182  }
183 #ifdef DBUS_ENABLE_LAUNCHD
184  else if (strcmp (method, "launchd") == 0)
185  {
186  const char *launchd_env_var = dbus_address_entry_get_value (entry, "env");
187  if (launchd_env_var == NULL)
188  {
189  _dbus_set_bad_address (error, "launchd", "env", NULL);
190  return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
191  }
192  *server_p = _dbus_server_new_for_launchd (launchd_env_var, error);
193 
194  if (*server_p != NULL)
195  {
196  _DBUS_ASSERT_ERROR_IS_CLEAR(error);
197  return DBUS_SERVER_LISTEN_OK;
198  }
199  else
200  {
201  _DBUS_ASSERT_ERROR_IS_SET(error);
202  return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
203  }
204  }
205 #endif
206  else
207  {
208  /* If we don't handle the method, we return NULL with the
209  * error unset
210  */
211  _DBUS_ASSERT_ERROR_IS_CLEAR(error);
212  return DBUS_SERVER_LISTEN_NOT_HANDLED;
213  }
214 }
215 
224 DBusServer*
226  dbus_bool_t abstract,
227  DBusError *error)
228 {
229  DBusServer *server;
230  int listen_fd;
231  DBusString address;
232  char *path_copy;
233  DBusString path_str;
234 
235  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
236 
237  if (!_dbus_string_init (&address))
238  {
240  return NULL;
241  }
242 
243  _dbus_string_init_const (&path_str, path);
244  if ((abstract &&
245  !_dbus_string_append (&address, "unix:abstract=")) ||
246  (!abstract &&
247  !_dbus_string_append (&address, "unix:path=")) ||
248  !_dbus_address_append_escaped (&address, &path_str))
249  {
251  goto failed_0;
252  }
253 
254  if (abstract)
255  {
256  path_copy = NULL;
257  }
258  else
259  {
260  path_copy = _dbus_strdup (path);
261  if (path_copy == NULL)
262  {
264  goto failed_0;
265  }
266  }
267 
268  listen_fd = _dbus_listen_unix_socket (path, abstract, error);
269 
270  if (listen_fd < 0)
271  {
272  _DBUS_ASSERT_ERROR_IS_SET (error);
273  goto failed_1;
274  }
275 
276  server = _dbus_server_new_for_socket (&listen_fd, 1, &address, 0);
277  if (server == NULL)
278  {
280  goto failed_2;
281  }
282 
283  if (path_copy != NULL)
284  _dbus_server_socket_own_filename(server, path_copy);
285 
286  _dbus_string_free (&address);
287 
288  return server;
289 
290  failed_2:
291  _dbus_close_socket (listen_fd, NULL);
292  failed_1:
293  dbus_free (path_copy);
294  failed_0:
295  _dbus_string_free (&address);
296 
297  return NULL;
298 }
299 
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:913
#define NULL
A null pointer, defined appropriately for C or C++.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:700
DBusServer * _dbus_server_new_for_launchd(const char *launchd_env_var, DBusError *error)
Creates a new server from launchd.
Internals of DBusServer object.
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
Definition: dbus-address.c:227
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
DBusServerListenResult _dbus_server_listen_platform_specific(DBusAddressEntry *entry, DBusServer **server_p, DBusError *error)
Tries to interpret the address entry in a platform-specific way, creating a platform-specific server ...
const char * dbus_address_entry_get_value(DBusAddressEntry *entry, const char *key)
Returns a value from a key of an entry.
Definition: dbus-address.c:244
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
dbus_bool_t _dbus_generate_random_ascii(DBusString *str, int n_bytes)
Generates the given number of random bytes, where the bytes are chosen from the alphanumeric ASCII su...
Definition: dbus-sysdeps.c:573
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
void _dbus_server_socket_own_filename(DBusServer *server, char *filename)
This is a bad hack since it&#39;s really unix domain socket specific.
DBusServer * _dbus_server_new_for_socket(int *fds, int n_fds, const DBusString *address, DBusNonceFile *noncefile)
Creates a new server listening on the given file descriptor.
Internals of DBusAddressEntry.
Definition: dbus-address.c:43
void _dbus_set_bad_address(DBusError *error, const char *address_problem_type, const char *address_problem_field, const char *address_problem_other)
Sets DBUS_ERROR_BAD_ADDRESS.
Definition: dbus-address.c:65
Object representing an exception.
Definition: dbus-errors.h:48
dbus_bool_t _dbus_address_append_escaped(DBusString *escaped, const DBusString *unescaped)
Appends an escaped version of one string to another string, using the D-Bus address escaping mechanis...
Definition: dbus-address.c:104
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:242
#define TRUE
Expands to &quot;1&quot;.
DBusServer * _dbus_server_new_for_domain_socket(const char *path, dbus_bool_t abstract, DBusError *error)
Creates a new server listening on the given Unix domain socket.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define FALSE
Expands to &quot;0&quot;.
char * _dbus_strdup(const char *str)
Duplicates a string.
dbus_bool_t _dbus_close_socket(int fd, DBusError *error)
Closes a socket.
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
Definition: dbus-string.c:446
int _dbus_listen_unix_socket(const char *path, dbus_bool_t abstract, DBusError *error)
Creates a socket and binds it to the given path, then listens on the socket.
int _dbus_listen_systemd_sockets(int **fds, DBusError *error)
Acquires one or more sockets passed in from systemd.