From 6e229d7655b00065cfdb0e79059f6bb014530ee7 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Wed, 18 Jul 2012 14:23:52 +0200 Subject: [PATCH] Daemon: Added DAEMON_FLAG_SIMULATE --- ucw/daemon.c | 12 ++++++++++++ ucw/daemon.h | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/ucw/daemon.c b/ucw/daemon.c index 00313b94..ec9a2187 100644 --- a/ucw/daemon.c +++ b/ucw/daemon.c @@ -82,6 +82,9 @@ 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 @@ -115,6 +118,12 @@ daemon_init(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); @@ -168,6 +177,9 @@ daemon_run(struct daemon_params *dp, void (*body)(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) diff --git a/ucw/daemon.h b/ucw/daemon.h index 9cdfe050..c5beacc0 100644 --- a/ucw/daemon.h +++ b/ucw/daemon.h @@ -30,6 +30,7 @@ struct daemon_params { /** Flags passed to the daemon helper. **/ enum daemon_flags { DAEMON_FLAG_PRESERVE_CWD = 1, // Skip chdir("/") + DAEMON_FLAG_SIMULATE = 2, // Simulate daemonization (avoid fork etc.) }; /** @@ -44,6 +45,9 @@ void daemon_init(struct daemon_params *dp); * a new process and does all necessary setup. Inside the new process, it calls * @body (and when it returns, it exits the process). In the original process, it writes * the PID file and returns. + * + * When `DAEMON_FLAG_SIMULATE` is set, it justs calls @body. This is useful + * for running of daemons in a debugger. **/ void daemon_run(struct daemon_params *dp, void (*body)(struct daemon_params *dp)); -- 2.39.5