2024-08-29 05:53:47 +00:00
|
|
|
#include "flock/flock.h"
|
|
|
|
|
2024-09-10 09:16:49 +00:00
|
|
|
#include "lib/obstacle.h"
|
2024-09-16 21:10:41 +00:00
|
|
|
#include "lib/runtime.h"
|
2024-08-29 05:53:47 +00:00
|
|
|
#include "lib/string.h"
|
|
|
|
#include "lib/timer.h"
|
|
|
|
#include "sysdep/unix/unix.h"
|
2024-09-09 20:19:44 +00:00
|
|
|
#include "sysdep/unix/io-loop.h"
|
2024-08-29 05:53:47 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2024-09-29 11:44:35 +00:00
|
|
|
#include <linux/mount.h>
|
2024-08-29 05:53:47 +00:00
|
|
|
#include <poll.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <signal.h>
|
2024-09-16 21:10:41 +00:00
|
|
|
#include <stdatomic.h>
|
2024-08-29 05:53:47 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/resource.h>
|
2024-09-29 11:44:35 +00:00
|
|
|
#include <sys/syscall.h>
|
2024-08-29 05:53:47 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/* Overall configuration */
|
|
|
|
struct flock_config flock_config;
|
|
|
|
|
|
|
|
/**
|
2024-09-10 09:16:49 +00:00
|
|
|
* Shutdown routines
|
2024-08-29 05:53:47 +00:00
|
|
|
*/
|
2024-09-10 09:16:49 +00:00
|
|
|
event_list shutdown_event_list;
|
|
|
|
struct shutdown_placeholder shutdown_placeholder;
|
2024-08-29 05:53:47 +00:00
|
|
|
|
2024-09-09 20:19:44 +00:00
|
|
|
static void
|
|
|
|
reboot_event_hook(void *data UNUSED)
|
|
|
|
{
|
|
|
|
log(L_ERR "Reboot requested but not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
poweroff_event_hook(void *data UNUSED)
|
|
|
|
{
|
2024-09-10 09:16:49 +00:00
|
|
|
log(L_INFO "Shutdown requested.");
|
|
|
|
ev_run_list(&shutdown_event_list);
|
|
|
|
}
|
|
|
|
|
2024-09-11 11:03:02 +00:00
|
|
|
static void
|
|
|
|
child_event_hook(void *data UNUSED)
|
|
|
|
{
|
|
|
|
log(L_INFO "Zombie elimination routine invoked.");
|
|
|
|
while (1) {
|
|
|
|
int status;
|
|
|
|
pid_t p = waitpid(-1, &status, WNOHANG);
|
|
|
|
|
|
|
|
if (p < 0)
|
|
|
|
{
|
|
|
|
log(L_ERR "Zombie elimination failed: %m");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const char *coreinfo = WCOREDUMP(status) ? " (core dumped)" : "";
|
|
|
|
|
|
|
|
if (WIFEXITED(status))
|
|
|
|
log(L_INFO "Process %d ended with status %d%s", p, WEXITSTATUS(status), coreinfo);
|
|
|
|
else if (WIFSIGNALED(status))
|
|
|
|
log(L_INFO "Process %d exited by signal %d (%s)%s", p, WTERMSIG(status), strsignal(WTERMSIG(status)), coreinfo);
|
|
|
|
else
|
|
|
|
log(L_ERR "Process %d exited with a strange status %d", p, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-10 09:16:49 +00:00
|
|
|
event reboot_event = { .hook = reboot_event_hook },
|
2024-09-11 11:03:02 +00:00
|
|
|
poweroff_event = { .hook = poweroff_event_hook },
|
|
|
|
child_event = { .hook = child_event_hook };
|
2024-09-10 09:16:49 +00:00
|
|
|
|
|
|
|
callback shutdown_done_callback;
|
|
|
|
|
|
|
|
static void
|
|
|
|
shutdown_done(callback *c UNUSED)
|
|
|
|
{
|
|
|
|
log(L_INFO "Shutdown finished.");
|
2024-09-09 20:19:44 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2024-09-10 09:16:49 +00:00
|
|
|
/**
|
|
|
|
* Signal handling
|
|
|
|
*
|
|
|
|
* We wanna behave as the init process inside the newly create PID namespace
|
|
|
|
* which means that the signals have different meanings than for other processes,
|
|
|
|
* For more information, see pid_namespaces(7).
|
|
|
|
*/
|
2024-08-29 05:53:47 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
hypervisor_reboot_sighandler(int signo UNUSED)
|
|
|
|
{
|
2024-09-09 20:19:44 +00:00
|
|
|
ev_send_loop(&main_birdloop, &reboot_event);
|
2024-08-29 05:53:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hypervisor_poweroff_sighandler(int signo UNUSED)
|
|
|
|
{
|
2024-09-09 20:19:44 +00:00
|
|
|
ev_send_loop(&main_birdloop, &poweroff_event);
|
2024-08-29 05:53:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hypervisor_fail_sighandler(int signo UNUSED)
|
|
|
|
{
|
|
|
|
int e = fork();
|
|
|
|
if (e == 0)
|
|
|
|
{
|
|
|
|
signal(SIGABRT, SIG_DFL);
|
|
|
|
abort();
|
|
|
|
}
|
2024-09-13 10:29:04 +00:00
|
|
|
|
2024-08-29 05:53:47 +00:00
|
|
|
if (e > 0)
|
|
|
|
waitpid(e, NULL, 0);
|
|
|
|
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
2024-09-11 11:03:02 +00:00
|
|
|
static void
|
|
|
|
hypervisor_child_sighandler(int signo UNUSED)
|
|
|
|
{
|
|
|
|
ev_send_loop(&main_birdloop, &child_event);
|
|
|
|
}
|
2024-08-29 05:53:47 +00:00
|
|
|
|
2024-09-13 10:29:04 +00:00
|
|
|
/*
|
2024-08-29 05:53:47 +00:00
|
|
|
* The Main.
|
|
|
|
*
|
|
|
|
* Bootstrapping and all the fiddling around before anything can actually
|
|
|
|
* be really executed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define SYSCALL(x, ...) ({ int e = x(__VA_ARGS__); if (e < 0) die("Failed to run %s at %s:%d: %m", #x, __FILE__, __LINE__); e; })
|
|
|
|
|
|
|
|
#define KILLABLE_SIGNALS SIGINT, SIGTERM, SIGHUP, SIGQUIT
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
usage(FILE *f)
|
|
|
|
{
|
|
|
|
fprintf(f,
|
2024-09-13 10:29:04 +00:00
|
|
|
"Usage: %s [options] name\n\n"
|
|
|
|
"Runs Flock hypervisor with the given name.\n"
|
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
"\t-s <path>\topen control socket at this path\n"
|
|
|
|
"\t-l \tshortcut for -s ./<name>.ctl\n"
|
|
|
|
"\n",
|
2024-08-29 05:53:47 +00:00
|
|
|
flock_config.exec_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv, char **argh UNUSED)
|
|
|
|
{
|
|
|
|
/* Prepare necessary infrastructure */
|
|
|
|
the_bird_lock();
|
|
|
|
times_update();
|
|
|
|
resource_init();
|
|
|
|
random_init();
|
|
|
|
|
|
|
|
birdloop_init();
|
2024-09-09 20:19:44 +00:00
|
|
|
|
2024-09-16 21:10:41 +00:00
|
|
|
struct global_runtime gr = *atomic_load_explicit(&global_runtime, memory_order_relaxed);
|
2024-10-01 08:59:07 +00:00
|
|
|
gr.alloc.keep_mem_max_global = BIRD_ALIGN(65536, page_size);
|
|
|
|
gr.alloc.keep_mem_max_local = BIRD_ALIGN(16384, page_size);
|
|
|
|
gr.alloc.at_once = BIRD_ALIGN(4096, page_size);
|
|
|
|
|
2024-09-16 21:10:41 +00:00
|
|
|
// gr.latency_debug = ~0;
|
|
|
|
switch_runtime(&gr);
|
|
|
|
|
2024-09-09 20:19:44 +00:00
|
|
|
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");
|
2024-09-10 09:16:49 +00:00
|
|
|
|
|
|
|
/* Shutdown hooks */
|
|
|
|
ev_init_list(&shutdown_event_list, &main_birdloop, "Shutdown event list");
|
|
|
|
callback_init(&shutdown_done_callback, shutdown_done, &main_birdloop);
|
|
|
|
obstacle_target_init(
|
|
|
|
&shutdown_placeholder.obstacles,
|
|
|
|
&shutdown_done_callback, &root_pool, "Shutdown");
|
|
|
|
|
2024-08-29 05:53:47 +00:00
|
|
|
boot_time = current_time();
|
|
|
|
|
|
|
|
log_switch(1, NULL, NULL);
|
|
|
|
|
2024-09-04 19:33:43 +00:00
|
|
|
/* Find the original UID/GIDs */
|
|
|
|
uid_t euid = geteuid(), egid = getegid();
|
|
|
|
|
2024-08-29 05:53:47 +00:00
|
|
|
/* Parse args */
|
|
|
|
flock_config.exec_name = argv[0] ?: "flock-sim";
|
|
|
|
int opt;
|
2024-09-13 10:29:04 +00:00
|
|
|
bool csp_local = 0;
|
2024-09-04 19:33:43 +00:00
|
|
|
while ((opt = getopt(argc, argv, "ls:")) != -1)
|
2024-08-29 05:53:47 +00:00
|
|
|
{
|
2024-09-04 19:33:43 +00:00
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case 'l':
|
2024-09-13 10:29:04 +00:00
|
|
|
csp_local = 1;
|
2024-09-04 19:33:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
2024-09-13 10:29:04 +00:00
|
|
|
csp_local = 0;
|
2024-09-04 19:33:43 +00:00
|
|
|
flock_config.control_socket_path = mb_strdup(&root_pool, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
usage(stderr);
|
|
|
|
return 2;
|
|
|
|
}
|
2024-08-29 05:53:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get hypervisor name */
|
|
|
|
if (optind != argc - 1)
|
|
|
|
{
|
|
|
|
usage(stderr);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
flock_config.hypervisor_name = argv[optind];
|
|
|
|
|
2024-09-13 10:29:04 +00:00
|
|
|
/* Fix the control socket path if -l was given */
|
|
|
|
if (csp_local)
|
|
|
|
flock_config.control_socket_path = mb_sprintf(&root_pool, "./%s.ctl", flock_config.hypervisor_name);
|
|
|
|
else if (!flock_config.control_socket_path)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "No control socket path given, use -s or -l.");
|
|
|
|
usage(stderr);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2024-08-29 05:53:47 +00:00
|
|
|
/* Mask signals for forking and other fragile stuff */
|
|
|
|
sigset_t oldmask;
|
|
|
|
sigset_t newmask;
|
|
|
|
sigemptyset(&newmask);
|
|
|
|
#define FROB(x) sigaddset(&newmask, x);
|
|
|
|
MACRO_FOREACH(FROB, KILLABLE_SIGNALS);
|
|
|
|
#undef FROB
|
|
|
|
sigprocmask(SIG_BLOCK, &newmask, &oldmask);
|
|
|
|
|
2024-09-04 19:33:43 +00:00
|
|
|
/* First we need to create the PID + mount + user namespace to acquire capabilities,
|
|
|
|
* and also time namespace for good measure */
|
|
|
|
SYSCALL(unshare, CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWUSER | CLONE_NEWTIME);
|
2024-08-29 05:53:47 +00:00
|
|
|
|
|
|
|
/* Then we have to fork() to become PID 1 of the new PID namespace */
|
|
|
|
pid_t init_pid = fork();
|
|
|
|
if (init_pid < 0)
|
|
|
|
die("Failed to become init: %m");
|
|
|
|
|
|
|
|
/* The parent process may end now
|
|
|
|
* TODO: allow wait() and/or writing PIDfile
|
|
|
|
* instead of just ending */
|
|
|
|
if (init_pid > 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* We also need to fix some UID/GID mappings to become local root.
|
|
|
|
* TODO: this will need an upgrade for full-scale containers. */
|
|
|
|
#define WRITE_ONCE(file, data, len) do { \
|
|
|
|
int fd = SYSCALL(open, file, O_WRONLY); \
|
|
|
|
int e = write(fd, data, len); \
|
|
|
|
if (e != len) die("Failed to write %s to %s", data, file); \
|
|
|
|
close(fd); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
{
|
|
|
|
char fixer[256];
|
|
|
|
int len = bsnprintf(fixer, sizeof fixer, "0 %d 1", euid);
|
|
|
|
WRITE_ONCE("/proc/self/uid_map", fixer, len);
|
|
|
|
|
|
|
|
WRITE_ONCE("/proc/self/setgroups", "deny", sizeof "deny");
|
|
|
|
|
|
|
|
len = bsnprintf(fixer, sizeof fixer, "0 %d 1", egid);
|
|
|
|
WRITE_ONCE("/proc/self/gid_map", fixer, len);
|
|
|
|
}
|
|
|
|
#undef WRITE_ONCE
|
|
|
|
|
|
|
|
/* Remounting proc to reflect the new PID namespace */
|
|
|
|
SYSCALL(mount, "none", "/", NULL, MS_REC | MS_PRIVATE, NULL);
|
|
|
|
SYSCALL(mount, "proc", "/proc", "proc", MS_NOSUID | MS_NODEV | MS_NOEXEC, NULL);
|
|
|
|
|
|
|
|
/* Now we are init but in the original network namespace,
|
|
|
|
* let's spawn a child to do external communication before unsharing */
|
|
|
|
hypervisor_exposed_fork();
|
|
|
|
|
2024-10-02 12:48:26 +00:00
|
|
|
/* And now we can unshare the networks */
|
2024-09-04 19:33:43 +00:00
|
|
|
SYSCALL(unshare, CLONE_NEWNET);
|
2024-08-29 05:53:47 +00:00
|
|
|
|
2024-10-02 12:48:26 +00:00
|
|
|
/* Before running in multiple threads, we also need to fork the container forker */
|
2024-09-17 20:51:19 +00:00
|
|
|
hypervisor_container_fork();
|
|
|
|
|
2024-10-02 12:48:26 +00:00
|
|
|
/* Control socket needs to exist */
|
|
|
|
hypervisor_control_socket();
|
|
|
|
|
2024-08-29 05:53:47 +00:00
|
|
|
/* Set signal handlers as this process is init in its PID namespace */
|
|
|
|
signal(SIGTERM, hypervisor_poweroff_sighandler);
|
|
|
|
signal(SIGINT, hypervisor_poweroff_sighandler);
|
|
|
|
signal(SIGHUP, hypervisor_reboot_sighandler);
|
|
|
|
signal(SIGQUIT, hypervisor_fail_sighandler);
|
2024-09-11 11:03:02 +00:00
|
|
|
signal(SIGCHLD, hypervisor_child_sighandler);
|
2024-08-29 05:53:47 +00:00
|
|
|
|
|
|
|
/* Unblock signals */
|
|
|
|
sigprocmask(SIG_SETMASK, &oldmask, NULL);
|
|
|
|
|
|
|
|
/* Check limits */
|
|
|
|
struct rlimit corelimit;
|
|
|
|
getrlimit(RLIMIT_CORE, &corelimit);
|
|
|
|
log(L_INFO "Core limit %u %u", corelimit.rlim_cur, corelimit.rlim_max);
|
|
|
|
|
2024-09-04 19:33:43 +00:00
|
|
|
/* Run worker threads */
|
|
|
|
struct thread_config tc = {};
|
|
|
|
bird_thread_commit(&tc);
|
|
|
|
|
2024-08-29 05:53:47 +00:00
|
|
|
/* Wait for Godot */
|
|
|
|
log(L_INFO "Hypervisor running");
|
2024-09-10 09:27:28 +00:00
|
|
|
birdloop_minimalist_main();
|
2024-08-29 05:53:47 +00:00
|
|
|
}
|