1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-run-session.c - run a child process in its own session
4 * Copyright © 2003-2006 Red Hat, Inc.
5 * Copyright © 2006 Thiago Macieira <thiago@kde.org>
6 * Copyright © 2011-2012 Nokia Corporation
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
35 #include <sys/types.h>
39 #include "dbus/dbus.h"
41 #define MAX_ADDR_LEN 512
42 #define PIPE_READ_END 0
43 #define PIPE_WRITE_END 1
47 * If you are in a shell and run "dbus-run-session myapp", here is what
48 * happens (compare and contrast with dbus-launch):
51 * \- dbus-run-session myapp
52 * \- dbus-daemon --nofork --print-address --session
55 * All processes are long-running.
57 * When myapp exits, dbus-run-session kills dbus-daemon and terminates.
59 * If dbus-daemon exits, dbus-run-session warns and continues to run.
63 * dbus-daemon --print-address -> bus_address_pipe -> d-r-s
66 static const char me[] = "dbus-run-session";
72 "%s [OPTIONS] [--] PROGRAM [ARGUMENTS]\n"
77 "--dbus-daemon=BINARY run BINARY instead of dbus-daemon\n"
78 "--config-file=FILENAME pass to dbus-daemon instead of --session\n"
88 "Copyright (C) 2003-2006 Red Hat, Inc.\n"
89 "Copyright (C) 2006 Thiago Macieira\n"
90 "Copyright © 2011-2012 Nokia Corporation\n"
92 "This is free software; see the source for copying conditions.\n"
93 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
101 fprintf (stderr, "%s: out of memory\n", me);
107 READ_STATUS_OK, /**< Read succeeded */
108 READ_STATUS_ERROR, /**< Some kind of error */
109 READ_STATUS_EOF /**< EOF returned */
120 memset (buf, '\0', maxlen);
121 maxlen -= 1; /* ensure nul term */
123 retval = READ_STATUS_OK;
131 to_read = maxlen - bytes;
139 if (chunk < 0 && errno == EINTR)
144 retval = READ_STATUS_ERROR;
149 retval = READ_STATUS_EOF;
156 if (retval == READ_STATUS_EOF &&
158 retval = READ_STATUS_OK;
161 if (retval != READ_STATUS_ERROR &&
163 buf[bytes-1] == '\n')
170 exec_dbus_daemon (const char *dbus_daemon,
171 int bus_address_pipe[2],
172 const char *config_file)
174 /* Child process, which execs dbus-daemon or dies trying */
175 #define MAX_FD_LEN 64
176 char write_address_fd_as_string[MAX_FD_LEN];
178 close (bus_address_pipe[PIPE_READ_END]);
180 sprintf (write_address_fd_as_string, "%d", bus_address_pipe[PIPE_WRITE_END]);
185 "--print-address", write_address_fd_as_string,
186 config_file ? "--config-file" : "--session",
187 config_file, /* has to be last in this varargs list */
190 fprintf (stderr, "%s: failed to execute message bus daemon '%s': %s\n",
191 me, dbus_daemon, strerror (errno));
195 exec_app (int prog_arg, char **argv)
197 execvp (argv[prog_arg], argv + prog_arg);
199 fprintf (stderr, "%s: failed to exec '%s': %s\n", me, argv[prog_arg],
205 main (int argc, char **argv)
208 int bus_address_pipe[2] = { 0, 0 };
209 const char *config_file = NULL;
210 const char *dbus_daemon = NULL;
211 char bus_address[MAX_ADDR_LEN] = { 0 };
212 const char *prev_arg = NULL;
214 int requires_arg = 0;
220 const char *arg = argv[i];
224 const char **arg_dest;
226 assert (prev_arg != NULL);
228 if (strcmp (prev_arg, "--config-file") == 0)
230 arg_dest = &config_file;
232 else if (strcmp (prev_arg, "--dbus-daemon") == 0)
234 arg_dest = &dbus_daemon;
238 /* shouldn't happen */
239 fprintf (stderr, "%s: internal error: %s not fully implemented\n",
244 if (*arg_dest != NULL)
246 fprintf (stderr, "%s: %s given twice\n", me, prev_arg);
257 if (strcmp (arg, "--help") == 0 ||
258 strcmp (arg, "-h") == 0 ||
259 strcmp (arg, "-?") == 0)
263 else if (strcmp (arg, "--version") == 0)
267 else if (strstr (arg, "--config-file=") == arg)
271 if (config_file != NULL)
273 fprintf (stderr, "%s: --config-file given twice\n", me);
277 file = strchr (arg, '=');
282 else if (strstr (arg, "--dbus-daemon=") == arg)
286 if (dbus_daemon != NULL)
288 fprintf (stderr, "%s: --dbus-daemon given twice\n", me);
292 file = strchr (arg, '=');
297 else if (strcmp (arg, "--config-file") == 0 ||
298 strcmp (arg, "--dbus-daemon") == 0)
302 else if (arg[0] == '-')
304 if (strcmp (arg, "--") != 0)
306 fprintf (stderr, "%s: option '%s' is unknown\n", me, arg);
325 /* "dbus-run-session" and "dbus-run-session ... --" are not allowed:
326 * there must be something to run */
327 if (prog_arg < 1 || prog_arg >= argc)
329 fprintf (stderr, "%s: a non-option argument is required\n", me);
335 fprintf (stderr, "%s: option '%s' requires an argument\n", me, prev_arg);
339 if (dbus_daemon == NULL)
340 dbus_daemon = "dbus-daemon";
342 if (pipe (bus_address_pipe) < 0)
344 fprintf (stderr, "%s: failed to create pipe: %s\n", me, strerror (errno));
352 fprintf (stderr, "%s: failed to fork: %s\n", me, strerror (errno));
359 exec_dbus_daemon (dbus_daemon, bus_address_pipe, config_file);
364 close (bus_address_pipe[PIPE_WRITE_END]);
366 switch (read_line (bus_address_pipe[PIPE_READ_END], bus_address, MAX_ADDR_LEN))
371 case READ_STATUS_EOF:
372 fprintf (stderr, "%s: EOF reading address from bus daemon\n", me);
376 case READ_STATUS_ERROR:
377 fprintf (stderr, "%s: error reading address from bus daemon: %s\n",
378 me, strerror (errno));
383 close (bus_address_pipe[PIPE_READ_END]);
385 if (!dbus_setenv ("DBUS_SESSION_BUS_ADDRESS", bus_address) ||
386 !dbus_setenv ("DBUS_SESSION_BUS_PID", NULL) ||
387 !dbus_setenv ("DBUS_SESSION_BUS_WINDOWID", NULL) ||
388 !dbus_setenv ("DBUS_STARTER_ADDRESS", NULL) ||
389 !dbus_setenv ("DBUS_STARTER_BUS_TYPE", NULL))
396 fprintf (stderr, "%s: failed to fork: %s\n", me, strerror (errno));
403 exec_app (prog_arg, argv);
411 pid_t child_pid = waitpid (-1, &child_status, 0);
413 if (child_pid == (pid_t) -1)
420 /* shouldn't happen: the only other documented errors are ECHILD,
421 * which shouldn't happen because we terminate when all our children
422 * have died, and EINVAL, which would indicate programming error */
423 fprintf (stderr, "%s: waitpid() failed: %s\n", me, strerror (errsv));
426 else if (child_pid == bus_pid)
428 /* no need to kill it, now */
431 if (WIFEXITED (child_status))
432 fprintf (stderr, "%s: dbus-daemon exited with code %d\n",
433 me, WEXITSTATUS (child_status));
434 else if (WIFSIGNALED (child_status))
435 fprintf (stderr, "%s: dbus-daemon terminated by signal %d\n",
436 me, WTERMSIG (child_status));
438 fprintf (stderr, "%s: dbus-daemon died or something\n", me);
440 else if (child_pid == app_pid)
443 kill (bus_pid, SIGTERM);
445 if (WIFEXITED (child_status))
446 return WEXITSTATUS (child_status);
448 /* if it died from a signal, behave like sh(1) */
449 if (WIFSIGNALED (child_status))
450 return 128 + WTERMSIG (child_status);
452 /* I give up (this should never be reached) */
453 fprintf (stderr, "%s: child process died or something\n", me);
458 fprintf (stderr, "%s: ignoring unknown child process %ld\n", me,