#define PLANCK_FEATURE_INITTAB 1 struct respawn_entry { pid_t pid; char tty[MAX_TTY]; char cmd[MAX_CMD]; }; static struct respawn_entry respawns[MAX_RESPAWN]; static int n_respawns; /* Where to read the inittab. Prefer /root/inittab (root's home — this box keeps * root-run config there); fall back to the conventional /etc/inittab. Backward- * compatible: builds that ship only /etc/inittab are unaffected. */ static const char *inittab_path(void) { static const char *p; if (!p) p = (access("/root/inittab", R_OK) == 0) ? "/root/inittab" : "/etc/inittab"; return p; } static int parse_inittab_line(const char *input, char *tty, size_t tty_size, char *action, size_t action_size, char *cmd, size_t cmd_size) { char line[MAX_LINE]; char *first_colon; char *second_colon; char *third_colon; char *nl; strncpy(line, input, sizeof(line) - 1); line[sizeof(line) - 1] = 0; if (line[0] == '#' || line[0] == '\n' || line[0] == 0) return 0; nl = strchr(line, '\n'); if (nl) *nl = 0; first_colon = strchr(line, ':'); if (!first_colon) return 0; second_colon = strchr(first_colon + 1, ':'); if (!second_colon) return 0; third_colon = strchr(second_colon + 1, ':'); if (!third_colon) return 0; *first_colon = 0; *second_colon = 0; *third_colon = 0; if (tty_size > 0) { strncpy(tty, line, tty_size - 1); tty[tty_size - 1] = 0; } if (action_size > 0) { strncpy(action, second_colon + 1, action_size - 1); action[action_size - 1] = 0; } if (cmd_size > 0) { strncpy(cmd, third_colon + 1, cmd_size - 1); cmd[cmd_size - 1] = 0; } return cmd[0] != 0; } static void run_inittab_action(const char *action) { FILE *f = fopen(inittab_path(), "r"); char line[MAX_LINE]; char tty[MAX_TTY]; char parsed_action[MAX_ACTION]; char cmd[MAX_CMD]; if (!f) return; while (fgets(line, sizeof(line), f)) { if (!parse_inittab_line(line, tty, sizeof(tty), parsed_action, sizeof(parsed_action), cmd, sizeof(cmd))) continue; if (strcmp(parsed_action, action) != 0) continue; log_msg(cmd); planck_run(cmd); } fclose(f); } /* vfork, never fork: a no-MMU kernel has no fork() (see planck-run.c). Everything the child needs * is prepared in the parent before vfork; after it the child only makes syscalls on its own file * table (vfork shares memory, not descriptors) and exits through exec or _exit. The termios struct * is the one object the child writes: it lives in the child's own frame and nothing reads it after. */ static pid_t spawn_respawn(struct respawn_entry *e) { char devpath[48]; pid_t p; if (e->tty[0]) planck_cat(devpath, sizeof(devpath), planck_cat(devpath, sizeof(devpath), 0, "/dev/"), e->tty); p = vfork(); if (p == 0) { if (e->tty[0]) { setsid(); close(0); close(1); close(2); open(devpath, O_RDWR); dup(0); dup(0); ioctl(0, 0x540E, 0); /* TIOCSCTTY */ /* Give the line a sane cooked termios. Without this a bare shell on a * serial tty inherits whatever the previous opener left — typically raw * (OPOST/ONLCR cleared), which staircases every line of output. */ { struct termios t; if (tcgetattr(0, &t) == 0) { t.c_iflag |= ICRNL; t.c_iflag &= ~(IGNCR | INLCR); t.c_oflag |= OPOST | ONLCR; t.c_lflag |= ICANON | ECHO | ECHOE | ECHOK | ISIG | IEXTEN; t.c_cflag |= CREAD; if (t.c_cc[VERASE] == 0) t.c_cc[VERASE] = 0177; tcsetattr(0, TCSANOW, &t); } } } /* Every respawn line runs through the shell, a tty line too: its command is a command LINE (arguments, * VAR=value in front), not the path of one file. An `exec` in the line leaves no wrapper shell behind. */ execl(PLANCK_SHELL, "sh", "-c", e->cmd, NULL); _exit(1); } return p; } static struct respawn_entry *add_respawn_entry(const char *tty, const char *cmd) { struct respawn_entry *e; if (n_respawns >= MAX_RESPAWN) return NULL; e = &respawns[n_respawns++]; memset(e, 0, sizeof(*e)); strncpy(e->tty, tty, sizeof(e->tty) - 1); strncpy(e->cmd, cmd, sizeof(e->cmd) - 1); return e; } static void start_respawn_entry(struct respawn_entry *e) { char buf[MAX_CMD + MAX_TTY + 32]; size_t lNumAt = planck_cat(buf, sizeof(buf), 0, "respawn: "); if (e->tty[0]) { lNumAt = planck_cat(buf, sizeof(buf), lNumAt, e->tty); lNumAt = planck_cat(buf, sizeof(buf), lNumAt, ": "); } planck_cat(buf, sizeof(buf), lNumAt, e->cmd); log_msg(buf); e->pid = spawn_respawn(e); } static void process_startup_entries(void) { FILE *f = fopen(inittab_path(), "r"); char line[MAX_LINE]; char tty[MAX_TTY]; char action[MAX_ACTION]; char cmd[MAX_CMD]; if (!f) return; n_respawns = 0; while (fgets(line, sizeof(line), f)) { struct respawn_entry *e; if (!parse_inittab_line(line, tty, sizeof(tty), action, sizeof(action), cmd, sizeof(cmd))) continue; if (strcmp(action, "sysinit") == 0) { log_msg(cmd); planck_run(cmd); continue; } if (strcmp(action, "builtin") == 0) { log_msg(cmd); if (strcmp(cmd, "ipv4") == 0) planck_ipv4_configure_from_cmdline(); if (strcmp(cmd, "coremark") == 0) planck_coremark_run(); #if PLANCK_WITH_MOUNT if (strncmp(cmd, "mount ", 6) == 0) planck_mount_builtin(cmd + 6); #endif continue; } #if PLANCK_WITH_LISTEN if (strcmp(action, "listen") == 0) { log_msg(cmd); planck_listen_add(cmd); continue; } #endif if (strcmp(action, "respawn") != 0) continue; e = add_respawn_entry(tty, cmd); if (!e) continue; start_respawn_entry(e); } fclose(f); } static int planck_handle_child_exit(pid_t died) { int i; for (i = 0; i < n_respawns; i++) { if (respawns[i].pid != died) continue; sleep(1); respawns[i].pid = spawn_respawn(&respawns[i]); return 1; } return 0; }