varnish-cache/lib/libvarnish/vpf.c
0
/*-
1
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
2
 *
3
 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
 * SUCH DAMAGE.
26
 * Derived from:
27
 * $FreeBSD: head/lib/libutil/pidfile.c 184091 2008-10-20 17:41:08Z des $
28
 */
29
30
#include "config.h"
31
32
#include <sys/param.h>
33
#include <sys/file.h>
34
#include <sys/stat.h>
35
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;
52
        dev_t   pf_dev;
53
        ino_t   pf_ino;
54
};
55
56
static int
57 98308
vpf_verify(const struct vpf_fh *pfh)
58
{
59
        struct stat sb;
60
61 98308
        if (pfh == NULL || pfh->pf_fd == -1)
62 0
                return (EINVAL);
63
        /*
64
         * Check remembered descriptor.
65
         */
66 98308
        if (fstat(pfh->pf_fd, &sb) == -1)
67 0
                return (errno);
68 98308
        if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
69 0
                return (EINVAL);
70 98308
        return (0);
71 98308
}
72
73
int
74 3605
VPF_Read(const char *path, pid_t *pidptr)
75
{
76
        char buf[16], *endptr;
77
        int error, fd, i;
78
79 3605
        fd = open(path, O_RDONLY | O_CLOEXEC);
80 3605
        if (fd == -1)
81 3415
                return (errno);
82
83 190
        i = read(fd, buf, sizeof(buf) - 1);
84 190
        error = errno;
85 190
        closefd(&fd);
86 190
        if (i == -1)
87 0
                return (error);
88 190
        else if (i == 0)
89 50
                return (EAGAIN);
90 140
        if (i > 0 && buf[i - 1] == '\n')
91 84
                i--;
92 140
        buf[i] = '\0';
93
94 140
        *pidptr = strtol(buf, &endptr, 10);
95 140
        if (endptr != &buf[i])
96 0
                return (EINVAL);
97
98 140
        return (0);
99 3605
}
100
101
struct vpf_fh *
102 49280
VPF_Open(const char *path, mode_t mode, pid_t *pidptr)
103
{
104
        struct vpf_fh *pfh;
105
        struct stat sb;
106
        int fd;
107
108
        /*
109
         * Open the PID file and obtain exclusive lock.
110
         * We truncate PID file here only to remove old PID immediately,
111
         * PID file will be truncated again in VPF_Write(), so
112
         * VPF_Write() can be called multiple times.
113
         */
114 98560
        fd = VFL_Open(path,
115 49280
            O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NONBLOCK, mode);
116 49280
        if (fd == -1) {
117 84
                if (errno == EWOULDBLOCK && pidptr != NULL) {
118 56
                        errno = VPF_Read(path, pidptr);
119 56
                        if (errno == 0)
120 56
                                errno = EEXIST;
121 56
                }
122 84
                return (NULL);
123
        }
124
125
        /*
126
         * Remember file information, so in VPF_Write() we are sure we write
127
         * to the proper descriptor.
128
         */
129 49196
        AZ(fstat(fd, &sb));
130
131 49196
        pfh = malloc(sizeof(*pfh));
132 49196
        AN(pfh);
133 49196
        pfh->pf_path = strdup(path);
134 49196
        AN(pfh->pf_path);
135
136 49196
        pfh->pf_fd = fd;
137 49196
        pfh->pf_dev = sb.st_dev;
138 49196
        pfh->pf_ino = sb.st_ino;
139
140 49196
        return (pfh);
141 49280
}
142
143
void
144 49196
VPF_Write(const struct vpf_fh *pfh)
145
{
146
        char pidstr[16];
147
148
        /*
149
         * Check remembered descriptor, so we don't overwrite some other
150
         * file if pidfile was closed and descriptor reused.
151
         */
152 49196
        if (vpf_verify(pfh) != 0)
153 0
                return;
154
155
        /*
156
         * Truncate PID file, so multiple calls of VPF_Write() are allowed.
157
         */
158 49196
        AZ(ftruncate(pfh->pf_fd, 0));
159
160 49196
        bprintf(pidstr, "%jd", (intmax_t)getpid());
161 49196
        assert(pwrite(pfh->pf_fd, pidstr, strlen(pidstr), 0) ==
162
            (ssize_t)strlen(pidstr));
163 49196
}
164
165
void
166 49112
VPF_Remove(struct vpf_fh *pfh)
167
{
168 49112
        if (vpf_verify(pfh) == 0) {
169 49112
                (void)unlink(pfh->pf_path);
170 49112
                closefd(&pfh->pf_fd);
171 49112
        }
172 49112
        free(pfh->pf_path);
173 49112
        free(pfh);
174 49112
}