diff --git a/lib/roken/write_pid.c b/lib/roken/write_pid.c index 71e1f766e..e5b72d615 100644 --- a/lib/roken/write_pid.c +++ b/lib/roken/write_pid.c @@ -49,7 +49,7 @@ pid_file_write (const char *progname) FILE *fp; char *ret; - asprintf (&ret, _PATH_VARRUN "%s.pid", progname); + asprintf (&ret, "%s%s.pid", _PATH_VARRUN, progname); if (ret == NULL) return NULL; fp = fopen (ret, "w"); @@ -71,3 +71,23 @@ pid_file_delete (char **filename) *filename = NULL; } } + +static char *pidfile_path; + +static void +pidfile_cleanup(void) +{ + if(pidfile_path != NULL) + pid_file_delete(&pidfile_path); +} + +void +pidfile(const char *basename) +{ + if(pidfile_path != NULL) + return; + if(basename == NULL) + basename = __progname; + pidfile_path = pid_file_write(basename); + atexit(pidfile_cleanup); +}