2 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * Derived from FreeBSD head/lib/libutil/pidfile.c r231938
27 * $P4: //depot/projects/trustedbsd/openbsm/compat/pidfile.h#1 $
30 #include <sys/param.h>
47 char pf_path[MAXPATHLEN + 1];
52 static int _pidfile_remove(struct pidfh *pfh, int freeit);
55 pidfile_verify(const struct pidfh *pfh)
59 if (pfh == NULL || pfh->pf_fd == -1)
62 * Check remembered descriptor.
64 if (fstat(pfh->pf_fd, &sb) == -1)
66 if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
72 pidfile_read(const char *path, pid_t *pidptr)
74 char buf[16], *endptr;
77 fd = open(path, O_RDONLY);
81 i = read(fd, buf, sizeof(buf) - 1);
82 error = errno; /* Remember errno in case close() wants to change it. */
90 *pidptr = strtol(buf, &endptr, 10);
91 if (endptr != &buf[i])
98 pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
102 int error, fd, len, count;
103 struct timespec rqtp;
111 pfh = malloc(sizeof(*pfh));
115 len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
117 if (len >= (int)sizeof(pfh->pf_path)) {
119 errno = ENAMETOOLONG;
124 * Open the PID file and obtain exclusive lock.
125 * We truncate PID file here only to remove old PID immediatelly,
126 * PID file will be truncated again in pidfile_write(), so
127 * pidfile_write() can be called multiple times.
129 fd = flopen(pfh->pf_path,
131 O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK | O_CLOEXEC, mode);
133 O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
136 if (errno == EWOULDBLOCK && pidptr != NULL) {
139 rqtp.tv_nsec = 5000000;
141 errno = pidfile_read(pfh->pf_path, pidptr);
142 if (errno != EAGAIN || --count == 0)
148 if (errno == 0 || errno == EAGAIN)
156 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
158 unlink(pfh->pf_path);
167 * Remember file information, so in pidfile_write() we are sure we write
168 * to the proper descriptor.
170 if (fstat(fd, &sb) == -1) {
172 unlink(pfh->pf_path);
180 pfh->pf_dev = sb.st_dev;
181 pfh->pf_ino = sb.st_ino;
187 pidfile_write(struct pidfh *pfh)
193 * Check remembered descriptor, so we don't overwrite some other
194 * file if pidfile was closed and descriptor reused.
196 errno = pidfile_verify(pfh);
199 * Don't close descriptor, because we are not sure if it's ours.
206 * Truncate PID file, so multiple calls of pidfile_write() are allowed.
208 if (ftruncate(fd, 0) == -1) {
210 _pidfile_remove(pfh, 0);
215 snprintf(pidstr, sizeof(pidstr), "%u", getpid());
216 if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
218 _pidfile_remove(pfh, 0);
227 pidfile_close(struct pidfh *pfh)
231 error = pidfile_verify(pfh);
237 if (close(pfh->pf_fd) == -1)
248 _pidfile_remove(struct pidfh *pfh, int freeit)
252 error = pidfile_verify(pfh);
258 if (unlink(pfh->pf_path) == -1)
260 if (close(pfh->pf_fd) == -1) {
276 pidfile_remove(struct pidfh *pfh)
279 return (_pidfile_remove(pfh, 1));
284 pidfile_fileno(const struct pidfh *pfh)
287 if (pfh == NULL || pfh->pf_fd == -1) {