1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-transport-unix.c UNIX socket subclasses of DBusTransport
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
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"
38 * @defgroup DBusTransportUnix DBusTransport implementations for UNIX
39 * @ingroup DBusInternals
40 * @brief Implementation details of DBusTransport on UNIX
46 * Creates a new transport for the given Unix domain socket
47 * path. This creates a client-side of a transport.
49 * @todo once we add a way to escape paths in a dbus
50 * address, this function needs to do escaping.
52 * @param path the path to the domain socket.
53 * @param abstract #TRUE to use abstract socket namespace
54 * @param error address where an error can be returned.
55 * @returns a new transport, or #NULL on failure.
58 _dbus_transport_new_for_domain_socket (const char *path,
63 DBusTransport *transport;
66 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
68 if (!_dbus_string_init (&address))
70 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
77 !_dbus_string_append (&address, "unix:abstract=")) ||
79 !_dbus_string_append (&address, "unix:path=")) ||
80 !_dbus_string_append (&address, path))
82 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
86 fd = _dbus_connect_unix_socket (path, abstract, error);
89 _DBUS_ASSERT_ERROR_IS_SET (error);
93 _dbus_verbose ("Successfully connected to unix socket %s\n",
96 transport = _dbus_transport_new_for_socket (fd, NULL, &address);
97 if (transport == NULL)
99 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
103 _dbus_string_free (&address);
108 _dbus_close_socket (fd, NULL);
110 _dbus_string_free (&address);
115 * Creates a new transport for the given binary and arguments. This
116 * creates a client-side of a transport. The process will be forked
117 * off and executed with stdin/stdout connected to a local AF_UNIX
120 * @param path the path to the domain socket.
121 * @param argv Parameters list
122 * @param error address where an error can be returned.
123 * @returns a new transport, or #NULL on failure.
125 static DBusTransport*
126 _dbus_transport_new_for_exec (const char *path,
131 DBusTransport *transport;
136 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
138 if (!_dbus_string_init (&address))
140 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
146 escaped = dbus_address_escape_value (path);
149 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
153 if (!_dbus_string_append (&address, "unixexec:path=") ||
154 !_dbus_string_append (&address, escaped))
156 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
165 for (i = 0; argv[i]; i++)
169 escaped = dbus_address_escape_value (argv[i]);
172 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
176 success = _dbus_string_append_printf (&address, ",argv%u=%s", i, escaped);
181 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
187 fd = _dbus_connect_exec (path, argv, error);
190 _DBUS_ASSERT_ERROR_IS_SET (error);
194 _dbus_verbose ("Successfully connected to process %s\n",
197 transport = _dbus_transport_new_for_socket (fd, NULL, &address);
198 if (transport == NULL)
200 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
204 _dbus_string_free (&address);
210 _dbus_close_socket (fd, NULL);
212 _dbus_string_free (&address);
217 * Opens platform specific transport types.
219 * @param entry the address entry to try opening
220 * @param transport_p return location for the opened transport
221 * @param error error to be set
222 * @returns result of the attempt
224 DBusTransportOpenResult
225 _dbus_transport_open_platform_specific (DBusAddressEntry *entry,
226 DBusTransport **transport_p,
231 method = dbus_address_entry_get_method (entry);
232 _dbus_assert (method != NULL);
234 if (strcmp (method, "unix") == 0)
236 const char *path = dbus_address_entry_get_value (entry, "path");
237 const char *tmpdir = dbus_address_entry_get_value (entry, "tmpdir");
238 const char *abstract = dbus_address_entry_get_value (entry, "abstract");
242 _dbus_set_bad_address (error, NULL, NULL,
243 "cannot use the \"tmpdir\" option for an address to connect to, only in an address to listen on");
244 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
247 if (path == NULL && abstract == NULL)
249 _dbus_set_bad_address (error, "unix",
252 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
255 if (path != NULL && abstract != NULL)
257 _dbus_set_bad_address (error, NULL, NULL,
258 "can't specify both \"path\" and \"abstract\" options in an address");
259 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
263 *transport_p = _dbus_transport_new_for_domain_socket (path, FALSE,
266 *transport_p = _dbus_transport_new_for_domain_socket (abstract, TRUE,
268 if (*transport_p == NULL)
270 _DBUS_ASSERT_ERROR_IS_SET (error);
271 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
275 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
276 return DBUS_TRANSPORT_OPEN_OK;
279 else if (strcmp (method, "unixexec") == 0)
285 path = dbus_address_entry_get_value (entry, "path");
288 _dbus_set_bad_address (error, NULL, NULL,
289 "No process path specified");
290 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
293 /* First count argv arguments */
296 char t[4+20+1]; /* "argv" plus space for a formatted base 10 64bit integer, plus NUL */
298 snprintf (t, sizeof(t), "argv%u", i);
300 if (!dbus_address_entry_get_value (entry, t))
304 /* Allocate string array */
305 argv = dbus_new0 (char*, i+1);
308 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
309 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
312 /* Fill in string array */
318 snprintf (t, sizeof(t), "argv%u", i);
320 p = dbus_address_entry_get_value (entry, t);
324 /* If argv0 isn't specified, fill in the path instead */
330 argv[i] = _dbus_strdup (p);
333 dbus_free_string_array (argv);
334 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
335 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
339 *transport_p = _dbus_transport_new_for_exec (path, argv, error);
340 dbus_free_string_array (argv);
342 if (*transport_p == NULL)
344 _DBUS_ASSERT_ERROR_IS_SET (error);
345 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
349 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
350 return DBUS_TRANSPORT_OPEN_OK;
353 #ifdef DBUS_ENABLE_LAUNCHD
354 else if (strcmp (method, "launchd") == 0)
356 DBusError tmp_error = DBUS_ERROR_INIT;
357 const char *launchd_env_var = dbus_address_entry_get_value (entry, "env");
358 const char *launchd_socket;
359 DBusString socket_path;
360 dbus_bool_t valid_socket;
362 if (!_dbus_string_init (&socket_path))
364 _DBUS_SET_OOM (error);
368 if (launchd_env_var == NULL)
370 _dbus_set_bad_address (error, "launchd", "env", NULL);
371 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
374 valid_socket = _dbus_lookup_launchd_socket (&socket_path, launchd_env_var, error);
376 if (dbus_error_is_set(error))
378 _dbus_string_free(&socket_path);
379 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
384 dbus_set_error(&tmp_error, DBUS_ERROR_BAD_ADDRESS,
385 "launchd's env var %s does not exist", launchd_env_var);
386 dbus_error_free(error);
387 dbus_move_error(&tmp_error, error);
388 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
391 launchd_socket = _dbus_string_get_const_data(&socket_path);
392 *transport_p = _dbus_transport_new_for_domain_socket (launchd_socket, FALSE, error);
394 if (*transport_p == NULL)
396 _DBUS_ASSERT_ERROR_IS_SET (error);
397 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
401 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
402 return DBUS_TRANSPORT_OPEN_OK;
408 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
409 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
415 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
418 _dbus_transport_unix_test (void)
425 dbus_error_init (&error);
427 c = dbus_connection_open ("unixexec:argv0=false,argv1=foobar,path=/bin/false", &error);
428 _dbus_assert (c != NULL);
429 _dbus_assert (!dbus_error_is_set (&error));
431 address = _dbus_connection_get_address (c);
432 _dbus_assert (address != NULL);
434 /* Let's see if the address got parsed, reordered and formatted correctly */
435 ret = strcmp (address, "unixexec:path=/bin/false,argv0=false,argv1=foobar") == 0;
437 dbus_connection_unref (c);