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>      //lint !e537
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 91075
vpf_verify(const struct vpf_fh *pfh)
58
{
59
        struct stat sb;
60
61 91075
        if (pfh == NULL || pfh->pf_fd == -1)
62 0
                return (EINVAL);
63
        /*
64
         * Check remembered descriptor.
65
         */
66 91075
        if (fstat(pfh->pf_fd, &sb) == -1)
67 0
                return (errno);
68 91075
        if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
69 0
                return (EINVAL);
70 91075
        return (0);
71 91075
}
72
73
int
74 3367
VPF_Read(const char *path, pid_t *pidptr)
75
{
76
        char buf[16], *endptr;
77
        int error, fd, i;
78
79 3367
        fd = open(path, O_RDONLY | O_CLOEXEC);
80 3367
        if (fd == -1)
81 3192
                return (errno);
82
83 175
        i = read(fd, buf, sizeof(buf) - 1);
84 175
        error = errno;
85 175
        closefd(&fd);
86 175
        if (i == -1)
87 0
                return (error);
88 175
        else if (i == 0)
89 50
                return (EAGAIN);
90 125
        if (i > 0 && buf[i - 1] == '\n')
91 75
                i--;
92 125
        buf[i] = '\0';
93
94 125
        *pidptr = strtol(buf, &endptr, 10);
95 125
        if (endptr != &buf[i])
96 0
                return (EINVAL);
97
98 125
        return (0);
99 3367
}
100
101
struct vpf_fh *
102 45725
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 91450
        fd = VFL_Open(path,
115 45725
            O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NONBLOCK, mode);
116 45725
        if (fd == -1) {
117 75
                if (errno == EWOULDBLOCK && pidptr != NULL) {
118 50
                        errno = VPF_Read(path, pidptr);
119 50
                        if (errno == 0)
120 50
                                errno = EEXIST;
121 50
                }
122 75
                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 45650
        AZ(fstat(fd, &sb));
130
131 45650
        pfh = malloc(sizeof(*pfh));
132 45650
        AN(pfh);
133 45650
        pfh->pf_path = strdup(path);
134 45650
        AN(pfh->pf_path);
135
136 45650
        pfh->pf_fd = fd;
137 45650
        pfh->pf_dev = sb.st_dev;
138 45650
        pfh->pf_ino = sb.st_ino;
139
140 45650
        return (pfh);
141 45725
}
142
143
void
144 45650
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 45650
        if (vpf_verify(pfh) != 0)
153 0
                return;
154
155
        /*
156
         * Truncate PID file, so multiple calls of VPF_Write() are allowed.
157
         */
158 45650
        AZ(ftruncate(pfh->pf_fd, 0));
159
160 45650
        bprintf(pidstr, "%jd", (intmax_t)getpid());
161 45650
        assert(pwrite(pfh->pf_fd, pidstr, strlen(pidstr), 0) ==
162
            (ssize_t)strlen(pidstr));
163 45650
}
164
165
void
166 45425
VPF_Remove(struct vpf_fh *pfh)
167
{
168 45425
        if (vpf_verify(pfh) == 0) {
169 45425
                (void)unlink(pfh->pf_path);
170 45425
                closefd(&pfh->pf_fd);
171 45425
        }
172 45425
        free(pfh->pf_path);
173 45425
        free(pfh);
174 45425
}