2018-11-23 13:13:23 +01:00
|
|
|
/* SPDX-License-Identifier: ISC */
|
2018-02-25 14:33:19 +01:00
|
|
|
#include "init.h"
|
|
|
|
|
2018-08-24 16:51:15 +02:00
|
|
|
static int sigfd = -1;
|
2019-03-17 18:33:12 +01:00
|
|
|
static int sockfd = -1;
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-08-24 16:51:15 +02:00
|
|
|
static void handle_signal(void)
|
2018-02-25 14:33:19 +01:00
|
|
|
{
|
|
|
|
struct signalfd_siginfo info;
|
|
|
|
int status;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
if (read(sigfd, &info, sizeof(info)) != sizeof(info)) {
|
|
|
|
perror("read on signal fd");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (info.ssi_signo) {
|
|
|
|
case SIGCHLD:
|
2018-04-07 15:35:19 +02:00
|
|
|
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
|
2018-02-25 14:33:19 +01:00
|
|
|
status = WIFEXITED(status) ? WEXITSTATUS(status) :
|
|
|
|
EXIT_FAILURE;
|
|
|
|
|
2018-08-24 16:51:15 +02:00
|
|
|
supervisor_handle_exited(pid, status);
|
2018-02-25 14:33:19 +01:00
|
|
|
}
|
|
|
|
break;
|
2018-08-24 16:54:54 +02:00
|
|
|
case SIGTERM:
|
|
|
|
supervisor_set_target(TGT_SHUTDOWN);
|
|
|
|
break;
|
2018-02-25 14:33:19 +01:00
|
|
|
case SIGINT:
|
2018-08-24 16:53:15 +02:00
|
|
|
supervisor_set_target(TGT_REBOOT);
|
2018-02-25 14:33:19 +01:00
|
|
|
break;
|
2019-03-25 17:16:34 +01:00
|
|
|
case SIGHUP:
|
|
|
|
supervisor_reload_config();
|
|
|
|
break;
|
2019-03-17 18:33:12 +01:00
|
|
|
case SIGUSR1:
|
|
|
|
if (sockfd >= 0) {
|
|
|
|
close(sockfd);
|
|
|
|
unlink(INIT_SOCK_PATH);
|
|
|
|
sockfd = -1;
|
|
|
|
}
|
|
|
|
sockfd = init_socket_create();
|
|
|
|
break;
|
2018-02-25 14:33:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 18:33:12 +01:00
|
|
|
static void handle_request(void)
|
|
|
|
{
|
2019-03-18 14:08:17 +01:00
|
|
|
struct sockaddr_un addr;
|
|
|
|
init_request_t rq;
|
|
|
|
socklen_t addrlen;
|
|
|
|
ssize_t ret;
|
|
|
|
retry:
|
|
|
|
memset(&rq, 0, sizeof(rq));
|
|
|
|
addrlen = sizeof(addr);
|
|
|
|
ret = recvfrom(sockfd, &rq, sizeof(rq), MSG_DONTWAIT | MSG_TRUNC,
|
2019-03-20 20:43:41 +01:00
|
|
|
(struct sockaddr *)&addr, &addrlen);
|
2019-03-18 14:08:17 +01:00
|
|
|
|
|
|
|
if (ret < 0 && errno == EINTR)
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
if ((size_t)ret < sizeof(rq))
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (rq.rq) {
|
|
|
|
case EIR_STATUS:
|
2019-03-27 17:48:13 +01:00
|
|
|
supervisor_answer_status_request(sockfd, &addr, addrlen,
|
|
|
|
rq.arg.status.filter);
|
2019-03-18 14:08:17 +01:00
|
|
|
break;
|
2019-03-28 14:15:39 +01:00
|
|
|
case EIR_START:
|
|
|
|
rq.arg.startstop.id = be32toh(rq.arg.startstop.id);
|
|
|
|
supervisor_start(rq.arg.startstop.id);
|
|
|
|
break;
|
|
|
|
case EIR_STOP:
|
|
|
|
rq.arg.startstop.id = be32toh(rq.arg.startstop.id);
|
|
|
|
supervisor_stop(rq.arg.startstop.id);
|
|
|
|
break;
|
2019-03-18 14:08:17 +01:00
|
|
|
}
|
2019-03-17 18:33:12 +01:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:51:15 +02:00
|
|
|
void target_completed(int target)
|
|
|
|
{
|
|
|
|
switch (target) {
|
|
|
|
case TGT_BOOT:
|
2019-03-17 18:33:12 +01:00
|
|
|
if (sockfd < 0)
|
|
|
|
sockfd = init_socket_create();
|
2018-08-24 16:51:15 +02:00
|
|
|
break;
|
2018-08-24 16:59:28 +02:00
|
|
|
case TGT_SHUTDOWN:
|
|
|
|
for (;;)
|
|
|
|
reboot(RB_POWER_OFF);
|
|
|
|
break;
|
|
|
|
case TGT_REBOOT:
|
|
|
|
for (;;)
|
|
|
|
reboot(RB_AUTOBOOT);
|
2018-08-24 16:51:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 14:33:19 +01:00
|
|
|
int main(void)
|
|
|
|
{
|
2019-03-17 18:33:12 +01:00
|
|
|
int i, ret, count;
|
2018-02-25 14:33:19 +01:00
|
|
|
struct pollfd pfd[2];
|
|
|
|
|
|
|
|
if (getpid() != 1) {
|
|
|
|
fputs("init does not have pid 1, terminating!\n", stderr);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-08-24 16:51:15 +02:00
|
|
|
supervisor_init();
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-08-24 16:51:15 +02:00
|
|
|
sigfd = sigsetup();
|
|
|
|
if (sigfd < 0)
|
2018-04-15 13:21:34 +02:00
|
|
|
return -1;
|
2018-02-25 14:33:19 +01:00
|
|
|
|
|
|
|
for (;;) {
|
2018-08-24 16:51:15 +02:00
|
|
|
while (supervisor_process_queues())
|
|
|
|
;
|
|
|
|
|
|
|
|
memset(pfd, 0, sizeof(pfd));
|
2019-03-17 18:33:12 +01:00
|
|
|
count = 0;
|
|
|
|
|
|
|
|
pfd[count].fd = sigfd;
|
|
|
|
pfd[count].events = POLLIN;
|
|
|
|
++count;
|
|
|
|
|
|
|
|
if (sockfd >= 0) {
|
|
|
|
pfd[count].fd = sockfd;
|
|
|
|
pfd[count].events = POLLIN;
|
|
|
|
++count;
|
|
|
|
}
|
2018-08-24 16:51:15 +02:00
|
|
|
|
2018-04-06 23:49:05 +02:00
|
|
|
ret = poll(pfd, count, -1);
|
2019-03-17 18:33:12 +01:00
|
|
|
if (ret <= 0)
|
|
|
|
continue;
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2019-03-17 18:33:12 +01:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
if (pfd[i].revents & POLLIN) {
|
|
|
|
if (pfd[i].fd == sigfd)
|
|
|
|
handle_signal();
|
|
|
|
if (pfd[i].fd == sockfd)
|
|
|
|
handle_request();
|
|
|
|
}
|
2018-02-25 14:33:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|