xref: /netbsd/lib/libutil/pidfile.c (revision bf9ec67e)
1 /*	$NetBSD: pidfile.c,v 1.7 2002/05/22 07:31:45 itojun 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 and Matthias Scheler.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 __RCSID("$NetBSD: pidfile.c,v 1.7 2002/05/22 07:31:45 itojun Exp $");
42 #endif
43 
44 #include <sys/param.h>
45 #include <paths.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <util.h>
51 
52 static int   pidfile_atexit_done;
53 static pid_t pidfile_pid;
54 static char *pidfile_basename;
55 static char *pidfile_path;
56 
57 static void pidfile_cleanup(void);
58 
59 int
60 pidfile(const char *basename)
61 {
62 	FILE *f;
63 
64 	/*
65 	 * Register handler which will remove the pidfile later.
66 	 */
67 	if (!pidfile_atexit_done) {
68 		if (atexit(pidfile_cleanup) < 0)
69 			return -1;
70 		pidfile_atexit_done = 1;
71 	}
72 
73 	if (basename == NULL)
74 		basename = getprogname();
75 
76 	/*
77 	 * If pidfile has already been created for the supplied basename
78 	 * we don't need to create a pidfile again.
79 	 */
80 	if (pidfile_path != NULL) {
81 		if (strcmp(pidfile_basename, basename) == 0)
82 			return 0;
83 		/*
84 		 * Remove existing pidfile if it was created by this process.
85 		 */
86 		pidfile_cleanup();
87 
88 		free(pidfile_path);
89 		pidfile_path = NULL;
90 		free(pidfile_basename);
91 		pidfile_basename = NULL;
92 	}
93 
94 	pidfile_pid = getpid();
95 
96 	pidfile_basename = strdup(basename);
97 	if (pidfile_basename == NULL)
98 		return -1;
99 
100 	/* _PATH_VARRUN includes trailing / */
101 	(void) asprintf(&pidfile_path, "%s%s.pid", _PATH_VARRUN, basename);
102 	if (pidfile_path == NULL) {
103 		free(pidfile_basename);
104 		pidfile_basename = NULL;
105 		return -1;
106 	}
107 
108 	if ((f = fopen(pidfile_path, "w")) == NULL) {
109 		free(pidfile_path);
110 		pidfile_path = NULL;
111 		free(pidfile_basename);
112 		pidfile_basename = NULL;
113 		return -1;
114 	}
115 
116 	(void) fprintf(f, "%d\n", pidfile_pid);
117 	(void) fclose(f);
118 	return 0;
119 }
120 
121 static void
122 pidfile_cleanup(void)
123 {
124 	/* Only remove the pidfile if it was created by this process. */
125 	if ((pidfile_path != NULL) && (pidfile_pid == getpid()))
126 		(void) unlink(pidfile_path);
127 }
128