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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: src/lib/libutil/pidfile.c,v 1.3 2006/06/23 01:42:03 brian Exp $");
30 #include <sys/param.h>
43 static int _pidfile_remove(struct pidfh
*pfh
, int freeit
);
46 pidfile_verify(struct pidfh
*pfh
)
50 if (pfh
== NULL
|| pfh
->pf_fd
== -1)
53 * Check remembered descriptor.
55 if (fstat(pfh
->pf_fd
, &sb
) == -1)
57 if (sb
.st_dev
!= pfh
->pf_dev
|| sb
.st_ino
!= pfh
->pf_ino
)
63 pidfile_read(const char *path
, pid_t
*pidptr
)
65 char buf
[16], *endptr
;
68 fd
= open(path
, O_RDONLY
);
72 i
= read(fd
, buf
, sizeof(buf
) - 1);
73 error
= errno
; /* Remember errno in case close() wants to change it. */
79 *pidptr
= strtol(buf
, &endptr
, 10);
80 if (endptr
!= &buf
[i
])
87 pidfile_open(const char *path
, mode_t mode
, pid_t
*pidptr
)
93 pfh
= malloc(sizeof(*pfh
));
98 snprintf(pfh
->pf_path
, sizeof(pfh
->pf_path
), "/var/run/%s.pid",
101 strlcpy(pfh
->pf_path
, path
, sizeof(pfh
->pf_path
));
103 if (strlen(pfh
->pf_path
) == sizeof(pfh
->pf_path
) - 1) {
105 errno
= ENAMETOOLONG
;
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.
115 fd
= open(pfh
->pf_path
, O_WRONLY
| O_CREAT
| O_NONBLOCK
, mode
);
118 lock
.l_type
= F_WRLCK
;
119 lock
.l_whence
= SEEK_SET
;
123 if (fcntl(fd
, F_SETLK
, &lock
) == -1) {
130 if (errno
== EWOULDBLOCK
&& pidptr
!= NULL
) {
131 errno
= pidfile_read(pfh
->pf_path
, pidptr
);
142 * Remember file information, so in pidfile_write() we are sure we write
143 * to the proper descriptor.
145 if (fstat(fd
, &sb
) == -1) {
147 unlink(pfh
->pf_path
);
155 pfh
->pf_dev
= sb
.st_dev
;
156 pfh
->pf_ino
= sb
.st_ino
;
162 pidfile_write(struct pidfh
*pfh
)
168 * Check remembered descriptor, so we don't overwrite some other
169 * file if pidfile was closed and descriptor reused.
171 errno
= pidfile_verify(pfh
);
174 * Don't close descriptor, because we are not sure if it's ours.
181 * Truncate PID file, so multiple calls of pidfile_write() are allowed.
183 if (ftruncate(fd
, 0) == -1) {
185 _pidfile_remove(pfh
, 0);
190 snprintf(pidstr
, sizeof(pidstr
), "%lu", getpid());
191 if (pwrite(fd
, pidstr
, strlen(pidstr
), 0) != (ssize_t
)strlen(pidstr
)) {
193 _pidfile_remove(pfh
, 0);
202 pidfile_close(struct pidfh
*pfh
)
206 error
= pidfile_verify(pfh
);
212 if (close(pfh
->pf_fd
) == -1)
223 _pidfile_remove(struct pidfh
*pfh
, int freeit
)
227 error
= pidfile_verify(pfh
);
233 if (unlink(pfh
->pf_path
) == -1)
235 if (flock(pfh
->pf_fd
, LOCK_UN
) == -1) {
239 if (close(pfh
->pf_fd
) == -1) {
255 pidfile_remove(struct pidfh
*pfh
)
258 return (_pidfile_remove(pfh
, 1));