Deleted Added
full compact
pidfile.c (171705) pidfile.c (171706)
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>
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 171705 2007-08-03 06:32:45Z des $");
28__FBSDID("$FreeBSD: head/lib/libutil/pidfile.c 171706 2007-08-03 09:20:28Z des $");
29
30#include <sys/param.h>
31#include <sys/file.h>
32#include <sys/stat.h>
33
29
30#include <sys/param.h>
31#include <sys/file.h>
32#include <sys/stat.h>
33
34#include <errno.h>
35#include <fcntl.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <unistd.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <unistd.h>
37#include <fcntl.h>
39#include <string.h>
38#include <string.h>
40
39#include <err.h>
40#include <errno.h>
41#include <libutil.h>
42
43static int _pidfile_remove(struct pidfh *pfh, int freeit);
44
45static int
46pidfile_verify(struct pidfh *pfh)
47{
48 struct stat sb;
49
50 if (pfh == NULL || pfh->pf_fd == -1)
51 return (EDOOFUS);
52 /*
53 * Check remembered descriptor.
54 */
55 if (fstat(pfh->pf_fd, &sb) == -1)
56 return (errno);
57 if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
58 return (EDOOFUS);
59 return (0);
60}
61
62static int
63pidfile_read(const char *path, pid_t *pidptr)
64{
65 char buf[16], *endptr;
66 int error, fd, i;
67
68 fd = open(path, O_RDONLY);
69 if (fd == -1)
70 return (errno);
71
72 i = read(fd, buf, sizeof(buf) - 1);
73 error = errno; /* Remember errno in case close() wants to change it. */
74 close(fd);
75 if (i == -1)
76 return (error);
77 buf[i] = '\0';
78
79 *pidptr = strtol(buf, &endptr, 10);
80 if (endptr != &buf[i])
81 return (EINVAL);
82
83 return (0);
84}
85
86struct pidfh *
87pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
88{
89 struct pidfh *pfh;
90 struct stat sb;
91 int error, fd, len;
92
93 pfh = malloc(sizeof(*pfh));
94 if (pfh == NULL)
95 return (NULL);
96
97 if (path == NULL)
98 len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
99 "/var/run/%s.pid", getprogname());
100 else
101 len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
102 "%s", path);
103 if (len >= (int)sizeof(pfh->pf_path)) {
104 free(pfh);
105 errno = ENAMETOOLONG;
106 return (NULL);
107 }
108
109 /*
110 * Open the PID file and obtain exclusive lock.
111 * We truncate PID file here only to remove old PID immediatelly,
112 * PID file will be truncated again in pidfile_write(), so
113 * pidfile_write() can be called multiple times.
114 */
115 fd = flopen(pfh->pf_path,
116 O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
117 if (fd == -1) {
118 if (errno == EWOULDBLOCK && pidptr != NULL) {
119 errno = pidfile_read(pfh->pf_path, pidptr);
120 if (errno == 0)
121 errno = EEXIST;
122 }
123 free(pfh);
124 return (NULL);
125 }
126 /*
127 * Remember file information, so in pidfile_write() we are sure we write
128 * to the proper descriptor.
129 */
130 if (fstat(fd, &sb) == -1) {
131 error = errno;
132 unlink(pfh->pf_path);
133 close(fd);
134 free(pfh);
135 errno = error;
136 return (NULL);
137 }
138
139 pfh->pf_fd = fd;
140 pfh->pf_dev = sb.st_dev;
141 pfh->pf_ino = sb.st_ino;
142
143 return (pfh);
144}
145
146int
147pidfile_write(struct pidfh *pfh)
148{
149 char pidstr[16];
150 int error, fd;
151
152 /*
153 * Check remembered descriptor, so we don't overwrite some other
154 * file if pidfile was closed and descriptor reused.
155 */
156 errno = pidfile_verify(pfh);
157 if (errno != 0) {
158 /*
159 * Don't close descriptor, because we are not sure if it's ours.
160 */
161 return (-1);
162 }
163 fd = pfh->pf_fd;
164
165 /*
166 * Truncate PID file, so multiple calls of pidfile_write() are allowed.
167 */
168 if (ftruncate(fd, 0) == -1) {
169 error = errno;
170 _pidfile_remove(pfh, 0);
171 errno = error;
172 return (-1);
173 }
174
175 snprintf(pidstr, sizeof(pidstr), "%u", getpid());
176 if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
177 error = errno;
178 _pidfile_remove(pfh, 0);
179 errno = error;
180 return (-1);
181 }
182
183 return (0);
184}
185
186int
187pidfile_close(struct pidfh *pfh)
188{
189 int error;
190
191 error = pidfile_verify(pfh);
192 if (error != 0) {
193 errno = error;
194 return (-1);
195 }
196
197 if (close(pfh->pf_fd) == -1)
198 error = errno;
199 free(pfh);
200 if (error != 0) {
201 errno = error;
202 return (-1);
203 }
204 return (0);
205}
206
207static int
208_pidfile_remove(struct pidfh *pfh, int freeit)
209{
41#include <libutil.h>
42
43static int _pidfile_remove(struct pidfh *pfh, int freeit);
44
45static int
46pidfile_verify(struct pidfh *pfh)
47{
48 struct stat sb;
49
50 if (pfh == NULL || pfh->pf_fd == -1)
51 return (EDOOFUS);
52 /*
53 * Check remembered descriptor.
54 */
55 if (fstat(pfh->pf_fd, &sb) == -1)
56 return (errno);
57 if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
58 return (EDOOFUS);
59 return (0);
60}
61
62static int
63pidfile_read(const char *path, pid_t *pidptr)
64{
65 char buf[16], *endptr;
66 int error, fd, i;
67
68 fd = open(path, O_RDONLY);
69 if (fd == -1)
70 return (errno);
71
72 i = read(fd, buf, sizeof(buf) - 1);
73 error = errno; /* Remember errno in case close() wants to change it. */
74 close(fd);
75 if (i == -1)
76 return (error);
77 buf[i] = '\0';
78
79 *pidptr = strtol(buf, &endptr, 10);
80 if (endptr != &buf[i])
81 return (EINVAL);
82
83 return (0);
84}
85
86struct pidfh *
87pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
88{
89 struct pidfh *pfh;
90 struct stat sb;
91 int error, fd, len;
92
93 pfh = malloc(sizeof(*pfh));
94 if (pfh == NULL)
95 return (NULL);
96
97 if (path == NULL)
98 len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
99 "/var/run/%s.pid", getprogname());
100 else
101 len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
102 "%s", path);
103 if (len >= (int)sizeof(pfh->pf_path)) {
104 free(pfh);
105 errno = ENAMETOOLONG;
106 return (NULL);
107 }
108
109 /*
110 * Open the PID file and obtain exclusive lock.
111 * We truncate PID file here only to remove old PID immediatelly,
112 * PID file will be truncated again in pidfile_write(), so
113 * pidfile_write() can be called multiple times.
114 */
115 fd = flopen(pfh->pf_path,
116 O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
117 if (fd == -1) {
118 if (errno == EWOULDBLOCK && pidptr != NULL) {
119 errno = pidfile_read(pfh->pf_path, pidptr);
120 if (errno == 0)
121 errno = EEXIST;
122 }
123 free(pfh);
124 return (NULL);
125 }
126 /*
127 * Remember file information, so in pidfile_write() we are sure we write
128 * to the proper descriptor.
129 */
130 if (fstat(fd, &sb) == -1) {
131 error = errno;
132 unlink(pfh->pf_path);
133 close(fd);
134 free(pfh);
135 errno = error;
136 return (NULL);
137 }
138
139 pfh->pf_fd = fd;
140 pfh->pf_dev = sb.st_dev;
141 pfh->pf_ino = sb.st_ino;
142
143 return (pfh);
144}
145
146int
147pidfile_write(struct pidfh *pfh)
148{
149 char pidstr[16];
150 int error, fd;
151
152 /*
153 * Check remembered descriptor, so we don't overwrite some other
154 * file if pidfile was closed and descriptor reused.
155 */
156 errno = pidfile_verify(pfh);
157 if (errno != 0) {
158 /*
159 * Don't close descriptor, because we are not sure if it's ours.
160 */
161 return (-1);
162 }
163 fd = pfh->pf_fd;
164
165 /*
166 * Truncate PID file, so multiple calls of pidfile_write() are allowed.
167 */
168 if (ftruncate(fd, 0) == -1) {
169 error = errno;
170 _pidfile_remove(pfh, 0);
171 errno = error;
172 return (-1);
173 }
174
175 snprintf(pidstr, sizeof(pidstr), "%u", getpid());
176 if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
177 error = errno;
178 _pidfile_remove(pfh, 0);
179 errno = error;
180 return (-1);
181 }
182
183 return (0);
184}
185
186int
187pidfile_close(struct pidfh *pfh)
188{
189 int error;
190
191 error = pidfile_verify(pfh);
192 if (error != 0) {
193 errno = error;
194 return (-1);
195 }
196
197 if (close(pfh->pf_fd) == -1)
198 error = errno;
199 free(pfh);
200 if (error != 0) {
201 errno = error;
202 return (-1);
203 }
204 return (0);
205}
206
207static int
208_pidfile_remove(struct pidfh *pfh, int freeit)
209{
210 struct flock lock;
211 int error;
212
210 int error;
211
213 lock.l_type = F_UNLCK;
214 lock.l_start = 0;
215 lock.l_whence = SEEK_SET;
216 lock.l_len = 0;
217
218 error = pidfile_verify(pfh);
219 if (error != 0) {
220 errno = error;
221 return (-1);
222 }
223
224 if (unlink(pfh->pf_path) == -1)
225 error = errno;
212 error = pidfile_verify(pfh);
213 if (error != 0) {
214 errno = error;
215 return (-1);
216 }
217
218 if (unlink(pfh->pf_path) == -1)
219 error = errno;
226 if (fcntl(pfh->pf_fd, F_SETLK, &lock) == -1) {
220 if (flock(pfh->pf_fd, LOCK_UN) == -1) {
227 if (error == 0)
228 error = errno;
229 }
230 if (close(pfh->pf_fd) == -1) {
231 if (error == 0)
232 error = errno;
233 }
234 if (freeit)
235 free(pfh);
236 else
237 pfh->pf_fd = -1;
238 if (error != 0) {
239 errno = error;
240 return (-1);
241 }
242 return (0);
243}
244
245int
246pidfile_remove(struct pidfh *pfh)
247{
248
249 return (_pidfile_remove(pfh, 1));
250}
221 if (error == 0)
222 error = errno;
223 }
224 if (close(pfh->pf_fd) == -1) {
225 if (error == 0)
226 error = errno;
227 }
228 if (freeit)
229 free(pfh);
230 else
231 pfh->pf_fd = -1;
232 if (error != 0) {
233 errno = error;
234 return (-1);
235 }
236 return (0);
237}
238
239int
240pidfile_remove(struct pidfh *pfh)
241{
242
243 return (_pidfile_remove(pfh, 1));
244}