/* init: the userland service manager. * * The kernel starts this as PID 1 after /ramfs and /share are mounted. It * owns the WM and the service processes: * - spawns /ramfs/bin/wm, adopts nothing (the kernel never starts a WM), * and relaunches it when it dies; * - starts/stops/restarts services and drains one tty per service into * /share/svc-.log; * - keeps /share/svc-.state and /share/init.state for the CLI/GUI; * - serves a small control protocol on its own tty. * * On restart it kills every leftover ring-3 process first, so a crashed init * always comes back to a clean userland tree. Kernel threads (kworker, * bootscan, netkexec, ...) are left alone. */ #include #include #include #include "service_proto.h" #include "ucmd_lib.h" #define INIT_LOG_MAX 8192u #define LOOP_MS 100u #define START_GRACE_MS 300u #define RESTART_MS 200u #define DRAIN_CHUNK 1024u struct svc_rt { const struct svc_def *def; unsigned int state; unsigned int desired; unsigned int pid; int tty; unsigned int started; unsigned int restart_at; int restart_pending; char *log; unsigned int log_len; int log_dirty; int state_dirty; }; static int ctl_tty; static int wm_pid; static unsigned int wm_restart_at; static struct svc_rt rt[SVC_MAX]; static char init_log[INIT_LOG_MAX]; static unsigned int init_log_len; static int init_log_dirty; static char line[256]; static unsigned int line_len; static unsigned int now_ms(void) { return u_ticks_ms(); } static unsigned int atou(const char *s) { unsigned int n = 0; while (*s >= '0' && *s <= '9') n = n * 10 + (unsigned int)(*s++ - '0'); return n; } /* Append bytes to a bounded linear log, dropping the oldest data first. */ static void append_log(char *buf, unsigned int *len, unsigned int cap, const char *s, unsigned int n) { if (!buf || !cap) return; if (n >= cap) { s += n - (cap - 1); n = cap - 1; } if (*len + n >= cap) { unsigned int drop = *len + n - (cap - 1); memmove(buf, buf + drop, *len - drop); *len -= drop; } memcpy(buf + *len, s, n); *len += n; } static void init_log_add(const char *s) { append_log(init_log, &init_log_len, INIT_LOG_MAX, s, (unsigned int)strlen(s)); init_log_dirty = 1; } static void init_log_addf(const char *fmt, unsigned int a) { char buf[80]; int n = snprintf(buf, sizeof(buf), fmt, a); if (n > 0) init_log_add(buf); } static void init_log_flush(void) { if (!init_log_dirty) return; if (u_write_file(SVC_INIT_LOG, init_log, init_log_len) == 0) init_log_dirty = 0; } static void svc_log_flush(struct svc_rt *s) { char path[64]; if (!s->log_dirty || !s->log || !s->log_len) return; if (svc_make_log_path(path, sizeof(path), s->def->name) == 0 && u_write_file(path, s->log, s->log_len) == 0) s->log_dirty = 0; } static void svc_state_write(struct svc_rt *s) { struct svc_state st; char path[64], buf[256]; if (!s->state_dirty) return; memset(&st, 0, sizeof(st)); { unsigned int i; for (i = 0; s->def->name[i] && i + 1 < sizeof(st.name); i++) st.name[i] = s->def->name[i]; st.name[i] = '\0'; } st.state = s->state; st.desired = s->desired; st.pid = (s->pid && u_alive(s->pid)) ? s->pid : 0; st.tty = s->tty > 0 ? (unsigned int)s->tty : 0; st.started = s->started; if (svc_make_state_path(path, sizeof(path), s->def->name) != 0 || svc_format_state(buf, sizeof(buf), &st) != 0) return; if (u_write_file(path, buf, (unsigned int)strlen(buf)) == 0) s->state_dirty = 0; } static void init_state_write(void) { struct init_state st; char buf[128]; st.pid = (unsigned int)u_getpid(); st.ctl = ctl_tty > 0 ? (unsigned int)ctl_tty : 0; st.wm = (wm_pid > 0 && u_alive((unsigned int)wm_pid)) ? (unsigned int)wm_pid : 0; st.valid = 1; if (svc_format_init_state(buf, sizeof(buf), &st) == 0) u_write_file(SVC_INIT_STATE, buf, (unsigned int)strlen(buf)); } /* Kill every user process except ourselves; kernel threads stay. This is * the clean-slate recovery path after an init crash. */ static void reset_userland(void) { struct sys_procinfo pi; unsigned int pids[64]; unsigned int n = 0, i; int self = u_getpid(); for (i = 0; n < 64 && u_procinfo(i, &pi) == 1; i++) { if (pi.pid && pi.pid != (unsigned int)self && !pi.kernel_thread) pids[n++] = pi.pid; } for (i = 0; i < n; i++) u_kill(pids[i]); if (n) { init_log_addf("init: reset %u user processes\n", n); u_sleep_ms(150); } } static void drain_service(struct svc_rt *s) { char buf[DRAIN_CHUNK]; unsigned int total = 0; int n; if (s->tty <= 0 || !s->log) return; while (total < 8192 && (n = u_tty_read(s->tty, buf, sizeof(buf))) > 0) { append_log(s->log, &s->log_len, SVC_LOG_MAX, buf, (unsigned int)n); s->log_dirty = 1; total += (unsigned int)n; } } static int svc_start(struct svc_rt *s) { char msg[96]; int pid; if (s->pid && u_alive(s->pid)) return 0; if (s->pid) { drain_service(s); if (s->tty > 0) u_tty_close(s->tty); s->pid = 0; s->tty = 0; } if (s->tty <= 0) s->tty = u_tty_alloc(); if (s->tty <= 0) { snprintf(msg, sizeof(msg), "init: %s: no tty\n", s->def->name); init_log_add(msg); snprintf(msg, sizeof(msg), "[init] %s: no tty\n", s->def->name); append_log(s->log, &s->log_len, SVC_LOG_MAX, msg, (unsigned int)strlen(msg)); s->desired = 0; s->state = SVC_STOPPED; s->state_dirty = 1; s->log_dirty = 1; return -1; } snprintf(msg, sizeof(msg), "[init] starting %s\n", s->def->name); append_log(s->log, &s->log_len, SVC_LOG_MAX, msg, (unsigned int)strlen(msg)); pid = u_spawn(s->def->path, s->def->args, s->tty, "/"); if (pid <= 0) { snprintf(msg, sizeof(msg), "[init] %s: spawn failed\n", s->def->name); append_log(s->log, &s->log_len, SVC_LOG_MAX, msg, (unsigned int)strlen(msg)); u_tty_close(s->tty); s->tty = 0; s->pid = 0; s->desired = 0; s->state = SVC_STOPPED; s->state_dirty = 1; s->log_dirty = 1; return -1; } s->pid = (unsigned int)pid; s->state = SVC_STARTING; s->started = now_ms(); s->restart_pending = 0; s->state_dirty = 1; s->log_dirty = 1; return 0; } static void svc_stop(struct svc_rt *s, int force) { s->desired = 0; s->restart_pending = 0; if (s->pid && u_alive(s->pid)) u_kill(s->pid); if (s->pid && u_alive(s->pid)) { /* Let svc_poll() drain the final bytes and observe the process * die; never untrack a still-live pid. */ s->state = SVC_STOPPING; s->state_dirty = 1; return; } if (s->pid) { char msg[80]; drain_service(s); if (s->tty > 0) u_tty_close(s->tty); snprintf(msg, sizeof(msg), "[init] %s: %s\n", s->def->name, force ? "killed" : "stopped"); append_log(s->log, &s->log_len, SVC_LOG_MAX, msg, (unsigned int)strlen(msg)); s->pid = 0; s->tty = 0; s->log_dirty = 1; } s->state = SVC_STOPPED; s->state_dirty = 1; } static void svc_restart(struct svc_rt *s) { s->desired = 1; s->restart_pending = 1; s->restart_at = now_ms() + RESTART_MS; if (s->pid && u_alive(s->pid)) u_kill(s->pid); s->state = SVC_RESTARTING; s->state_dirty = 1; } static void svc_poll(struct svc_rt *s) { unsigned int now = now_ms(); if (s->pid && u_alive(s->pid)) { drain_service(s); if (s->state == SVC_STARTING && now - s->started >= START_GRACE_MS) { s->state = SVC_RUNNING; s->state_dirty = 1; } return; } if (s->pid) { char msg[80]; drain_service(s); if (s->tty > 0) u_tty_close(s->tty); s->tty = 0; snprintf(msg, sizeof(msg), "[init] %s: exited\n", s->def->name); append_log(s->log, &s->log_len, SVC_LOG_MAX, msg, (unsigned int)strlen(msg)); s->pid = 0; s->log_dirty = 1; if (s->restart_pending && s->desired) { s->state = SVC_RESTARTING; s->state_dirty = 1; } else { s->desired = 0; s->state = SVC_STOPPED; s->state_dirty = 1; } return; } if (s->desired && s->restart_pending) { if ((int)(now - s->restart_at) >= 0) svc_start(s); return; } if (s->desired && !s->restart_pending) svc_start(s); } static void wm_spawn(void) { char msg[64]; int pid; if (wm_pid > 0 && u_alive((unsigned int)wm_pid)) return; pid = u_spawn("/ramfs/bin/wm", "", 0, "/"); if (pid <= 0) { init_log_add("init: wm spawn failed\n"); wm_pid = 0; wm_restart_at = now_ms() + 1000; init_state_write(); return; } wm_pid = pid; wm_restart_at = 0; snprintf(msg, sizeof(msg), "init: wm started pid %d\n", pid); init_log_add(msg); init_state_write(); } static void wm_poll(void) { unsigned int now = now_ms(); if (wm_pid > 0 && !u_alive((unsigned int)wm_pid)) { char msg[64]; snprintf(msg, sizeof(msg), "init: wm pid %d exited\n", wm_pid); init_log_add(msg); wm_pid = 0; wm_restart_at = now + RESTART_MS; init_state_write(); } if (wm_pid <= 0 && (int)(now - wm_restart_at) >= 0) wm_spawn(); } static int read_text(const char *path, char *buf, unsigned int max) { int n = u_read_file(path, buf, max); if (n < 0) return -1; if ((unsigned int)n >= max) n = (int)max - 1; buf[n] = '\0'; return n; } static void load_services(void) { char path[64], buf[512], log[128]; unsigned int i, now = now_ms(); for (i = 0; i < svc_def_count && i < SVC_MAX; i++) { struct svc_rt *s = &rt[i]; struct svc_state st; int n; memset(s, 0, sizeof(*s)); s->def = &svc_defs[i]; s->tty = 0; s->state = SVC_STOPPED; s->desired = (svc_defs[i].flags & SVC_AUTOSTART) ? 1u : 0u; s->log = (char *)malloc(SVC_LOG_MAX); s->log_len = 0; if (svc_make_state_path(path, sizeof(path), s->def->name) == 0 && read_text(path, buf, sizeof(buf)) > 0 && svc_parse_state(buf, (unsigned int)strlen(buf), &st) == 0) { s->desired = st.desired ? 1u : 0u; } if (s->desired) { s->restart_pending = 1; /* Leave the reaper time to drop the old NIC/disk leases. */ s->restart_at = now + 300; } if (s->log && svc_make_log_path(path, sizeof(path), s->def->name) == 0) { n = u_read_file(path, s->log, SVC_LOG_MAX); if (n > 0) s->log_len = (unsigned int)n; } s->state_dirty = 1; if (!s->log) { snprintf(log, sizeof(log), "init: %s: no log memory\n", s->def->name); init_log_add(log); } } } static void respond(int resp, unsigned int seq, int code, int pid, const char *msg) { char rsp[160]; int n = snprintf(rsp, sizeof(rsp), "%s %u %d %d %s\n", SVC_CTL_MAGIC, seq, code, pid, msg); if (n > 0 && (unsigned int)n < sizeof(rsp)) u_tty_write(resp, rsp, (unsigned int)n); } static char *next_tok(char **pp) { char *p = *pp; char *s; while (*p == ' ' || *p == '\t') p++; if (!*p) { *pp = p; return NULL; } s = p; while (*p && *p != ' ' && *p != '\t') p++; if (*p) *p++ = '\0'; *pp = p; return s; } static void handle_line(char *s, unsigned int len) { char *p = s, *magic, *seqs, *op, *name, *resps; unsigned int seq; int resp, code = 0, pid = 0; const char *msg = "ok"; int idx; if (!len) return; magic = next_tok(&p); seqs = next_tok(&p); op = next_tok(&p); name = next_tok(&p); resps = next_tok(&p); if (!magic || !seqs || !op || !name || !resps) return; if (strcmp(magic, SVC_CTL_MAGIC) != 0) return; seq = atou(seqs); resp = (int)atou(resps); if (resp <= 0) return; if (strcmp(name, "init") == 0) { if (strcmp(op, "ping") == 0) { pid = u_getpid(); } else { code = 5; msg = "init is protected"; } } else if (strcmp(name, "wm") == 0) { if (strcmp(op, "start") == 0) { if (wm_pid > 0 && u_alive((unsigned int)wm_pid)) { code = 3; pid = wm_pid; msg = "already running"; } else { wm_spawn(); if (wm_pid > 0) { pid = wm_pid; msg = "started"; } else { code = 1; msg = "spawn failed"; } } } else if (strcmp(op, "restart") == 0 || strcmp(op, "kill") == 0) { if (wm_pid > 0 && u_alive((unsigned int)wm_pid)) u_kill((unsigned int)wm_pid); wm_pid = 0; wm_restart_at = now_ms() + RESTART_MS; init_state_write(); msg = "restarting wm"; } else if (strcmp(op, "stop") == 0) { code = 5; msg = "wm is a system service"; } else { code = 1; msg = "bad op"; } } else { idx = svc_find(name); if (idx < 0) { code = 4; msg = "unknown service"; } else { struct svc_rt *r = &rt[idx]; if (strcmp(op, "start") == 0) { if (r->pid && u_alive(r->pid)) { code = 3; pid = (int)r->pid; msg = "already running"; } else { r->desired = 1; r->restart_pending = 0; if (svc_start(r) == 0) { pid = (int)r->pid; msg = "started"; } else { code = 1; msg = "spawn failed"; } } } else if (strcmp(op, "stop") == 0) { svc_stop(r, 0); msg = "stopping"; } else if (strcmp(op, "kill") == 0) { svc_stop(r, 1); msg = "killed"; } else if (strcmp(op, "restart") == 0) { svc_restart(r); msg = "restarting"; } else { code = 1; msg = "bad op"; } } } respond(resp, seq, code, pid, msg); } static void process_control(void) { char buf[128]; int n; while ((n = u_tty_read(ctl_tty, buf, sizeof(buf))) > 0) { int i; for (i = 0; i < n; i++) { if (buf[i] == '\n' || buf[i] == '\r') { line[line_len] = '\0'; handle_line(line, line_len); line_len = 0; } else if (line_len + 1 < sizeof(line)) { line[line_len++] = buf[i]; } else { line_len = 0; } } } } void _start(const char *arg) { unsigned int i; (void)arg; if (!u_is_init()) { u_puts("init: only the kernel starts this\n"); u_exit(1); } /* Coming back from a crash: do not let old user processes keep any * state or ttys. This also frees the tty slots before we allocate the * control channel. */ reset_userland(); ctl_tty = u_tty_alloc(); if (ctl_tty <= 0) u_exit(1); init_log_add("init: started\n"); load_services(); init_state_write(); wm_pid = 0; wm_restart_at = 0; wm_spawn(); init_state_write(); init_log_flush(); for (;;) { process_control(); wm_poll(); for (i = 0; i < svc_def_count && i < SVC_MAX; i++) svc_poll(&rt[i]); init_log_flush(); for (i = 0; i < svc_def_count && i < SVC_MAX; i++) { svc_log_flush(&rt[i]); svc_state_write(&rt[i]); } u_sleep_ms(LOOP_MS); } }