#include "lib/timer.h"
#include "sysdep/unix/unix.h"
+#include "sysdep/unix/io-loop.h"
#include <errno.h>
#include <fcntl.h>
* For more information, see pid_namespaces(7).
*/
-static sig_atomic_t signal_received;
-#define SIGREQ_REBOOT 1
-#define SIGREQ_POWEROFF 2
-#define SIGREQ_FAIL 4
+static void
+reboot_event_hook(void *data UNUSED)
+{
+ 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
hypervisor_reboot_sighandler(int signo UNUSED)
{
- signal_received |= SIGREQ_REBOOT;
+ ev_send_loop(&main_birdloop, &reboot_event);
}
static void
hypervisor_poweroff_sighandler(int signo UNUSED)
{
- signal_received |= SIGREQ_POWEROFF;
+ ev_send_loop(&main_birdloop, &poweroff_event);
}
static void
hypervisor_fail_sighandler(int signo UNUSED)
{
- signal_received |= SIGREQ_FAIL;
-
int e = fork();
if (e == 0)
{
random_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();
log_switch(1, NULL, NULL);
log(L_INFO "Hypervisor running");
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);
+
+ bool events =
+ !ev_list_empty(&global_event_list) ||
+ !ev_list_empty(&global_work_list) ||
+ !ev_list_empty(&main_birdloop.event_list);
+
+ int poll_tout = (events ? 0 : 3000); /* Time in milliseconds */
+ timer *t;
+ if (t = timers_first(&main_birdloop.time))
+ {
+ times_update();
+ int timeout = (tm_remains(t) TO_MS) + 1;
+ poll_tout = MIN(poll_tout, timeout);
+ }
- uint s = signal_received;
- signal_received &= ~s;
+ struct pollfd pfd = {
+ .fd = main_birdloop.thread->wakeup.fd[0],
+ .events = POLLIN,
+ };
- if (s & SIGREQ_FAIL)
- bug("Fail flag should never propagate from signal");
- else if (s & SIGREQ_POWEROFF)
- return 0;
- else if (s & SIGREQ_REBOOT)
- log(L_ERR "Reboot requested but not implemented");
+ poll(&pfd, 1, poll_tout);
}
}