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