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