D-Bus  1.14.99
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 
56 static DBusTransport*
57 _dbus_transport_new_for_exec (const char *path,
58  char *const argv[],
59  DBusError *error)
60 {
61  DBusSocket fd = DBUS_SOCKET_INIT;
62  DBusTransport *transport;
63  DBusString address;
64  unsigned i;
65  char *escaped;
66 
67  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
68 
69  if (!_dbus_string_init (&address))
70  {
72  return NULL;
73  }
74 
75  escaped = dbus_address_escape_value (path);
76  if (!escaped)
77  {
79  goto failed;
80  }
81 
82  if (!_dbus_string_append (&address, "unixexec:path=") ||
83  !_dbus_string_append (&address, escaped))
84  {
86  dbus_free (escaped);
87  goto failed;
88  }
89 
90  dbus_free (escaped);
91 
92  if (argv)
93  {
94  for (i = 0; argv[i]; i++)
95  {
96  dbus_bool_t success;
97 
98  escaped = dbus_address_escape_value (argv[i]);
99  if (!escaped)
100  {
102  goto failed;
103  }
104 
105  success = _dbus_string_append_printf (&address, ",argv%u=%s", i, escaped);
106  dbus_free (escaped);
107 
108  if (!success)
109  {
111  goto failed;
112  }
113  }
114  }
115 
116  fd = _dbus_connect_exec (path, argv, error);
117  if (!_dbus_socket_is_valid (fd))
118  {
119  _DBUS_ASSERT_ERROR_IS_SET (error);
120  goto failed;
121  }
122 
123  _dbus_verbose ("Successfully connected to process %s\n",
124  path);
125 
126  transport = _dbus_transport_new_for_socket (fd, NULL, &address);
127  if (transport == NULL)
128  {
130  goto failed;
131  }
132 
133  _dbus_string_free (&address);
134 
135  return transport;
136 
137  failed:
138  if (_dbus_socket_is_valid (fd))
139  _dbus_close_socket (&fd, NULL);
140 
141  _dbus_string_free (&address);
142  return NULL;
143 }
144 
145 
146 DBusTransportOpenResult
147 _dbus_transport_open_unixexec (DBusAddressEntry *entry,
148  DBusTransport **transport_p,
149  DBusError *error)
150 {
151  const char *method;
152 
153  method = dbus_address_entry_get_method (entry);
154  _dbus_assert (method != NULL);
155 
156  if (strcmp (method, "unixexec") == 0)
157  {
158  const char *path;
159  unsigned i;
160  char **argv;
161 
162  path = dbus_address_entry_get_value (entry, "path");
163  if (path == NULL)
164  {
166  "No process path specified");
167  return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
168  }
169 
170  /* First count argv arguments */
171  for (i = 1; ; i++)
172  {
173  char t[4+20+1]; /* "argv" plus space for a formatted base 10 64bit integer, plus NUL */
174 
175  snprintf (t, sizeof(t), "argv%u", i);
176 
177  if (!dbus_address_entry_get_value (entry, t))
178  break;
179  }
180 
181  /* Allocate string array */
182  argv = dbus_new0 (char*, i+1);
183  if (!argv)
184  {
186  return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
187  }
188 
189  /* Fill in string array */
190  for (i = 0; ; i++)
191  {
192  char t[4+20+1];
193  const char *p;
194 
195  snprintf (t, sizeof(t), "argv%u", i);
196 
197  p = dbus_address_entry_get_value (entry, t);
198  if (!p)
199  {
200  if (i == 0)
201  /* If argv0 isn't specified, fill in the path instead */
202  p = path;
203  else
204  break;
205  }
206 
207  argv[i] = _dbus_strdup (p);
208  if (!argv[i])
209  {
210  dbus_free_string_array (argv);
212  return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
213  }
214  }
215 
216  *transport_p = _dbus_transport_new_for_exec (path, argv, error);
217  dbus_free_string_array (argv);
218 
219  if (*transport_p == NULL)
220  {
221  _DBUS_ASSERT_ERROR_IS_SET (error);
222  return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
223  }
224  else
225  {
226  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
227  return DBUS_TRANSPORT_OPEN_OK;
228  }
229  }
230  else
231  {
232  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
233  return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
234  }
235 }
236 
245 DBusTransportOpenResult
247  DBusTransport **transport_p,
248  DBusError *error)
249 {
250 #ifdef DBUS_ENABLE_LAUNCHD
251  const char *method;
252 
253  method = dbus_address_entry_get_method (entry);
254  _dbus_assert (method != NULL);
255 
256  if (strcmp (method, "launchd") == 0)
257  {
258  DBusError tmp_error = DBUS_ERROR_INIT;
259  const char *launchd_env_var = dbus_address_entry_get_value (entry, "env");
260  const char *launchd_socket;
261  DBusString socket_path;
262  dbus_bool_t valid_socket;
263 
264  if (!_dbus_string_init (&socket_path))
265  {
266  _DBUS_SET_OOM (error);
267  return FALSE;
268  }
269 
270  if (launchd_env_var == NULL)
271  {
272  _dbus_set_bad_address (error, "launchd", "env", NULL);
273  return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
274  }
275 
276  valid_socket = _dbus_lookup_launchd_socket (&socket_path, launchd_env_var, error);
277 
278  if (dbus_error_is_set(error))
279  {
280  _dbus_string_free(&socket_path);
281  return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
282  }
283 
284  if (!valid_socket)
285  {
287  "launchd's env var %s does not exist", launchd_env_var);
288  dbus_error_free(error);
289  dbus_move_error(&tmp_error, error);
290  return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
291  }
292 
293  launchd_socket = _dbus_string_get_const_data(&socket_path);
294  *transport_p = _dbus_transport_new_for_domain_socket (launchd_socket, FALSE, error);
295 
296  if (*transport_p == NULL)
297  {
298  _DBUS_ASSERT_ERROR_IS_SET (error);
299  return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
300  }
301  else
302  {
303  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
304  return DBUS_TRANSPORT_OPEN_OK;
305  }
306  }
307  else
308 #endif /* DBUS_ENABLE_LAUNCHD */
309  {
310  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
311  return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
312  }
313 }
314 
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 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:980
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:1147
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.
dbus_bool_t _dbus_close_socket(DBusSocket *fd, DBusError *error)
Closes a socket and invalidates it.
DBusSocket _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.
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.