Update pidfile() documentation and add unit test for __pidfile_name

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2016-01-21 23:30:08 +01:00
parent 253460b4b8
commit 5d07594709
3 changed files with 15 additions and 5 deletions
+7 -3
View File
@@ -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)`
+7 -2
View File
@@ -21,6 +21,7 @@
#include <signal.h>
#include <unistd.h>
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);
+1
View File
@@ -1,3 +1,4 @@
CFLAGS = -W -Wall -Wextra -Werror
OBJS = pidfile.o chomp.o fexist.o
ASROOT = 1
TARGET = clean $(OBJS) pidfilefn.test