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 #define MAX_ADDR_LEN 512
40 #define PIPE_READ_END 0
41 #define PIPE_WRITE_END 1
45 * If you are in a shell and run "dbus-run-session myapp", here is what
46 * happens (compare and contrast with dbus-launch):
49 * \- dbus-run-session myapp
50 * \- dbus-daemon --nofork --print-address --session
53 * All processes are long-running.
55 * When myapp exits, dbus-run-session kills dbus-daemon and terminates.
57 * If dbus-daemon exits, dbus-run-session warns and continues to run.
61 * dbus-daemon --print-address -> bus_address_pipe -> d-r-s
64 static const char me[] = "dbus-run-session";
70 "%s [OPTIONS] [--] PROGRAM [ARGUMENTS]\n"
75 "--dbus-daemon=BINARY run BINARY instead of dbus-daemon\n"
76 "--config-file=FILENAME pass to dbus-daemon instead of --session\n"
86 "Copyright (C) 2003-2006 Red Hat, Inc.\n"
87 "Copyright (C) 2006 Thiago Macieira\n"
88 "Copyright © 2011-2012 Nokia Corporation\n"
90 "This is free software; see the source for copying conditions.\n"
91 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
99 fprintf (stderr, "%s: out of memory\n", me);
104 xmalloc (size_t bytes)
111 ret = malloc (bytes);
121 READ_STATUS_OK, /**< Read succeeded */
122 READ_STATUS_ERROR, /**< Some kind of error */
123 READ_STATUS_EOF /**< EOF returned */
134 memset (buf, '\0', maxlen);
135 maxlen -= 1; /* ensure nul term */
137 retval = READ_STATUS_OK;
145 to_read = maxlen - bytes;
153 if (chunk < 0 && errno == EINTR)
158 retval = READ_STATUS_ERROR;
163 retval = READ_STATUS_EOF;
170 if (retval == READ_STATUS_EOF &&
172 retval = READ_STATUS_OK;
175 if (retval != READ_STATUS_ERROR &&
177 buf[bytes-1] == '\n')
184 exec_dbus_daemon (const char *dbus_daemon,
185 int bus_address_pipe[2],
186 const char *config_file)
188 /* Child process, which execs dbus-daemon or dies trying */
189 #define MAX_FD_LEN 64
190 char write_address_fd_as_string[MAX_FD_LEN];
192 close (bus_address_pipe[PIPE_READ_END]);
194 sprintf (write_address_fd_as_string, "%d", bus_address_pipe[PIPE_WRITE_END]);
199 "--print-address", write_address_fd_as_string,
200 config_file ? "--config-file" : "--session",
201 config_file, /* has to be last in this varargs list */
204 fprintf (stderr, "%s: failed to execute message bus daemon '%s': %s\n",
205 me, dbus_daemon, strerror (errno));
209 exec_app (int prog_arg, char **argv)
211 execvp (argv[prog_arg], argv + prog_arg);
213 fprintf (stderr, "%s: failed to exec '%s': %s\n", me, argv[prog_arg],
219 main (int argc, char **argv)
222 int bus_address_pipe[2] = { 0, 0 };
223 const char *config_file = NULL;
224 const char *dbus_daemon = NULL;
225 char bus_address[MAX_ADDR_LEN] = { 0 };
226 const char *prev_arg = NULL;
228 int requires_arg = 0;
235 const char *arg = argv[i];
239 const char **arg_dest;
241 assert (prev_arg != NULL);
243 if (strcmp (prev_arg, "--config-file") == 0)
245 arg_dest = &config_file;
247 else if (strcmp (prev_arg, "--dbus-daemon") == 0)
249 arg_dest = &dbus_daemon;
253 /* shouldn't happen */
254 fprintf (stderr, "%s: internal error: %s not fully implemented\n",
259 if (*arg_dest != NULL)
261 fprintf (stderr, "%s: %s given twice\n", me, prev_arg);
272 if (strcmp (arg, "--help") == 0 ||
273 strcmp (arg, "-h") == 0 ||
274 strcmp (arg, "-?") == 0)
278 else if (strcmp (arg, "--version") == 0)
282 else if (strstr (arg, "--config-file=") == arg)
286 if (config_file != NULL)
288 fprintf (stderr, "%s: --config-file given twice\n", me);
292 file = strchr (arg, '=');
297 else if (strstr (arg, "--dbus-daemon=") == arg)
301 if (dbus_daemon != NULL)
303 fprintf (stderr, "%s: --dbus-daemon given twice\n", me);
307 file = strchr (arg, '=');
312 else if (strcmp (arg, "--config-file") == 0 ||
313 strcmp (arg, "--dbus-daemon") == 0)
317 else if (arg[0] == '-')
319 if (strcmp (arg, "--") != 0)
321 fprintf (stderr, "%s: option '%s' is unknown\n", me, arg);
340 /* "dbus-run-session" and "dbus-run-session ... --" are not allowed:
341 * there must be something to run */
342 if (prog_arg < 1 || prog_arg >= argc)
344 fprintf (stderr, "%s: a non-option argument is required\n", me);
350 fprintf (stderr, "%s: option '%s' requires an argument\n", me, prev_arg);
354 if (dbus_daemon == NULL)
355 dbus_daemon = "dbus-daemon";
357 if (pipe (bus_address_pipe) < 0)
359 fprintf (stderr, "%s: failed to create pipe: %s\n", me, strerror (errno));
367 fprintf (stderr, "%s: failed to fork: %s\n", me, strerror (errno));
374 exec_dbus_daemon (dbus_daemon, bus_address_pipe, config_file);
379 close (bus_address_pipe[PIPE_WRITE_END]);
381 switch (read_line (bus_address_pipe[PIPE_READ_END], bus_address, MAX_ADDR_LEN))
386 case READ_STATUS_EOF:
387 fprintf (stderr, "%s: EOF reading address from bus daemon\n", me);
391 case READ_STATUS_ERROR:
392 fprintf (stderr, "%s: error reading address from bus daemon: %s\n",
393 me, strerror (errno));
398 close (bus_address_pipe[PIPE_READ_END]);
400 envvar = xmalloc (strlen ("DBUS_SESSION_BUS_ADDRESS=") +
401 strlen (bus_address) + 1);
402 strcpy (envvar, "DBUS_SESSION_BUS_ADDRESS=");
403 strcat (envvar, bus_address);
410 fprintf (stderr, "%s: failed to fork: %s\n", me, strerror (errno));
417 exec_app (prog_arg, argv);
425 pid_t child_pid = waitpid (-1, &child_status, 0);
427 if (child_pid == (pid_t) -1)
434 /* shouldn't happen: the only other documented errors are ECHILD,
435 * which shouldn't happen because we terminate when all our children
436 * have died, and EINVAL, which would indicate programming error */
437 fprintf (stderr, "%s: waitpid() failed: %s\n", me, strerror (errsv));
440 else if (child_pid == bus_pid)
442 /* no need to kill it, now */
445 if (WIFEXITED (child_status))
446 fprintf (stderr, "%s: dbus-daemon exited with code %d\n",
447 me, WEXITSTATUS (child_status));
448 else if (WIFSIGNALED (child_status))
449 fprintf (stderr, "%s: dbus-daemon terminated by signal %d\n",
450 me, WTERMSIG (child_status));
452 fprintf (stderr, "%s: dbus-daemon died or something\n", me);
454 else if (child_pid == app_pid)
457 kill (bus_pid, SIGTERM);
459 if (WIFEXITED (child_status))
460 return WEXITSTATUS (child_status);
462 /* if it died from a signal, behave like sh(1) */
463 if (WIFSIGNALED (child_status))
464 return 128 + WTERMSIG (child_status);
466 /* I give up (this should never be reached) */
467 fprintf (stderr, "%s: child process died or something\n", me);
472 fprintf (stderr, "%s: ignoring unknown child process %ld\n", me,