]> gitweb @ CieloNegro.org - daemon.git/blob - pidfile.c
Removed unportable dependency on <err.h>.
[daemon.git] / pidfile.c
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 "pidfile.h"
28 #include "flopen.h"
29
30 #include <sys/cdefs.h>
31 #include <sys/file.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <time.h>
39 #include <errno.h>
40
41 static int _pidfile_remove(struct pidfh *pfh, int freeit);
42
43 static int
44 pidfile_verify(struct pidfh *pfh)
45 {
46         struct stat sb;
47
48         if (pfh == NULL || pfh->pf_fd == -1)
49                 return (EINVAL);
50         /*
51          * Check remembered descriptor.
52          */
53         if (fstat(pfh->pf_fd, &sb) == -1)
54                 return (errno);
55         if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
56                 return (EINVAL);
57         return (0);
58 }
59
60 static int
61 pidfile_read(const char *path, pid_t *pidptr)
62 {
63         char buf[16], *endptr;
64         int error, fd, i;
65
66         fd = open(path, O_RDONLY);
67         if (fd == -1)
68                 return (errno);
69
70         i = read(fd, buf, sizeof(buf) - 1);
71         error = errno;  /* Remember errno in case close() wants to change it. */
72         close(fd);
73         if (i == -1)
74                 return (error);
75         else if (i == 0)
76                 return (EAGAIN);
77         buf[i] = '\0';
78
79         *pidptr = strtol(buf, &endptr, 10);
80         if (endptr != &buf[i])
81                 return (EINVAL);
82
83         return (0);
84 }
85
86 struct pidfh *
87 pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
88 {
89         struct pidfh *pfh;
90         struct stat sb;
91         int error, fd, len, count;
92         struct timespec rqtp;
93
94         pfh = malloc(sizeof(*pfh));
95         if (pfh == NULL)
96                 return (NULL);
97
98         if (path == NULL)
99                 len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
100                     "/var/run/%s.pid", getprogname());
101         else
102                 len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
103                     "%s", path);
104         if (len >= (int)sizeof(pfh->pf_path)) {
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 = flopen(pfh->pf_path,
117             O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
118         if (fd == -1) {
119                 count = 0;
120                 rqtp.tv_sec = 0;
121                 rqtp.tv_nsec = 5000000;
122                 if (errno == EWOULDBLOCK && pidptr != NULL) {
123                 again:
124                         errno = pidfile_read(pfh->pf_path, pidptr);
125                         if (errno == 0)
126                                 errno = EEXIST;
127                         else if (errno == EAGAIN) {
128                                 if (++count <= 3) {
129                                         nanosleep(&rqtp, 0);
130                                         goto again;
131                                 }
132                         }
133                 }
134                 free(pfh);
135                 return (NULL);
136         }
137         /*
138          * Remember file information, so in pidfile_write() we are sure we write
139          * to the proper descriptor.
140          */
141         if (fstat(fd, &sb) == -1) {
142                 error = errno;
143                 unlink(pfh->pf_path);
144                 close(fd);
145                 free(pfh);
146                 errno = error;
147                 return (NULL);
148         }
149
150         pfh->pf_fd = fd;
151         pfh->pf_dev = sb.st_dev;
152         pfh->pf_ino = sb.st_ino;
153
154         return (pfh);
155 }
156
157 int
158 pidfile_write(struct pidfh *pfh)
159 {
160         char pidstr[16];
161         int error, fd;
162
163         /*
164          * Check remembered descriptor, so we don't overwrite some other
165          * file if pidfile was closed and descriptor reused.
166          */
167         errno = pidfile_verify(pfh);
168         if (errno != 0) {
169                 /*
170                  * Don't close descriptor, because we are not sure if it's ours.
171                  */
172                 return (-1);
173         }
174         fd = pfh->pf_fd;
175
176         /*
177          * Truncate PID file, so multiple calls of pidfile_write() are allowed.
178          */
179         if (ftruncate(fd, 0) == -1) {
180                 error = errno;
181                 _pidfile_remove(pfh, 0);
182                 errno = error;
183                 return (-1);
184         }
185
186         snprintf(pidstr, sizeof(pidstr), "%u", getpid());
187         if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
188                 error = errno;
189                 _pidfile_remove(pfh, 0);
190                 errno = error;
191                 return (-1);
192         }
193
194         return (0);
195 }
196
197 int
198 pidfile_close(struct pidfh *pfh)
199 {
200         int error;
201
202         error = pidfile_verify(pfh);
203         if (error != 0) {
204                 errno = error;
205                 return (-1);
206         }
207
208         if (close(pfh->pf_fd) == -1)
209                 error = errno;
210         free(pfh);
211         if (error != 0) {
212                 errno = error;
213                 return (-1);
214         }
215         return (0);
216 }
217
218 static int
219 _pidfile_remove(struct pidfh *pfh, int freeit)
220 {
221         int error;
222
223         error = pidfile_verify(pfh);
224         if (error != 0) {
225                 errno = error;
226                 return (-1);
227         }
228
229         if (unlink(pfh->pf_path) == -1)
230                 error = errno;
231         if (flock(pfh->pf_fd, LOCK_UN) == -1) {
232                 if (error == 0)
233                         error = errno;
234         }
235         if (close(pfh->pf_fd) == -1) {
236                 if (error == 0)
237                         error = errno;
238         }
239         if (freeit)
240                 free(pfh);
241         else
242                 pfh->pf_fd = -1;
243         if (error != 0) {
244                 errno = error;
245                 return (-1);
246         }
247         return (0);
248 }
249
250 int
251 pidfile_remove(struct pidfh *pfh)
252 {
253
254         return (_pidfile_remove(pfh, 1));
255 }