From 5d07594709dd167f4c76c3e5199a32e50c59946b Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Thu, 21 Jan 2016 23:30:08 +0100 Subject: [PATCH] Update pidfile() documentation and add unit test for __pidfile_name Signed-off-by: Joachim Nilsson --- README.md | 10 +++++++--- pidfilefn.c | 9 +++++++-- pidfilefn.mk | 1 + 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e794ddb..7054086 100644 --- a/README.md +++ b/README.md @@ -177,8 +177,10 @@ Generic Functions - `pidfile(basename)` Create a daemon PID file in `_PATH_VARRUN` using either `basename` or, - if `basename` is `NULL`, `__progname`. The file name has the form - `/var/run/basename.pid`. + if `basename` is `NULL`, `__progname`. The resulting file name is + available to the user as a read-only pointer: + + extern char *__pidfile_name; Use this function to create a PID file for your daemon when it is ready to receive signals. A client application may poll for the @@ -242,7 +244,9 @@ The following are the popular OpenBSD string functions. http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/pidfile.3 **Note:** this version of `pidfile()` has been extended to handle it - being called multiple times. See previous section for details. + being called multiple times, and also to export the path to the PID + file `__pidfile_name`, similar to `__progname`. See previous section + for details. - `strlcpy(dst, src, len)` diff --git a/pidfilefn.c b/pidfilefn.c index 6e2b104..fc0557c 100644 --- a/pidfilefn.c +++ b/pidfilefn.c @@ -21,6 +21,7 @@ #include #include +extern char *__pidfile_name; extern char *chomp(char *str); /** @@ -126,12 +127,12 @@ int pidfile_signal(const char *pidfile, int signal) extern char *__progname; static char PIDFILE[42]; -static void sigterm_handler(int signo) +static void sigterm_handler(int UNUSED(signo)) { printf("Exiting ...\n"); } -static void sigalrm_handler(int signo) +static void sigalrm_handler(int UNUSED(signo)) { printf("Testing pidfile() ...\n"); pidfile(NULL); @@ -159,6 +160,10 @@ int main(void) printf("Reading pid file, should be %d ...\n", getpid()); printf("=> %d\n", pidfile_read(PIDFILE)); + printf("\nComparing __pidfile_name with our guessed PID filename ...\n"); + printf("strcmp(\"%s\",\n \"%s\") => %s\n\n", __pidfile_name, PIDFILE, + !strcmp(__pidfile_name, PIDFILE) ? "OK" : "FAIL"); + /* Occular verification that calling pidfile() again updates mtime */ snprintf(cmd, sizeof(cmd), "ls -l --full-time %s", PIDFILE); system(cmd); diff --git a/pidfilefn.mk b/pidfilefn.mk index 3b5a5c1..30eec5e 100644 --- a/pidfilefn.mk +++ b/pidfilefn.mk @@ -1,3 +1,4 @@ +CFLAGS = -W -Wall -Wextra -Werror OBJS = pidfile.o chomp.o fexist.o ASROOT = 1 TARGET = clean $(OBJS) pidfilefn.test