diff options
-rw-r--r-- | src/unistd/daemonise.c | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/src/unistd/daemonise.c b/src/unistd/daemonise.c index f3ebe3a..7b0bc56 100644 --- a/src/unistd/daemonise.c +++ b/src/unistd/daemonise.c @@ -21,8 +21,10 @@ #include <fcntl.h> #include <signal.h> #include <string.h> +#include <stdio.h> #include <stdlib.h> #include <errno.h> +#include <sys/stat.h> #include <sys/resource.h> @@ -145,10 +147,11 @@ int daemonise(const char* name, int flags) struct rlimit rlimit; int pipe_rw[2] = { -1, -1 }; sigset_t set; - char* r; - char* w; + char** r; + char** w; char* run; int i, closeerr, fd = -1; + pid_t pid; int saved_errno; @@ -213,7 +216,7 @@ int daemonise(const char* name, int flags) t (pid = fork(), pid == -1); close(pipe_rw[!!pid]), pipe_rw[!!pid] = 1; if (pid) - exit(read(pipe_rw[0], &b, (size_t)1) <= 0); + exit(read(pipe_rw[0], &fd, (size_t)1) <= 0); /* Temporarily become session leader. */ t (setsid() == -1); @@ -229,26 +232,26 @@ int daemonise(const char* name, int flags) run = getenv("XDG_RUNTIME_DIR"); if (run && *run) { - pidpath = malloc(sizeof("/.pid") + (strlen(run) + strlen(name)) * sizeof(char)); - t (pidfile == NULL); - stpcpy(stpcpy(stpcpy(stpcpy(pidpath, run), "/"), name), ".pid"); + __pidfile = malloc(sizeof("/.pid") + (strlen(run) + strlen(name)) * sizeof(char)); + t (__pidfile == NULL); + stpcpy(stpcpy(stpcpy(stpcpy(__pidfile, run), "/"), name), ".pid"); } else { - pidpath = malloc(sizeof("/run/.pid") + strlen(name) * sizeof(char)); - t (pidfile == NULL); - stpcpy(stpcpy(stpcpy(pidpath, "/run/"), name), ".pid"); + __pidfile = malloc(sizeof("/run/.pid") + strlen(name) * sizeof(char)); + t (__pidfile == NULL); + stpcpy(stpcpy(stpcpy(__pidfile, "/run/"), name), ".pid"); } - fd = open(pidpath, O_WRONLY | O_CREAT | O_EXCL, 0644); + fd = open(__pidfile, O_WRONLY | O_CREAT | O_EXCL, 0644); if (fd == -1) { saved_errno = errno; - free(pidpath), pidpath = NULL; + free(__pidfile), __pidfile = NULL; errno = saved_errno; goto fail; } pid = getpid(); - t (dprintf(fd, "%lli\n", (long long int)pid)) < 0; + t (dprintf(fd, "%lli\n", (long long int)pid) < 0); t (close(fd) && (errno != EINTR)); no_pid_file: @@ -271,7 +274,7 @@ int daemonise(const char* name, int flags) fd = -1; /* We are done! Let the original process exit. */ - if ((write(pipe_rw[1], &b, (size_t)1) <= 0) || + if ((write(pipe_rw[1], &fd, (size_t)1) <= 0) || (close(pipe_rw[1]) && (errno != EINTR))) { if (flags & DAEMONISE_KEEP_STDERR) @@ -309,11 +312,11 @@ int daemonise(const char* name, int flags) int undaemonise(void) { int r, saved_errno; - if (pidfile == NULL) + if (__pidfile == NULL) return 0; - r = unlink(pidfile); + r = unlink(__pidfile); saved_errno = errno; - free(pidfile), pidfile = NULL; + free(__pidfile), __pidfile = NULL; errno = saved_errno; return r; } |