pidfile.h revision 302408
1228690Sdes/*- 2348980Sdes * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3228690Sdes * All rights reserved. 4228690Sdes * 5228690Sdes * Redistribution and use in source and binary forms, with or without 6228690Sdes * modification, are permitted provided that the following conditions 7228690Sdes * are met: 8228690Sdes * 1. Redistributions of source code must retain the above copyright 9228690Sdes * notice, this list of conditions and the following disclaimer. 10228690Sdes * 2. Redistributions in binary form must reproduce the above copyright 11228690Sdes * notice, this list of conditions and the following disclaimer in the 12228690Sdes * documentation and/or other materials provided with the distribution. 13236109Sdes * 14236109Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15236109Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16228690Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17228690Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18228690Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19228690Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20228690Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21228690Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22228690Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23228690Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24228690Sdes * SUCH DAMAGE. 25228690Sdes * 26228690Sdes * Derived from FreeBSD head/lib/libutil/pidfile.c r231938 27228690Sdes */ 28228690Sdes 29348980Sdes#include <sys/param.h> 30228690Sdes#include <sys/file.h> 31348980Sdes#include <sys/stat.h> 32228690Sdes 33228690Sdes#include <stdio.h> 34228690Sdes#include <stdlib.h> 35228690Sdes#include <unistd.h> 36228690Sdes#include <fcntl.h> 37271624Sdes#include <string.h> 38228690Sdes#include <time.h> 39228690Sdes#include <err.h> 40228690Sdes#include <errno.h> 41228690Sdes 42228690Sdes#include "flopen.h" 43228690Sdes 44228690Sdesstruct pidfh { 45228690Sdes int pf_fd; 46228690Sdes char pf_path[MAXPATHLEN + 1]; 47228690Sdes dev_t pf_dev; 48228690Sdes ino_t pf_ino; 49228690Sdes}; 50228690Sdes 51228690Sdesstatic int _pidfile_remove(struct pidfh *pfh, int freeit); 52228690Sdes 53228690Sdesstatic int 54228690Sdespidfile_verify(const struct pidfh *pfh) 55228690Sdes{ 56228690Sdes struct stat sb; 57228690Sdes 58228690Sdes if (pfh == NULL || pfh->pf_fd == -1) 59236109Sdes return (EINVAL); 60228690Sdes /* 61228690Sdes * Check remembered descriptor. 62228690Sdes */ 63228690Sdes if (fstat(pfh->pf_fd, &sb) == -1) 64228690Sdes return (errno); 65348980Sdes if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino) 66 return (EINVAL); 67 return (0); 68} 69 70static int 71pidfile_read(const char *path, pid_t *pidptr) 72{ 73 char buf[16], *endptr; 74 int error, fd, i; 75 76 fd = open(path, O_RDONLY); 77 if (fd == -1) 78 return (errno); 79 80 i = read(fd, buf, sizeof(buf) - 1); 81 error = errno; /* Remember errno in case close() wants to change it. */ 82 close(fd); 83 if (i == -1) 84 return (error); 85 else if (i == 0) 86 return (EAGAIN); 87 buf[i] = '\0'; 88 89 *pidptr = strtol(buf, &endptr, 10); 90 if (endptr != &buf[i]) 91 return (EINVAL); 92 93 return (0); 94} 95 96static struct pidfh * 97pidfile_open(const char *path, mode_t mode, pid_t *pidptr) 98{ 99 struct pidfh *pfh; 100 struct stat sb; 101 int error, fd, len, count; 102 struct timespec rqtp; 103 104 if (pidptr != NULL) 105 *pidptr = -1; 106 107 if (path == NULL) 108 return (NULL); 109 110 pfh = malloc(sizeof(*pfh)); 111 if (pfh == NULL) 112 return (NULL); 113 114 len = snprintf(pfh->pf_path, sizeof(pfh->pf_path), 115 "%s", path); 116 if (len >= (int)sizeof(pfh->pf_path)) { 117 free(pfh); 118 errno = ENAMETOOLONG; 119 return (NULL); 120 } 121 122 /* 123 * Open the PID file and obtain exclusive lock. 124 * We truncate PID file here only to remove old PID immediatelly, 125 * PID file will be truncated again in pidfile_write(), so 126 * pidfile_write() can be called multiple times. 127 */ 128 fd = flopen(pfh->pf_path, 129#ifdef O_CLOEXEC 130 O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK | O_CLOEXEC, mode); 131#else 132 O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode); 133#endif 134 if (fd == -1) { 135 if (errno == EWOULDBLOCK && pidptr != NULL) { 136 count = 20; 137 rqtp.tv_sec = 0; 138 rqtp.tv_nsec = 5000000; 139 for (;;) { 140 errno = pidfile_read(pfh->pf_path, pidptr); 141 if (errno != EAGAIN || --count == 0) 142 break; 143 nanosleep(&rqtp, 0); 144 } 145 if (errno == EAGAIN) 146 *pidptr = -1; 147 if (errno == 0 || errno == EAGAIN) 148 errno = EEXIST; 149 } 150 free(pfh); 151 return (NULL); 152 } 153 154#ifndef O_CLOEXEC 155 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { 156 error = errno; 157 unlink(pfh->pf_path); 158 close(fd); 159 free(pfh); 160 errno = error; 161 return (NULL); 162 } 163#endif 164 165 /* 166 * Remember file information, so in pidfile_write() we are sure we write 167 * to the proper descriptor. 168 */ 169 if (fstat(fd, &sb) == -1) { 170 error = errno; 171 unlink(pfh->pf_path); 172 close(fd); 173 free(pfh); 174 errno = error; 175 return (NULL); 176 } 177 178 pfh->pf_fd = fd; 179 pfh->pf_dev = sb.st_dev; 180 pfh->pf_ino = sb.st_ino; 181 182 return (pfh); 183} 184 185static int 186pidfile_write(struct pidfh *pfh) 187{ 188 char pidstr[16]; 189 int error, fd; 190 191 /* 192 * Check remembered descriptor, so we don't overwrite some other 193 * file if pidfile was closed and descriptor reused. 194 */ 195 errno = pidfile_verify(pfh); 196 if (errno != 0) { 197 /* 198 * Don't close descriptor, because we are not sure if it's ours. 199 */ 200 return (-1); 201 } 202 fd = pfh->pf_fd; 203 204 /* 205 * Truncate PID file, so multiple calls of pidfile_write() are allowed. 206 */ 207 if (ftruncate(fd, 0) == -1) { 208 error = errno; 209 _pidfile_remove(pfh, 0); 210 errno = error; 211 return (-1); 212 } 213 214 snprintf(pidstr, sizeof(pidstr), "%u", getpid()); 215 if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) { 216 error = errno; 217 _pidfile_remove(pfh, 0); 218 errno = error; 219 return (-1); 220 } 221 222 return (0); 223} 224 225static int 226pidfile_close(struct pidfh *pfh) 227{ 228 int error; 229 230 error = pidfile_verify(pfh); 231 if (error != 0) { 232 errno = error; 233 return (-1); 234 } 235 236 if (close(pfh->pf_fd) == -1) 237 error = errno; 238 free(pfh); 239 if (error != 0) { 240 errno = error; 241 return (-1); 242 } 243 return (0); 244} 245 246static int 247_pidfile_remove(struct pidfh *pfh, int freeit) 248{ 249 int error; 250 251 error = pidfile_verify(pfh); 252 if (error != 0) { 253 errno = error; 254 return (-1); 255 } 256 257 if (unlink(pfh->pf_path) == -1) 258 error = errno; 259 if (close(pfh->pf_fd) == -1) { 260 if (error == 0) 261 error = errno; 262 } 263 if (freeit) 264 free(pfh); 265 else 266 pfh->pf_fd = -1; 267 if (error != 0) { 268 errno = error; 269 return (-1); 270 } 271 return (0); 272} 273 274static int 275pidfile_remove(struct pidfh *pfh) 276{ 277 278 return (_pidfile_remove(pfh, 1)); 279} 280 281#if 0 282static int 283pidfile_fileno(const struct pidfh *pfh) 284{ 285 286 if (pfh == NULL || pfh->pf_fd == -1) { 287 errno = EINVAL; 288 return (-1); 289 } 290 return (pfh->pf_fd); 291} 292#endif 293