]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libbsd/lib/contrib/src/pidfile.c
update
[l4.git] / l4 / pkg / libbsd / lib / contrib / src / 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 <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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 <time.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <libutil.h>
43
44 static int _pidfile_remove(struct pidfh *pfh, int freeit);
45
46 static int
47 pidfile_verify(struct pidfh *pfh)
48 {
49         struct stat sb;
50
51         if (pfh == NULL || pfh->pf_fd == -1)
52                 return (EINVAL);
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 (EINVAL);
60         return (0);
61 }
62
63 static int
64 pidfile_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         else if (i == 0)
79                 return (EAGAIN);
80         buf[i] = '\0';
81
82         *pidptr = strtol(buf, &endptr, 10);
83         if (endptr != &buf[i])
84                 return (EINVAL);
85
86         return (0);
87 }
88
89 struct pidfh *
90 pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
91 {
92         struct pidfh *pfh;
93         struct stat sb;
94         int error, fd, len, count;
95         struct timespec rqtp;
96
97         pfh = malloc(sizeof(*pfh));
98         if (pfh == NULL)
99                 return (NULL);
100
101         if (path == NULL) {
102                 len = asprintf(&pfh->pf_path, "/var/run/%s.pid", getprogname());
103                 if (len < 0) {
104                         free(pfh);
105                         return (NULL);
106                 }
107         } else
108                 pfh->pf_path = strdup(path);
109
110         /*
111          * Open the PID file and obtain exclusive lock.
112          * We truncate PID file here only to remove old PID immediately,
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->pf_path);
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                 free(pfh->pf_path);
146                 close(fd);
147                 free(pfh);
148                 errno = error;
149                 return (NULL);
150         }
151
152         pfh->pf_fd = fd;
153         pfh->pf_dev = sb.st_dev;
154         pfh->pf_ino = sb.st_ino;
155
156         return (pfh);
157 }
158
159 int
160 pidfile_write(struct pidfh *pfh)
161 {
162         char pidstr[16];
163         int error, fd;
164
165         /*
166          * Check remembered descriptor, so we don't overwrite some other
167          * file if pidfile was closed and descriptor reused.
168          */
169         errno = pidfile_verify(pfh);
170         if (errno != 0) {
171                 /*
172                  * Don't close descriptor, because we are not sure if it's ours.
173                  */
174                 return (-1);
175         }
176         fd = pfh->pf_fd;
177
178         /*
179          * Truncate PID file, so multiple calls of pidfile_write() are allowed.
180          */
181         if (ftruncate(fd, 0) == -1) {
182                 error = errno;
183                 _pidfile_remove(pfh, 0);
184                 errno = error;
185                 return (-1);
186         }
187
188         snprintf(pidstr, sizeof(pidstr), "%u", getpid());
189         if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
190                 error = errno;
191                 _pidfile_remove(pfh, 0);
192                 errno = error;
193                 return (-1);
194         }
195
196         return (0);
197 }
198
199 int
200 pidfile_close(struct pidfh *pfh)
201 {
202         int error;
203
204         error = pidfile_verify(pfh);
205         if (error != 0) {
206                 errno = error;
207                 return (-1);
208         }
209
210         if (close(pfh->pf_fd) == -1)
211                 error = errno;
212         free(pfh->pf_path);
213         free(pfh);
214         if (error != 0) {
215                 errno = error;
216                 return (-1);
217         }
218         return (0);
219 }
220
221 static int
222 _pidfile_remove(struct pidfh *pfh, int freeit)
223 {
224         int error;
225
226         error = pidfile_verify(pfh);
227         if (error != 0) {
228                 errno = error;
229                 return (-1);
230         }
231
232         if (unlink(pfh->pf_path) == -1)
233                 error = errno;
234         if (close(pfh->pf_fd) == -1) {
235                 if (error == 0)
236                         error = errno;
237         }
238         if (freeit) {
239                 free(pfh->pf_path);
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 }