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