diff options
author | Mattias Andrée <maandree@member.fsf.org> | 2015-12-23 16:22:52 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@member.fsf.org> | 2015-12-23 16:23:00 +0100 |
commit | 2db8a86efd31bdab108769363c6a107289453bc1 (patch) | |
tree | 36e5e5dd5e099c962727e0145c81e162b4e20b2e | |
parent | daemonise: may also be redistributed under the term of the expat license (diff) | |
download | slibc-2db8a86efd31bdab108769363c6a107289453bc1.tar.gz slibc-2db8a86efd31bdab108769363c6a107289453bc1.tar.bz2 slibc-2db8a86efd31bdab108769363c6a107289453bc1.tar.xz |
daemonise: user-private daemons
Signed-off-by: Mattias Andrée <maandree@member.fsf.org>
-rw-r--r-- | include/unistd.h | 4 | ||||
-rw-r--r-- | src/unistd/daemonise.c | 20 |
2 files changed, 21 insertions, 3 deletions
diff --git a/include/unistd.h b/include/unistd.h index 9b78fb1..4629e8e 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -1134,6 +1134,10 @@ int daemon(int, int) * you make the function call. The process can have become * partially deamonised. * + * If $XDG_RUNTIME_DIR is set and is not empty, its value + * should be used instead of /run for the runtime data-files + * directory, in which the PID file is stored. + * * @etymology (Daemonise) the process! * * @param name The name of the daemon. Use a hardcoded value, diff --git a/src/unistd/daemonise.c b/src/unistd/daemonise.c index 8c8ab1d..7c415fa 100644 --- a/src/unistd/daemonise.c +++ b/src/unistd/daemonise.c @@ -92,6 +92,10 @@ extern char** environ; * you make the function call. The process can have become * partially deamonised. * + * If $XDG_RUNTIME_DIR is set and is not empty, its value + * should be used instead of /run for the runtime data-files + * directory, in which the PID file is stored. + * * @etymology (Daemonise) the process! * * @param name The name of the daemon. Use a hardcoded value, @@ -126,7 +130,7 @@ extern char** environ; * * @since Always. */ -int daemonise(const char* name, int flags) /* TODO user-private daemons */ +int daemonise(const char* name, int flags) { #define t(...) do { if (__VA_ARGS__) goto fail; } while (0) @@ -135,6 +139,7 @@ int daemonise(const char* name, int flags) /* TODO user-private daemons */ sigset_t set; char* r; char* w; + char* run; int i, closeerr, fd = -1; int saved_errno; @@ -213,8 +218,17 @@ int daemonise(const char* name, int flags) /* TODO user-private daemons */ /* Create PID file. */ if (flags & DAEMONISE_NO_PID_FILE) goto no_pid_file; - pidpath = alloca(sizeof("/run/.pid") + strlen(name) * sizeof(char)); - stpcpy(stpcpy(stpcpy(pidpath, "/run/"), name), ".pid"); + char* run = getenv("XDG_RUNTIME_DIR"); + if (run && *run) + { + pidpath = alloca(sizeof("/.pid") + (strlen(run) + strlen(name)) * sizeof(char)); + stpcpy(stpcpy(stpcpy(stpcpy(pidpath, run), "/", name), ".pid"); + } + else + { + pidpath = alloca(sizeof("/run/.pid") + strlen(name) * sizeof(char)); + stpcpy(stpcpy(stpcpy(pidpath, "/run/"), name), ".pid"); + } fd = open(pidpath, O_WRONLY | O_CREAT | O_EXCL, 0644); t (fd == -1); pid = getpid(); |