1 /*- 2 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/file.h> 32 #include <sys/stat.h> 33 34 #include <err.h> 35 #include <errno.h> 36 #include <fcntl.h> 37 #include <libgen.h> 38 #include <libutil.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <time.h> 43 #include <unistd.h> 44 45 struct pidfh { 46 int pf_dirfd; 47 int pf_fd; 48 char pf_dir[MAXPATHLEN + 1]; 49 char pf_filename[MAXPATHLEN + 1]; 50 dev_t pf_dev; 51 ino_t pf_ino; 52 }; 53 54 static int _pidfile_remove(struct pidfh *pfh, int freeit); 55 56 static int 57 pidfile_verify(const struct pidfh *pfh) 58 { 59 struct stat sb; 60 61 if (pfh == NULL || pfh->pf_fd == -1) 62 return (EDOOFUS); 63 /* 64 * Check remembered descriptor. 65 */ 66 if (fstat(pfh->pf_fd, &sb) == -1) 67 return (errno); 68 if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino) 69 return (EDOOFUS); 70 return (0); 71 } 72 73 static int 74 pidfile_read(int dirfd, const char *filename, pid_t *pidptr) 75 { 76 char buf[16], *endptr; 77 int error, fd, i; 78 79 fd = openat(dirfd, filename, O_RDONLY | O_CLOEXEC); 80 if (fd == -1) 81 return (errno); 82 83 i = read(fd, buf, sizeof(buf) - 1); 84 error = errno; /* Remember errno in case close() wants to change it. */ 85 close(fd); 86 if (i == -1) 87 return (error); 88 else if (i == 0) 89 return (EAGAIN); 90 buf[i] = '\0'; 91 92 *pidptr = strtol(buf, &endptr, 10); 93 if (endptr != &buf[i]) 94 return (EINVAL); 95 96 return (0); 97 } 98 99 struct pidfh * 100 pidfile_open(const char *path, mode_t mode, pid_t *pidptr) 101 { 102 struct pidfh *pfh; 103 struct stat sb; 104 int error, fd, dirfd, dirlen, filenamelen, count; 105 struct timespec rqtp; 106 107 pfh = malloc(sizeof(*pfh)); 108 if (pfh == NULL) 109 return (NULL); 110 111 if (path == NULL) { 112 dirlen = snprintf(pfh->pf_dir, sizeof(pfh->pf_dir), 113 "/var/run/"); 114 filenamelen = snprintf(pfh->pf_filename, 115 sizeof(pfh->pf_filename), "%s.pid", getprogname()); 116 } else { 117 dirlen = snprintf(pfh->pf_dir, sizeof(pfh->pf_dir), 118 "%s", path); 119 filenamelen = snprintf(pfh->pf_filename, 120 sizeof(pfh->pf_filename), "%s", path); 121 122 dirname(pfh->pf_dir); 123 basename(pfh->pf_filename); 124 } 125 126 if (dirlen >= (int)sizeof(pfh->pf_dir) || 127 filenamelen >= (int)sizeof(pfh->pf_filename)) { 128 free(pfh); 129 errno = ENAMETOOLONG; 130 return (NULL); 131 } 132 133 dirfd = open(pfh->pf_dir, O_CLOEXEC | O_DIRECTORY | O_NONBLOCK); 134 if (dirfd == -1) { 135 error = errno; 136 free(pfh); 137 errno = error; 138 return (NULL); 139 } 140 141 /* 142 * Open the PID file and obtain exclusive lock. 143 * We truncate PID file here only to remove old PID immediately, 144 * PID file will be truncated again in pidfile_write(), so 145 * pidfile_write() can be called multiple times. 146 */ 147 fd = flopenat(dirfd, pfh->pf_filename, 148 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NONBLOCK, mode); 149 if (fd == -1) { 150 if (errno == EWOULDBLOCK) { 151 if (pidptr == NULL) { 152 errno = EEXIST; 153 } else { 154 count = 20; 155 rqtp.tv_sec = 0; 156 rqtp.tv_nsec = 5000000; 157 for (;;) { 158 errno = pidfile_read(dirfd, 159 pfh->pf_filename, pidptr); 160 if (errno != EAGAIN || --count == 0) 161 break; 162 nanosleep(&rqtp, 0); 163 } 164 if (errno == EAGAIN) 165 *pidptr = -1; 166 if (errno == 0 || errno == EAGAIN) 167 errno = EEXIST; 168 } 169 } 170 error = errno; 171 close(dirfd); 172 free(pfh); 173 errno = error; 174 return (NULL); 175 } 176 177 /* 178 * Remember file information, so in pidfile_write() we are sure we write 179 * to the proper descriptor. 180 */ 181 if (fstat(fd, &sb) == -1) { 182 error = errno; 183 unlinkat(dirfd, pfh->pf_filename, 0); 184 close(dirfd); 185 close(fd); 186 free(pfh); 187 errno = error; 188 return (NULL); 189 } 190 191 pfh->pf_dirfd = dirfd; 192 pfh->pf_fd = fd; 193 pfh->pf_dev = sb.st_dev; 194 pfh->pf_ino = sb.st_ino; 195 196 return (pfh); 197 } 198 199 int 200 pidfile_write(struct pidfh *pfh) 201 { 202 char pidstr[16]; 203 int error, fd; 204 205 /* 206 * Check remembered descriptor, so we don't overwrite some other 207 * file if pidfile was closed and descriptor reused. 208 */ 209 errno = pidfile_verify(pfh); 210 if (errno != 0) { 211 /* 212 * Don't close descriptor, because we are not sure if it's ours. 213 */ 214 return (-1); 215 } 216 fd = pfh->pf_fd; 217 218 /* 219 * Truncate PID file, so multiple calls of pidfile_write() are allowed. 220 */ 221 if (ftruncate(fd, 0) == -1) { 222 error = errno; 223 _pidfile_remove(pfh, 0); 224 errno = error; 225 return (-1); 226 } 227 228 snprintf(pidstr, sizeof(pidstr), "%u", getpid()); 229 if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) { 230 error = errno; 231 _pidfile_remove(pfh, 0); 232 errno = error; 233 return (-1); 234 } 235 236 return (0); 237 } 238 239 int 240 pidfile_close(struct pidfh *pfh) 241 { 242 int error; 243 244 error = pidfile_verify(pfh); 245 if (error != 0) { 246 errno = error; 247 return (-1); 248 } 249 250 if (close(pfh->pf_fd) == -1) 251 error = errno; 252 if (close(pfh->pf_dirfd) == -1 && error == 0) 253 error = errno; 254 255 free(pfh); 256 if (error != 0) { 257 errno = error; 258 return (-1); 259 } 260 return (0); 261 } 262 263 static int 264 _pidfile_remove(struct pidfh *pfh, int freeit) 265 { 266 int error; 267 268 error = pidfile_verify(pfh); 269 if (error != 0) { 270 errno = error; 271 return (-1); 272 } 273 274 if (unlinkat(pfh->pf_dirfd, pfh->pf_filename, 0) == -1) 275 error = errno; 276 if (close(pfh->pf_fd) == -1 && error == 0) 277 error = errno; 278 if (close(pfh->pf_dirfd) == -1 && error == 0) 279 error = errno; 280 if (freeit) 281 free(pfh); 282 else 283 pfh->pf_fd = -1; 284 if (error != 0) { 285 errno = error; 286 return (-1); 287 } 288 return (0); 289 } 290 291 int 292 pidfile_remove(struct pidfh *pfh) 293 { 294 295 return (_pidfile_remove(pfh, 1)); 296 } 297 298 int 299 pidfile_fileno(const struct pidfh *pfh) 300 { 301 302 if (pfh == NULL || pfh->pf_fd == -1) { 303 errno = EDOOFUS; 304 return (-1); 305 } 306 return (pfh->pf_fd); 307 } 308