From a412f49bb0f5079db6ce0a46aa6ab5b08b2c4394 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 23 Jul 2008 13:08:31 -0700 Subject: [PATCH] New function make_pidfile_name(). The upcoming ofp-kill utility wants to use this. --- include/daemon.h | 1 + lib/daemon.c | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/include/daemon.h b/include/daemon.h index 208cee9c..d7273e02 100644 --- a/include/daemon.h +++ b/include/daemon.h @@ -34,6 +34,7 @@ #ifndef DAEMON_H #define DAEMON_H 1 +char *make_pidfile_name(const char *name); void set_pidfile(const char *name); void set_detach(void); void daemonize(void); diff --git a/lib/daemon.c b/lib/daemon.c index b153dc0c..b5977172 100644 --- a/lib/daemon.c +++ b/lib/daemon.c @@ -50,6 +50,16 @@ static bool detach; /* Name of pidfile (null if none). */ static char *pidfile; +/* Returns the file name that would be used for a pidfile if 'name' were + * provided to set_pidfile(). The caller must free the returned string. */ +char * +make_pidfile_name(const char *name) +{ + return (!name ? xasprintf("%s/%s.pid", RUNDIR, program_name) + : *name == '/' ? xstrdup(name) + : xasprintf("%s/%s", RUNDIR, name)); +} + /* Sets up a following call to daemonize() to create a pidfile named 'name'. * If 'name' begins with '/', then it is treated as an absolute path. * Otherwise, it is taken relative to RUNDIR, which is $(prefix)/var/run by @@ -60,9 +70,7 @@ void set_pidfile(const char *name) { free(pidfile); - pidfile = (!name ? xasprintf("%s/%s.pid", RUNDIR, program_name) - : *name == '/' ? xstrdup(name) - : xasprintf("%s/%s", RUNDIR, name)); + pidfile = make_pidfile_name(name); } /* Sets up a following call to daemonize() to detach from the foreground -- 2.30.2