0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-10-18 18:08:45 +00:00

Flock: Converted signalling through sigatomic_t to full event sending

This commit is contained in:
Maria Matejka 2024-09-09 22:19:44 +02:00
parent 1d8fcee05c
commit a8862fd7aa

View File

@ -4,6 +4,7 @@
#include "lib/timer.h" #include "lib/timer.h"
#include "sysdep/unix/unix.h" #include "sysdep/unix/unix.h"
#include "sysdep/unix/io-loop.h"
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
@ -29,28 +30,37 @@ struct flock_config flock_config;
* For more information, see pid_namespaces(7). * For more information, see pid_namespaces(7).
*/ */
static sig_atomic_t signal_received; static void
#define SIGREQ_REBOOT 1 reboot_event_hook(void *data UNUSED)
#define SIGREQ_POWEROFF 2 {
#define SIGREQ_FAIL 4 log(L_ERR "Reboot requested but not implemented");
}
static void
poweroff_event_hook(void *data UNUSED)
{
log(L_INFO "Shutdown requested. TODO: properly clean up");
exit(0);
}
static event reboot_event = { .hook = reboot_event_hook },
poweroff_event = { .hook = poweroff_event_hook };
static void static void
hypervisor_reboot_sighandler(int signo UNUSED) hypervisor_reboot_sighandler(int signo UNUSED)
{ {
signal_received |= SIGREQ_REBOOT; ev_send_loop(&main_birdloop, &reboot_event);
} }
static void static void
hypervisor_poweroff_sighandler(int signo UNUSED) hypervisor_poweroff_sighandler(int signo UNUSED)
{ {
signal_received |= SIGREQ_POWEROFF; ev_send_loop(&main_birdloop, &poweroff_event);
} }
static void static void
hypervisor_fail_sighandler(int signo UNUSED) hypervisor_fail_sighandler(int signo UNUSED)
{ {
signal_received |= SIGREQ_FAIL;
int e = fork(); int e = fork();
if (e == 0) if (e == 0)
{ {
@ -95,6 +105,10 @@ main(int argc, char **argv, char **argh UNUSED)
random_init(); random_init();
birdloop_init(); birdloop_init();
ev_init_list(&global_event_list, &main_birdloop, "Global event list");
ev_init_list(&global_work_list, &main_birdloop, "Global work list");
ev_init_list(&main_birdloop.event_list, &main_birdloop, "Global fast event list");
boot_time = current_time(); boot_time = current_time();
log_switch(1, NULL, NULL); log_switch(1, NULL, NULL);
@ -216,16 +230,31 @@ main(int argc, char **argv, char **argh UNUSED)
log(L_INFO "Hypervisor running"); log(L_INFO "Hypervisor running");
while (1) while (1)
{ {
pause(); times_update();
ev_run_list(&global_event_list);
ev_run_list(&global_work_list);
ev_run_list(&main_birdloop.event_list);
timers_fire(&main_birdloop.time);
uint s = signal_received; bool events =
signal_received &= ~s; !ev_list_empty(&global_event_list) ||
!ev_list_empty(&global_work_list) ||
!ev_list_empty(&main_birdloop.event_list);
if (s & SIGREQ_FAIL) int poll_tout = (events ? 0 : 3000); /* Time in milliseconds */
bug("Fail flag should never propagate from signal"); timer *t;
else if (s & SIGREQ_POWEROFF) if (t = timers_first(&main_birdloop.time))
return 0; {
else if (s & SIGREQ_REBOOT) times_update();
log(L_ERR "Reboot requested but not implemented"); int timeout = (tm_remains(t) TO_MS) + 1;
poll_tout = MIN(poll_tout, timeout);
}
struct pollfd pfd = {
.fd = main_birdloop.thread->wakeup.fd[0],
.events = POLLIN,
};
poll(&pfd, 1, poll_tout);
} }
} }