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 29#include <sys/param.h> 30#include <sys/file.h> 31#include <sys/stat.h> 32 33#include <stdio.h> 34#include <stdlib.h> 35#include <unistd.h> 36#include <fcntl.h> 37#include <string.h> 38#include <err.h> 39#include <errno.h> 40#include <libutil.h> 41 42static int _pidfile_remove(struct pidfh *pfh, int freeit); 43 44static int 45pidfile_verify(struct pidfh *pfh) 46{ 47 struct stat sb; 48 49 if (pfh == NULL || pfh->pf_fd == -1) 50 return EINVAL; 51 /* 52 * Check remembered descriptor. 53 */ 54 if (fstat(pfh->pf_fd, &sb) == -1) 55 return (errno); 56 if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino) 57 return EINVAL; 58 return (0); 59} 60 61static int 62pidfile_read(const char *path, pid_t *pidptr) 63{ 64 char buf[16], *endptr; 65 int error, fd, i; 66 67 fd = open(path, O_RDONLY); 68 if (fd == -1) 69 return (errno); 70 71 i = read(fd, buf, sizeof(buf) - 1); 72 error = errno; /* Remember errno in case close() wants to change it. */ 73 close(fd); 74 if (i == -1) 75 return (error); 76 buf[i] = '\0'; 77 78 *pidptr = strtol(buf, &endptr, 10); 79 if (endptr != &buf[i]) 80 return (EINVAL); 81 82 return (0); 83} 84 85struct pidfh * 86pidfile_open(const char *path, mode_t mode, pid_t *pidptr) 87{ 88 struct pidfh *pfh; 89 struct stat sb; 90 int error, fd; 91 92 pfh = malloc(sizeof(*pfh)); 93 if (pfh == NULL) 94 return (NULL); 95 96 if (path == NULL) { 97 snprintf(pfh->pf_path, sizeof(pfh->pf_path), "/var/run/%s.pid", 98 getprogname()); 99 } else { 100 strlcpy(pfh->pf_path, path, sizeof(pfh->pf_path)); 101 } 102 if (strlen(pfh->pf_path) == sizeof(pfh->pf_path) - 1) { 103 free(pfh); 104 errno = ENAMETOOLONG; 105 return (NULL); 106 } 107 108 /* 109 * Open the PID file and obtain exclusive lock. 110 * We truncate PID file here only to remove old PID immediatelly, 111 * PID file will be truncated again in pidfile_write(), so 112 * pidfile_write() can be called multiple times. 113 */ 114 fd = open(pfh->pf_path, 115 O_WRONLY | O_CREAT | O_EXLOCK | O_TRUNC | O_NONBLOCK, mode); 116 if (fd == -1) { 117 if (errno == EWOULDBLOCK && pidptr != NULL) { 118 errno = pidfile_read(pfh->pf_path, pidptr); 119 if (errno == 0) 120 errno = EEXIST; 121 } 122 free(pfh); 123 return (NULL); 124 } 125 /* 126 * Remember file information, so in pidfile_write() we are sure we write 127 * to the proper descriptor. 128 */ 129 if (fstat(fd, &sb) == -1) { 130 error = errno; 131 unlink(pfh->pf_path); 132 close(fd); 133 free(pfh); 134 errno = error; 135 return (NULL); 136 } 137 138 pfh->pf_fd = fd; 139 pfh->pf_dev = sb.st_dev; 140 pfh->pf_ino = sb.st_ino; 141 142 return (pfh); 143} 144 145int 146pidfile_write(struct pidfh *pfh) 147{ 148 char pidstr[16]; 149 int error, fd; 150 151 /* 152 * Check remembered descriptor, so we don't overwrite some other 153 * file if pidfile was closed and descriptor reused. 154 */ 155 errno = pidfile_verify(pfh); 156 if (errno != 0) { 157 /* 158 * Don't close descriptor, because we are not sure if it's ours. 159 */ 160 return (-1); 161 } 162 fd = pfh->pf_fd; 163 164 /* 165 * Truncate PID file, so multiple calls of pidfile_write() are allowed. 166 */ 167 if (ftruncate(fd, 0) == -1) { 168 error = errno; 169 _pidfile_remove(pfh, 0); 170 errno = error; 171 return (-1); 172 } 173 174 snprintf(pidstr, sizeof(pidstr), "%u", getpid()); 175 if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) { 176 error = errno; 177 _pidfile_remove(pfh, 0); 178 errno = error; 179 return (-1); 180 } 181 182 return (0); 183} 184 185int 186pidfile_close(struct pidfh *pfh) 187{ 188 int error; 189 190 error = pidfile_verify(pfh); 191 if (error != 0) { 192 errno = error; 193 return (-1); 194 } 195 196 if (close(pfh->pf_fd) == -1) 197 error = errno; 198 free(pfh); 199 if (error != 0) { 200 errno = error; 201 return (-1); 202 } 203 return (0); 204} 205 206static int 207_pidfile_remove(struct pidfh *pfh, int freeit) 208{ 209 int error; 210 211 error = pidfile_verify(pfh); 212 if (error != 0) { 213 errno = error; 214 return (-1); 215 } 216 217 if (unlink(pfh->pf_path) == -1) 218 error = errno; 219 if (flock(pfh->pf_fd, LOCK_UN) == -1) { 220 if (error == 0) 221 error = errno; 222 } 223 if (close(pfh->pf_fd) == -1) { 224 if (error == 0) 225 error = errno; 226 } 227 if (freeit) 228 free(pfh); 229 else 230 pfh->pf_fd = -1; 231 if (error != 0) { 232 errno = error; 233 return (-1); 234 } 235 return (0); 236} 237 238int 239pidfile_remove(struct pidfh *pfh) 240{ 241 242 return (_pidfile_remove(pfh, 1)); 243} 244