X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Fdaemon.c;h=9b136d8ac2c171434d6d2d893dc4d824b73068f7;hb=a6368763d08042207963c941b1c52b5fafcb0cb3;hp=0012db4605bb111e30bcd6c65a729312c5a10956;hpb=7c6ad545b6423bab37bbb8658db9e35964cc9537;p=libucw.git diff --git a/ucw/daemon.c b/ucw/daemon.c index 0012db46..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,9 +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; @@ -29,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; @@ -53,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; @@ -76,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 @@ -110,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(); @@ -162,8 +181,12 @@ void daemon_run(struct daemon_params *dp, void (*body)(struct daemon_params *dp) } } -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) @@ -174,12 +197,43 @@ 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"); msg(L_INFO, "uid=%d/%d gid=%d/%d", (int) getuid(), (int) geteuid(), (int) getgid(), (int) getegid()); - sleep(60); + + 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); }