1231200Smm/*-
2231200Smm * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3231200Smm * All rights reserved.
4231200Smm *
5231200Smm * Redistribution and use in source and binary forms, with or without
6231200Smm * modification, are permitted provided that the following conditions
7231200Smm * are met:
8231200Smm * 1. Redistributions of source code must retain the above copyright
9231200Smm *    notice, this list of conditions and the following disclaimer.
10231200Smm * 2. Redistributions in binary form must reproduce the above copyright
11231200Smm *    notice, this list of conditions and the following disclaimer in the
12231200Smm *    documentation and/or other materials provided with the distribution.
13231200Smm *
14231200Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15231200Smm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16231200Smm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17231200Smm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18231200Smm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19231200Smm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20231200Smm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21231200Smm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22231200Smm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23231200Smm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24231200Smm * SUCH DAMAGE.
25231200Smm *
26231200Smm * Derived from FreeBSD head/lib/libutil/pidfile.c r231938
27231200Smm */
28231200Smm
29231200Smm#include <sys/param.h>
30231200Smm#include <sys/file.h>
31231200Smm#include <sys/stat.h>
32231200Smm
33231200Smm#include <stdio.h>
34231200Smm#include <stdlib.h>
35231200Smm#include <unistd.h>
36232153Smm#include <fcntl.h>
37231200Smm#include <string.h>
38231200Smm#include <time.h>
39231200Smm#include <err.h>
40231200Smm#include <errno.h>
41231200Smm
42232153Smm#include "flopen.h"
43231200Smm
44231200Smmstruct pidfh {
45231200Smm	int	pf_fd;
46231200Smm	char	pf_path[MAXPATHLEN + 1];
47231200Smm	dev_t	pf_dev;
48231200Smm	ino_t	pf_ino;
49231200Smm};
50231200Smm
51232153Smmstatic int _pidfile_remove(struct pidfh *pfh, int freeit);
52232153Smm
53231200Smmstatic int
54232153Smmpidfile_verify(const struct pidfh *pfh)
55232153Smm{
56231200Smm	struct stat sb;
57231200Smm
58231200Smm	if (pfh == NULL || pfh->pf_fd == -1)
59232153Smm		return (EINVAL);
60232153Smm	/*
61231200Smm	 * Check remembered descriptor.
62232153Smm	 */
63232153Smm	if (fstat(pfh->pf_fd, &sb) == -1)
64231200Smm		return (errno);
65232153Smm	if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
66231200Smm		return (EINVAL);
67232153Smm	return (0);
68232153Smm}
69232153Smm
70232153Smmstatic int
71231200Smmpidfile_read(const char *path, pid_t *pidptr)
72232153Smm{
73232153Smm	char buf[16], *endptr;
74232153Smm	int error, fd, i;
75232153Smm
76231200Smm	fd = open(path, O_RDONLY);
77232153Smm	if (fd == -1)
78232153Smm		return (errno);
79232153Smm
80232153Smm	i = read(fd, buf, sizeof(buf) - 1);
81231200Smm	error = errno;	/* Remember errno in case close() wants to change it. */
82232153Smm	close(fd);
83232153Smm	if (i == -1)
84232153Smm		return (error);
85232153Smm	else if (i == 0)
86231200Smm		return (EAGAIN);
87231200Smm	buf[i] = '\0';
88231200Smm
89231200Smm	*pidptr = strtol(buf, &endptr, 10);
90231200Smm	if (endptr != &buf[i])
91232153Smm		return (EINVAL);
92232153Smm
93232153Smm	return (0);
94232153Smm}
95231200Smm
96232153Smmstatic struct pidfh *
97232153Smmpidfile_open(const char *path, mode_t mode, pid_t *pidptr)
98232153Smm{
99232153Smm	struct pidfh *pfh;
100231200Smm	struct stat sb;
101232153Smm	int error, fd, len, count;
102232153Smm	struct timespec rqtp;
103232153Smm
104232153Smm	if (pidptr != NULL)
105232153Smm		*pidptr = -1;
106232153Smm
107232153Smm	if (path == NULL)
108232153Smm		return (NULL);
109231200Smm
110232153Smm	pfh = malloc(sizeof(*pfh));
111232153Smm	if (pfh == NULL)
112232153Smm		return (NULL);
113232153Smm
114232153Smm	len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
115232153Smm	    "%s", path);
116232153Smm	if (len >= (int)sizeof(pfh->pf_path)) {
117232153Smm		free(pfh);
118232153Smm		errno = ENAMETOOLONG;
119248616Smm		return (NULL);
120231200Smm	}
121231200Smm
122231200Smm	/*
123231200Smm	 * Open the PID file and obtain exclusive lock.
124231200Smm	 * We truncate PID file here only to remove old PID immediatelly,
125231200Smm	 * PID file will be truncated again in pidfile_write(), so
126231200Smm	 * 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
185static int
186pidfile_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
225static int
226pidfile_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
246static 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
274static int
275pidfile_remove(struct pidfh *pfh)
276{
277
278	return (_pidfile_remove(pfh, 1));
279}
280
281#if 0
282static int
283pidfile_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