Author: Cem Keylan <email@example.com>
Date: Fri, 24 Apr 2020 15:20:27 +0300
shinit: add shutdown script support
2 files changed, 15 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
@@ -1,14 +1,17 @@
-Basic init daemon in POSIX sh with only 3 lines of code.
+Basic init daemon in POSIX sh with only 5 lines of code. It supports
+acting upon signals.
+On USR1 signal it will poweroff, and on INT signal it will reboot.
-Before installing, edit the second command to use your boot script. If you
-are using Carbs Linux or KISS, you don't need to change it.
+Before installing, edit the second command to use your boot/poweroff
+script. If you are using Carbs Linux or KISS, you don't need to change it.
You can then install with `make`.
diff --git a/shinit b/shinit
@@ -6,10 +6,18 @@
# Run the boot script
+# Add signal traps for poweroff and reboot
+trap '/lib/init/rc.shutdown poweroff' USR1
+trap '/lib/init/rc.shutdown reboot' INT
# Sleep for a day. As sleep is not a builtin for every
# shell, we don't want to keep spawning processes, hence
# the long sleep time.
# We also don't want to run 'while :; do :; done' as
# it would lead to high cpu usage.
-while :; do sleep 86400; done
+# Since we want to trap signals, We fork and wait for the
+# sleep. If sleep is not forked, signals will not be acted
+while :; do sleep 86400 & wait ; done