D-Bus 1.13.18

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
57 DBusTransport*
58 _dbus_transport_new_for_domain_socket (const char *path,
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 {
70 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 {
80 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 {
97 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
98 goto failed_1;
99 }
100
101 _dbus_string_free (&address);
102
103 return transport;
104
105 failed_1:
106 _dbus_close_socket (fd, NULL);
107 failed_0:
108 _dbus_string_free (&address);
109 return NULL;
110 }
111
123 static 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 {
138 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
139 return NULL;
140 }
141
142 escaped = dbus_address_escape_value (path);
143 if (!escaped)
144 {
145 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
146 goto failed;
147 }
148
149 if (!_dbus_string_append (&address, "unixexec:path=") ||
150 !_dbus_string_append (&address, escaped))
151 {
152 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 {
168 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 {
177 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 {
196 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
197 goto failed;
198 }
199
200 _dbus_string_free (&address);
201
202 return transport;
203
204 failed:
205 if (fd.fd >= 0)
206 _dbus_close_socket (fd, NULL);
207
208 _dbus_string_free (&address);
209 return NULL;
210 }
211
220 DBusTransportOpenResult
221 _dbus_transport_open_platform_specific (DBusAddressEntry *entry,
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 {
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;
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 {
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;
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 {
284 _dbus_set_bad_address (error, NULL, NULL,
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 {
304 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 {
329 dbus_free_string_array (argv);
330 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
331 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
332 }
333 }
334
335 *transport_p = _dbus_transport_new_for_exec (path, argv, error);
336 dbus_free_string_array (argv);
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 {
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;
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
_dbus_transport_open_platform_specific
DBusTransportOpenResult _dbus_transport_open_platform_specific(DBusAddressEntry *entry, DBusTransport **transport_p, DBusError *error)
Opens platform specific transport types.
Definition: dbus-transport-unix.c:221
_dbus_string_free
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:271
_dbus_lookup_launchd_socket
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.
Definition: dbus-sysdeps-unix.c:4322
dbus_address_entry_get_method
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
Definition: dbus-address.c:230
_dbus_connect_unix_socket
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.
Definition: dbus-sysdeps-unix.c:936
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:182
_dbus_connect_exec
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.
Definition: dbus-sysdeps-unix.c:1033
TRUE
#define TRUE
_dbus_transport_new_for_domain_socket
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.
Definition: dbus-transport-unix.c:58
dbus_address_entry_get_value
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
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:704
DBUS_ERROR_BAD_ADDRESS
#define DBUS_ERROR_BAD_ADDRESS
A D-Bus bus address was malformed.
Definition: dbus-protocol.h:371
DBusString
Definition: dbus-string.h:43
_dbus_string_append_printf
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:1138
DBusSocket
Socket interface.
Definition: dbus-sysdeps.h:181
DBusAddressEntry
Internals of DBusAddressEntry.
Definition: dbus-address.c:47
FALSE
#define FALSE
DBusTransport
Object representing a transport such as a socket.
Definition: dbus-transport-protected.h:81
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition: dbus-protocol.h:361
DBUS_ERROR_INIT
#define DBUS_ERROR_INIT
Definition: dbus-errors.h:62
_dbus_set_bad_address
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
dbus_error_is_set
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329
dbus_address_escape_value
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
_dbus_close_socket
dbus_bool_t _dbus_close_socket(DBusSocket fd, DBusError *error)
Closes a socket.
Definition: dbus-sysdeps-unix.c:286
_dbus_assert
#define _dbus_assert(condition)
Definition: dbus-internals.h:153
dbus_error_free
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_strdup
char * _dbus_strdup(const char *str)
Duplicates a string.
Definition: dbus-internals.c:589
DBusError
Object representing an exception.
Definition: dbus-errors.h:49
dbus_set_error
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
dbus_free_string_array
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:752
dbus_new0
#define dbus_new0(type, count)
Definition: dbus-memory.h:58
_dbus_transport_new_for_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.
Definition: dbus-transport-socket.c:1291
_dbus_string_append
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:959
dbus_move_error
void dbus_move_error(DBusError *src, DBusError *dest)
Moves an error src into dest, freeing src and overwriting dest.
Definition: dbus-errors.c:279
dbus_bool_t
dbus_uint32_t dbus_bool_t
Definition: dbus-types.h:35
NULL
#define NULL