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: head/lib/libutil/pidfile.c 229942 2012-01-10 21:47:58Z ghelmer $");
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 <time.h>
40#include <err.h>
41#include <errno.h>
42#include <libutil.h>
43
44static int _pidfile_remove(struct pidfh *pfh, int freeit);
45
46static int
47pidfile_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
63static int
64pidfile_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 else if (i == 0)
79 return (EAGAIN);
80 buf[i] = '\0';
81
82 *pidptr = strtol(buf, &endptr, 10);
83 if (endptr != &buf[i])
84 return (EINVAL);
85
86 return (0);
87}
88
89struct pidfh *
90pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
91{
92 struct pidfh *pfh;
93 struct stat sb;
94 int error, fd, len, count;
95 struct timespec rqtp;
96
97 pfh = malloc(sizeof(*pfh));
98 if (pfh == NULL)
99 return (NULL);
100
101 if (path == NULL)
102 len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
103 "/var/run/%s.pid", getprogname());
104 else
105 len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
106 "%s", path);
107 if (len >= (int)sizeof(pfh->pf_path)) {
108 free(pfh);
109 errno = ENAMETOOLONG;
110 return (NULL);
111 }
112
113 /*
114 * Open the PID file and obtain exclusive lock.
115 * We truncate PID file here only to remove old PID immediatelly,
116 * PID file will be truncated again in pidfile_write(), so
117 * pidfile_write() can be called multiple times.
118 */
119 fd = flopen(pfh->pf_path,
120 O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
121 if (fd == -1) {
122 if (errno == EWOULDBLOCK && pidptr != NULL) {
123 count = 20;
124 rqtp.tv_sec = 0;
125 rqtp.tv_nsec = 5000000;
126 for (;;) {
127 errno = pidfile_read(pfh->pf_path, pidptr);
128 if (errno != EAGAIN || --count == 0)
129 break;
130 nanosleep(&rqtp, 0);
131 }
132 if (errno == EAGAIN)
133 *pidptr = -1;
134 if (errno == 0 || errno == EAGAIN)
135 errno = EEXIST;
136 }
137 free(pfh);
138 return (NULL);
139 }
140
141 /*
142 * Prevent the file descriptor from escaping to other
143 * programs via exec(3).
144 */
145 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
146 error = errno;
147 unlink(pfh->pf_path);
148 close(fd);
149 free(pfh);
150 errno = error;
151 return (NULL);
152 }
153
154 /*
155 * Remember file information, so in pidfile_write() we are sure we write
156 * to the proper descriptor.
157 */
158 if (fstat(fd, &sb) == -1) {
159 error = errno;
160 unlink(pfh->pf_path);
161 close(fd);
162 free(pfh);
163 errno = error;
164 return (NULL);
165 }
166
167 pfh->pf_fd = fd;
168 pfh->pf_dev = sb.st_dev;
169 pfh->pf_ino = sb.st_ino;
170
171 return (pfh);
172}
173
174int
175pidfile_write(struct pidfh *pfh)
176{
177 char pidstr[16];
178 int error, fd;
179
180 /*
181 * Check remembered descriptor, so we don't overwrite some other
182 * file if pidfile was closed and descriptor reused.
183 */
184 errno = pidfile_verify(pfh);
185 if (errno != 0) {
186 /*
187 * Don't close descriptor, because we are not sure if it's ours.
188 */
189 return (-1);
190 }
191 fd = pfh->pf_fd;
192
193 /*
194 * Truncate PID file, so multiple calls of pidfile_write() are allowed.
195 */
196 if (ftruncate(fd, 0) == -1) {
197 error = errno;
198 _pidfile_remove(pfh, 0);
199 errno = error;
200 return (-1);
201 }
202
203 snprintf(pidstr, sizeof(pidstr), "%u", getpid());
204 if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
205 error = errno;
206 _pidfile_remove(pfh, 0);
207 errno = error;
208 return (-1);
209 }
210
211 return (0);
212}
213
214int
215pidfile_close(struct pidfh *pfh)
216{
217 int error;
218
219 error = pidfile_verify(pfh);
220 if (error != 0) {
221 errno = error;
222 return (-1);
223 }
224
225 if (close(pfh->pf_fd) == -1)
226 error = errno;
227 free(pfh);
228 if (error != 0) {
229 errno = error;
230 return (-1);
231 }
232 return (0);
233}
234
235static int
236_pidfile_remove(struct pidfh *pfh, int freeit)
237{
238 int error;
239
240 error = pidfile_verify(pfh);
241 if (error != 0) {
242 errno = error;
243 return (-1);
244 }
245
246 if (unlink(pfh->pf_path) == -1)
247 error = errno;
248 if (close(pfh->pf_fd) == -1) {
249 if (error == 0)
250 error = errno;
251 }
252 if (freeit)
253 free(pfh);
254 else
255 pfh->pf_fd = -1;
256 if (error != 0) {
257 errno = error;
258 return (-1);
259 }
260 return (0);
261}
262
263int
264pidfile_remove(struct pidfh *pfh)
265{
266
267 return (_pidfile_remove(pfh, 1));
268}
269
270int
271pidfile_fileno(struct pidfh *pfh)
272{
273
274 if (pfh == NULL || pfh->pf_fd == -1) {
275 errno = EDOOFUS;
276 return (-1);
277 }
278 return (pfh->pf_fd);
279}
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: head/lib/libutil/pidfile.c 229942 2012-01-10 21:47:58Z ghelmer $");
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 <time.h>
40#include <err.h>
41#include <errno.h>
42#include <libutil.h>
43
44static int _pidfile_remove(struct pidfh *pfh, int freeit);
45
46static int
47pidfile_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
63static int
64pidfile_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 else if (i == 0)
79 return (EAGAIN);
80 buf[i] = '\0';
81
82 *pidptr = strtol(buf, &endptr, 10);
83 if (endptr != &buf[i])
84 return (EINVAL);
85
86 return (0);
87}
88
89struct pidfh *
90pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
91{
92 struct pidfh *pfh;
93 struct stat sb;
94 int error, fd, len, count;
95 struct timespec rqtp;
96
97 pfh = malloc(sizeof(*pfh));
98 if (pfh == NULL)
99 return (NULL);
100
101 if (path == NULL)
102 len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
103 "/var/run/%s.pid", getprogname());
104 else
105 len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
106 "%s", path);
107 if (len >= (int)sizeof(pfh->pf_path)) {
108 free(pfh);
109 errno = ENAMETOOLONG;
110 return (NULL);
111 }
112
113 /*
114 * Open the PID file and obtain exclusive lock.
115 * We truncate PID file here only to remove old PID immediatelly,
116 * PID file will be truncated again in pidfile_write(), so
117 * pidfile_write() can be called multiple times.
118 */
119 fd = flopen(pfh->pf_path,
120 O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
121 if (fd == -1) {
122 if (errno == EWOULDBLOCK && pidptr != NULL) {
123 count = 20;
124 rqtp.tv_sec = 0;
125 rqtp.tv_nsec = 5000000;
126 for (;;) {
127 errno = pidfile_read(pfh->pf_path, pidptr);
128 if (errno != EAGAIN || --count == 0)
129 break;
130 nanosleep(&rqtp, 0);
131 }
132 if (errno == EAGAIN)
133 *pidptr = -1;
134 if (errno == 0 || errno == EAGAIN)
135 errno = EEXIST;
136 }
137 free(pfh);
138 return (NULL);
139 }
140
141 /*
142 * Prevent the file descriptor from escaping to other
143 * programs via exec(3).
144 */
145 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
146 error = errno;
147 unlink(pfh->pf_path);
148 close(fd);
149 free(pfh);
150 errno = error;
151 return (NULL);
152 }
153
154 /*
155 * Remember file information, so in pidfile_write() we are sure we write
156 * to the proper descriptor.
157 */
158 if (fstat(fd, &sb) == -1) {
159 error = errno;
160 unlink(pfh->pf_path);
161 close(fd);
162 free(pfh);
163 errno = error;
164 return (NULL);
165 }
166
167 pfh->pf_fd = fd;
168 pfh->pf_dev = sb.st_dev;
169 pfh->pf_ino = sb.st_ino;
170
171 return (pfh);
172}
173
174int
175pidfile_write(struct pidfh *pfh)
176{
177 char pidstr[16];
178 int error, fd;
179
180 /*
181 * Check remembered descriptor, so we don't overwrite some other
182 * file if pidfile was closed and descriptor reused.
183 */
184 errno = pidfile_verify(pfh);
185 if (errno != 0) {
186 /*
187 * Don't close descriptor, because we are not sure if it's ours.
188 */
189 return (-1);
190 }
191 fd = pfh->pf_fd;
192
193 /*
194 * Truncate PID file, so multiple calls of pidfile_write() are allowed.
195 */
196 if (ftruncate(fd, 0) == -1) {
197 error = errno;
198 _pidfile_remove(pfh, 0);
199 errno = error;
200 return (-1);
201 }
202
203 snprintf(pidstr, sizeof(pidstr), "%u", getpid());
204 if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
205 error = errno;
206 _pidfile_remove(pfh, 0);
207 errno = error;
208 return (-1);
209 }
210
211 return (0);
212}
213
214int
215pidfile_close(struct pidfh *pfh)
216{
217 int error;
218
219 error = pidfile_verify(pfh);
220 if (error != 0) {
221 errno = error;
222 return (-1);
223 }
224
225 if (close(pfh->pf_fd) == -1)
226 error = errno;
227 free(pfh);
228 if (error != 0) {
229 errno = error;
230 return (-1);
231 }
232 return (0);
233}
234
235static int
236_pidfile_remove(struct pidfh *pfh, int freeit)
237{
238 int error;
239
240 error = pidfile_verify(pfh);
241 if (error != 0) {
242 errno = error;
243 return (-1);
244 }
245
246 if (unlink(pfh->pf_path) == -1)
247 error = errno;
248 if (close(pfh->pf_fd) == -1) {
249 if (error == 0)
250 error = errno;
251 }
252 if (freeit)
253 free(pfh);
254 else
255 pfh->pf_fd = -1;
256 if (error != 0) {
257 errno = error;
258 return (-1);
259 }
260 return (0);
261}
262
263int
264pidfile_remove(struct pidfh *pfh)
265{
266
267 return (_pidfile_remove(pfh, 1));
268}
269
270int
271pidfile_fileno(struct pidfh *pfh)
272{
273
274 if (pfh == NULL || pfh->pf_fd == -1) {
275 errno = EDOOFUS;
276 return (-1);
277 }
278 return (pfh->pf_fd);
279}