D-Bus  1.14.10
dbus-server-launchd.c
1 /* dbus-server-launchd.c Server methods for interacting with launchd.
2  * Copyright (C) 2007, Tanner Lovelace <lovelace@wayfarer.org>
3  * Copyright (C) 2008, Colin Walters <walters@verbum.org>
4  * Copyright (C) 2008-2009, Benjamin Reed <rangerrick@befunk.com>
5  * Copyright (C) 2009, Jonas Bähr <jonas.baehr@web.de>
6  *
7  * Permission is hereby granted, free of charge, to any person
8  * obtaining a copy of this software and associated documentation
9  * files (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use, copy,
11  * modify, merge, publish, distribute, sublicense, and/or sell copies
12  * of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26  */
27 
28 #include <config.h>
29 #include "dbus-server-launchd.h"
30 
39 #ifdef DBUS_ENABLE_LAUNCHD
40 #include <launch.h>
41 #include <errno.h>
42 #include <unistd.h>
43 
44 #include "dbus-misc.h"
45 #include "dbus-server-socket.h"
46 #include "dbus-sysdeps-unix.h"
47 
48 /* put other private launchd functions here */
49 
50 #endif /* DBUS_ENABLE_LAUNCHD */
51 
66 DBusServer *
67 _dbus_server_new_for_launchd (const char *launchd_env_var, DBusError * error)
68  {
69 #ifdef DBUS_ENABLE_LAUNCHD
70  DBusServer *server;
71  DBusString address;
72  int launchd_fd = -1;
73  launch_data_t sockets_dict, checkin_response;
74  launch_data_t checkin_request;
75  launch_data_t listening_fd_array, listening_fd;
76  launch_data_t environment_dict, environment_param;
77  const char *launchd_socket_path, *display;
78 
79  launchd_socket_path = _dbus_getenv (launchd_env_var);
80  display = _dbus_getenv ("DISPLAY");
81 
82  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
83 
84  if (launchd_socket_path == NULL || *launchd_socket_path == '\0')
85  {
87  "launchd's environment variable %s is empty, but should contain a socket path.\n", launchd_env_var);
88  return NULL;
89  }
90 
91  if (!_dbus_string_init (&address))
92  {
94  return NULL;
95  }
96  if (!_dbus_string_append (&address, "unix:path="))
97  {
99  goto l_failed_0;
100  }
101  if (!_dbus_string_append (&address, launchd_socket_path))
102  {
104  goto l_failed_0;
105  }
106 
107  if ((checkin_request = launch_data_new_string (LAUNCH_KEY_CHECKIN)) == NULL)
108  {
110  "launch_data_new_string(\"%s\") Unable to create string.\n",
111  LAUNCH_KEY_CHECKIN);
112  goto l_failed_0;
113  }
114 
115  if ((checkin_response = launch_msg (checkin_request)) == NULL)
116  {
118  "launch_msg(\"%s\") IPC failure: %s\n",
119  LAUNCH_KEY_CHECKIN, strerror (errno));
120  goto l_failed_0;
121  }
122 
123  if (LAUNCH_DATA_ERRNO == launch_data_get_type (checkin_response))
124  {
125  dbus_set_error (error, DBUS_ERROR_FAILED, "Check-in failed: %s\n",
126  strerror (launch_data_get_errno (checkin_response)));
127  goto l_failed_0;
128  }
129 
130  sockets_dict =
131  launch_data_dict_lookup (checkin_response, LAUNCH_JOBKEY_SOCKETS);
132  if (NULL == sockets_dict)
133  {
135  "No sockets found to answer requests on!\n");
136  goto l_failed_0;
137  }
138 
139  listening_fd_array =
140  launch_data_dict_lookup (sockets_dict, "unix_domain_listener");
141  if (NULL == listening_fd_array)
142  {
144  "No known sockets found to answer requests on!\n");
145  goto l_failed_0;
146  }
147 
148  if (launch_data_array_get_count (listening_fd_array) != 1)
149  {
151  "Expected 1 socket from launchd, got %d.\n",
152  launch_data_array_get_count (listening_fd_array));
153  goto l_failed_0;
154  }
155 
156  listening_fd = launch_data_array_get_index (listening_fd_array, 0);
157  launchd_fd = launch_data_get_fd (listening_fd);
158 
159  _dbus_fd_set_close_on_exec (launchd_fd);
160 
161  if (launchd_fd < 0)
162  {
163  _DBUS_ASSERT_ERROR_IS_SET (error);
164  goto l_failed_0;
165  if (display == NULL || *display == '\0')
166  {
167  environment_dict = launch_data_dict_lookup (checkin_response, LAUNCH_JOBKEY_USERENVIRONMENTVARIABLES);
168  if (NULL == environment_dict)
169  {
170  _dbus_warn ("Unable to retrieve user environment from launchd.");
171  }
172  else
173  {
174  environment_param = launch_data_dict_lookup (environment_dict, "DISPLAY");
175  if (NULL == environment_param)
176  {
177  _dbus_warn ("Unable to retrieve DISPLAY from launchd.");
178  }
179  else
180  {
181  display = launch_data_get_string(environment_param);
182  dbus_setenv ("DISPLAY", display);
183  }
184  }
185  }
186 
187  }
188 
189  server = _dbus_server_new_for_socket (&launchd_fd, 1, &address, 0, error);
190  if (server == NULL)
191  {
192  goto l_failed_0;
193  }
194 
195  _dbus_string_free (&address);
196 
197  return server;
198 
199  l_failed_0:
200  if (launchd_fd >= 0)
201  close (launchd_fd);
202 
203  _dbus_string_free (&address);
204 
205  return NULL;
206 #else /* DBUS_ENABLE_LAUNCHD */
208  "address type 'launchd' requested, but launchd support not compiled in");
209  return NULL;
210 #endif
211  }
212 
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_warn(const char *format,...)
Prints a warning message to stderr.
#define NULL
A null pointer, defined appropriately for C or C++.
DBUS_EXPORT dbus_bool_t dbus_setenv(const char *variable, const char *value)
Wrapper for setenv().
Definition: dbus-sysdeps.c:124
#define DBUS_ERROR_BAD_ADDRESS
A D-Bus bus address was malformed.
#define DBUS_ERROR_IO_ERROR
Something went wrong reading or writing to a socket, for example.
#define DBUS_ERROR_LIMITS_EXCEEDED
Some limited resource is exhausted.
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
DBusServer * _dbus_server_new_for_launchd(const char *launchd_env_var, DBusError *error)
Creates a new server from launchd.
DBusServer * _dbus_server_new_for_socket(DBusSocket *fds, int n_fds, const DBusString *address, DBusNonceFile *noncefile, DBusError *error)
Creates a new server listening on the given file descriptor.
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
void _dbus_fd_set_close_on_exec(int fd)
Sets the file descriptor to be close on exec.
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:195
Object representing an exception.
Definition: dbus-errors.h:49
Internals of DBusServer object.