From 6d32d9dbdd0d9629ba3b8738ba559e23cca89299 Mon Sep 17 00:00:00 2001 From: "Anna (navi) Figueiredo Gomes" Date: Sat, 21 Sep 2024 11:12:14 +0200 Subject: [PATCH] openrc-run: Chdir into HOME for user services. Some services might expect to be in home, and may behave unexpectedly for the user, e.g. any program started via dbus, and this matches systemd-user behaviour. --- src/openrc-run/openrc-run.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/openrc-run/openrc-run.c b/src/openrc-run/openrc-run.c index 748e1d8f9..c4c0c6315 100644 --- a/src/openrc-run/openrc-run.c +++ b/src/openrc-run/openrc-run.c @@ -1099,6 +1099,7 @@ int main(int argc, char **argv) char *path = NULL; char *lnk = NULL; char *dir, *save = NULL, *saveLnk = NULL; + const char *workingdir = "/"; char *pidstr = NULL; size_t l = 0, ll; const char *file; @@ -1160,8 +1161,12 @@ int main(int argc, char **argv) if (argc < 3) usage(EXIT_FAILURE); - /* Change dir to / to ensure all init scripts don't use stuff in pwd */ - if (chdir("/") == -1) + /* Change dir to / to ensure all init scripts don't use stuff in pwd + * For user services, change to the user's HOME instead. */ + if (rc_is_user() && !(workingdir = getenv("HOME"))) + eerrorx("HOME is unset."); + + if (chdir(workingdir) == -1) eerror("chdir: %s", strerror(errno)); if ((runlevel = xstrdup(getenv("RC_RUNLEVEL"))) == NULL) {