/* Micro-init bootstrap. The real policy lives in the ring-3 init binary; this * file only loads it, records its pid and restarts it if it ever exits. */ #include "init.h" #include "process.h" #include "serial.h" #include "timer.h" #include "vfs.h" static unsigned int init_pid; static unsigned int init_last_start; static int init_supported = -1; /* -1 unknown, 0 no binary, 1 present */ unsigned int init_server_pid(void) { return init_pid; } void init_server_note_pid(unsigned int pid) { init_pid = pid; } int init_server_start(void) { struct vfs_file f; struct vfs_mount *m = 0; unsigned int now = get_ticks_ms(); int pid; if (init_supported == 0) return 0; if (init_pid && process_is_alive(init_pid)) return 1; if (init_last_start && now - init_last_start < 1000) return 0; init_last_start = now; if (vfs_load_file("/ramfs/bin/init", &f, &m) != 0) { init_supported = 0; serial_puts(SERIAL_COM1, "init: /ramfs/bin/init not found; no WM\n"); return 0; } init_supported = 1; pid = process_create_user_full("init", f.data, f.size, "", "/", 0, 0, PROC_CREATE_INIT); vfs_release_file(m, &f); if (pid <= 0) { serial_puts(SERIAL_COM1, "init: cannot start /ramfs/bin/init\n"); return 0; } process_set_weight((unsigned int)pid, 12); serial_puts(SERIAL_COM1, "init: started\n"); return 1; } void init_server_supervise(void) { if (init_supported == 0) return; if (init_pid && process_is_alive(init_pid)) return; if (init_pid) { serial_puts(SERIAL_COM1, "init: process died; restarting\n"); init_pid = 0; } /* init_server_start() throttles itself, so a crash loop retries once a * second and a startup failure is not retried at the kworker rate. */ init_server_start(); }