xref: /freebsd-src/contrib/pf/pflogd/pidfile.c (revision 6cec9cad762b6476313fb1f8e931a1647822db6b)
1*e298b784SMax Laier /*	$FreeBSD$ */
2*e298b784SMax Laier /*	$OpenBSD: pidfile.c,v 1.5 2002/05/26 09:29:02 deraadt Exp $	*/
3*e298b784SMax Laier /*	$NetBSD: pidfile.c,v 1.4 2001/02/19 22:43:42 cgd Exp $	*/
4*e298b784SMax Laier 
5*e298b784SMax Laier /*-
6*e298b784SMax Laier  * Copyright (c) 1999 The NetBSD Foundation, Inc.
7*e298b784SMax Laier  * All rights reserved.
8*e298b784SMax Laier  *
9*e298b784SMax Laier  * This code is derived from software contributed to The NetBSD Foundation
10*e298b784SMax Laier  * by Jason R. Thorpe.
11*e298b784SMax Laier  *
12*e298b784SMax Laier  * Redistribution and use in source and binary forms, with or without
13*e298b784SMax Laier  * modification, are permitted provided that the following conditions
14*e298b784SMax Laier  * are met:
15*e298b784SMax Laier  * 1. Redistributions of source code must retain the above copyright
16*e298b784SMax Laier  *    notice, this list of conditions and the following disclaimer.
17*e298b784SMax Laier  * 2. Redistributions in binary form must reproduce the above copyright
18*e298b784SMax Laier  *    notice, this list of conditions and the following disclaimer in the
19*e298b784SMax Laier  *    documentation and/or other materials provided with the distribution.
20*e298b784SMax Laier  *
21*e298b784SMax Laier  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22*e298b784SMax Laier  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23*e298b784SMax Laier  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24*e298b784SMax Laier  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25*e298b784SMax Laier  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26*e298b784SMax Laier  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27*e298b784SMax Laier  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*e298b784SMax Laier  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29*e298b784SMax Laier  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30*e298b784SMax Laier  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31*e298b784SMax Laier  * POSSIBILITY OF SUCH DAMAGE.
32*e298b784SMax Laier  */
33*e298b784SMax Laier 
34*e298b784SMax Laier #if defined(LIBC_SCCS) && !defined(lint)
35*e298b784SMax Laier static const char rcsid[] = "$OpenBSD: pidfile.c,v 1.5 2002/05/26 09:29:02 deraadt Exp $";
36*e298b784SMax Laier #endif /* LIBC_SCCS and not lint */
37*e298b784SMax Laier 
38*e298b784SMax Laier #include <sys/param.h>
39*e298b784SMax Laier #include <errno.h>
40*e298b784SMax Laier #include <paths.h>
41*e298b784SMax Laier #include <stdio.h>
42*e298b784SMax Laier #include <stdlib.h>
43*e298b784SMax Laier #include <unistd.h>
44*e298b784SMax Laier #ifdef __FreeBSD__
45*e298b784SMax Laier #include "pidfile.h"
46*e298b784SMax Laier #else
47*e298b784SMax Laier #include <util.h>
48*e298b784SMax Laier #endif
49*e298b784SMax Laier 
50*e298b784SMax Laier static char *pidfile_path;
51*e298b784SMax Laier static pid_t pidfile_pid;
52*e298b784SMax Laier 
53*e298b784SMax Laier static void pidfile_cleanup(void);
54*e298b784SMax Laier 
55*e298b784SMax Laier extern char *__progname;
56*e298b784SMax Laier 
57*e298b784SMax Laier int
pidfile(const char * basename)58*e298b784SMax Laier pidfile(const char *basename)
59*e298b784SMax Laier {
60*e298b784SMax Laier 	FILE *f;
61*e298b784SMax Laier 	int save_errno;
62*e298b784SMax Laier 	pid_t pid;
63*e298b784SMax Laier 
64*e298b784SMax Laier 	if (basename == NULL)
65*e298b784SMax Laier 		basename = __progname;
66*e298b784SMax Laier 
67*e298b784SMax Laier 	if (pidfile_path != NULL) {
68*e298b784SMax Laier 		free(pidfile_path);
69*e298b784SMax Laier 		pidfile_path = NULL;
70*e298b784SMax Laier 	}
71*e298b784SMax Laier 
72*e298b784SMax Laier 	/* _PATH_VARRUN includes trailing / */
73*e298b784SMax Laier 	(void) asprintf(&pidfile_path, "%s%s.pid", _PATH_VARRUN, basename);
74*e298b784SMax Laier 	if (pidfile_path == NULL)
75*e298b784SMax Laier 		return (-1);
76*e298b784SMax Laier 
77*e298b784SMax Laier 	if ((f = fopen(pidfile_path, "w")) == NULL) {
78*e298b784SMax Laier 		save_errno = errno;
79*e298b784SMax Laier 		free(pidfile_path);
80*e298b784SMax Laier 		pidfile_path = NULL;
81*e298b784SMax Laier 		errno = save_errno;
82*e298b784SMax Laier 		return (-1);
83*e298b784SMax Laier 	}
84*e298b784SMax Laier 
85*e298b784SMax Laier 	pid = getpid();
86*e298b784SMax Laier 	if (fprintf(f, "%ld\n", (long)pid) <= 0 || fclose(f) != 0) {
87*e298b784SMax Laier 		save_errno = errno;
88*e298b784SMax Laier 		(void) unlink(pidfile_path);
89*e298b784SMax Laier 		free(pidfile_path);
90*e298b784SMax Laier 		pidfile_path = NULL;
91*e298b784SMax Laier 		errno = save_errno;
92*e298b784SMax Laier 		return (-1);
93*e298b784SMax Laier 	}
94*e298b784SMax Laier 
95*e298b784SMax Laier 	pidfile_pid = pid;
96*e298b784SMax Laier 	if (atexit(pidfile_cleanup) < 0) {
97*e298b784SMax Laier 		save_errno = errno;
98*e298b784SMax Laier 		(void) unlink(pidfile_path);
99*e298b784SMax Laier 		free(pidfile_path);
100*e298b784SMax Laier 		pidfile_path = NULL;
101*e298b784SMax Laier 		pidfile_pid = 0;
102*e298b784SMax Laier 		errno = save_errno;
103*e298b784SMax Laier 		return (-1);
104*e298b784SMax Laier 	}
105*e298b784SMax Laier 
106*e298b784SMax Laier 	return (0);
107*e298b784SMax Laier }
108*e298b784SMax Laier 
109*e298b784SMax Laier static void
pidfile_cleanup(void)110*e298b784SMax Laier pidfile_cleanup(void)
111*e298b784SMax Laier {
112*e298b784SMax Laier 
113*e298b784SMax Laier 	if (pidfile_path != NULL && pidfile_pid == getpid())
114*e298b784SMax Laier 		(void) unlink(pidfile_path);
115*e298b784SMax Laier }
116