1*0a6a1f1dSLionel Sambuc /* $NetBSD: pidfile.c,v 1.11 2015/01/22 19:04:28 christos Exp $ */
20c3983b2SBen Gras
30c3983b2SBen Gras /*-
40c3983b2SBen Gras * Copyright (c) 1999 The NetBSD Foundation, Inc.
50c3983b2SBen Gras * All rights reserved.
60c3983b2SBen Gras *
70c3983b2SBen Gras * This code is derived from software contributed to The NetBSD Foundation
8dba3562dSLionel Sambuc * by Jason R. Thorpe, Matthias Scheler and Julio Merino.
90c3983b2SBen Gras *
100c3983b2SBen Gras * Redistribution and use in source and binary forms, with or without
110c3983b2SBen Gras * modification, are permitted provided that the following conditions
120c3983b2SBen Gras * are met:
130c3983b2SBen Gras * 1. Redistributions of source code must retain the above copyright
140c3983b2SBen Gras * notice, this list of conditions and the following disclaimer.
150c3983b2SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
160c3983b2SBen Gras * notice, this list of conditions and the following disclaimer in the
170c3983b2SBen Gras * documentation and/or other materials provided with the distribution.
180c3983b2SBen Gras *
190c3983b2SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
200c3983b2SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
210c3983b2SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220c3983b2SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
230c3983b2SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
240c3983b2SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
250c3983b2SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
260c3983b2SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
270c3983b2SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
280c3983b2SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
290c3983b2SBen Gras * POSSIBILITY OF SUCH DAMAGE.
300c3983b2SBen Gras */
310c3983b2SBen Gras
320c3983b2SBen Gras #include <sys/cdefs.h>
330c3983b2SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
34*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: pidfile.c,v 1.11 2015/01/22 19:04:28 christos Exp $");
350c3983b2SBen Gras #endif
360c3983b2SBen Gras
370c3983b2SBen Gras #include <sys/param.h>
38dba3562dSLionel Sambuc
390c3983b2SBen Gras #include <paths.h>
40dba3562dSLionel Sambuc #include <stdbool.h>
410c3983b2SBen Gras #include <stdlib.h>
420c3983b2SBen Gras #include <stdio.h>
430c3983b2SBen Gras #include <string.h>
440c3983b2SBen Gras #include <unistd.h>
450c3983b2SBen Gras #include <util.h>
460c3983b2SBen Gras
470c3983b2SBen Gras static pid_t pidfile_pid;
480c3983b2SBen Gras static char *pidfile_path;
490c3983b2SBen Gras
50dba3562dSLionel Sambuc /* Deletes an existent pidfile iff it was created by this process. */
51dba3562dSLionel Sambuc static void
pidfile_cleanup(void)52dba3562dSLionel Sambuc pidfile_cleanup(void)
530c3983b2SBen Gras {
540c3983b2SBen Gras
55dba3562dSLionel Sambuc if ((pidfile_path != NULL) && (pidfile_pid == getpid()))
56dba3562dSLionel Sambuc (void) unlink(pidfile_path);
570c3983b2SBen Gras }
580c3983b2SBen Gras
59dba3562dSLionel Sambuc /* Registers an atexit(3) handler to delete the pidfile we have generated.
60dba3562dSLionel Sambuc * We only register the handler when we create a pidfile, so we can assume
61dba3562dSLionel Sambuc * that the pidfile exists.
62dba3562dSLionel Sambuc *
63dba3562dSLionel Sambuc * Returns 0 on success or -1 if the handler could not be registered. */
64dba3562dSLionel Sambuc static int
register_atexit_handler(void)65dba3562dSLionel Sambuc register_atexit_handler(void)
66dba3562dSLionel Sambuc {
67dba3562dSLionel Sambuc static bool done = false;
680c3983b2SBen Gras
69dba3562dSLionel Sambuc if (!done) {
70dba3562dSLionel Sambuc if (atexit(pidfile_cleanup) < 0)
71dba3562dSLionel Sambuc return -1;
72dba3562dSLionel Sambuc done = true;
73dba3562dSLionel Sambuc }
74dba3562dSLionel Sambuc
750c3983b2SBen Gras return 0;
76dba3562dSLionel Sambuc }
77dba3562dSLionel Sambuc
78dba3562dSLionel Sambuc /* Given a new pidfile name in 'path', deletes any previously-created pidfile
79dba3562dSLionel Sambuc * if the previous file differs to the new one.
80dba3562dSLionel Sambuc *
81dba3562dSLionel Sambuc * If a previous file is deleted, returns 1, which means that a new pidfile
82dba3562dSLionel Sambuc * must be created. Otherwise, this returns 0, which means that the existing
83dba3562dSLionel Sambuc * file does not need to be touched. */
84dba3562dSLionel Sambuc static int
cleanup_old_pidfile(const char * path)85dba3562dSLionel Sambuc cleanup_old_pidfile(const char* path)
86dba3562dSLionel Sambuc {
87dba3562dSLionel Sambuc if (pidfile_path != NULL) {
88dba3562dSLionel Sambuc if (strcmp(pidfile_path, path) != 0) {
890c3983b2SBen Gras pidfile_cleanup();
900c3983b2SBen Gras
910c3983b2SBen Gras free(pidfile_path);
920c3983b2SBen Gras pidfile_path = NULL;
93dba3562dSLionel Sambuc
94dba3562dSLionel Sambuc return 1;
95dba3562dSLionel Sambuc } else
96dba3562dSLionel Sambuc return 0;
97dba3562dSLionel Sambuc } else
98dba3562dSLionel Sambuc return 1;
99dba3562dSLionel Sambuc }
100dba3562dSLionel Sambuc
101dba3562dSLionel Sambuc /* Constructs a name for a pidfile in the default location (/var/run). If
102*0a6a1f1dSLionel Sambuc * 'bname' is NULL, uses the name of the current program for the name of
103dba3562dSLionel Sambuc * the pidfile.
104dba3562dSLionel Sambuc *
105dba3562dSLionel Sambuc * Returns a pointer to a dynamically-allocatd string containing the absolute
106dba3562dSLionel Sambuc * path to the pidfile; NULL on failure. */
107dba3562dSLionel Sambuc static char *
generate_varrun_path(const char * bname)108*0a6a1f1dSLionel Sambuc generate_varrun_path(const char *bname)
109dba3562dSLionel Sambuc {
110dba3562dSLionel Sambuc char *path;
111dba3562dSLionel Sambuc
112*0a6a1f1dSLionel Sambuc if (bname == NULL)
113*0a6a1f1dSLionel Sambuc bname = getprogname();
114dba3562dSLionel Sambuc
115dba3562dSLionel Sambuc /* _PATH_VARRUN includes trailing / */
116*0a6a1f1dSLionel Sambuc if (asprintf(&path, "%s%s.pid", _PATH_VARRUN, bname) == -1)
117*0a6a1f1dSLionel Sambuc return NULL;
118dba3562dSLionel Sambuc return path;
119dba3562dSLionel Sambuc }
120dba3562dSLionel Sambuc
121dba3562dSLionel Sambuc /* Creates a pidfile with the provided name. The new pidfile is "registered"
122dba3562dSLionel Sambuc * in the global variables pidfile_path and pidfile_pid so that any further
123dba3562dSLionel Sambuc * call to pidfile(3) can check if we are recreating the same file or a new
124dba3562dSLionel Sambuc * one.
125dba3562dSLionel Sambuc *
126dba3562dSLionel Sambuc * Returns 0 on success or -1 if there is any error. */
127dba3562dSLionel Sambuc static int
create_pidfile(const char * path)128dba3562dSLionel Sambuc create_pidfile(const char* path)
129dba3562dSLionel Sambuc {
130dba3562dSLionel Sambuc FILE *f;
131dba3562dSLionel Sambuc
132dba3562dSLionel Sambuc if (register_atexit_handler() == -1)
133dba3562dSLionel Sambuc return -1;
134dba3562dSLionel Sambuc
135dba3562dSLionel Sambuc if (cleanup_old_pidfile(path) == 0)
136dba3562dSLionel Sambuc return 0;
137dba3562dSLionel Sambuc
138dba3562dSLionel Sambuc pidfile_path = strdup(path);
139dba3562dSLionel Sambuc if (pidfile_path == NULL)
140dba3562dSLionel Sambuc return -1;
141dba3562dSLionel Sambuc
142dba3562dSLionel Sambuc if ((f = fopen(path, "w")) == NULL) {
143dba3562dSLionel Sambuc free(pidfile_path);
144dba3562dSLionel Sambuc pidfile_path = NULL;
145dba3562dSLionel Sambuc return -1;
1460c3983b2SBen Gras }
1470c3983b2SBen Gras
1480c3983b2SBen Gras pidfile_pid = getpid();
1490c3983b2SBen Gras
1500c3983b2SBen Gras (void) fprintf(f, "%d\n", pidfile_pid);
1510c3983b2SBen Gras (void) fclose(f);
152dba3562dSLionel Sambuc
1530c3983b2SBen Gras return 0;
1540c3983b2SBen Gras }
1550c3983b2SBen Gras
156dba3562dSLionel Sambuc int
pidfile(const char * path)157dba3562dSLionel Sambuc pidfile(const char *path)
1580c3983b2SBen Gras {
159dba3562dSLionel Sambuc
160dba3562dSLionel Sambuc if (path == NULL || strchr(path, '/') == NULL) {
161dba3562dSLionel Sambuc char *default_path;
162dba3562dSLionel Sambuc
163dba3562dSLionel Sambuc if ((default_path = generate_varrun_path(path)) == NULL)
164dba3562dSLionel Sambuc return -1;
165dba3562dSLionel Sambuc
166dba3562dSLionel Sambuc if (create_pidfile(default_path) == -1) {
167dba3562dSLionel Sambuc free(default_path);
168dba3562dSLionel Sambuc return -1;
169dba3562dSLionel Sambuc }
170dba3562dSLionel Sambuc
171dba3562dSLionel Sambuc free(default_path);
172dba3562dSLionel Sambuc return 0;
173dba3562dSLionel Sambuc } else
174dba3562dSLionel Sambuc return create_pidfile(path);
1750c3983b2SBen Gras }
176