diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/dpid.c | 31 | ||||
-rw-r--r-- | src/dpid.h | 14 |
3 files changed, 46 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 129660f..c2719ed 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,3 +27,4 @@ libdaemon_la_SOURCES = \ dnonblock.c dnonblock.h \ dpid.c dpid.h +libdaemon_la_LDFLAGS = -version-info 1:0:1 @@ -25,6 +25,7 @@ #include <errno.h> #include <sys/types.h> #include <sys/stat.h> +#include <time.h> #include "dpid.h" #include "dlog.h" @@ -89,6 +90,36 @@ int daemon_pid_file_kill(int s) { return 0; } +int daemon_pid_file_kill_wait(int s, int m) { + pid_t pid; + time_t t; + + if ((pid = daemon_pid_file_is_running()) < 0) + return -1; + + if (kill(pid, s) < 0) + return -1; + + t = time(NULL) + m; + + for (;;) { + int r; + struct timeval tv = { 0, 100000 }; + + if (time(NULL) > t) + return -1; + + if ((r = kill(pid, 0)) < 0 && errno != ESRCH) + return -1; + + if (r) + return 0; + + if (select(0, NULL, NULL, NULL, &tv) < 0) + return -1; + } +} + int daemon_pid_file_create(void) { const char *fn; FILE *f; @@ -70,4 +70,18 @@ pid_t daemon_pid_file_is_running(void); */ int daemon_pid_file_kill(int s); +/** If this variable is defined to 1 iff daemon_pid_file_kill_wait() is supported.*/ +#define DAEMON_PID_FILE_KILL_WAIT_AVAILABLE 1 + +/** Similar to daemon_pid_file_kill() but waits until the process + * died. This functions is new in libdaemon 0.3. The macro + * DAEMON_PID_FILE_KILL_WAIT_AVAILABLE is defined iff libdaemon + * supports this function. + * + * @param s The signal to send + * @param m Seconds to wait at maximum + * @return zero on success, nonzero on failure (timeout condition is considered a failure) + */ +int daemon_pid_file_kill_wait(int s, int m); + #endif |