]> git.madduck.net Git - code/molly-guard.git/blob - shutdown.in

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

cac4d6312fae78d93785b76626e10ec9f6833c0b
[code/molly-guard.git] / shutdown.in
1 #!/bin/sh
2 #
3 # shutdown -- wrapper script to guard against accidental shutdowns
4 #
5 # Copyright © martin f. krafft <madduck@madduck.net>
6 # Released under the terms of the Artistic Licence 2.0
7 #
8 set -eu
9
10 ME=molly-guard
11 VERSION=0.4
12
13 SCRIPTSDIR="@ETCDIR@/run.d"
14
15 CMD="${0##*/}"
16 EXEC="/sbin/$CMD"
17
18 case "$CMD" in
19   halt|reboot|shutdown|poweroff)
20     if [ ! -f $EXEC ]; then
21       echo "E: not a regular file: $EXEC" >&2
22       exit 4
23     fi
24     if [ ! -x $EXEC ]; then
25       echo "E: not an executable: $EXEC" >&2
26       exit 3
27     fi
28     ;;
29   *)
30     echo "E: unsupported command: $CMD" >&2
31     exit 1
32     ;;
33 esac
34
35 usage()
36 {
37   cat <<-_eousage
38         Usage: $ME [options] [-- script options]
39                (shielding $EXEC)
40         
41         molly-guard's primary goal is to guard against accidental
42         shutdowns/reboots. $ME will run all scripts in $SCRIPTSDIR and only
43         invokes $EXEC if all scripts exited successfully.
44
45         Specifying --molly-guard-do-nothing as argument to the command will
46         make $ME echo the command it would execute rather than actually
47         executing it.
48
49         Options following the double hyphen will be passed unchanged to the
50         scripts.
51
52         Please see molly-guard(8) for more information.
53
54         The actual command's help output follows:
55
56         _eousage
57 }
58
59 CMDARGS=
60 SCRIPTARGS=
61 END_OF_ARGS=0
62 DO_NOTHING=0
63 for arg in "$@"; do
64   case "$arg" in
65     (*-molly-guard-do-nothing) DO_NOTHING=1;;
66     (*-help)
67       usage 2>&1
68       eval $EXEC --help 2>&1
69       exit 0
70       ;;
71     --) END_OF_ARGS=1;;
72     *) 
73       if [ $END_OF_ARGS -eq 0 ]; then
74         CMDARGS="${args:+$args }$arg"
75       else
76         SCRIPTARGS="${args:+$args }--arg $arg"
77       fi
78       ;;
79   esac
80 done
81
82 do_real_cmd()
83 {
84   if [ $DO_NOTHING -eq 1 ]; then
85     echo "$ME: would run: $EXEC $CMDARGS"
86     exit 0
87   else
88     eval exec $EXEC "$CMDARGS"
89   fi
90 }
91
92 if [ $DO_NOTHING -eq 1 ]; then
93   echo "I: demo mode; $ME will not do anything due to --molly-guard-do-nothing." >&2
94 fi
95
96 # pass through certain commands
97 case "$CMD $CMDARGS" in
98   (*shutdown\ *-c*)
99     # allow canceling shutdowns
100     echo "I: executing $CMD $CMDARGS regardless of check results." >&2
101     do_real_cmd
102     ;;
103 esac
104
105 MOLLYGUARD_CMD=$CMD; export MOLLYGUARD_CMD
106 MOLLYGUARD_DO_NOTHING=$DO_NOTHING; export MOLLYGUARD_DO_NOTHING
107 MOLLYGUARD_SETTINGS="@ETCDIR@/rc"; export MOLLYGUARD_SETTINGS
108
109 for script in $(run-parts --test $SCRIPTSDIR); do
110   ret=0
111   eval $script $SCRIPTARGS || ret=$?
112   if [ $ret -ne 0 ]; then
113     echo "W: aborting $CMD due to ${script##*/} exiting with code $ret." >&2
114     exit $ret
115   fi
116 done
117
118 do_real_cmd