From bb49885ffa9d9bdf7d30a072467c53a2866896e1 Mon Sep 17 00:00:00 2001 From: David King Date: Fri, 22 Jun 2018 14:57:00 +0100 Subject: [PATCH] dbus-launch: use HOME as the working directory Session buses started as part of a systemd --user session are launched with the current working directory being the home directory of the user. Applications which are launched via dbus activation inherit the working directory from the session bus dbus-daemon. When dbus-launch is used to start dbus-daemon, as is commonly the case with a session manager such as gnome-session, this leads to applications having a default working directory of "/", which is undesirable (as an example, the default directory in a GTK+ save dialog becomes "/"). As an improvement, make dbus-launch use the value of the environment variable HOME, if it is set, as the current working directory. Signed-off-by: David King Bug: https://bugs.freedesktop.org/show_bug.cgi?id=106987 Bug-RedHat: https://bugzilla.redhat.com/show_bug.cgi?id=1470310 --- doc/dbus-launch.1.xml.in | 4 ++++ tools/dbus-launch.c | 22 ++++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/doc/dbus-launch.1.xml.in b/doc/dbus-launch.1.xml.in index 2fcea03f..55796f78 100644 --- a/doc/dbus-launch.1.xml.in +++ b/doc/dbus-launch.1.xml.in @@ -51,6 +51,10 @@ backticks or the $() construct can be used to read information from instance and print the address and PID of that instance to standard output. +If the environment variable HOME is set, it is used as the current +working directory. Otherwise, the root directory (/) is +used. + You may specify a program to be run; in this case, dbus-launch will launch a session bus instance, set the appropriate environment variables so the specified program can find the bus, and then execute the diff --git a/tools/dbus-launch.c b/tools/dbus-launch.c index 425914a1..77ffe4cf 100644 --- a/tools/dbus-launch.c +++ b/tools/dbus-launch.c @@ -622,17 +622,23 @@ babysit (int exit_with_session, verbose ("babysitting, exit_with_session = %d, child_pid = %ld, read_bus_pid_fd = %d\n", exit_with_session, (long) child_pid, read_bus_pid_fd); - /* We chdir ("/") since we are persistent and daemon-like, and fork - * again so dbus-launch can reap the parent. However, we don't - * setsid() or close fd 0 because the idea is to remain attached - * to the tty and the X server in order to kill the message bus - * when the session ends. + /* We chdir () since we are persistent and daemon-like, either to $HOME + * to match the behaviour of a session bus started by systemd --user, or + * otherwise "/". We fork again so dbus-launch can reap the parent. + * However, we don't setsid() or close fd 0 because the idea is to + * remain attached to the tty and the X server in order to kill the + * message bus when the session ends. */ - if (chdir ("/") < 0) + s = getenv ("HOME"); + + if (s == NULL || *s == '\0') + s = "/"; + + if (chdir (s) < 0) { - fprintf (stderr, "Could not change to root directory: %s\n", - strerror (errno)); + fprintf (stderr, "Could not change to working directory \"%s\": %s\n", + s, strerror (errno)); exit (1); } -- 2.17.1