xref: /freebsd/contrib/openbsm/compat/pidfile.h (revision 640235e2c2ba32947f7c59d168437ffa1280f1e6)
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  * Derived from FreeBSD head/lib/libutil/pidfile.c r231938
27  */
28 
29 #include <sys/param.h>
30 #include <sys/file.h>
31 #include <sys/stat.h>
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <time.h>
39 #include <err.h>
40 #include <errno.h>
41 
42 #include "flopen.h"
43 
44 struct pidfh {
45 	int	pf_fd;
46 	char	pf_path[MAXPATHLEN + 1];
47 	dev_t	pf_dev;
48 	ino_t	pf_ino;
49 };
50 
51 static int _pidfile_remove(struct pidfh *pfh, int freeit);
52 
53 static int
54 pidfile_verify(const struct pidfh *pfh)
55 {
56 	struct stat sb;
57 
58 	if (pfh == NULL || pfh->pf_fd == -1)
59 		return (EINVAL);
60 	/*
61 	 * Check remembered descriptor.
62 	 */
63 	if (fstat(pfh->pf_fd, &sb) == -1)
64 		return (errno);
65 	if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
66 		return (EINVAL);
67 	return (0);
68 }
69 
70 static int
71 pidfile_read(const char *path, pid_t *pidptr)
72 {
73 	char buf[16], *endptr;
74 	int error, fd, i;
75 
76 	fd = open(path, O_RDONLY);
77 	if (fd == -1)
78 		return (errno);
79 
80 	i = read(fd, buf, sizeof(buf) - 1);
81 	error = errno;	/* Remember errno in case close() wants to change it. */
82 	close(fd);
83 	if (i == -1)
84 		return (error);
85 	else if (i == 0)
86 		return (EAGAIN);
87 	buf[i] = '\0';
88 
89 	*pidptr = strtol(buf, &endptr, 10);
90 	if (endptr != &buf[i])
91 		return (EINVAL);
92 
93 	return (0);
94 }
95 
96 static struct pidfh *
97 pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
98 {
99 	struct pidfh *pfh;
100 	struct stat sb;
101 	int error, fd, len, count;
102 	struct timespec rqtp;
103 
104 	if (pidptr != NULL)
105 		*pidptr = -1;
106 
107 	if (path == NULL)
108 		return (NULL);
109 
110 	pfh = malloc(sizeof(*pfh));
111 	if (pfh == NULL)
112 		return (NULL);
113 
114 	len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
115 	    "%s", path);
116 	if (len >= (int)sizeof(pfh->pf_path)) {
117 		free(pfh);
118 		errno = ENAMETOOLONG;
119 		return (NULL);
120 	}
121 
122 	/*
123 	 * Open the PID file and obtain exclusive lock.
124 	 * We truncate PID file here only to remove old PID immediatelly,
125 	 * PID file will be truncated again in pidfile_write(), so
126 	 * pidfile_write() can be called multiple times.
127 	 */
128 	fd = flopen(pfh->pf_path,
129 #ifdef O_CLOEXEC
130 	    O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK | O_CLOEXEC, mode);
131 #else
132 	    O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
133 #endif
134 	if (fd == -1) {
135 		if (errno == EWOULDBLOCK && pidptr != NULL) {
136 			count = 20;
137 			rqtp.tv_sec = 0;
138 			rqtp.tv_nsec = 5000000;
139 			for (;;) {
140 				errno = pidfile_read(pfh->pf_path, pidptr);
141 				if (errno != EAGAIN || --count == 0)
142 					break;
143 				nanosleep(&rqtp, 0);
144 			}
145 			if (errno == EAGAIN)
146 				*pidptr = -1;
147 			if (errno == 0 || errno == EAGAIN)
148 				errno = EEXIST;
149 		}
150 		free(pfh);
151 		return (NULL);
152 	}
153 
154 #ifndef O_CLOEXEC
155 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
156 		error = errno;
157 		unlink(pfh->pf_path);
158 		close(fd);
159 		free(pfh);
160 		errno = error;
161 		return (NULL);
162 	}
163 #endif
164 
165 	/*
166 	 * Remember file information, so in pidfile_write() we are sure we write
167 	 * to the proper descriptor.
168 	 */
169 	if (fstat(fd, &sb) == -1) {
170 		error = errno;
171 		unlink(pfh->pf_path);
172 		close(fd);
173 		free(pfh);
174 		errno = error;
175 		return (NULL);
176 	}
177 
178 	pfh->pf_fd = fd;
179 	pfh->pf_dev = sb.st_dev;
180 	pfh->pf_ino = sb.st_ino;
181 
182 	return (pfh);
183 }
184 
185 static int
186 pidfile_write(struct pidfh *pfh)
187 {
188 	char pidstr[16];
189 	int error, fd;
190 
191 	/*
192 	 * Check remembered descriptor, so we don't overwrite some other
193 	 * file if pidfile was closed and descriptor reused.
194 	 */
195 	errno = pidfile_verify(pfh);
196 	if (errno != 0) {
197 		/*
198 		 * Don't close descriptor, because we are not sure if it's ours.
199 		 */
200 		return (-1);
201 	}
202 	fd = pfh->pf_fd;
203 
204 	/*
205 	 * Truncate PID file, so multiple calls of pidfile_write() are allowed.
206 	 */
207 	if (ftruncate(fd, 0) == -1) {
208 		error = errno;
209 		_pidfile_remove(pfh, 0);
210 		errno = error;
211 		return (-1);
212 	}
213 
214 	snprintf(pidstr, sizeof(pidstr), "%u", getpid());
215 	if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
216 		error = errno;
217 		_pidfile_remove(pfh, 0);
218 		errno = error;
219 		return (-1);
220 	}
221 
222 	return (0);
223 }
224 
225 static int
226 pidfile_close(struct pidfh *pfh)
227 {
228 	int error;
229 
230 	error = pidfile_verify(pfh);
231 	if (error != 0) {
232 		errno = error;
233 		return (-1);
234 	}
235 
236 	if (close(pfh->pf_fd) == -1)
237 		error = errno;
238 	free(pfh);
239 	if (error != 0) {
240 		errno = error;
241 		return (-1);
242 	}
243 	return (0);
244 }
245 
246 static int
247 _pidfile_remove(struct pidfh *pfh, int freeit)
248 {
249 	int error;
250 
251 	error = pidfile_verify(pfh);
252 	if (error != 0) {
253 		errno = error;
254 		return (-1);
255 	}
256 
257 	if (unlink(pfh->pf_path) == -1)
258 		error = errno;
259 	if (close(pfh->pf_fd) == -1) {
260 		if (error == 0)
261 			error = errno;
262 	}
263 	if (freeit)
264 		free(pfh);
265 	else
266 		pfh->pf_fd = -1;
267 	if (error != 0) {
268 		errno = error;
269 		return (-1);
270 	}
271 	return (0);
272 }
273 
274 static int
275 pidfile_remove(struct pidfh *pfh)
276 {
277 
278 	return (_pidfile_remove(pfh, 1));
279 }
280 
281 #if 0
282 static int
283 pidfile_fileno(const struct pidfh *pfh)
284 {
285 
286 	if (pfh == NULL || pfh->pf_fd == -1) {
287 		errno = EINVAL;
288 		return (-1);
289 	}
290 	return (pfh->pf_fd);
291 }
292 #endif
293