├── LICENSE ├── Makefile ├── README.md ├── poweroff └── shinit /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Cem Keylan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PREFIX = /usr/local 2 | BINDIR = ${PREFIX}/bin 3 | 4 | install: 5 | mkdir -p ${DESTDIR}${BINDIR} 6 | cp shinit poweroff ${DESTDIR}${BINDIR} 7 | chmod 755 ${DESTDIR}${BINDIR}/shinit 8 | chmod 755 ${DESTDIR}${BINDIR}/poweroff 9 | ln -sf poweroff ${DESTDIR}${BINDIR}/reboot 10 | 11 | uninstall: 12 | rm -f ${DESTDIR}${BINDIR}/shinit \ 13 | ${DESTDIR}${BINDIR}/poweroff \ 14 | ${DESTDIR}${BINDIR}/reboot 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | shinit 2 | ====== 3 | 4 | Basic init daemon in POSIX sh with only 5 lines of code. It supports acting upon 5 | signals. 6 | 7 | On USR1 signal it will poweroff, and on INT signal it will reboot. 8 | 9 | 10 | Installing 11 | ---------- 12 | 13 | Before installing, edit the second command to use your boot/poweroff script. If 14 | you are using Carbs Linux or KISS, you don't need to change it. 15 | 16 | You can then install with `make`. 17 | 18 | make install 19 | 20 | 21 | Note on halting the system 22 | -------------------------- 23 | 24 | shinit does **NOT** deal with system halting. You will need an extra utility for 25 | that given purpose. `ubase halt` deals with this. You can also use compile 26 | this really simple C program that tells to kernel to shutdown or reboot. 27 | 28 | ``` c 29 | #include 30 | 31 | int 32 | main(int argc, char *argv[]) 33 | { 34 | switch ((int) argv[argc < 2 ? 0 : 1][0]) { 35 | case 'p': reboot(RB_POWER_OFF); break; 36 | case 'r': reboot(RB_AUTOBOOT); break; 37 | default: return 1; 38 | } 39 | return 0; 40 | } 41 | ``` 42 | 43 | You can compile the following program to something named `halt` or whatever you 44 | like. If you call `halt p` it will power off, if you call `halt r` it will 45 | reboot. Just note that this needs to be at the end of your init script, because 46 | this command will shut your computer down straight away. You can do the 47 | following check at the end of your init script: 48 | 49 | ``` sh 50 | case "$1" in 51 | poweroff) halt p ;; 52 | reboot) halt r ;; 53 | esac 54 | ``` 55 | -------------------------------------------------------------------------------- /poweroff: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # An example way to poweroff/reboot. 3 | 4 | case "${0##*/}" in poweroff) kill -s USR1 1 ;; reboot) kill -s INT 1 ; esac 5 | -------------------------------------------------------------------------------- /shinit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Exit with status 1 if pid is not 1 4 | [ $$ -eq 1 ] || exit 1 5 | 6 | # Run the boot script 7 | /lib/init/rc.boot 8 | 9 | # Add signal traps for poweroff and reboot 10 | trap '/lib/init/rc.shutdown poweroff' USR1 11 | trap '/lib/init/rc.shutdown reboot' INT 12 | 13 | # Sleep for a day. As sleep is not a builtin for every 14 | # shell, we don't want to keep spawning processes, hence 15 | # the long sleep time. 16 | # 17 | # We also don't want to run 'while :; do :; done' as 18 | # it would lead to high cpu usage. 19 | # 20 | # Since we want to trap signals, We fork and wait for the 21 | # sleep. If sleep is not forked, signals will not be acted 22 | # upon. 23 | while :; do sleep 86400 & wait ; done 24 | --------------------------------------------------------------------------------