troglobit / finit

Fast init for Linux. Cookies included
https://troglobit.com/projects/finit/
MIT License
622 stars 61 forks source link

HOOK_SYSTEM_DN should pass argument or env to differentiate shutdown type #315

Closed hongkongkiwi closed 1 year ago

hongkongkiwi commented 1 year ago

Is it possible to pass an env to the hook script run at HOOK_SYSTEM_DN to specify the type of shutdown?

I would like to know in my script whether the shutdown is a halt, poweroff or reboot (I will send on this value to a watchdog subprocessor so it knows what to expect next).

I was thinking, we could just set an environment variable which is passed to the script, that way it's totally backwards compatible, but I can check the variable value to know.

troglobit commented 1 year ago

Ah, yes that's a good idea! The hook script implementation doesn't support any environment variables right now, but I'll have a look at it. Shouldn't be too complicated, and possible to squeeze in before the -rc phase.

hongkongkiwi commented 1 year ago

Fantastic! thank you.

I have a couple of related questions about hook scripts:

troglobit commented 1 year ago
  1. No, they only need to be executable
  2. Scripts are sorted and run in alphabetical order, like run-parts(8), so 01-foo runs before 02-bar, but bar runs before foo if they have no numerical prefix.

There's also support for S[0-9]foo and K[0-9]foo start-stop style scripts, where the scripts get start and stop appended to their command line. This was added way back to support SysV init systems. E.g., S01foo is called as S01foo start, not foo start, although S01foo may of course be a symlink to another directory/foo

hongkongkiwi commented 1 year ago

Can I suggest that we also pass the hook name string as well to all scripts?

This can help me to differentiate hooks (that way I can symlink all scripts to one script and have it just use a case statement to handle different hook cases).

Right now, I have one script per event doing the same stuff, but nice just to symlink them all to one master script (I can ofcourse have a subscript call a master script but that's more lag).

So I guess we could have something like (obviously shutdown type would be unset where it's not relevant):

FINIT_HOOK_NAME="HOOK_SYSTEM_DN"
FINIT_HOOK_SHUTDOWN_TYPE="SHUTDOWN"
troglobit commented 1 year ago

I'll see what I can do, but it should be possible.

troglobit commented 1 year ago

Slight changes to the syntax compared to your proposal, for details see https://github.com/troglobit/finit/blob/master/doc/plugins.md#hooks

Here's an example run:

~/src/myLinux(main)$ make run
make[1]: Entering directory '/home/jocke/src/myLinux/buildroot'
make[2]: Nothing to be done for 'Makefile'.
make[1]: Nothing to be done for 'Makefile'.
make[1]: Leaving directory '/home/jocke/src/myLinux/buildroot'
Starting Qemu  ::  Ctrl-a x -- exit | Ctrl-a c -- toggle console/monitor
WE ARE BANNER =====================================================================
CALLING INITCTL ... Wed Nov 23 21:40:40 UTC 2022
initctl: no such task or service(s): mdevd
RETURNEDINITCTL ... Wed Nov 23 21:40:40 UTC 2022
● ● ●  myLinux 2022.08.1 ═══════════════════════════════════════════════
[ OK ] Mounting filesystems from /etc/fstab.ro
[ OK ] Cold plugging system
[ OK ] Populating device tree
[ OK ] Restoring system clock (UTC) from RTC
[ OK ] Initializing random number generator
[ OK ] Starting MDEVD Extended Hotplug Daemon
[ OK ] Starting Chrony Time Daemon
[ OK ] Starting System log daemon
[ OK ] Bringing up network interfaces ...
[ OK ] Starting Dropbear SSH daemon
[ OK ] Starting mDNS-SD daemon
[ OK ] Starting Mini snmpd
[ OK ] Starting Static multicast routing daemon
[ OK ] Starting SSDP Responder
[ OK ] Starting System watchdog daemon
[ OK ] Starting Web interface
[ OK ] Calling /etc/rc.local

myLinux - Troglobit Software Inc.
anarchy login: root
Password: 
                 ___    __                    
 .--------.--.--|   |  |__.-----.--.--.--.--.  (@-   
 |        |  |  |.  |  |  |     |  |  |_   _|  //\  :: Troglobit Software
 |__|__|__|___  |.  |__|__|__|__|_____|__.__|  V_/_ :: https://troglobit.com
          |_____|:  1   |
                |::.. . | A N A R C H Y rel.
                `-------'
root@anarchy:~# poweroff 

WE ARE GOING DOWN!
FINIT_HOOK_NAME: hook/sys/shutdown
FINIT_SHUTDOWN:  poweroff
CALLING INITCTL ... Wed Nov 23 23:40:57 UTC 2022
initctl: Failed connecting to finit
initctl: Failed connecting to finit
initctl: Failed connecting to finit
initctl: no such task or service(s): dropbear
RETURNEDINITCTL ... Wed Nov 23 23:40:57 UTC 2022
[ OK ] Saving system time (UTC) to RTC
[ OK ] Saving random seed
[ OK ] Stopping Dropbear SSH daemon
[ OK ] Stopping Getty on /dev/console
[ OK ] Stopping mDNS-SD daemon
[ OK ] Stopping Mini snmpd
[ OK ] Stopping Static multicast routing daemon
[ OK ] Stopping Simple NTP daemon
[ OK ] Stopping SSDP Responder
[ OK ] Stopping System watchdog daemon
[ OK ] Stopping Web interface
[ OK ] Taking down network interfaces ...
[ OK ] Unmounting /tmp
[ OK ] Unmounting /mnt/rw
[ OK ] Unmounting /mnt
[ OK ] Unmounting /var/lib
[ OK ] Unmounting /var
[ OK ] Unmounting /etc
[ OK ] Unmounting /root
[ OK ] Unmounting /home
THE END IS NEIGH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
FINIT_HOOK_NAME: hook/sys/down
FINIT_SHUTDOWN:  poweroff
[   19.501038] reboot: Power down
~/src/myLinux(main)$