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>
38 #define MAX_ADDR_LEN 512
39 #define PIPE_READ_END 0
40 #define PIPE_WRITE_END 1
44 * If you are in a shell and run "dbus-run-session myapp", here is what
45 * happens (compare and contrast with dbus-launch):
48 * \- dbus-run-session myapp
49 * \- dbus-daemon --nofork --print-address --session
52 * All processes are long-running.
54 * When myapp exits, dbus-run-session kills dbus-daemon and terminates.
56 * If dbus-daemon exits, dbus-run-session warns and continues to run.
60 * dbus-daemon --print-address -> bus_address_pipe -> d-r-s
63 static const char me[] = "dbus-run-session";
69 "%s [OPTIONS] [--] PROGRAM [ARGUMENTS]\n"
74 "--dbus-daemon=BINARY run BINARY instead of dbus-daemon\n"
75 "--config-file=FILENAME pass to dbus-daemon instead of --session\n"
85 "Copyright (C) 2003-2006 Red Hat, Inc.\n"
86 "Copyright (C) 2006 Thiago Macieira\n"
87 "Copyright © 2011-2012 Nokia Corporation\n"
89 "This is free software; see the source for copying conditions.\n"
90 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
98 fprintf (stderr, "%s: out of memory\n", me);
103 xmalloc (size_t bytes)
110 ret = malloc (bytes);
120 READ_STATUS_OK, /**< Read succeeded */
121 READ_STATUS_ERROR, /**< Some kind of error */
122 READ_STATUS_EOF /**< EOF returned */
133 memset (buf, '\0', maxlen);
134 maxlen -= 1; /* ensure nul term */
136 retval = READ_STATUS_OK;
144 to_read = maxlen - bytes;
152 if (chunk < 0 && errno == EINTR)
157 retval = READ_STATUS_ERROR;
162 retval = READ_STATUS_EOF;
169 if (retval == READ_STATUS_EOF &&
171 retval = READ_STATUS_OK;
174 if (retval != READ_STATUS_ERROR &&
176 buf[bytes-1] == '\n')
183 exec_dbus_daemon (const char *dbus_daemon,
184 int bus_address_pipe[2],
185 const char *config_file)
187 /* Child process, which execs dbus-daemon or dies trying */
188 #define MAX_FD_LEN 64
189 char write_address_fd_as_string[MAX_FD_LEN];
191 close (bus_address_pipe[PIPE_READ_END]);
193 sprintf (write_address_fd_as_string, "%d", bus_address_pipe[PIPE_WRITE_END]);
198 "--print-address", write_address_fd_as_string,
199 config_file ? "--config-file" : "--session",
200 config_file, /* has to be last in this varargs list */
203 fprintf (stderr, "%s: failed to execute message bus daemon '%s': %s\n",
204 me, dbus_daemon, strerror (errno));
208 exec_app (int prog_arg, char **argv)
210 execvp (argv[prog_arg], argv + prog_arg);
212 fprintf (stderr, "%s: failed to exec '%s': %s\n", me, argv[prog_arg],
218 main (int argc, char **argv)
221 int bus_address_pipe[2] = { 0, 0 };
222 const char *config_file = NULL;
223 const char *dbus_daemon = NULL;
224 char bus_address[MAX_ADDR_LEN] = { 0 };
225 const char *prev_arg = NULL;
227 int requires_arg = 0;
234 const char *arg = argv[i];
238 const char **arg_dest;
240 assert (prev_arg != NULL);
242 if (strcmp (prev_arg, "--config-file") == 0)
244 arg_dest = &config_file;
246 else if (strcmp (prev_arg, "--dbus-daemon") == 0)
248 arg_dest = &dbus_daemon;
252 /* shouldn't happen */
253 fprintf (stderr, "%s: internal error: %s not fully implemented\n",
258 if (*arg_dest != NULL)
260 fprintf (stderr, "%s: %s given twice\n", me, prev_arg);
271 if (strcmp (arg, "--help") == 0 ||
272 strcmp (arg, "-h") == 0 ||
273 strcmp (arg, "-?") == 0)
277 else if (strcmp (arg, "--version") == 0)
281 else if (strstr (arg, "--config-file=") == arg)
285 if (config_file != NULL)
287 fprintf (stderr, "%s: --config-file given twice\n", me);
291 file = strchr (arg, '=');
296 else if (strstr (arg, "--dbus-daemon=") == arg)
300 if (dbus_daemon != NULL)
302 fprintf (stderr, "%s: --dbus-daemon given twice\n", me);
306 file = strchr (arg, '=');
311 else if (strcmp (arg, "--config-file") == 0 ||
312 strcmp (arg, "--dbus-daemon") == 0)
316 else if (arg[0] == '-')
318 if (strcmp (arg, "--") != 0)
320 fprintf (stderr, "%s: option '%s' is unknown\n", me, arg);
339 /* "dbus-run-session" and "dbus-run-session ... --" are not allowed:
340 * there must be something to run */
341 if (prog_arg < 1 || prog_arg >= argc)
343 fprintf (stderr, "%s: a non-option argument is required\n", me);
349 fprintf (stderr, "%s: option '%s' requires an argument\n", me, prev_arg);
353 if (dbus_daemon == NULL)
354 dbus_daemon = "dbus-daemon";
356 if (pipe (bus_address_pipe) < 0)
358 fprintf (stderr, "%s: failed to create pipe: %s\n", me, strerror (errno));
366 fprintf (stderr, "%s: failed to fork: %s\n", me, strerror (errno));
373 exec_dbus_daemon (dbus_daemon, bus_address_pipe, config_file);
378 close (bus_address_pipe[PIPE_WRITE_END]);
380 switch (read_line (bus_address_pipe[PIPE_READ_END], bus_address, MAX_ADDR_LEN))
385 case READ_STATUS_EOF:
386 fprintf (stderr, "%s: EOF reading address from bus daemon\n", me);
390 case READ_STATUS_ERROR:
391 fprintf (stderr, "%s: error reading address from bus daemon: %s\n",
392 me, strerror (errno));
397 close (bus_address_pipe[PIPE_READ_END]);
399 envvar = xmalloc (strlen ("DBUS_SESSION_BUS_ADDRESS=") +
400 strlen (bus_address) + 1);
401 strcpy (envvar, "DBUS_SESSION_BUS_ADDRESS=");
402 strcat (envvar, bus_address);
409 fprintf (stderr, "%s: failed to fork: %s\n", me, strerror (errno));
416 exec_app (prog_arg, argv);
424 pid_t child_pid = waitpid (-1, &child_status, 0);
426 if (child_pid == (pid_t) -1)
433 /* shouldn't happen: the only other documented errors are ECHILD,
434 * which shouldn't happen because we terminate when all our children
435 * have died, and EINVAL, which would indicate programming error */
436 fprintf (stderr, "%s: waitpid() failed: %s\n", me, strerror (errsv));
439 else if (child_pid == bus_pid)
441 /* no need to kill it, now */
444 if (WIFEXITED (child_status))
445 fprintf (stderr, "%s: dbus-daemon exited with code %d\n",
446 me, WEXITSTATUS (child_status));
447 else if (WIFSIGNALED (child_status))
448 fprintf (stderr, "%s: dbus-daemon terminated by signal %d\n",
449 me, WTERMSIG (child_status));
451 fprintf (stderr, "%s: dbus-daemon died or something\n", me);
453 else if (child_pid == app_pid)
456 kill (bus_pid, SIGTERM);
458 if (WIFEXITED (child_status))
459 return WEXITSTATUS (child_status);
461 /* if it died from a signal, behave like sh(1) */
462 if (WIFSIGNALED (child_status))
463 return 128 + WTERMSIG (child_status);
465 /* I give up (this should never be reached) */
466 fprintf (stderr, "%s: child process died or something\n", me);
471 fprintf (stderr, "%s: ignoring unknown child process %ld\n", me,