init.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Micro-init bootstrap. The real policy lives in the ring-3 init binary; this
  2. * file only loads it, records its pid and restarts it if it ever exits. */
  3. #include "init.h"
  4. #include "process.h"
  5. #include "serial.h"
  6. #include "timer.h"
  7. #include "vfs.h"
  8. static unsigned int init_pid;
  9. static unsigned int init_last_start;
  10. static int init_supported = -1; /* -1 unknown, 0 no binary, 1 present */
  11. unsigned int init_server_pid(void)
  12. {
  13. return init_pid;
  14. }
  15. void init_server_note_pid(unsigned int pid)
  16. {
  17. init_pid = pid;
  18. }
  19. int init_server_start(void)
  20. {
  21. struct vfs_file f;
  22. struct vfs_mount *m = 0;
  23. unsigned int now = get_ticks_ms();
  24. int pid;
  25. if (init_supported == 0)
  26. return 0;
  27. if (init_pid && process_is_alive(init_pid))
  28. return 1;
  29. if (init_last_start && now - init_last_start < 1000)
  30. return 0;
  31. init_last_start = now;
  32. if (vfs_load_file("/ramfs/bin/init", &f, &m) != 0) {
  33. init_supported = 0;
  34. serial_puts(SERIAL_COM1,
  35. "init: /ramfs/bin/init not found; no WM\n");
  36. return 0;
  37. }
  38. init_supported = 1;
  39. pid = process_create_user_full("init", f.data, f.size, "", "/", 0, 0,
  40. PROC_CREATE_INIT);
  41. vfs_release_file(m, &f);
  42. if (pid <= 0) {
  43. serial_puts(SERIAL_COM1, "init: cannot start /ramfs/bin/init\n");
  44. return 0;
  45. }
  46. process_set_weight((unsigned int)pid, 12);
  47. serial_puts(SERIAL_COM1, "init: started\n");
  48. return 1;
  49. }
  50. void init_server_supervise(void)
  51. {
  52. if (init_supported == 0)
  53. return;
  54. if (init_pid && process_is_alive(init_pid))
  55. return;
  56. if (init_pid) {
  57. serial_puts(SERIAL_COM1, "init: process died; restarting\n");
  58. init_pid = 0;
  59. }
  60. /* init_server_start() throttles itself, so a crash loop retries once a
  61. * second and a startup failure is not retried at the kworker rate. */
  62. init_server_start();
  63. }