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,
62 DBusSocket fd = DBUS_SOCKET_INIT;
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);
75 !_dbus_string_append (&address, "unix:abstract=")) ||
77 !_dbus_string_append (&address, "unix:path=")) ||
78 !_dbus_string_append (&address, path))
80 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
84 fd.fd = _dbus_connect_unix_socket (path, abstract, error);
87 _DBUS_ASSERT_ERROR_IS_SET (error);
91 _dbus_verbose ("Successfully connected to unix socket %s\n",
94 transport = _dbus_transport_new_for_socket (fd, NULL, &address);
95 if (transport == NULL)
97 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
101 _dbus_string_free (&address);
106 _dbus_close_socket (fd, NULL);
108 _dbus_string_free (&address);
113 * Creates a new transport for the given binary and arguments. This
114 * creates a client-side of a transport. The process will be forked
115 * off and executed with stdin/stdout connected to a local AF_UNIX
118 * @param path the path to the domain socket.
119 * @param argv Parameters list
120 * @param error address where an error can be returned.
121 * @returns a new transport, or #NULL on failure.
123 static DBusTransport*
124 _dbus_transport_new_for_exec (const char *path,
128 DBusSocket fd = DBUS_SOCKET_INIT;
129 DBusTransport *transport;
134 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
136 if (!_dbus_string_init (&address))
138 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
142 escaped = dbus_address_escape_value (path);
145 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
149 if (!_dbus_string_append (&address, "unixexec:path=") ||
150 !_dbus_string_append (&address, escaped))
152 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
161 for (i = 0; argv[i]; i++)
165 escaped = dbus_address_escape_value (argv[i]);
168 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
172 success = _dbus_string_append_printf (&address, ",argv%u=%s", i, escaped);
177 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
183 fd.fd = _dbus_connect_exec (path, argv, error);
186 _DBUS_ASSERT_ERROR_IS_SET (error);
190 _dbus_verbose ("Successfully connected to process %s\n",
193 transport = _dbus_transport_new_for_socket (fd, NULL, &address);
194 if (transport == NULL)
196 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
200 _dbus_string_free (&address);
206 _dbus_close_socket (fd, NULL);
208 _dbus_string_free (&address);
213 * Opens platform specific transport types.
215 * @param entry the address entry to try opening
216 * @param transport_p return location for the opened transport
217 * @param error error to be set
218 * @returns result of the attempt
220 DBusTransportOpenResult
221 _dbus_transport_open_platform_specific (DBusAddressEntry *entry,
222 DBusTransport **transport_p,
227 method = dbus_address_entry_get_method (entry);
228 _dbus_assert (method != NULL);
230 if (strcmp (method, "unix") == 0)
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");
238 _dbus_set_bad_address (error, NULL, NULL,
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;
243 if (path == NULL && abstract == NULL)
245 _dbus_set_bad_address (error, "unix",
248 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
251 if (path != NULL && abstract != NULL)
253 _dbus_set_bad_address (error, NULL, NULL,
254 "can't specify both \"path\" and \"abstract\" options in an address");
255 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
259 *transport_p = _dbus_transport_new_for_domain_socket (path, FALSE,
262 *transport_p = _dbus_transport_new_for_domain_socket (abstract, TRUE,
264 if (*transport_p == NULL)
266 _DBUS_ASSERT_ERROR_IS_SET (error);
267 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
271 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
272 return DBUS_TRANSPORT_OPEN_OK;
275 else if (strcmp (method, "unixexec") == 0)
281 path = dbus_address_entry_get_value (entry, "path");
284 _dbus_set_bad_address (error, NULL, NULL,
285 "No process path specified");
286 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
289 /* First count argv arguments */
292 char t[4+20+1]; /* "argv" plus space for a formatted base 10 64bit integer, plus NUL */
294 snprintf (t, sizeof(t), "argv%u", i);
296 if (!dbus_address_entry_get_value (entry, t))
300 /* Allocate string array */
301 argv = dbus_new0 (char*, i+1);
304 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
305 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
308 /* Fill in string array */
314 snprintf (t, sizeof(t), "argv%u", i);
316 p = dbus_address_entry_get_value (entry, t);
320 /* If argv0 isn't specified, fill in the path instead */
326 argv[i] = _dbus_strdup (p);
329 dbus_free_string_array (argv);
330 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
331 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
335 *transport_p = _dbus_transport_new_for_exec (path, argv, error);
336 dbus_free_string_array (argv);
338 if (*transport_p == NULL)
340 _DBUS_ASSERT_ERROR_IS_SET (error);
341 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
345 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
346 return DBUS_TRANSPORT_OPEN_OK;
349 #ifdef DBUS_ENABLE_LAUNCHD
350 else if (strcmp (method, "launchd") == 0)
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;
358 if (!_dbus_string_init (&socket_path))
360 _DBUS_SET_OOM (error);
364 if (launchd_env_var == NULL)
366 _dbus_set_bad_address (error, "launchd", "env", NULL);
367 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
370 valid_socket = _dbus_lookup_launchd_socket (&socket_path, launchd_env_var, error);
372 if (dbus_error_is_set(error))
374 _dbus_string_free(&socket_path);
375 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
380 dbus_set_error(&tmp_error, DBUS_ERROR_BAD_ADDRESS,
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;
387 launchd_socket = _dbus_string_get_const_data(&socket_path);
388 *transport_p = _dbus_transport_new_for_domain_socket (launchd_socket, FALSE, error);
390 if (*transport_p == NULL)
392 _DBUS_ASSERT_ERROR_IS_SET (error);
393 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
397 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
398 return DBUS_TRANSPORT_OPEN_OK;
404 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
405 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
411 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
414 _dbus_transport_unix_test (void)
421 dbus_error_init (&error);
423 c = dbus_connection_open ("unixexec:argv0=false,argv1=foobar,path=/bin/false", &error);
424 _dbus_assert (c != NULL);
425 _dbus_assert (!dbus_error_is_set (&error));
427 address = _dbus_connection_get_address (c);
428 _dbus_assert (address != NULL);
430 /* Let's see if the address got parsed, reordered and formatted correctly */
431 ret = strcmp (address, "unixexec:path=/bin/false,argv0=false,argv1=foobar") == 0;
433 dbus_connection_unref (c);