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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/*
* cleaner_exec.c - old cleaner control routines
*
* Licensed under LGPLv2: the complete text of the GNU Lesser General
* Public License can be found in COPYING file of the nilfs-utils
* package.
*
* Copyright (C) 2007-2012 Nippon Telegraph and Telephone Corporation.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */
#include <stdio.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif /* HAVE_FCNTL_H */
#if HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#if HAVE_LIMITS_H
#include <limits.h> /* ULONG_MAX */
#endif /* HAVE_LIMITS_H */
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif /* HAVE_SYS_STAT_H */
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */
#if HAVE_TIME_H
#include <time.h>
#endif /* HAVE_TIME_H */
#if HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif /* HAVE_SYS_WAIT_H */
#if HAVE_SYSLOG_H
#include <syslog.h>
#endif /* HAVE_SYSLOG_H */
#include <signal.h>
#include <stdarg.h>
#include <errno.h>
#include <assert.h>
#include "cleaner_exec.h"
#include "compat.h"
#include "nls.h"
#include "util.h"
/* Intervals for binary exponential backoff */
#define WAIT_CLEANERD_MIN_BACKOFF_TIME 5000 /* in micro-seconds */
#define WAIT_CLEANERD_MAX_BACKOFF_TIME 2 /* in seconds */
/* Intervals for periodic wait retry */
#define WAIT_CLEANERD_RETRY_INTERVAL 2 /* in seconds */
#define WAIT_CLEANERD_RETRY_TIMEOUT 8 /* in seconds */
static const char cleanerd[] = "/sbin/" NILFS_CLEANERD_NAME;
static const char cleanerd_protperiod_opt[] = "-p";
static void default_logger(int priority, const char *fmt, ...)
{
va_list args;
if (priority >= LOG_INFO)
return;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
fputs(_("\n"), stderr);
va_end(args);
}
static void default_printf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
static void default_flush(void)
{
fflush(stdout);
}
void (*nilfs_cleaner_logger)(int priority, const char *fmt, ...)
= default_logger;
void (*nilfs_cleaner_printf)(const char *fmt, ...) = default_printf;
void (*nilfs_cleaner_flush)(void) = default_flush;
static inline int process_is_alive(pid_t pid)
{
return (kill(pid, 0) == 0);
}
static int receive_pid(int fd, pid_t *ppid)
{
char buf[100];
unsigned long pid;
FILE *fp;
int ret;
fp = fdopen(fd, "r");
if (!fp) {
nilfs_cleaner_logger(
LOG_ERR, _("Error: fdopen failed: %m"));
close(fd);
goto fail;
}
/* read process ID of cleanerd through the given fd */
while (fgets(buf, sizeof(buf), fp) != NULL) {
/*
* fgets() is blocked during no child processes
* respond, but it will escape returning a NULL value
* and terminate the loop when all child processes
* close the given pipe (fd) including call of exit().
*/
ret = sscanf(buf, "NILFS_CLEANERD_PID=%lu", &pid);
if (ret == 1) {
*ppid = pid;
fclose(fp); /* this also closes fd */
return 0;
}
}
fclose(fp);
fail:
nilfs_cleaner_logger(
LOG_WARNING, _("Warning: cannot get pid of cleanerd"));
return -1;
}
int nilfs_launch_cleanerd(const char *device, const char *mntdir,
unsigned long protperiod, pid_t *ppid)
{
const char *dargs[6];
struct stat statbuf;
sigset_t sigs;
int i = 0;
int ret;
char buf[256];
int pipes[2];
ret = stat(cleanerd, &statbuf);
if (unlikely(ret != 0)) {
nilfs_cleaner_logger(LOG_ERR, _("Error: %s not found"),
NILFS_CLEANERD_NAME);
return -1;
}
ret = pipe(pipes);
if (unlikely(ret < 0)) {
nilfs_cleaner_logger(
LOG_ERR, _("Error: failed to create pipe: %m"));
return -1;
}
ret = fork();
if (ret == 0) {
/* child */
ret = setgid(getgid());
if (unlikely(ret < 0)) {
nilfs_cleaner_logger(
LOG_ERR,
_("Error: failed to drop setgid privileges"));
nilfs_cleaner_flush();
_exit(EXIT_FAILURE);
}
ret = setuid(getuid());
if (unlikely(ret < 0)) {
nilfs_cleaner_logger(
LOG_ERR,
_("Error: failed to drop setuid privileges"));
nilfs_cleaner_flush();
_exit(EXIT_FAILURE);
}
dargs[i++] = cleanerd;
if (protperiod != ULONG_MAX) {
dargs[i++] = cleanerd_protperiod_opt;
snprintf(buf, sizeof(buf), "%lu", protperiod);
dargs[i++] = buf;
}
dargs[i++] = device;
dargs[i++] = mntdir;
dargs[i] = NULL;
sigfillset(&sigs);
sigdelset(&sigs, SIGTRAP);
sigdelset(&sigs, SIGSEGV);
sigprocmask(SIG_UNBLOCK, &sigs, NULL);
close(pipes[0]);
ret = dup2(pipes[1], STDOUT_FILENO);
if (unlikely(ret < 0)) {
nilfs_cleaner_logger(
LOG_ERR,
_("Error: failed to duplicate pipe: %m"));
nilfs_cleaner_flush();
_exit(EXIT_FAILURE);
}
close(pipes[1]);
execv(cleanerd, (char **)dargs);
_exit(EXIT_FAILURE); /* reach only if failed */
} else if (likely(ret > 0)) {
/* parent */
close(pipes[1]);
/*
* fork() returns a pid of the child process, but we
* cannot use it because cleanerd internally fork()s
* and changes pid.
*/
ret = receive_pid(pipes[0], ppid);
if (ret < 0)
*ppid = 0;
/*
* always return a success code because cleanerd has
* already started.
*/
return 0;
}
nilfs_cleaner_logger(LOG_ERR, _("Error: could not fork: %m"));
close(pipes[0]);
close(pipes[1]);
return -1;
}
int nilfs_ping_cleanerd(pid_t pid)
{
return process_is_alive(pid);
}
static void recalc_backoff_time(struct timespec *interval)
{
/* binary exponential backoff */
interval->tv_sec <<= 1;
interval->tv_nsec <<= 1;
if (interval->tv_nsec >= 1000000000) {
ldiv_t q = ldiv(interval->tv_nsec, 1000000000);
interval->tv_sec += q.quot;
interval->tv_nsec = q.rem;
}
}
static int nilfs_wait_cleanerd(const char *device, pid_t pid)
{
struct timespec waittime;
struct timespec start, end, now;
int ret;
if (!process_is_alive(pid))
return 0;
ret = clock_gettime(CLOCK_MONOTONIC, &start);
if (unlikely(ret < 0)) {
nilfs_cleaner_logger(LOG_ERR,
_("failed to get monotonic clock: %s"),
strerror(errno));
return -1;
}
waittime.tv_sec = 0;
waittime.tv_nsec = WAIT_CLEANERD_MIN_BACKOFF_TIME * 1000;
end.tv_sec = start.tv_sec + WAIT_CLEANERD_MAX_BACKOFF_TIME;
end.tv_nsec = start.tv_nsec;
for (;;) {
ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &waittime, NULL);
if (ret < 0 && errno == EINTR)
return -1;
if (!process_is_alive(pid))
return 0;
ret = clock_gettime(CLOCK_MONOTONIC, &now);
if (unlikely(ret < 0) || !timespeccmp(&now, &end, <))
break;
recalc_backoff_time(&waittime);
}
nilfs_cleaner_printf(_("cleanerd (pid=%ld) still exists on %s. waiting."),
(long)pid, device);
nilfs_cleaner_flush();
waittime.tv_sec = WAIT_CLEANERD_RETRY_INTERVAL;
waittime.tv_nsec = 0;
end.tv_sec = start.tv_sec + WAIT_CLEANERD_RETRY_TIMEOUT;
end.tv_nsec = start.tv_nsec;
for (;;) {
ret = clock_gettime(CLOCK_MONOTONIC, &now);
if (unlikely(ret < 0) || !timespeccmp(&now, &end, <))
break;
ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &waittime, NULL);
if (ret < 0 && errno == EINTR) {
nilfs_cleaner_printf(_("interrupted\n"));
nilfs_cleaner_flush();
return -1;
}
if (!process_is_alive(pid)) {
nilfs_cleaner_printf(_("done\n"));
nilfs_cleaner_flush();
return 0;
}
nilfs_cleaner_printf(_("."));
nilfs_cleaner_flush();
}
nilfs_cleaner_printf(_("failed\n"));
nilfs_cleaner_flush();
return -1; /* wait failed */
}
int nilfs_shutdown_cleanerd(const char *device, pid_t pid)
{
int ret;
nilfs_cleaner_logger(LOG_INFO, _("kill cleanerd (pid=%ld) on %s"),
(long)pid, device);
if (kill(pid, SIGTERM) < 0) {
int errsv = errno;
ret = 0;
if (errsv == ESRCH) {
goto out;
} else {
nilfs_cleaner_logger(
LOG_ERR, _("Error: cannot kill cleanerd: %s"),
strerror(errsv));
ret = -1;
goto out;
}
}
ret = nilfs_wait_cleanerd(device, pid);
if (ret < 0)
nilfs_cleaner_logger(LOG_INFO, _("wait timeout"));
else
nilfs_cleaner_logger(LOG_INFO,
_("cleanerd (pid=%ld) stopped"),
pid);
out:
return ret;
}
|