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