varnish-cache/lib/libvarnish/vpf.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
 * Derived from:
26
 * $FreeBSD: head/lib/libutil/pidfile.c 184091 2008-10-20 17:41:08Z des $
27
 */
28
29
#include "config.h"
30
31
#include <sys/param.h>
32
#include <sys/file.h>
33
#include <sys/stat.h>
34
35
#include <errno.h>
36
#include <fcntl.h>
37
#include <stdint.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <string.h>
41
#include <unistd.h>
42
43
#include "vdef.h"
44
45
#include "vas.h"        // XXX Flexelint "not used" - but req'ed for assert()
46
#include "vfl.h"
47
#include "vpf.h"
48
49
struct vpf_fh {
50
        int     pf_fd;
51
        char    pf_path[MAXPATHLEN + 1];
52
        dev_t   pf_dev;
53
        ino_t   pf_ino;
54
};
55
56
static int _VPF_Remove(struct vpf_fh *pfh, int freeit);
57
58
static int
59 8544
vpf_verify(const struct vpf_fh *pfh)
60
{
61
        struct stat sb;
62
63 8544
        if (pfh == NULL || pfh->pf_fd == -1)
64 0
                return (EINVAL);
65
        /*
66
         * Check remembered descriptor.
67
         */
68 8544
        if (fstat(pfh->pf_fd, &sb) == -1)
69 0
                return (errno);
70 8544
        if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
71 0
                return (EINVAL);
72 8544
        return (0);
73
}
74
75
int
76 456
VPF_read(const char *path, pid_t *pidptr)
77
{
78
        char buf[16], *endptr;
79
        int error, fd, i;
80
81 456
        fd = open(path, O_RDONLY | O_CLOEXEC);
82 456
        if (fd == -1)
83 447
                return (errno);
84
85 9
        i = read(fd, buf, sizeof(buf) - 1);
86 9
        error = errno;  /* Remember errno in case close() wants to change it. */
87 9
        (void)close(fd);
88 9
        if (i == -1)
89 0
                return (error);
90 9
        else if (i == 0)
91 0
                return (EAGAIN);
92 9
        if (i > 0 && buf[i - 1] == '\n')
93 3
                i--;
94 9
        buf[i] = '\0';
95
96 9
        *pidptr = strtol(buf, &endptr, 10);
97 9
        if (endptr != &buf[i])
98 0
                return (EINVAL);
99
100 9
        return (0);
101
}
102
103
struct vpf_fh *
104 4302
VPF_Open(const char *path, mode_t mode, pid_t *pidptr)
105
{
106
        struct vpf_fh *pfh;
107
        struct stat sb;
108
        int error, fd, len;
109
110 4302
        pfh = malloc(sizeof(*pfh));
111 4302
        if (pfh == NULL)
112 0
                return (NULL);
113
114 4302
        assert(path != NULL);
115 4302
        len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
116
            "%s", path);
117
118 4302
        if (len >= (int)sizeof(pfh->pf_path)) {
119 0
                free(pfh);
120 0
                errno = ENAMETOOLONG;
121 0
                return (NULL);
122
        }
123
124
        /*
125
         * Open the PID file and obtain exclusive lock.
126
         * We truncate PID file here only to remove old PID immediately,
127
         * PID file will be truncated again in VPF_Write(), so
128
         * VPF_Write() can be called multiple times.
129
         */
130 4302
        fd = VFL_Open(pfh->pf_path,
131
            O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NONBLOCK, mode);
132 4302
        if (fd == -1) {
133 9
                if (errno == EWOULDBLOCK && pidptr != NULL) {
134 6
                        errno = VPF_read(pfh->pf_path, pidptr);
135 6
                        if (errno == 0)
136 6
                                errno = EEXIST;
137
                }
138 9
                free(pfh);
139 9
                return (NULL);
140
        }
141
142
        /*
143
         * Remember file information, so in VPF_Write() we are sure we write
144
         * to the proper descriptor.
145
         */
146 4293
        if (fstat(fd, &sb) == -1) {
147 0
                error = errno;
148 0
                (void)unlink(pfh->pf_path);
149 0
                (void)close(fd);
150 0
                free(pfh);
151 0
                errno = error;
152 0
                return (NULL);
153
        }
154
155 4293
        pfh->pf_fd = fd;
156 4293
        pfh->pf_dev = sb.st_dev;
157 4293
        pfh->pf_ino = sb.st_ino;
158
159 4293
        return (pfh);
160
}
161
162
int
163 4275
VPF_Write(struct vpf_fh *pfh)
164
{
165
        char pidstr[16];
166
        int error, fd;
167
168
        /*
169
         * Check remembered descriptor, so we don't overwrite some other
170
         * file if pidfile was closed and descriptor reused.
171
         */
172 4275
        errno = vpf_verify(pfh);
173 4275
        if (errno != 0) {
174
                /*
175
                 * Don't close descriptor, because we are not sure if it's ours.
176
                 */
177 0
                return (-1);
178
        }
179 4275
        fd = pfh->pf_fd;
180
181
        /*
182
         * Truncate PID file, so multiple calls of VPF_Write() are allowed.
183
         */
184 4275
        if (ftruncate(fd, 0) == -1) {
185 0
                error = errno;
186 0
                (void)_VPF_Remove(pfh, 0);
187 0
                errno = error;
188 0
                return (-1);
189
        }
190
191 4275
        error = snprintf(pidstr, sizeof(pidstr), "%jd", (intmax_t)getpid());
192 4275
        assert(error < sizeof pidstr);
193 4275
        if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
194 0
                error = errno;
195 0
                (void)_VPF_Remove(pfh, 0);
196 0
                errno = error;
197 0
                return (-1);
198
        }
199
200 4275
        return (0);
201
}
202
203
int
204 0
VPF_Close(struct vpf_fh *pfh)
205
{
206
        int error;
207
208 0
        error = vpf_verify(pfh);
209 0
        if (error != 0) {
210 0
                errno = error;
211 0
                return (-1);
212
        }
213
214 0
        if (close(pfh->pf_fd) == -1)
215 0
                error = errno;
216 0
        free(pfh);
217 0
        if (error != 0) {
218 0
                errno = error;
219 0
                return (-1);
220
        }
221 0
        return (0);
222
}
223
224
static int
225 4269
_VPF_Remove(struct vpf_fh *pfh, int freeit)
226
{
227
        int error;
228
229 4269
        error = vpf_verify(pfh);
230 4269
        if (error != 0) {
231 0
                errno = error;
232 0
                return (-1);
233
        }
234
235 4269
        if (unlink(pfh->pf_path) == -1)
236 87
                error = errno;
237 4269
        if (close(pfh->pf_fd) == -1) {
238 0
                if (error == 0)
239 0
                        error = errno;
240
        }
241 4269
        if (freeit)
242 4269
                free(pfh);
243
        else
244 0
                pfh->pf_fd = -1;
245 4269
        if (error != 0) {
246 87
                errno = error;
247 87
                return (-1);
248
        }
249 4182
        return (0);
250
}
251
252
int
253 4269
VPF_Remove(struct vpf_fh *pfh)
254
{
255
256 4269
        return (_VPF_Remove(pfh, 1));
257
}