1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*
* Copyright (c) 1997, 98, 2000, 01
* Motoyuki Kasahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/*
* This program requires the following Autoconf macros:
* AC_C_CONST
* AC_TYPE_PID_T
* AC_HEADER_STDC
* AC_CHECK_HEADERS(unistd.h, stdlib.h)
* AC_CHECK_FUNCS(strtol)
* AC_HEADER_STAT
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <errno.h>
#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
#include <string.h>
#if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
#include <memory.h>
#endif /* not STDC_HEADERS and HAVE_MEMORY_H */
#else /* not STDC_HEADERS and not HAVE_STRING_H */
#include <strings.h>
#endif /* not STDC_HEADERS and not HAVE_STRING_H */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifndef HAVE_STRTOL
#ifdef __STDC__
long strtol(const char *, char **, int);
#else /* not __STDC__ */
long strtol();
#endif /* not __STDC__ */
#endif /* not HAVE_STRTOL */
#ifdef USE_FAKELOG
#include "fakelog.h"
#endif
#ifdef STAT_MACROS_BROKEN
#ifdef S_ISREG
#undef S_ISREG
#endif
#ifdef S_ISDIR
#undef S_ISDIR
#endif
#endif /* STAT_MACROS_BROKEN */
#ifndef S_ISREG
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif
#ifndef S_ISDIR
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
/*
* Character type tests and conversions.
*/
#define isdigit(c) ('0' <= (c) && (c) <= '9')
#define isupper(c) ('A' <= (c) && (c) <= 'Z')
#define islower(c) ('a' <= (c) && (c) <= 'z')
#define isalpha(c) (isupper(c) || islower(c))
#define isalnum(c) (isupper(c) || islower(c) || isdigit(c))
#define isxdigit(c) \
(isdigit(c) || ('A' <= (c) && (c) <= 'F') || ('a' <= (c) && (c) <= 'f'))
#define toupper(c) (('a' <= (c) && (c) <= 'z') ? (c) - 0x20 : (c))
#define tolower(c) (('A' <= (c) && (c) <= 'Z') ? (c) + 0x20 : (c))
/*
* Maximum Line length of a pid file.
*/
#define LOGPID_SIZE_LINE 63
/*
* Log the PID of the current process to file specified at `file_name'.
* If succeed, 0 is returned. Otherwise -1 is returned.
*/
int
log_pid_file(file_name)
const char *file_name;
{
struct stat status;
FILE *file;
/*
* Does the file already exist?
*/
if (stat(file_name, &status) == 0 && S_ISREG(status.st_mode)) {
syslog(LOG_NOTICE, "warning: the PID file already exists: %s",
file_name);
}
/*
* Open the file.
*/
file = fopen(file_name, "w");
if (file == NULL) {
syslog(LOG_ERR, "failed to open the file, %s: %s", strerror(errno),
file_name);
goto failed;
}
/*
* Write a PID of the process.
*/
fprintf(file, "%d\n", (int)getpid());
if (ferror(file)) {
syslog(LOG_ERR, "failed to write to the file, %s: %s", strerror(errno),
file_name);
goto failed;
}
/*
* Close the file.
*/
if (fclose(file) == EOF) {
syslog(LOG_ERR, "failed to close the file, %s: %s", strerror(errno),
file_name);
file = NULL;
goto failed;
}
syslog(LOG_DEBUG, "debug: write a PID to the file: %s", file_name);
return 0;
/*
* An error occurs...
*/
failed:
if (file != NULL && fclose(file) == EOF) {
syslog(LOG_ERR, "failed to close the file, %s: %s", strerror(errno),
file_name);
}
syslog(LOG_ERR, "failed to write a PID to the file: %s", file_name);
return -1;
}
/*
* Remove a PID file.
* If succeed, 0 is returned. Otherwise -1 is returned.
*/
int
remove_pid_file(file_name)
const char *file_name;
{
if (unlink(file_name) == 0) {
syslog(LOG_DEBUG, "debug: remove the PID file: %s", file_name);
return 0;
} else if (errno == ENOENT) {
syslog(LOG_NOTICE, "warning: the PID file not exist: %s", file_name);
return 0;
}
syslog(LOG_ERR, "unlink failed(), %s: %s", strerror(errno), file_name);
syslog(LOG_ERR, "failed to remove the PID file: %s", file_name);
return -1;
}
/*
* Check for existance of a pid file.
* If exists, 0 is returned. Otherwise -1 is returned.
*/
int
probe_pid_file(file_name)
const char *file_name;
{
struct stat status;
/*
* Does the file already exist?
*/
if (stat(file_name, &status) == 0 || S_ISREG(status.st_mode))
return 0;
return -1;
}
/*
* Read a PID from a pid file.
* If succeed, 0 is returned. Otherwise -1 is returned.
*/
int
read_pid_file(file_name, pid)
const char *file_name;
pid_t *pid;
{
FILE *file;
char buffer[LOGPID_SIZE_LINE + 1];
char *end_p;
/*
* Open the file.
*/
file = fopen(file_name, "r");
if (file == NULL) {
syslog(LOG_ERR, "failed to open the file, %s: %s", strerror(errno),
file_name);
goto failed;
}
/*
* Read a PID string from the file.
*/
if (fgets(buffer, LOGPID_SIZE_LINE + 1, file) == NULL) {
syslog(LOG_ERR, "failed to read the file, %s: %s", strerror(errno),
file_name);
goto failed;
}
/*
* Parse the PID string.
*/
*pid = strtol(buffer, &end_p, 10);
if (!isdigit(buffer[0]) || (*end_p != '\0' && *end_p != '\n')) {
syslog(LOG_ERR, "invalid PID: %s", file_name);
goto failed;
}
/*
* Close the file.
*/
if (fclose(file) == EOF) {
syslog(LOG_ERR, "failed to close the file, %s: %s", strerror(errno),
file_name);
file = NULL;
goto failed;
}
return 0;
/*
* An error occurs...
*/
failed:
if (file != NULL && fclose(file) == EOF) {
syslog(LOG_ERR, "failed to close the file, %s: %s", strerror(errno),
file_name);
}
syslog(LOG_ERR, "failed to read a PID from the file: %s", file_name);
return -1;
}
|