Welcome to little lamb

Code » anopa » commit f8db66e

shutdown: Check argv[0] for use via symlinks

author Olivier Brunel
2015-10-16 13:02:26 UTC
committer Olivier Brunel
2015-10-16 18:17:10 UTC
parent 053b6e16b363a5eee62b64a58a472b191483d68c

shutdown: Check argv[0] for use via symlinks

So one can use symlinks "halt", "reboot" and "poweroff" to use this.
Can help make things easier, and more "compatible" with sysvinit.

doc/aa-shutdown.pod +10 -0
src/scripts/aa-shutdown +16 -3

diff --git a/doc/aa-shutdown.pod b/doc/aa-shutdown.pod
index 6c75580..695b852 100644
--- a/doc/aa-shutdown.pod
+++ b/doc/aa-shutdown.pod
@@ -39,3 +39,13 @@ B<s6-svscan> (using B<s6-svscanctl>).
 
 It obviously expects it to be running (as PID 1) using I</run/services/.scandir>
 as its scandir.
+
+Additionally, if called as B<halt>, B<reboot> or B<poweroff> then it
+automatically defaults to the corresponding action. This allows to use symlinks
+by those names pointing to B<aa-shutdown>(1). Note that in this case:
+
+- An option isn't required; i.e. calling it as `reboot` will trigger the
+  reboot, as would `aa-shutdown --reboot` have done;
+
+- If an option is specified, it takes precedence. So calling it `reboot -p` will
+  power off the machine.
diff --git a/src/scripts/aa-shutdown b/src/scripts/aa-shutdown
index e9f1864..914edd1 100644
--- a/src/scripts/aa-shutdown
+++ b/src/scripts/aa-shutdown
@@ -44,11 +44,24 @@ EOF
     exit 0
 }
 
-if [ $# -ne 1 ]; then
+cmd=
+case "${0##*/}" in
+    halt)
+        cmd=s
+        ;;
+    reboot)
+        cmd=r
+        ;;
+    poweroff)
+        cmd=p
+        ;;
+esac
+
+if [ $# -ne 1 ] && [ -z $cmd ]; then
     dieusage 1
 fi
 
-case $1 in
+case "$1" in
     -H|--halt)
         cmd=s
         ;;
@@ -65,7 +78,7 @@ case $1 in
         dieversion
         ;;
     *)
-        dieusage 1
+        [ -z cmd ] && dieusage 1
 esac
 
 exec s6-svscanctl -${cmd}b /run/services/.scandir