xref: /haiku/src/libs/util/pidfile.c (revision 56e857a499a1bfb85707e1f9967478abddad1eee)
1 /*-
2  * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: src/lib/libutil/pidfile.c,v 1.3 2006/06/23 01:42:03 brian Exp $");
29 
30 #include <sys/param.h>
31 #include <sys/file.h>
32 #include <sys/stat.h>
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <string.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <inttypes.h>
42 #include <libutil.h>
43 
44 static int _pidfile_remove(struct pidfh *pfh, int freeit);
45 
46 static int
pidfile_verify(struct pidfh * pfh)47 pidfile_verify(struct pidfh *pfh)
48 {
49 	struct stat sb;
50 
51 	if (pfh == NULL || pfh->pf_fd == -1)
52 		return (EDOOFUS);
53 	/*
54 	 * Check remembered descriptor.
55 	 */
56 	if (fstat(pfh->pf_fd, &sb) == -1)
57 		return (errno);
58 	if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
59 		return (EDOOFUS);
60 	return (0);
61 }
62 
63 static int
pidfile_read(const char * path,pid_t * pidptr)64 pidfile_read(const char *path, pid_t *pidptr)
65 {
66 	char buf[16], *endptr;
67 	int error, fd, i;
68 
69 	fd = open(path, O_RDONLY);
70 	if (fd == -1)
71 		return (errno);
72 
73 	i = read(fd, buf, sizeof(buf) - 1);
74 	error = errno;	/* Remember errno in case close() wants to change it. */
75 	close(fd);
76 	if (i == -1)
77 		return (error);
78 	buf[i] = '\0';
79 
80 	*pidptr = strtol(buf, &endptr, 10);
81 	if (endptr != &buf[i])
82 		return (EINVAL);
83 
84 	return (0);
85 }
86 
87 struct pidfh *
pidfile_open(const char * path,mode_t mode,pid_t * pidptr)88 pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
89 {
90 	struct pidfh *pfh;
91 	struct stat sb;
92 	int error, fd;
93 
94 	pfh = malloc(sizeof(*pfh));
95 	if (pfh == NULL)
96 		return (NULL);
97 
98 	if (path == NULL) {
99 		snprintf(pfh->pf_path, sizeof(pfh->pf_path), "/var/run/%s.pid",
100 		    getprogname());
101 	} else {
102 		strlcpy(pfh->pf_path, path, sizeof(pfh->pf_path));
103 	}
104 	if (strlen(pfh->pf_path) == sizeof(pfh->pf_path) - 1) {
105 		free(pfh);
106 		errno = ENAMETOOLONG;
107 		return (NULL);
108 	}
109 
110 	/*
111 	 * Open the PID file and obtain exclusive lock.
112 	 * We truncate PID file here only to remove old PID immediatelly,
113 	 * PID file will be truncated again in pidfile_write(), so
114 	 * pidfile_write() can be called multiple times.
115 	 */
116 	fd = open(pfh->pf_path, O_WRONLY | O_CREAT | O_NONBLOCK, mode);
117 	if (fd >= 0) {
118 		struct flock lock;
119 		lock.l_type = F_WRLCK;
120 		lock.l_whence = SEEK_SET;
121 		lock.l_start = 0;
122 		lock.l_len = 0;
123 
124 		if (fcntl(fd, F_SETLK, &lock) == -1) {
125 			close(fd);
126 			fd = -1;
127 		}
128 	}
129 
130 	if (fd == -1) {
131 		if (errno == EWOULDBLOCK && pidptr != NULL) {
132 			errno = pidfile_read(pfh->pf_path, pidptr);
133 			if (errno == 0)
134 				errno = EEXIST;
135 		}
136 		free(pfh);
137 		return (NULL);
138 	}
139 
140 	ftruncate(fd, 0);
141 
142 	/*
143 	 * Remember file information, so in pidfile_write() we are sure we write
144 	 * to the proper descriptor.
145 	 */
146 	if (fstat(fd, &sb) == -1) {
147 		error = errno;
148 		unlink(pfh->pf_path);
149 		close(fd);
150 		free(pfh);
151 		errno = error;
152 		return (NULL);
153 	}
154 
155 	pfh->pf_fd = fd;
156 	pfh->pf_dev = sb.st_dev;
157 	pfh->pf_ino = sb.st_ino;
158 
159 	return (pfh);
160 }
161 
162 int
pidfile_write(struct pidfh * pfh)163 pidfile_write(struct pidfh *pfh)
164 {
165 	char pidstr[16];
166 	int error, fd;
167 
168 	/*
169 	 * Check remembered descriptor, so we don't overwrite some other
170 	 * file if pidfile was closed and descriptor reused.
171 	 */
172 	errno = pidfile_verify(pfh);
173 	if (errno != 0) {
174 		/*
175 		 * Don't close descriptor, because we are not sure if it's ours.
176 		 */
177 		return (-1);
178 	}
179 	fd = pfh->pf_fd;
180 
181 	/*
182 	 * Truncate PID file, so multiple calls of pidfile_write() are allowed.
183 	 */
184 	if (ftruncate(fd, 0) == -1) {
185 		error = errno;
186 		_pidfile_remove(pfh, 0);
187 		errno = error;
188 		return (-1);
189 	}
190 
191 	snprintf(pidstr, sizeof(pidstr), "%" PRIu32, getpid());
192 	if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
193 		error = errno;
194 		_pidfile_remove(pfh, 0);
195 		errno = error;
196 		return (-1);
197 	}
198 
199 	return (0);
200 }
201 
202 int
pidfile_close(struct pidfh * pfh)203 pidfile_close(struct pidfh *pfh)
204 {
205 	int error;
206 
207 	error = pidfile_verify(pfh);
208 	if (error != 0) {
209 		errno = error;
210 		return (-1);
211 	}
212 
213 	if (close(pfh->pf_fd) == -1)
214 		error = errno;
215 	free(pfh);
216 	if (error != 0) {
217 		errno = error;
218 		return (-1);
219 	}
220 	return (0);
221 }
222 
223 static int
_pidfile_remove(struct pidfh * pfh,int freeit)224 _pidfile_remove(struct pidfh *pfh, int freeit)
225 {
226 	int error;
227 
228 	error = pidfile_verify(pfh);
229 	if (error != 0) {
230 		errno = error;
231 		return (-1);
232 	}
233 
234 	if (unlink(pfh->pf_path) == -1)
235 		error = errno;
236 	if (flock(pfh->pf_fd, LOCK_UN) == -1) {
237 		if (error == 0)
238 			error = errno;
239 	}
240 	if (close(pfh->pf_fd) == -1) {
241 		if (error == 0)
242 			error = errno;
243 	}
244 	if (freeit)
245 		free(pfh);
246 	else
247 		pfh->pf_fd = -1;
248 	if (error != 0) {
249 		errno = error;
250 		return (-1);
251 	}
252 	return (0);
253 }
254 
255 int
pidfile_remove(struct pidfh * pfh)256 pidfile_remove(struct pidfh *pfh)
257 {
258 
259 	return (_pidfile_remove(pfh, 1));
260 }
261