D-Bus 1.14.10
dbus-transport-unix.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-transport-unix.c UNIX socket subclasses of DBusTransport
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
26#include <stdio.h>
27
28#include "dbus-internals.h"
29#include "dbus-connection-internal.h"
30#include "dbus-transport-unix.h"
31#include "dbus-transport-socket.h"
32#include "dbus-transport-protected.h"
33#include "dbus-watch.h"
34#include "dbus-sysdeps-unix.h"
35#include "dbus-test.h"
36
59 dbus_bool_t abstract,
60 DBusError *error)
61{
62 DBusSocket fd = DBUS_SOCKET_INIT;
63 DBusTransport *transport;
64 DBusString address;
65
66 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
67
68 if (!_dbus_string_init (&address))
69 {
71 return NULL;
72 }
73
74 if ((abstract &&
75 !_dbus_string_append (&address, "unix:abstract=")) ||
76 (!abstract &&
77 !_dbus_string_append (&address, "unix:path=")) ||
78 !_dbus_string_append (&address, path))
79 {
81 goto failed_0;
82 }
83
84 fd.fd = _dbus_connect_unix_socket (path, abstract, error);
85 if (fd.fd < 0)
86 {
87 _DBUS_ASSERT_ERROR_IS_SET (error);
88 goto failed_0;
89 }
90
91 _dbus_verbose ("Successfully connected to unix socket %s\n",
92 path);
93
94 transport = _dbus_transport_new_for_socket (fd, NULL, &address);
95 if (transport == NULL)
96 {
98 goto failed_1;
99 }
100
101 _dbus_string_free (&address);
102
103 return transport;
104
105 failed_1:
107 failed_0:
108 _dbus_string_free (&address);
109 return NULL;
110}
111
123static DBusTransport*
124_dbus_transport_new_for_exec (const char *path,
125 char *const argv[],
126 DBusError *error)
127{
128 DBusSocket fd = DBUS_SOCKET_INIT;
129 DBusTransport *transport;
130 DBusString address;
131 unsigned i;
132 char *escaped;
133
134 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
135
136 if (!_dbus_string_init (&address))
137 {
139 return NULL;
140 }
141
142 escaped = dbus_address_escape_value (path);
143 if (!escaped)
144 {
146 goto failed;
147 }
148
149 if (!_dbus_string_append (&address, "unixexec:path=") ||
150 !_dbus_string_append (&address, escaped))
151 {
153 dbus_free (escaped);
154 goto failed;
155 }
156
157 dbus_free (escaped);
158
159 if (argv)
160 {
161 for (i = 0; argv[i]; i++)
162 {
163 dbus_bool_t success;
164
165 escaped = dbus_address_escape_value (argv[i]);
166 if (!escaped)
167 {
169 goto failed;
170 }
171
172 success = _dbus_string_append_printf (&address, ",argv%u=%s", i, escaped);
173 dbus_free (escaped);
174
175 if (!success)
176 {
178 goto failed;
179 }
180 }
181 }
182
183 fd.fd = _dbus_connect_exec (path, argv, error);
184 if (fd.fd < 0)
185 {
186 _DBUS_ASSERT_ERROR_IS_SET (error);
187 goto failed;
188 }
189
190 _dbus_verbose ("Successfully connected to process %s\n",
191 path);
192
193 transport = _dbus_transport_new_for_socket (fd, NULL, &address);
194 if (transport == NULL)
195 {
197 goto failed;
198 }
199
200 _dbus_string_free (&address);
201
202 return transport;
203
204 failed:
205 if (fd.fd >= 0)
207
208 _dbus_string_free (&address);
209 return NULL;
210}
211
220DBusTransportOpenResult
222 DBusTransport **transport_p,
223 DBusError *error)
224{
225 const char *method;
226
227 method = dbus_address_entry_get_method (entry);
228 _dbus_assert (method != NULL);
229
230 if (strcmp (method, "unix") == 0)
231 {
232 const char *path = dbus_address_entry_get_value (entry, "path");
233 const char *tmpdir = dbus_address_entry_get_value (entry, "tmpdir");
234 const char *abstract = dbus_address_entry_get_value (entry, "abstract");
235
236 if (tmpdir != NULL)
237 {
239 "cannot use the \"tmpdir\" option for an address to connect to, only in an address to listen on");
240 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
241 }
242
243 if (path == NULL && abstract == NULL)
244 {
245 _dbus_set_bad_address (error, "unix",
246 "path or abstract",
247 NULL);
248 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
249 }
250
251 if (path != NULL && abstract != NULL)
252 {
254 "can't specify both \"path\" and \"abstract\" options in an address");
255 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
256 }
257
258 if (path)
259 *transport_p = _dbus_transport_new_for_domain_socket (path, FALSE,
260 error);
261 else
262 *transport_p = _dbus_transport_new_for_domain_socket (abstract, TRUE,
263 error);
264 if (*transport_p == NULL)
265 {
266 _DBUS_ASSERT_ERROR_IS_SET (error);
267 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
268 }
269 else
270 {
271 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
272 return DBUS_TRANSPORT_OPEN_OK;
273 }
274 }
275 else if (strcmp (method, "unixexec") == 0)
276 {
277 const char *path;
278 unsigned i;
279 char **argv;
280
281 path = dbus_address_entry_get_value (entry, "path");
282 if (path == NULL)
283 {
285 "No process path specified");
286 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
287 }
288
289 /* First count argv arguments */
290 for (i = 1; ; i++)
291 {
292 char t[4+20+1]; /* "argv" plus space for a formatted base 10 64bit integer, plus NUL */
293
294 snprintf (t, sizeof(t), "argv%u", i);
295
296 if (!dbus_address_entry_get_value (entry, t))
297 break;
298 }
299
300 /* Allocate string array */
301 argv = dbus_new0 (char*, i+1);
302 if (!argv)
303 {
305 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
306 }
307
308 /* Fill in string array */
309 for (i = 0; ; i++)
310 {
311 char t[4+20+1];
312 const char *p;
313
314 snprintf (t, sizeof(t), "argv%u", i);
315
316 p = dbus_address_entry_get_value (entry, t);
317 if (!p)
318 {
319 if (i == 0)
320 /* If argv0 isn't specified, fill in the path instead */
321 p = path;
322 else
323 break;
324 }
325
326 argv[i] = _dbus_strdup (p);
327 if (!argv[i])
328 {
331 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
332 }
333 }
334
335 *transport_p = _dbus_transport_new_for_exec (path, argv, error);
337
338 if (*transport_p == NULL)
339 {
340 _DBUS_ASSERT_ERROR_IS_SET (error);
341 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
342 }
343 else
344 {
345 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
346 return DBUS_TRANSPORT_OPEN_OK;
347 }
348 }
349#ifdef DBUS_ENABLE_LAUNCHD
350 else if (strcmp (method, "launchd") == 0)
351 {
352 DBusError tmp_error = DBUS_ERROR_INIT;
353 const char *launchd_env_var = dbus_address_entry_get_value (entry, "env");
354 const char *launchd_socket;
355 DBusString socket_path;
356 dbus_bool_t valid_socket;
357
358 if (!_dbus_string_init (&socket_path))
359 {
360 _DBUS_SET_OOM (error);
361 return FALSE;
362 }
363
364 if (launchd_env_var == NULL)
365 {
366 _dbus_set_bad_address (error, "launchd", "env", NULL);
367 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
368 }
369
370 valid_socket = _dbus_lookup_launchd_socket (&socket_path, launchd_env_var, error);
371
372 if (dbus_error_is_set(error))
373 {
374 _dbus_string_free(&socket_path);
375 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
376 }
377
378 if (!valid_socket)
379 {
381 "launchd's env var %s does not exist", launchd_env_var);
382 dbus_error_free(error);
383 dbus_move_error(&tmp_error, error);
384 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
385 }
386
387 launchd_socket = _dbus_string_get_const_data(&socket_path);
388 *transport_p = _dbus_transport_new_for_domain_socket (launchd_socket, FALSE, error);
389
390 if (*transport_p == NULL)
391 {
392 _DBUS_ASSERT_ERROR_IS_SET (error);
393 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
394 }
395 else
396 {
397 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
398 return DBUS_TRANSPORT_OPEN_OK;
399 }
400 }
401#endif
402 else
403 {
404 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
405 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
406 }
407}
408
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:68
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
Definition: dbus-address.c:230
char * dbus_address_escape_value(const char *value)
Escapes the given string as a value in a key=value pair for a D-Bus address.
Definition: dbus-address.c:586
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:247
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
void dbus_move_error(DBusError *src, DBusError *dest)
Moves an error src into dest, freeing src and overwriting dest.
Definition: dbus-errors.c:279
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_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
char * _dbus_strdup(const char *str)
Duplicates a string.
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:692
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:740
#define DBUS_ERROR_BAD_ADDRESS
A D-Bus bus address was malformed.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
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:966
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:182
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:278
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
Definition: dbus-string.c:1145
int _dbus_connect_unix_socket(const char *path, dbus_bool_t abstract, DBusError *error)
Creates a socket and connects it to the UNIX domain socket at the given path.
dbus_bool_t _dbus_lookup_launchd_socket(DBusString *socket_path, const char *launchd_env_var, DBusError *error)
quries launchd for a specific env var which holds the socket path.
int _dbus_connect_exec(const char *path, char *const argv[], DBusError *error)
Creates a UNIX domain socket and connects it to the specified process to execute.
dbus_bool_t _dbus_close_socket(DBusSocket fd, DBusError *error)
Closes a socket.
DBusTransport * _dbus_transport_new_for_socket(DBusSocket fd, const DBusString *server_guid, const DBusString *address)
Creates a new transport for the given socket file descriptor.
DBusTransport * _dbus_transport_new_for_domain_socket(const char *path, dbus_bool_t abstract, DBusError *error)
Creates a new transport for the given Unix domain socket path.
DBusTransportOpenResult _dbus_transport_open_platform_specific(DBusAddressEntry *entry, DBusTransport **transport_p, DBusError *error)
Opens platform specific transport types.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
Internals of DBusAddressEntry.
Definition: dbus-address.c:47
Object representing an exception.
Definition: dbus-errors.h:49
Socket interface.
Definition: dbus-sysdeps.h:181
Object representing a transport such as a socket.