state.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <sys/reboot.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include "procd.h"
  18. #include "syslog.h"
  19. #include "hotplug.h"
  20. #include "watchdog.h"
  21. enum {
  22. STATE_NONE = 0,
  23. STATE_EARLY,
  24. STATE_INIT,
  25. STATE_RUNNING,
  26. STATE_SHUTDOWN,
  27. STATE_HALT,
  28. __STATE_MAX,
  29. };
  30. static int state = STATE_NONE;
  31. static int reboot_event;
  32. static void state_enter(void)
  33. {
  34. switch (state) {
  35. case STATE_EARLY:
  36. LOG("- early -\n");
  37. watchdog_init(0);
  38. hotplug("/etc/hotplug.json");
  39. procd_coldplug();
  40. break;
  41. case STATE_INIT:
  42. // try to reopen incase the wdt was not available before coldplug
  43. watchdog_init(0);
  44. LOG("- init -\n");
  45. log_init();
  46. procd_connect_ubus();
  47. procd_inittab();
  48. procd_inittab_run("respawn");
  49. procd_inittab_run("askconsole");
  50. procd_inittab_run("askfirst");
  51. procd_inittab_run("sysinit");
  52. break;
  53. case STATE_RUNNING:
  54. LOG("- init complete -\n");
  55. break;
  56. case STATE_SHUTDOWN:
  57. LOG("- shutdown -\n");
  58. procd_inittab_run("shutdown");
  59. sync();
  60. break;
  61. case STATE_HALT:
  62. LOG("- reboot -\n");
  63. reboot(reboot_event);
  64. break;
  65. default:
  66. ERROR("Unhandled state %d\n", state);
  67. return;
  68. };
  69. }
  70. void procd_state_next(void)
  71. {
  72. DEBUG(2, "Change state %d -> %d\n", state, state + 1);
  73. state++;
  74. state_enter();
  75. }
  76. void procd_shutdown(int event)
  77. {
  78. DEBUG(1, "Shutting down system with event %x\n", event);
  79. reboot_event = event;
  80. state = STATE_SHUTDOWN;
  81. state_enter();
  82. }