diff options
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | doc/README.html.in | 7 | ||||
-rw-r--r-- | doc/style.css | 16 | ||||
-rw-r--r-- | libdaemon/Makefile.am | 2 | ||||
-rw-r--r-- | libdaemon/dlog.c | 2 | ||||
-rw-r--r-- | libdaemon/dlog.h | 2 | ||||
-rw-r--r-- | libdaemon/dpid.c | 113 |
7 files changed, 98 insertions, 46 deletions
diff --git a/configure.ac b/configure.ac index c89af5a..292a905 100644 --- a/configure.ac +++ b/configure.ac @@ -20,7 +20,7 @@ # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. AC_PREREQ(2.59) -AC_INIT([libdaemon],[0.7],[mzqnrzba (at) 0pointer (dot) de]) +AC_INIT([libdaemon],[0.8],[mzqnrzba (at) 0pointer (dot) de]) AC_CONFIG_SRCDIR([libdaemon/dfork.c]) AC_CONFIG_HEADERS([config.h]) AM_INIT_AUTOMAKE([foreign -Wall]) diff --git a/doc/README.html.in b/doc/README.html.in index fb6ebbc..755a39e 100644 --- a/doc/README.html.in +++ b/doc/README.html.in @@ -10,7 +10,7 @@ <body> <h1><a name="top">libdaemon @PACKAGE_VERSION@</a></h1> -<p><i>Copyright 2003,2004 Lennart Poettering <@PACKAGE_BUGREPORT@></i></p> +<p><i>Copyright 2003-2005 Lennart Poettering <@PACKAGE_BUGREPORT@></i></p> <ul class="toc"> <li><a href="#license">License</a></li> @@ -42,6 +42,9 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.</p> <h2><a name="news">News</a></h2> +<div class="news-date">Sat Jun 4 2005: </div> +<p class="news-text"><a href="@PACKAGE_URL@libdaemon-0.8.tar.gz">Version 0.8</a> released; changes include: proper PID file locking</p> + <div class="news-date">Sat Dec 18 2004: </div> <p class="news-text"><a href="@PACKAGE_URL@libdaemon-0.7.tar.gz">Version 0.7</a> released; changes include: minor cleanups; C++ compatibility; gcc 2.95 compatiblity</p> @@ -131,7 +134,7 @@ compilation and <tt>make install</tt> (as root) for installation of <p>If you want to be notified whenever I release a new version of this software use the subscription feature of <a href="http://freshmeat.net/projects/libdaemon/">Freshmeat</a>.</p> <hr/> -<address class="grey">Lennart Poettering <@PACKAGE_BUGREPORT@>, December 2004</address> +<address class="grey">Lennart Poettering <@PACKAGE_BUGREPORT@>, June 2005</address> <div class="grey"><i>$Id$</i></div> </body> diff --git a/doc/style.css b/doc/style.css index 995b68e..27ea945 100644 --- a/doc/style.css +++ b/doc/style.css @@ -18,16 +18,8 @@ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. ***/ -body { color: black; background-color: white; margin: 0.5cm; } +body { color: black; background-color: white; } a:link, a:visited { color: #900000; } -p { margin-left: 0.5cm; margin-right: 0.5cm; } -div.news-date { margin-left: 0.5cm; font-size: 80%; color: #4f0000; } -p.news-text { margin-left: 1cm; } -h1 { color: #00009F; } -h2 { color: #00009F; } -h3 { color: #00004F; margin-left: 0.5cm; } -ul { margin-left: .5cm; } -ol { margin-left: .5cm; } -pre { margin-left: .5cm; background-color: #f0f0f0; padding: 0.4cm;} -.grey { color: #afafaf; } - +div.news-date { font-size: 80%; font-style: italic; } +pre { background-color: #f0f0f0; padding: 0.4cm; } +.grey { color: #8f8f8f; font-size: 80%; } diff --git a/libdaemon/Makefile.am b/libdaemon/Makefile.am index c14000c..f0dd5e6 100644 --- a/libdaemon/Makefile.am +++ b/libdaemon/Makefile.am @@ -39,4 +39,4 @@ libdaemon_la_SOURCES = \ daemon.h \ $(pkg_include_HEADERS) -libdaemon_la_LDFLAGS = -version-info 2:1:2 +libdaemon_la_LDFLAGS = -version-info 2:2:2 diff --git a/libdaemon/dlog.c b/libdaemon/dlog.c index 1fc3e08..88c4e60 100644 --- a/libdaemon/dlog.c +++ b/libdaemon/dlog.c @@ -29,7 +29,7 @@ #include "dlog.h" enum daemon_log_flags daemon_log_use = DAEMON_LOG_AUTO|DAEMON_LOG_STDERR; -char* daemon_log_ident = 0; +char* daemon_log_ident = NULL; void daemon_log(int prio, const char* template, ...) { va_list arglist; diff --git a/libdaemon/dlog.h b/libdaemon/dlog.h index 0f74dde..55b641a 100644 --- a/libdaemon/dlog.h +++ b/libdaemon/dlog.h @@ -32,7 +32,7 @@ extern "C" { * Contains a robust API for logging messages */ -/** Specifies where to send the log messages to +/** Specifies where to send the log messages to. The global variable daemon_log_use takes values of this type. */ enum daemon_log_flags { DAEMON_LOG_SYSLOG = 1, /**< Log messages are written to syslog */ diff --git a/libdaemon/dpid.c b/libdaemon/dpid.c index e41e37c..7c90297 100644 --- a/libdaemon/dpid.c +++ b/libdaemon/dpid.c @@ -32,13 +32,15 @@ #include <sys/types.h> #include <sys/stat.h> #include <time.h> +#include <fcntl.h> +#include <stddef.h> #include "dpid.h" #include "dlog.h" #define VARRUN "/var/run" -const char *daemon_pid_file_ident = 0; +const char *daemon_pid_file_ident = NULL; daemon_pid_file_proc_t daemon_pid_file_proc = daemon_pid_file_proc_default; const char *daemon_pid_file_proc_default(void) { @@ -47,41 +49,74 @@ const char *daemon_pid_file_proc_default(void) { return fn; } +static int lock_file(int fd, int enable) { + struct flock f; + + memset(&f, 0, sizeof(f)); + f.l_type = enable ? F_WRLCK : F_UNLCK; + f.l_whence = SEEK_SET; + f.l_start = 0; + f.l_len = 0; + + if (fcntl(fd, F_SETLKW, &f) < 0) { + daemon_log(LOG_WARNING, "fcntl(F_SETLKW) failed: %s", strerror(errno)); + return -1; + } + + return 0; +} + pid_t daemon_pid_file_is_running(void) { const char *fn; static char txt[256]; - FILE *f; - pid_t pid; - + int fd = -1, locked = -1; + pid_t ret = (pid_t) -1, pid; + ssize_t l; if (!(fn = daemon_pid_file_proc())) - return (pid_t) -1; + goto finish; - if (!(f = fopen(fn, "r"))) - return (pid_t) -1; + if ((fd = open(fn, O_RDWR, 0644)) < 0) { + if (errno != ENOENT) + daemon_log(LOG_WARNING, "Failed to open PID file: %s", strerror(errno)); - if (!(fgets(txt, sizeof(txt), f))) { - daemon_log(LOG_WARNING, "PID file corrupt, removing. (%s)", fn); - unlink(fn); - fclose(f); - return (pid_t) -1; + goto finish; } - fclose(f); + if ((locked = lock_file(fd, 1)) < 0) + goto finish; + + if ((l = read(fd, txt, sizeof(txt)-1)) < 0) { + daemon_log(LOG_WARNING, "read(): %s", strerror(errno)); + unlink(fn); + goto finish; + } + txt[l] = 0; + if ((pid = (pid_t) atoi(txt)) <= 0) { daemon_log(LOG_WARNING, "PID file corrupt, removing. (%s)", fn); unlink(fn); - return (pid_t) -1; + goto finish; } if (kill(pid, 0) != 0 && errno != EPERM) { - daemon_log(LOG_WARNING, "Daemon %u killed: %s; removing PID file. (%s)", pid, strerror(errno), fn); + daemon_log(LOG_WARNING, "Process %lu died: %s; removing PID file. (%s)", (unsigned long) pid, strerror(errno), fn); unlink(fn); - return (pid_t) -1; + goto finish; } + + ret = pid; - return pid; +finish: + + if (fd >= 0) { + if (locked >= 0) + lock_file(fd, 0); + close(fd); + } + + return ret; } int daemon_pid_file_kill(int s) { @@ -128,23 +163,45 @@ int daemon_pid_file_kill_wait(int s, int m) { int daemon_pid_file_create(void) { const char *fn; - FILE *f; - mode_t save; + int fd = -1; + int ret = -1; + int locked = -1; + char t[64]; + ssize_t l; if (!(fn = daemon_pid_file_proc())) - return -1; + goto finish; - save = umask(022); - - if (!(f = fopen(fn, "w"))) - return -1; + if ((fd = open(fn, O_CREAT|O_RDWR|O_EXCL, 0644)) < 0) { + fprintf(stderr, "open(%s): %s", fn, strerror(errno)); + goto finish; + } - fprintf(f, "%u\n", getpid()); - fclose(f); + if ((locked = lock_file(fd, 1)) < 0) { + unlink(fn); + goto finish; + } - umask(save); + snprintf(t, sizeof(t), "%lu\n", (unsigned long) getpid()); - return 0; + if (write(fd, t, l = strlen(t)) != l) { + daemon_log(LOG_WARNING, "write(): %s", strerror(errno)); + unlink(fn); + goto finish; + } + + ret = 0; + +finish: + + if (fd >= 0) { + if (locked >= 0) + lock_file(fd, 0); + + close(fd); + } + + return ret; } int daemon_pid_file_remove(void) { |