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