- case DAEMON_CONTROL_CHECK:
- if (pid)
- st = DAEMON_STATUS_OK;
- else
- st = DAEMON_STATUS_NOT_RUNNING;
- break;
- case DAEMON_CONTROL_START:
- if (pid)
- st = DAEMON_STATUS_ALREADY_DONE;
- else
- {
- pid_t pp = fork();
- if (pp < 0)
- {
- daemon_control_err(dc, "Cannot fork: %m");
- goto done;
- }
- if (pp)
- {
- close(guard_fd);
- execvp(dc->argv[0], dc->argv);
- fprintf(stderr, "Cannot execute `%s': %m\n", dc->argv[0]);
- exit(DAEMON_STATUS_ERROR);
- }
- int stat;
- int ec = waitpid(pp, &stat, 0);
- if (ec < 0)
- {
- daemon_control_err(dc, "Cannot wait: %m");
- goto done;
- }
- if (WIFEXITED(stat) && WEXITSTATUS(stat) == DAEMON_STATUS_ERROR)
- {
- daemon_control_err(dc, "Cannot execute the daemon");
- goto done;
- }
- char ecmsg[EXIT_STATUS_MSG_SIZE];
- if (format_exit_status(ecmsg, stat))
- {
- daemon_control_err(dc, "Daemon %s", ecmsg);
- goto done;
- }
- pid = daemon_read_pid(dc);
- if (!pid)
- daemon_control_err(dc, "Daemon failed to write the PID file `%s'", dc->pid_file);
- else
- st = DAEMON_STATUS_OK;
- }
- break;
- case DAEMON_CONTROL_STOP:
- if (!pid)
- return DAEMON_STATUS_ALREADY_DONE;
- int sig = dc->signal ? : SIGTERM;
- if (kill(pid, sig) < 0)