X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Fdaemon.c;h=9b136d8ac2c171434d6d2d893dc4d824b73068f7;hb=0eb6d8317cdbdb28663ff779d31684b3c7a47274;hp=0c83bdeb5aba8f89106a89a027a121f2a8d2a39c;hpb=d657737a4257516391ab62f86a4aedc0cc14809c;p=libucw.git diff --git a/ucw/daemon.c b/ucw/daemon.c index 0c83bdeb..9b136d8a 100644 --- a/ucw/daemon.c +++ b/ucw/daemon.c @@ -1,7 +1,7 @@ /* * UCW Library -- Daemonization * - * (c) 2012 Martin Mares + * (c) 2012--2014 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -18,8 +18,12 @@ #include #include #include +#include +#include +#include -static void daemon_resolve_ugid(struct daemon_params *dp) +void +daemon_resolve_ugid(struct daemon_params *dp) { // Resolve user name const char *u = dp->run_as_user; @@ -28,8 +32,8 @@ static void daemon_resolve_ugid(struct daemon_params *dp) { if (u[0] == '#') { - uns id; - const char *err = str_to_uns(&id, u, NULL, 10 | STN_WHOLE); + uint id; + const char *err = str_to_uint(&id, u, NULL, 10 | STN_WHOLE); if (err) die("Cannot parse user `%s': %s", u, err); dp->run_as_uid = id; @@ -52,8 +56,8 @@ static void daemon_resolve_ugid(struct daemon_params *dp) { if (g[0] == '#') { - uns id; - const char *err = str_to_uns(&id, g, NULL, 10 | STN_WHOLE); + uint id; + const char *err = str_to_uint(&id, g, NULL, 10 | STN_WHOLE); if (err) die("Cannot parse group `%s': %s", g, err); dp->run_as_gid = id; @@ -75,10 +79,24 @@ static void daemon_resolve_ugid(struct daemon_params *dp) } } -void daemon_init(struct daemon_params *dp) +void daemon_switch_ugid(struct daemon_params *dp) +{ + if (dp->want_setgid && setresgid(dp->run_as_gid, dp->run_as_gid, dp->run_as_gid) < 0) + die("Cannot set GID to %d: %m", (int) dp->run_as_gid); + if (dp->want_setgid > 1 && initgroups(dp->run_as_user, dp->run_as_gid) < 0) + die("Cannot initialize groups: %m"); + if (dp->want_setuid && setresuid(dp->run_as_uid, dp->run_as_uid, dp->run_as_uid) < 0) + die("Cannot set UID to %d: %m", (int) dp->run_as_uid); +} + +void +daemon_init(struct daemon_params *dp) { daemon_resolve_ugid(dp); + if (dp->flags & DAEMON_FLAG_SIMULATE) + return; + if (dp->pid_file) { // Check that PID file path is absolute @@ -89,12 +107,14 @@ void daemon_init(struct daemon_params *dp) dp->pid_fd = open(dp->pid_file, O_RDWR | O_CREAT, 0666); if (dp->pid_fd < 0) die("Cannot open `%s': %m", dp->pid_file); + int fl = fcntl(dp->pid_fd, F_GETFD); + if (fl < 0 || fcntl(dp->pid_fd, F_SETFD, fl | FD_CLOEXEC)) + die("Cannot set FD_CLOEXEC: %m"); // Try to lock it with an exclusive lock - struct flock fl = { .l_type = F_WRLCK, .l_whence = SEEK_SET }; - if (fcntl(dp->pid_fd, F_SETLK, &fl) < 0) + if (flock(dp->pid_fd, LOCK_EX | LOCK_NB) < 0) { - if (errno == EAGAIN || errno == EACCES) + if (errno == EINTR || errno == EWOULDBLOCK) die("Daemon is already running (`%s' locked)", dp->pid_file); else die("Cannot lock `%s': %m", dp->pid_file); @@ -107,15 +127,17 @@ void daemon_init(struct daemon_params *dp) } } -void daemon_run(struct daemon_params *dp, void (*body)(struct daemon_params *dp)) +void +daemon_run(struct daemon_params *dp, void (*body)(struct daemon_params *dp)) { + if (dp->flags & DAEMON_FLAG_SIMULATE) + { + body(dp); + return; + } + // Switch GID and UID - if (dp->want_setgid && setresgid(dp->run_as_gid, dp->run_as_gid, dp->run_as_gid) < 0) - die("Cannot set GID to %d: %m", (int) dp->run_as_gid); - if (dp->want_setgid > 1 && initgroups(dp->run_as_user, dp->run_as_gid) < 0) - die("Cannot initialize groups: %m"); - if (dp->want_setuid && setresuid(dp->run_as_uid, dp->run_as_uid, dp->run_as_uid) < 0) - die("Cannot set UID to %d: %m", (int) dp->run_as_uid); + daemon_switch_ugid(dp); // Create a new session and close stdio setsid(); @@ -140,13 +162,12 @@ void daemon_run(struct daemon_params *dp, void (*body)(struct daemon_params *dp) die("Cannot fork: %m"); if (!pid) { - // The PID file is left open, so that it is kept locked - // FIXME: Downgrade the lock to shared + // We still keep the PID file open and thus locked body(dp); exit(0); } - - // Write PID and release the PID file + + // Write PID if (dp->pid_file) { char buf[32]; @@ -154,14 +175,18 @@ void daemon_run(struct daemon_params *dp, void (*body)(struct daemon_params *dp) ASSERT(c <= (int) sizeof(buf)); if (lseek(dp->pid_fd, 0, SEEK_SET) < 0 || write(dp->pid_fd, buf, c) != c || - ftruncate(dp->pid_fd, c) < 0 || + ftruncate(dp->pid_fd, c) || close(dp->pid_fd) < 0) die("Cannot write PID to `%s': %m", dp->pid_file); } } -void daemon_exit(struct daemon_params *dp) +void +daemon_exit(struct daemon_params *dp) { + if (dp->flags & DAEMON_FLAG_SIMULATE) + return; + if (dp->pid_file) { if (unlink(dp->pid_file) < 0) @@ -172,21 +197,70 @@ void daemon_exit(struct daemon_params *dp) #ifdef TEST +#include + +static volatile sig_atomic_t terminate; + +static void term_handler(int sig UNUSED) +{ + msg(L_INFO | L_SIGHANDLER, "SIGTERM received, terminating in a while"); + terminate = 1; +} + +static void hup_handler(int sig UNUSED) +{ + msg(L_INFO | L_SIGHANDLER, "SIGHUP received"); +} + static void body(struct daemon_params *dp) { log_fork(); msg(L_INFO, "Daemon is running"); - sleep(60); + msg(L_INFO, "uid=%d/%d gid=%d/%d", (int) getuid(), (int) geteuid(), (int) getgid(), (int) getegid()); + + struct sigaction sa_term = { .sa_handler = term_handler }; + struct sigaction sa_hup = { .sa_handler = hup_handler }; + if (sigaction(SIGTERM, &sa_term, NULL) < 0 || + sigaction(SIGHUP, &sa_hup, NULL) < 0) + ASSERT(0); + + while (!terminate) + { + if (!sleep(60)) + { + msg(L_INFO, "Timeout elapsed, terminating in a while"); + break; + } + } + + sleep(2); msg(L_INFO, "Daemon is shutting down"); daemon_exit(dp); } -int main(void) +int main(int argc, char **argv) { struct daemon_params dp = { .pid_file = "/tmp/123", }; + int opt; + while ((opt = getopt(argc, argv, "p:u:g:")) >= 0) + switch (opt) + { + case 'p': + dp.pid_file = optarg; + break; + case 'u': + dp.run_as_user = optarg; + break; + case 'g': + dp.run_as_group = optarg; + break; + default: + die("Invalid arguments"); + } + daemon_init(&dp); daemon_run(&dp, body); msg(L_INFO, "Main program has ended");