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