ucmd_init.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /* init: the userland service manager.
  2. *
  3. * The kernel starts this as PID 1 after /ramfs and /share are mounted. It
  4. * owns the WM and the service processes:
  5. * - spawns /ramfs/bin/wm, adopts nothing (the kernel never starts a WM),
  6. * and relaunches it when it dies;
  7. * - starts/stops/restarts services and drains one tty per service into
  8. * /share/svc-<name>.log;
  9. * - keeps /share/svc-<name>.state and /share/init.state for the CLI/GUI;
  10. * - serves a small control protocol on its own tty.
  11. *
  12. * On restart it kills every leftover ring-3 process first, so a crashed init
  13. * always comes back to a clean userland tree. Kernel threads (kworker,
  14. * bootscan, netkexec, ...) are left alone.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "service_proto.h"
  20. #include "ucmd_lib.h"
  21. #define INIT_LOG_MAX 8192u
  22. #define LOOP_MS 100u
  23. #define START_GRACE_MS 300u
  24. #define RESTART_MS 200u
  25. #define DRAIN_CHUNK 1024u
  26. struct svc_rt {
  27. const struct svc_def *def;
  28. unsigned int state;
  29. unsigned int desired;
  30. unsigned int pid;
  31. int tty;
  32. unsigned int started;
  33. unsigned int restart_at;
  34. int restart_pending;
  35. char *log;
  36. unsigned int log_len;
  37. int log_dirty;
  38. int state_dirty;
  39. };
  40. static int ctl_tty;
  41. static int wm_pid;
  42. static unsigned int wm_restart_at;
  43. static struct svc_rt rt[SVC_MAX];
  44. static char init_log[INIT_LOG_MAX];
  45. static unsigned int init_log_len;
  46. static int init_log_dirty;
  47. static char line[256];
  48. static unsigned int line_len;
  49. static unsigned int now_ms(void)
  50. {
  51. return u_ticks_ms();
  52. }
  53. static unsigned int atou(const char *s)
  54. {
  55. unsigned int n = 0;
  56. while (*s >= '0' && *s <= '9')
  57. n = n * 10 + (unsigned int)(*s++ - '0');
  58. return n;
  59. }
  60. /* Append bytes to a bounded linear log, dropping the oldest data first. */
  61. static void append_log(char *buf, unsigned int *len, unsigned int cap,
  62. const char *s, unsigned int n)
  63. {
  64. if (!buf || !cap)
  65. return;
  66. if (n >= cap) {
  67. s += n - (cap - 1);
  68. n = cap - 1;
  69. }
  70. if (*len + n >= cap) {
  71. unsigned int drop = *len + n - (cap - 1);
  72. memmove(buf, buf + drop, *len - drop);
  73. *len -= drop;
  74. }
  75. memcpy(buf + *len, s, n);
  76. *len += n;
  77. }
  78. static void init_log_add(const char *s)
  79. {
  80. append_log(init_log, &init_log_len, INIT_LOG_MAX, s,
  81. (unsigned int)strlen(s));
  82. init_log_dirty = 1;
  83. }
  84. static void init_log_addf(const char *fmt, unsigned int a)
  85. {
  86. char buf[80];
  87. int n = snprintf(buf, sizeof(buf), fmt, a);
  88. if (n > 0)
  89. init_log_add(buf);
  90. }
  91. static void init_log_flush(void)
  92. {
  93. if (!init_log_dirty)
  94. return;
  95. if (u_write_file(SVC_INIT_LOG, init_log, init_log_len) == 0)
  96. init_log_dirty = 0;
  97. }
  98. static void svc_log_flush(struct svc_rt *s)
  99. {
  100. char path[64];
  101. if (!s->log_dirty || !s->log || !s->log_len)
  102. return;
  103. if (svc_make_log_path(path, sizeof(path), s->def->name) == 0 &&
  104. u_write_file(path, s->log, s->log_len) == 0)
  105. s->log_dirty = 0;
  106. }
  107. static void svc_state_write(struct svc_rt *s)
  108. {
  109. struct svc_state st;
  110. char path[64], buf[256];
  111. if (!s->state_dirty)
  112. return;
  113. memset(&st, 0, sizeof(st));
  114. {
  115. unsigned int i;
  116. for (i = 0; s->def->name[i] && i + 1 < sizeof(st.name); i++)
  117. st.name[i] = s->def->name[i];
  118. st.name[i] = '\0';
  119. }
  120. st.state = s->state;
  121. st.desired = s->desired;
  122. st.pid = (s->pid && u_alive(s->pid)) ? s->pid : 0;
  123. st.tty = s->tty > 0 ? (unsigned int)s->tty : 0;
  124. st.started = s->started;
  125. if (svc_make_state_path(path, sizeof(path), s->def->name) != 0 ||
  126. svc_format_state(buf, sizeof(buf), &st) != 0)
  127. return;
  128. if (u_write_file(path, buf, (unsigned int)strlen(buf)) == 0)
  129. s->state_dirty = 0;
  130. }
  131. static void init_state_write(void)
  132. {
  133. struct init_state st;
  134. char buf[128];
  135. st.pid = (unsigned int)u_getpid();
  136. st.ctl = ctl_tty > 0 ? (unsigned int)ctl_tty : 0;
  137. st.wm = (wm_pid > 0 && u_alive((unsigned int)wm_pid)) ?
  138. (unsigned int)wm_pid : 0;
  139. st.valid = 1;
  140. if (svc_format_init_state(buf, sizeof(buf), &st) == 0)
  141. u_write_file(SVC_INIT_STATE, buf, (unsigned int)strlen(buf));
  142. }
  143. /* Kill every user process except ourselves; kernel threads stay. This is
  144. * the clean-slate recovery path after an init crash. */
  145. static void reset_userland(void)
  146. {
  147. struct sys_procinfo pi;
  148. unsigned int pids[64];
  149. unsigned int n = 0, i;
  150. int self = u_getpid();
  151. for (i = 0; n < 64 && u_procinfo(i, &pi) == 1; i++) {
  152. if (pi.pid && pi.pid != (unsigned int)self && !pi.kernel_thread)
  153. pids[n++] = pi.pid;
  154. }
  155. for (i = 0; i < n; i++)
  156. u_kill(pids[i]);
  157. if (n) {
  158. init_log_addf("init: reset %u user processes\n", n);
  159. u_sleep_ms(150);
  160. }
  161. }
  162. static void drain_service(struct svc_rt *s)
  163. {
  164. char buf[DRAIN_CHUNK];
  165. unsigned int total = 0;
  166. int n;
  167. if (s->tty <= 0 || !s->log)
  168. return;
  169. while (total < 8192 &&
  170. (n = u_tty_read(s->tty, buf, sizeof(buf))) > 0) {
  171. append_log(s->log, &s->log_len, SVC_LOG_MAX, buf,
  172. (unsigned int)n);
  173. s->log_dirty = 1;
  174. total += (unsigned int)n;
  175. }
  176. }
  177. static int svc_start(struct svc_rt *s)
  178. {
  179. char msg[96];
  180. int pid;
  181. if (s->pid && u_alive(s->pid))
  182. return 0;
  183. if (s->pid) {
  184. drain_service(s);
  185. if (s->tty > 0)
  186. u_tty_close(s->tty);
  187. s->pid = 0;
  188. s->tty = 0;
  189. }
  190. if (s->tty <= 0)
  191. s->tty = u_tty_alloc();
  192. if (s->tty <= 0) {
  193. snprintf(msg, sizeof(msg), "init: %s: no tty\n", s->def->name);
  194. init_log_add(msg);
  195. snprintf(msg, sizeof(msg), "[init] %s: no tty\n", s->def->name);
  196. append_log(s->log, &s->log_len, SVC_LOG_MAX, msg,
  197. (unsigned int)strlen(msg));
  198. s->desired = 0;
  199. s->state = SVC_STOPPED;
  200. s->state_dirty = 1;
  201. s->log_dirty = 1;
  202. return -1;
  203. }
  204. snprintf(msg, sizeof(msg), "[init] starting %s\n", s->def->name);
  205. append_log(s->log, &s->log_len, SVC_LOG_MAX, msg,
  206. (unsigned int)strlen(msg));
  207. pid = u_spawn(s->def->path, s->def->args, s->tty, "/");
  208. if (pid <= 0) {
  209. snprintf(msg, sizeof(msg), "[init] %s: spawn failed\n",
  210. s->def->name);
  211. append_log(s->log, &s->log_len, SVC_LOG_MAX, msg,
  212. (unsigned int)strlen(msg));
  213. u_tty_close(s->tty);
  214. s->tty = 0;
  215. s->pid = 0;
  216. s->desired = 0;
  217. s->state = SVC_STOPPED;
  218. s->state_dirty = 1;
  219. s->log_dirty = 1;
  220. return -1;
  221. }
  222. s->pid = (unsigned int)pid;
  223. s->state = SVC_STARTING;
  224. s->started = now_ms();
  225. s->restart_pending = 0;
  226. s->state_dirty = 1;
  227. s->log_dirty = 1;
  228. return 0;
  229. }
  230. static void svc_stop(struct svc_rt *s, int force)
  231. {
  232. s->desired = 0;
  233. s->restart_pending = 0;
  234. if (s->pid && u_alive(s->pid))
  235. u_kill(s->pid);
  236. if (s->pid && u_alive(s->pid)) {
  237. /* Let svc_poll() drain the final bytes and observe the process
  238. * die; never untrack a still-live pid. */
  239. s->state = SVC_STOPPING;
  240. s->state_dirty = 1;
  241. return;
  242. }
  243. if (s->pid) {
  244. char msg[80];
  245. drain_service(s);
  246. if (s->tty > 0)
  247. u_tty_close(s->tty);
  248. snprintf(msg, sizeof(msg), "[init] %s: %s\n", s->def->name,
  249. force ? "killed" : "stopped");
  250. append_log(s->log, &s->log_len, SVC_LOG_MAX, msg,
  251. (unsigned int)strlen(msg));
  252. s->pid = 0;
  253. s->tty = 0;
  254. s->log_dirty = 1;
  255. }
  256. s->state = SVC_STOPPED;
  257. s->state_dirty = 1;
  258. }
  259. static void svc_restart(struct svc_rt *s)
  260. {
  261. s->desired = 1;
  262. s->restart_pending = 1;
  263. s->restart_at = now_ms() + RESTART_MS;
  264. if (s->pid && u_alive(s->pid))
  265. u_kill(s->pid);
  266. s->state = SVC_RESTARTING;
  267. s->state_dirty = 1;
  268. }
  269. static void svc_poll(struct svc_rt *s)
  270. {
  271. unsigned int now = now_ms();
  272. if (s->pid && u_alive(s->pid)) {
  273. drain_service(s);
  274. if (s->state == SVC_STARTING &&
  275. now - s->started >= START_GRACE_MS) {
  276. s->state = SVC_RUNNING;
  277. s->state_dirty = 1;
  278. }
  279. return;
  280. }
  281. if (s->pid) {
  282. char msg[80];
  283. drain_service(s);
  284. if (s->tty > 0)
  285. u_tty_close(s->tty);
  286. s->tty = 0;
  287. snprintf(msg, sizeof(msg), "[init] %s: exited\n", s->def->name);
  288. append_log(s->log, &s->log_len, SVC_LOG_MAX, msg,
  289. (unsigned int)strlen(msg));
  290. s->pid = 0;
  291. s->log_dirty = 1;
  292. if (s->restart_pending && s->desired) {
  293. s->state = SVC_RESTARTING;
  294. s->state_dirty = 1;
  295. } else {
  296. s->desired = 0;
  297. s->state = SVC_STOPPED;
  298. s->state_dirty = 1;
  299. }
  300. return;
  301. }
  302. if (s->desired && s->restart_pending) {
  303. if ((int)(now - s->restart_at) >= 0)
  304. svc_start(s);
  305. return;
  306. }
  307. if (s->desired && !s->restart_pending)
  308. svc_start(s);
  309. }
  310. static void wm_spawn(void)
  311. {
  312. char msg[64];
  313. int pid;
  314. if (wm_pid > 0 && u_alive((unsigned int)wm_pid))
  315. return;
  316. pid = u_spawn("/ramfs/bin/wm", "", 0, "/");
  317. if (pid <= 0) {
  318. init_log_add("init: wm spawn failed\n");
  319. wm_pid = 0;
  320. wm_restart_at = now_ms() + 1000;
  321. init_state_write();
  322. return;
  323. }
  324. wm_pid = pid;
  325. wm_restart_at = 0;
  326. snprintf(msg, sizeof(msg), "init: wm started pid %d\n", pid);
  327. init_log_add(msg);
  328. init_state_write();
  329. }
  330. static void wm_poll(void)
  331. {
  332. unsigned int now = now_ms();
  333. if (wm_pid > 0 && !u_alive((unsigned int)wm_pid)) {
  334. char msg[64];
  335. snprintf(msg, sizeof(msg), "init: wm pid %d exited\n", wm_pid);
  336. init_log_add(msg);
  337. wm_pid = 0;
  338. wm_restart_at = now + RESTART_MS;
  339. init_state_write();
  340. }
  341. if (wm_pid <= 0 && (int)(now - wm_restart_at) >= 0)
  342. wm_spawn();
  343. }
  344. static int read_text(const char *path, char *buf, unsigned int max)
  345. {
  346. int n = u_read_file(path, buf, max);
  347. if (n < 0)
  348. return -1;
  349. if ((unsigned int)n >= max)
  350. n = (int)max - 1;
  351. buf[n] = '\0';
  352. return n;
  353. }
  354. static void load_services(void)
  355. {
  356. char path[64], buf[512], log[128];
  357. unsigned int i, now = now_ms();
  358. for (i = 0; i < svc_def_count && i < SVC_MAX; i++) {
  359. struct svc_rt *s = &rt[i];
  360. struct svc_state st;
  361. int n;
  362. memset(s, 0, sizeof(*s));
  363. s->def = &svc_defs[i];
  364. s->tty = 0;
  365. s->state = SVC_STOPPED;
  366. s->desired = (svc_defs[i].flags & SVC_AUTOSTART) ? 1u : 0u;
  367. s->log = (char *)malloc(SVC_LOG_MAX);
  368. s->log_len = 0;
  369. if (svc_make_state_path(path, sizeof(path), s->def->name) == 0 &&
  370. read_text(path, buf, sizeof(buf)) > 0 &&
  371. svc_parse_state(buf, (unsigned int)strlen(buf), &st) == 0) {
  372. s->desired = st.desired ? 1u : 0u;
  373. }
  374. if (s->desired) {
  375. s->restart_pending = 1;
  376. /* Leave the reaper time to drop the old NIC/disk leases. */
  377. s->restart_at = now + 300;
  378. }
  379. if (s->log &&
  380. svc_make_log_path(path, sizeof(path), s->def->name) == 0) {
  381. n = u_read_file(path, s->log, SVC_LOG_MAX);
  382. if (n > 0)
  383. s->log_len = (unsigned int)n;
  384. }
  385. s->state_dirty = 1;
  386. if (!s->log) {
  387. snprintf(log, sizeof(log), "init: %s: no log memory\n",
  388. s->def->name);
  389. init_log_add(log);
  390. }
  391. }
  392. }
  393. static void respond(int resp, unsigned int seq, int code, int pid,
  394. const char *msg)
  395. {
  396. char rsp[160];
  397. int n = snprintf(rsp, sizeof(rsp), "%s %u %d %d %s\n",
  398. SVC_CTL_MAGIC, seq, code, pid, msg);
  399. if (n > 0 && (unsigned int)n < sizeof(rsp))
  400. u_tty_write(resp, rsp, (unsigned int)n);
  401. }
  402. static char *next_tok(char **pp)
  403. {
  404. char *p = *pp;
  405. char *s;
  406. while (*p == ' ' || *p == '\t')
  407. p++;
  408. if (!*p) {
  409. *pp = p;
  410. return NULL;
  411. }
  412. s = p;
  413. while (*p && *p != ' ' && *p != '\t')
  414. p++;
  415. if (*p)
  416. *p++ = '\0';
  417. *pp = p;
  418. return s;
  419. }
  420. static void handle_line(char *s, unsigned int len)
  421. {
  422. char *p = s, *magic, *seqs, *op, *name, *resps;
  423. unsigned int seq;
  424. int resp, code = 0, pid = 0;
  425. const char *msg = "ok";
  426. int idx;
  427. if (!len)
  428. return;
  429. magic = next_tok(&p);
  430. seqs = next_tok(&p);
  431. op = next_tok(&p);
  432. name = next_tok(&p);
  433. resps = next_tok(&p);
  434. if (!magic || !seqs || !op || !name || !resps)
  435. return;
  436. if (strcmp(magic, SVC_CTL_MAGIC) != 0)
  437. return;
  438. seq = atou(seqs);
  439. resp = (int)atou(resps);
  440. if (resp <= 0)
  441. return;
  442. if (strcmp(name, "init") == 0) {
  443. if (strcmp(op, "ping") == 0) {
  444. pid = u_getpid();
  445. } else {
  446. code = 5;
  447. msg = "init is protected";
  448. }
  449. } else if (strcmp(name, "wm") == 0) {
  450. if (strcmp(op, "start") == 0) {
  451. if (wm_pid > 0 && u_alive((unsigned int)wm_pid)) {
  452. code = 3;
  453. pid = wm_pid;
  454. msg = "already running";
  455. } else {
  456. wm_spawn();
  457. if (wm_pid > 0) {
  458. pid = wm_pid;
  459. msg = "started";
  460. } else {
  461. code = 1;
  462. msg = "spawn failed";
  463. }
  464. }
  465. } else if (strcmp(op, "restart") == 0 ||
  466. strcmp(op, "kill") == 0) {
  467. if (wm_pid > 0 && u_alive((unsigned int)wm_pid))
  468. u_kill((unsigned int)wm_pid);
  469. wm_pid = 0;
  470. wm_restart_at = now_ms() + RESTART_MS;
  471. init_state_write();
  472. msg = "restarting wm";
  473. } else if (strcmp(op, "stop") == 0) {
  474. code = 5;
  475. msg = "wm is a system service";
  476. } else {
  477. code = 1;
  478. msg = "bad op";
  479. }
  480. } else {
  481. idx = svc_find(name);
  482. if (idx < 0) {
  483. code = 4;
  484. msg = "unknown service";
  485. } else {
  486. struct svc_rt *r = &rt[idx];
  487. if (strcmp(op, "start") == 0) {
  488. if (r->pid && u_alive(r->pid)) {
  489. code = 3;
  490. pid = (int)r->pid;
  491. msg = "already running";
  492. } else {
  493. r->desired = 1;
  494. r->restart_pending = 0;
  495. if (svc_start(r) == 0) {
  496. pid = (int)r->pid;
  497. msg = "started";
  498. } else {
  499. code = 1;
  500. msg = "spawn failed";
  501. }
  502. }
  503. } else if (strcmp(op, "stop") == 0) {
  504. svc_stop(r, 0);
  505. msg = "stopping";
  506. } else if (strcmp(op, "kill") == 0) {
  507. svc_stop(r, 1);
  508. msg = "killed";
  509. } else if (strcmp(op, "restart") == 0) {
  510. svc_restart(r);
  511. msg = "restarting";
  512. } else {
  513. code = 1;
  514. msg = "bad op";
  515. }
  516. }
  517. }
  518. respond(resp, seq, code, pid, msg);
  519. }
  520. static void process_control(void)
  521. {
  522. char buf[128];
  523. int n;
  524. while ((n = u_tty_read(ctl_tty, buf, sizeof(buf))) > 0) {
  525. int i;
  526. for (i = 0; i < n; i++) {
  527. if (buf[i] == '\n' || buf[i] == '\r') {
  528. line[line_len] = '\0';
  529. handle_line(line, line_len);
  530. line_len = 0;
  531. } else if (line_len + 1 < sizeof(line)) {
  532. line[line_len++] = buf[i];
  533. } else {
  534. line_len = 0;
  535. }
  536. }
  537. }
  538. }
  539. void _start(const char *arg)
  540. {
  541. unsigned int i;
  542. (void)arg;
  543. if (!u_is_init()) {
  544. u_puts("init: only the kernel starts this\n");
  545. u_exit(1);
  546. }
  547. /* Coming back from a crash: do not let old user processes keep any
  548. * state or ttys. This also frees the tty slots before we allocate the
  549. * control channel. */
  550. reset_userland();
  551. ctl_tty = u_tty_alloc();
  552. if (ctl_tty <= 0)
  553. u_exit(1);
  554. init_log_add("init: started\n");
  555. load_services();
  556. init_state_write();
  557. wm_pid = 0;
  558. wm_restart_at = 0;
  559. wm_spawn();
  560. init_state_write();
  561. init_log_flush();
  562. for (;;) {
  563. process_control();
  564. wm_poll();
  565. for (i = 0; i < svc_def_count && i < SVC_MAX; i++)
  566. svc_poll(&rt[i]);
  567. init_log_flush();
  568. for (i = 0; i < svc_def_count && i < SVC_MAX; i++) {
  569. svc_log_flush(&rt[i]);
  570. svc_state_write(&rt[i]);
  571. }
  572. u_sleep_ms(LOOP_MS);
  573. }
  574. }