Serge Bazanski | eac8f73 | 2021-10-05 23:30:37 +0200 | [diff] [blame] | 1 | // minit is a barebones Linux-compatible init (PID 1) process. |
| 2 | // |
| 3 | // Its goal is to run the Metropolis core executable and reap any children that |
| 4 | // it stumbles upon. It does not support running under a TTY and is not |
| 5 | // configurable in any way. |
| 6 | // |
| 7 | // The only reason this exists is because Go's child process reaping (when |
| 8 | // using os/exec.Command) races any PID 1 process reaping, thereby preventing |
| 9 | // running a complex Go binary as PID 1. In the future this might be rewritten |
| 10 | // in a memory-safe language like Zig or Rust, but this implementation will do |
| 11 | // for now, as long as it keeps having basically zero attack surface. |
| 12 | // |
| 13 | // This code has been vaguely inspired by github.com/Yelp/dumb-init and |
| 14 | // github.com/krallin/tini, two already existing minimal init implementations. |
| 15 | // These, however, attempt to handle being run in a TTY and some |
| 16 | // configurability, as they're meant to be run in containers. We don't need any |
| 17 | // of that, and we'd rather have as little C as possible. |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <linux/reboot.h> |
| 21 | #include <signal.h> |
Serge Bazanski | 83a28c9 | 2022-04-19 13:59:38 +0200 | [diff] [blame] | 22 | #include <stdarg.h> |
Serge Bazanski | eac8f73 | 2021-10-05 23:30:37 +0200 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/reboot.h> |
| 27 | #include <sys/wait.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | void handle_signal(pid_t child_pid, int signum); |
| 31 | |
Serge Bazanski | 83a28c9 | 2022-04-19 13:59:38 +0200 | [diff] [blame] | 32 | #define NUM_CONSOLES 3 |
| 33 | FILE *consoles[NUM_CONSOLES] = {}; |
| 34 | |
| 35 | // open_consoles populates the consoles array with FILE pointers to opened |
| 36 | // character devices that should receive log messages. Some of these pointers |
| 37 | // are likely to be null, meaning that particular console is not available. |
| 38 | void open_consoles() { |
| 39 | consoles[0] = fopen("/dev/console", "w"); |
| 40 | consoles[1] = fopen("/dev/tty0", "w"); |
| 41 | consoles[2] = fopen("/dev/ttyS0", "w"); |
| 42 | |
| 43 | // Set all open consoles to be line-buffered. |
| 44 | for (int i = 0; i < NUM_CONSOLES; i++) { |
| 45 | if (consoles[i] == NULL) { |
| 46 | continue; |
| 47 | } |
| 48 | setvbuf(consoles[i], NULL, _IOLBF, BUFSIZ); |
| 49 | } |
| 50 | |
| 51 | // TODO(q3k): disable hardware and software flow control on TTYs. This |
| 52 | // shouldn't be necessary on our current platform, but should be ensured |
| 53 | // regardless, to make sure we never block writing to any console. |
| 54 | } |
| 55 | |
| 56 | // cprintf emits a format string to all opened consoles. |
| 57 | void cprintf(const char *fmt, ...) { |
| 58 | va_list args; |
| 59 | va_start(args, fmt); |
| 60 | |
| 61 | for (int i = 0; i < NUM_CONSOLES; i++) { |
| 62 | FILE *console = consoles[i]; |
| 63 | if (console == NULL) { |
| 64 | continue; |
| 65 | } |
| 66 | vfprintf(console, fmt, args); |
| 67 | } |
| 68 | |
| 69 | va_end(args); |
| 70 | } |
| 71 | |
Serge Bazanski | eac8f73 | 2021-10-05 23:30:37 +0200 | [diff] [blame] | 72 | int main() { |
| 73 | // Block all signals. We'll unblock them in the child. |
| 74 | sigset_t all_signals; |
| 75 | sigfillset(&all_signals); |
| 76 | sigprocmask(SIG_BLOCK, &all_signals, NULL); |
| 77 | |
Serge Bazanski | 83a28c9 | 2022-04-19 13:59:38 +0200 | [diff] [blame] | 78 | open_consoles(); |
| 79 | |
Serge Bazanski | eac8f73 | 2021-10-05 23:30:37 +0200 | [diff] [blame] | 80 | // Say hello. |
Serge Bazanski | 83a28c9 | 2022-04-19 13:59:38 +0200 | [diff] [blame] | 81 | cprintf( |
Serge Bazanski | eac8f73 | 2021-10-05 23:30:37 +0200 | [diff] [blame] | 82 | "\n" |
| 83 | " Metropolis Cluster Operating System\n" |
Serge Bazanski | 83a28c9 | 2022-04-19 13:59:38 +0200 | [diff] [blame] | 84 | " Copyright 2020-2022 The Monogon Project Authors\n" |
Serge Bazanski | eac8f73 | 2021-10-05 23:30:37 +0200 | [diff] [blame] | 85 | "\n" |
| 86 | ); |
| 87 | |
| 88 | |
| 89 | pid_t pid = fork(); |
| 90 | if (pid < 0) { |
Serge Bazanski | 83a28c9 | 2022-04-19 13:59:38 +0200 | [diff] [blame] | 91 | cprintf("fork(): %s\n", strerror(errno)); |
Serge Bazanski | eac8f73 | 2021-10-05 23:30:37 +0200 | [diff] [blame] | 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | if (pid == 0) { |
| 96 | // In the child. Unblock all signals. |
| 97 | sigprocmask(SIG_UNBLOCK, &all_signals, NULL); |
| 98 | if (setsid() == -1) { |
Serge Bazanski | 83a28c9 | 2022-04-19 13:59:38 +0200 | [diff] [blame] | 99 | cprintf("setsid: %s\n", strerror(errno)); |
Serge Bazanski | eac8f73 | 2021-10-05 23:30:37 +0200 | [diff] [blame] | 100 | return 1; |
| 101 | } |
| 102 | |
| 103 | // Then, start the core executable. |
| 104 | char *argv[] = { |
| 105 | "/core", |
| 106 | NULL, |
| 107 | }; |
| 108 | execvp(argv[0], argv); |
Serge Bazanski | 83a28c9 | 2022-04-19 13:59:38 +0200 | [diff] [blame] | 109 | cprintf("execvpe(/core) failed: %s\n", strerror(errno)); |
Serge Bazanski | eac8f73 | 2021-10-05 23:30:37 +0200 | [diff] [blame] | 110 | return 1; |
| 111 | } |
| 112 | |
| 113 | // In the parent. Wait for any signal, then handle it and any other pending |
| 114 | // ones. |
| 115 | for (;;) { |
| 116 | int signum; |
| 117 | sigwait(&all_signals, &signum); |
| 118 | handle_signal(pid, signum); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // handle_signal is called by the main reap loop for every signal received. It |
| 123 | // reaps children if SIGCHLD is received, and otherwise dispatches the signal to |
| 124 | // its direct child. |
| 125 | void handle_signal(pid_t child_pid, int signum) { |
| 126 | // Anything other than SIGCHLD should just be forwarded to the child. |
| 127 | if (signum != SIGCHLD) { |
| 128 | kill(-child_pid, signum); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | // A SIGCHLD was received. Go through all children and reap them, checking |
| 133 | // if any of them is our direct child. |
| 134 | |
| 135 | // exit_status will be set if the direct child process exited. |
| 136 | int exit_status = -1; |
| 137 | |
| 138 | pid_t killed_pid; |
| 139 | int status; |
| 140 | while ((killed_pid = waitpid(-1, &status, WNOHANG)) > 0) { |
| 141 | if (killed_pid != child_pid) { |
| 142 | // Something else than our direct child died, just reap it. |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | // Our direct child exited. Translate its status into an exit code. |
| 147 | if (WIFEXITED(status)) { |
| 148 | // For processes which exited, just use the exit code directly. |
| 149 | exit_status = WEXITSTATUS(status); |
| 150 | } else if (WIFSIGNALED(status)) { |
| 151 | // Otherwise, emulate what sh/bash do and return 128 + the signal |
| 152 | // number that the child received. |
| 153 | exit_status = 128 + WTERMSIG(status); |
| 154 | } else { |
| 155 | // Something unexpected happened. Attempt to handle this gracefully, |
| 156 | // but complain. |
Serge Bazanski | 83a28c9 | 2022-04-19 13:59:38 +0200 | [diff] [blame] | 157 | cprintf("child status not EXITED nor SIGNALED: %d\n", status); |
Serge Bazanski | eac8f73 | 2021-10-05 23:30:37 +0200 | [diff] [blame] | 158 | exit_status = 1; |
| 159 | } |
Serge Bazanski | eac8f73 | 2021-10-05 23:30:37 +0200 | [diff] [blame] | 160 | |
Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame^] | 161 | // Direct child exited, let's also exit. |
| 162 | if (exit_status >= 0) { |
| 163 | if (exit_status == 0) { |
| 164 | reboot(LINUX_REBOOT_CMD_RESTART); |
| 165 | return; |
| 166 | } |
| 167 | cprintf("\n Metropolis encountered an uncorrectable error and this node must be restarted.\n"); |
| 168 | cprintf("core exit status: %d\n", exit_status); |
| 169 | sync(); |
Serge Bazanski | 83a28c9 | 2022-04-19 13:59:38 +0200 | [diff] [blame] | 170 | cprintf(" Disks synced, rebooting...\n\n"); |
Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame^] | 171 | reboot(LINUX_REBOOT_CMD_RESTART); |
Serge Bazanski | eac8f73 | 2021-10-05 23:30:37 +0200 | [diff] [blame] | 172 | } |
Serge Bazanski | eac8f73 | 2021-10-05 23:30:37 +0200 | [diff] [blame] | 173 | } |
| 174 | } |