X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Fdaemon-ctrl.c;h=21132e3241a5f0d0b649b9bf632612a644b7f90b;hb=4d8858ba28597c458bef262399b553e5da89442a;hp=8d9bf72fbc62b6a1dda99e7bef03d56a7f563410;hpb=633be7d5705507f6bd9b0284f571660a6a325305;p=libucw.git diff --git a/ucw/daemon-ctrl.c b/ucw/daemon-ctrl.c index 8d9bf72f..21132e32 100644 --- a/ucw/daemon-ctrl.c +++ b/ucw/daemon-ctrl.c @@ -32,8 +32,10 @@ daemon_control_err(struct daemon_control_params *dc, char *msg, ...) } static int -daemon_read_pid(struct daemon_control_params *dc, int will_wait) +daemon_read_pid(struct daemon_control_params *dc, int will_wait, int *stalep) { + *stalep = 0; + int pid_fd = open(dc->pid_file, O_RDONLY); if (pid_fd < 0) { @@ -47,6 +49,7 @@ daemon_read_pid(struct daemon_control_params *dc, int will_wait) { // The lock file is stale close(pid_fd); + *stalep = 1; return 0; } @@ -87,6 +90,7 @@ enum daemon_control_status daemon_control(struct daemon_control_params *dc) { enum daemon_control_status st = DAEMON_STATUS_ERROR; + int sig, stale, stale2; int guard_fd = open(dc->guard_file, O_RDWR | O_CREAT, 0666); if (guard_fd < 0) @@ -95,7 +99,7 @@ daemon_control(struct daemon_control_params *dc) return daemon_control_err(dc, "Cannot lock guard file `%s': %m", dc->guard_file); // Read the PID file - int pid = daemon_read_pid(dc, 0); + int pid = daemon_read_pid(dc, 0, &stale); if (pid < 0) goto done; @@ -104,6 +108,8 @@ daemon_control(struct daemon_control_params *dc) case DAEMON_CONTROL_CHECK: if (pid) st = DAEMON_STATUS_OK; + else if (stale) + st = DAEMON_STATUS_STALE; else st = DAEMON_STATUS_NOT_RUNNING; break; @@ -118,7 +124,7 @@ daemon_control(struct daemon_control_params *dc) daemon_control_err(dc, "Cannot fork: %m"); goto done; } - if (pp) + if (!pp) { close(guard_fd); execvp(dc->argv[0], dc->argv); @@ -140,26 +146,26 @@ daemon_control(struct daemon_control_params *dc) char ecmsg[EXIT_STATUS_MSG_SIZE]; if (format_exit_status(ecmsg, stat)) { - daemon_control_err(dc, "Daemon %s", ecmsg); + daemon_control_err(dc, "Daemon %s %s", dc->argv[0], ecmsg); goto done; } - pid = daemon_read_pid(dc, 0); + pid = daemon_read_pid(dc, 0, &stale2); if (!pid) - daemon_control_err(dc, "Daemon failed to write the PID file `%s'", dc->pid_file); + daemon_control_err(dc, "Daemon %s failed to write the PID file `%s'", dc->argv[0], dc->pid_file); else - st = DAEMON_STATUS_OK; + st = stale ? DAEMON_STATUS_STALE : DAEMON_STATUS_OK; } break; case DAEMON_CONTROL_STOP: if (!pid) - return DAEMON_STATUS_ALREADY_DONE; - int sig = dc->signal ? : SIGTERM; + return stale ? DAEMON_STATUS_STALE : DAEMON_STATUS_ALREADY_DONE; + sig = dc->signal ? : SIGTERM; if (kill(pid, sig) < 0) { - daemon_control_err(dc, "Cannot send signal %d: %m", dc->signal); + daemon_control_err(dc, "Cannot send signal %d: %m", sig); goto done; } - pid = daemon_read_pid(dc, 1); + pid = daemon_read_pid(dc, 1, &stale2); ASSERT(pid <= 0); if (!pid) st = DAEMON_STATUS_OK; @@ -167,8 +173,9 @@ daemon_control(struct daemon_control_params *dc) case DAEMON_CONTROL_SIGNAL: if (!pid) return DAEMON_STATUS_NOT_RUNNING; - if (kill(pid, dc->signal) < 0) - daemon_control_err(dc, "Cannot send signal %d: %m", dc->signal); + sig = dc->signal ? : SIGHUP; + if (kill(pid, sig) < 0) + daemon_control_err(dc, "Cannot send signal %d: %m", sig); else st = DAEMON_STATUS_OK; break;