xref: /freebsd/contrib/blocklist/port/pidfile.c (revision 48e64ca13d4f36795ac718911b805e3e9a726f1b)
1 /*	$NetBSD: pidfile.c,v 1.2 2025/02/11 17:48:30 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe, Matthias Scheler and Julio Merino.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #ifdef HAVE_SYS_CDEFS_H
36 #include <sys/cdefs.h>
37 #endif
38 #if defined(LIBC_SCCS) && !defined(lint)
39 __RCSID("$NetBSD: pidfile.c,v 1.2 2025/02/11 17:48:30 christos Exp $");
40 #endif
41 
42 #include <sys/param.h>
43 
44 #include <paths.h>
45 #include <stdbool.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <unistd.h>
50 #ifdef HAVE_LIBUTIL_H
51 #include <libutil.h>
52 #endif
53 #ifdef HAVE_UTIL_H
54 #include <util.h>
55 #endif
56 
57 static pid_t pidfile_pid;
58 static char *pidfile_path;
59 
60 /* Deletes an existent pidfile iff it was created by this process. */
61 static void
pidfile_cleanup(void)62 pidfile_cleanup(void)
63 {
64 
65 	if ((pidfile_path != NULL) && (pidfile_pid == getpid()))
66 		(void) unlink(pidfile_path);
67 }
68 
69 /* Registers an atexit(3) handler to delete the pidfile we have generated.
70  * We only register the handler when we create a pidfile, so we can assume
71  * that the pidfile exists.
72  *
73  * Returns 0 on success or -1 if the handler could not be registered. */
74 static int
register_atexit_handler(void)75 register_atexit_handler(void)
76 {
77 	static bool done = false;
78 
79 	if (!done) {
80 		if (atexit(pidfile_cleanup) < 0)
81 			return -1;
82 		done = true;
83 	}
84 
85 	return 0;
86 }
87 
88 /* Given a new pidfile name in 'path', deletes any previously-created pidfile
89  * if the previous file differs to the new one.
90  *
91  * If a previous file is deleted, returns 1, which means that a new pidfile
92  * must be created.  Otherwise, this returns 0, which means that the existing
93  * file does not need to be touched. */
94 static int
cleanup_old_pidfile(const char * path)95 cleanup_old_pidfile(const char* path)
96 {
97 	if (pidfile_path != NULL) {
98 		if (strcmp(pidfile_path, path) != 0) {
99 			pidfile_cleanup();
100 
101 			free(pidfile_path);
102 			pidfile_path = NULL;
103 
104 			return 1;
105 		} else
106 			return 0;
107 	} else
108 		return 1;
109 }
110 
111 /* Constructs a name for a pidfile in the default location (/var/run).  If
112  * 'basename' is NULL, uses the name of the current program for the name of
113  * the pidfile.
114  *
115  * Returns a pointer to a dynamically-allocatd string containing the absolute
116  * path to the pidfile; NULL on failure. */
117 static char *
generate_varrun_path(const char * bname)118 generate_varrun_path(const char *bname)
119 {
120 	char *path;
121 
122 	if (bname == NULL)
123 		bname = getprogname();
124 
125 	/* _PATH_VARRUN includes trailing / */
126 	if (asprintf(&path, "%s%s.pid", _PATH_VARRUN, bname) == -1)
127 		return NULL;
128 	return path;
129 }
130 
131 /* Creates a pidfile with the provided name.  The new pidfile is "registered"
132  * in the global variables pidfile_path and pidfile_pid so that any further
133  * call to pidfile(3) can check if we are recreating the same file or a new
134  * one.
135  *
136  * Returns 0 on success or -1 if there is any error. */
137 static int
create_pidfile(const char * path)138 create_pidfile(const char* path)
139 {
140 	FILE *f;
141 
142 	if (register_atexit_handler() == -1)
143 		return -1;
144 
145 	if (cleanup_old_pidfile(path) == 0)
146 		return 0;
147 
148 	pidfile_path = strdup(path);
149 	if (pidfile_path == NULL)
150 		return -1;
151 
152 	if ((f = fopen(path, "w")) == NULL) {
153 		free(pidfile_path);
154 		pidfile_path = NULL;
155 		return -1;
156 	}
157 
158 	pidfile_pid = getpid();
159 
160 	(void) fprintf(f, "%d\n", pidfile_pid);
161 	(void) fclose(f);
162 
163 	return 0;
164 }
165 
166 int
pidfile(const char * path)167 pidfile(const char *path)
168 {
169 
170 	if (path == NULL || strchr(path, '/') == NULL) {
171 		char *default_path;
172 
173 		if ((default_path = generate_varrun_path(path)) == NULL)
174 			return -1;
175 
176 		if (create_pidfile(default_path) == -1) {
177 			free(default_path);
178 			return -1;
179 		}
180 
181 		free(default_path);
182 		return 0;
183 	} else
184 		return create_pidfile(path);
185 }
186