pidfile.c revision 157671
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 157671 2006-04-11 23:10:02Z jmg $");
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 <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	int fd;
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	buf[i] = '\0';
79
80	*pidptr = strtol(buf, &endptr, 10);
81	if (endptr != &buf[i])
82		return (EINVAL);
83
84	return (0);
85}
86
87struct pidfh *
88pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
89{
90	struct pidfh *pfh;
91	struct stat sb;
92	int error, fd;
93
94	pfh = malloc(sizeof(*pfh));
95	if (pfh == NULL)
96		return (NULL);
97
98	if (path == NULL) {
99		snprintf(pfh->pf_path, sizeof(pfh->pf_path), "/var/run/%s.pid",
100		    getprogname());
101	} else {
102		strlcpy(pfh->pf_path, path, sizeof(pfh->pf_path));
103	}
104	if (strlen(pfh->pf_path) == sizeof(pfh->pf_path) - 1) {
105		free(pfh);
106		errno = ENAMETOOLONG;
107		return (NULL);
108	}
109
110	/*
111	 * Open the PID file and obtain exclusive lock.
112	 * We truncate PID file here only to remove old PID immediatelly,
113	 * PID file will be truncated again in pidfile_write(), so
114	 * pidfile_write() can be called multiple times.
115	 */
116	fd = open(pfh->pf_path,
117	    O_WRONLY | O_CREAT | O_EXLOCK | O_TRUNC | O_NONBLOCK, mode);
118	if (fd == -1) {
119		if (errno == EWOULDBLOCK && pidptr != NULL) {
120			errno = pidfile_read(pfh->pf_path, pidptr);
121			if (errno == 0)
122				errno = EEXIST;
123		}
124		free(pfh);
125		return (NULL);
126	}
127	/*
128	 * Remember file information, so in pidfile_write() we are sure we write
129	 * to the proper descriptor.
130	 */
131	if (fstat(fd, &sb) == -1) {
132		error = errno;
133		unlink(pfh->pf_path);
134		close(fd);
135		free(pfh);
136		errno = error;
137		return (NULL);
138	}
139
140	pfh->pf_fd = fd;
141	pfh->pf_dev = sb.st_dev;
142	pfh->pf_ino = sb.st_ino;
143
144	return (pfh);
145}
146
147int
148pidfile_write(struct pidfh *pfh)
149{
150	struct stat sb;
151	char pidstr[16];
152	int error, fd;
153
154	/*
155	 * Check remembered descriptor, so we don't overwrite some other
156	 * file if pidfile was closed and descriptor reused.
157	 */
158	errno = pidfile_verify(pfh);
159	if (errno != 0) {
160		/*
161		 * Don't close descriptor, because we are not sure if it's ours.
162		 */
163		return (-1);
164	}
165	fd = pfh->pf_fd;
166
167	/*
168	 * Truncate PID file, so multiple calls of pidfile_write() are allowed.
169	 */
170	if (ftruncate(fd, 0) == -1) {
171		error = errno;
172		_pidfile_remove(pfh, 0);
173		errno = error;
174		return (-1);
175	}
176
177	snprintf(pidstr, sizeof(pidstr), "%u", getpid());
178	if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
179		error = errno;
180		_pidfile_remove(pfh, 0);
181		errno = error;
182		return (-1);
183	}
184
185	return (0);
186}
187
188int
189pidfile_close(struct pidfh *pfh)
190{
191	int error;
192
193	error = pidfile_verify(pfh);
194	if (error != 0) {
195		errno = error;
196		return (-1);
197	}
198
199	if (close(pfh->pf_fd) == -1)
200		error = errno;
201	free(pfh);
202	if (error != 0) {
203		errno = error;
204		return (-1);
205	}
206	return (0);
207}
208
209static int
210_pidfile_remove(struct pidfh *pfh, int freeit)
211{
212	int error;
213
214	error = pidfile_verify(pfh);
215	if (error != 0) {
216		errno = error;
217		return (-1);
218	}
219
220	if (unlink(pfh->pf_path) == -1)
221		error = errno;
222	if (flock(pfh->pf_fd, LOCK_UN) == -1) {
223		if (error == 0)
224			error = errno;
225	}
226	if (close(pfh->pf_fd) == -1) {
227		if (error == 0)
228			error = errno;
229	}
230	if (freeit)
231		free(pfh);
232	else
233		pfh->pf_fd = -1;
234	if (error != 0) {
235		errno = error;
236		return (-1);
237	}
238	return (0);
239}
240
241int
242pidfile_remove(struct pidfh *pfh)
243{
244
245	return (_pidfile_remove(pfh, 1));
246}
247