2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/capsicum.h>
53 char pf_dir[MAXPATHLEN + 1];
54 char pf_filename[MAXPATHLEN + 1];
59 static int _pidfile_remove(struct pidfh *pfh, int freeit);
62 pidfile_verify(const struct pidfh *pfh)
66 if (pfh == NULL || pfh->pf_fd == -1)
69 * Check remembered descriptor.
71 if (fstat(pfh->pf_fd, &sb) == -1)
73 if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
79 pidfile_read_impl(int dirfd, const char *filename, pid_t *pidptr)
81 char buf[16], *endptr;
84 fd = openat(dirfd, filename, O_RDONLY | O_CLOEXEC);
88 i = read(fd, buf, sizeof(buf) - 1);
89 error = errno; /* Remember errno in case close() wants to change it. */
97 *pidptr = strtol(buf, &endptr, 10);
98 if (endptr != &buf[i])
105 pidfile_read(int dirfd, const char *filename, pid_t *pidptr)
107 struct timespec rqtp;
112 rqtp.tv_nsec = 5000000;
114 errno = pidfile_read_impl(dirfd, filename, pidptr);
115 if (errno != EAGAIN || --count == 0)
125 pidfile_open(const char *pathp, mode_t mode, pid_t *pidptr)
127 char path[MAXPATHLEN];
130 int error, fd, dirfd, dirlen, filenamelen;
131 cap_rights_t caprights;
133 pfh = malloc(sizeof(*pfh));
138 dirlen = snprintf(pfh->pf_dir, sizeof(pfh->pf_dir),
140 filenamelen = snprintf(pfh->pf_filename,
141 sizeof(pfh->pf_filename), "%s.pid", getprogname());
143 if (strlcpy(path, pathp, sizeof(path)) >= sizeof(path)) {
145 errno = ENAMETOOLONG;
148 dirlen = strlcpy(pfh->pf_dir, dirname(path),
149 sizeof(pfh->pf_dir));
150 (void)strlcpy(path, pathp, sizeof(path));
151 filenamelen = strlcpy(pfh->pf_filename, basename(path),
152 sizeof(pfh->pf_filename));
155 if (dirlen >= (int)sizeof(pfh->pf_dir) ||
156 filenamelen >= (int)sizeof(pfh->pf_filename)) {
158 errno = ENAMETOOLONG;
162 dirfd = open(pfh->pf_dir, O_CLOEXEC | O_DIRECTORY | O_NONBLOCK);
171 * Open the PID file and obtain exclusive lock.
172 * We truncate PID file here only to remove old PID immediately,
173 * PID file will be truncated again in pidfile_write(), so
174 * pidfile_write() can be called multiple times.
176 fd = flopenat(dirfd, pfh->pf_filename,
177 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NONBLOCK, mode);
179 if (errno == EWOULDBLOCK) {
180 if (pidptr == NULL) {
183 errno = pidfile_read(dirfd,
184 pfh->pf_filename, pidptr);
185 if (errno == 0 || errno == EAGAIN)
197 * Remember file information, so in pidfile_write() we are sure we write
198 * to the proper descriptor.
200 if (fstat(fd, &sb) == -1) {
204 if (cap_rights_limit(dirfd,
205 cap_rights_init(&caprights, CAP_UNLINKAT)) < 0 && errno != ENOSYS) {
209 if (cap_rights_limit(fd, cap_rights_init(&caprights, CAP_PWRITE,
210 CAP_FSTAT, CAP_FTRUNCATE, CAP_EVENT)) < 0 &&
215 pfh->pf_dirfd = dirfd;
217 pfh->pf_dev = sb.st_dev;
218 pfh->pf_ino = sb.st_ino;
224 unlinkat(dirfd, pfh->pf_filename, 0);
233 pidfile_write(struct pidfh *pfh)
239 * Check remembered descriptor, so we don't overwrite some other
240 * file if pidfile was closed and descriptor reused.
242 errno = pidfile_verify(pfh);
245 * Don't close descriptor, because we are not sure if it's ours.
252 * Truncate PID file, so multiple calls of pidfile_write() are allowed.
254 if (ftruncate(fd, 0) == -1) {
256 _pidfile_remove(pfh, 0);
261 snprintf(pidstr, sizeof(pidstr), "%u", getpid());
262 if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
264 _pidfile_remove(pfh, 0);
273 pidfile_close(struct pidfh *pfh)
277 error = pidfile_verify(pfh);
283 if (close(pfh->pf_fd) == -1)
285 if (close(pfh->pf_dirfd) == -1 && error == 0)
297 _pidfile_remove(struct pidfh *pfh, int freeit)
301 error = pidfile_verify(pfh);
307 if (funlinkat(pfh->pf_dirfd, pfh->pf_filename, pfh->pf_fd, 0) == -1) {
308 if (errno == EDEADLK)
312 if (close(pfh->pf_fd) == -1 && error == 0)
314 if (close(pfh->pf_dirfd) == -1 && error == 0)
328 pidfile_remove(struct pidfh *pfh)
331 return (_pidfile_remove(pfh, 1));
335 pidfile_fileno(const struct pidfh *pfh)
338 if (pfh == NULL || pfh->pf_fd == -1) {
346 pidfile_signal(const char *pathp, int sig, pid_t *pidptr)
351 fd = flopenat(AT_FDCWD, pathp,
352 O_RDONLY | O_CLOEXEC | O_NONBLOCK);
355 * The file exists but is not locked,
356 * so the daemon is dead. Nothing to do.
362 if (errno != EWOULDBLOCK) {
365 errno = pidfile_read(AT_FDCWD, pathp, &pid);
369 * Refuse to send broadcast or group signals, this has
370 * happened due to the bugs in pidfile(3).