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

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:

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