]> Pileus Git - mkinit/blob - src/mkinit
Update mkinit
[mkinit] / src / mkinit
1 #!/bin/bash
2
3 # Copyright (C) 2009 Andy Spencer
4 # See ../COPYING for terms
5
6 # GLobals
7 COMMAND=boot
8 TESTING=false
9 if [ $$ == 1 ]; then
10         DAEMON=true
11 else
12         DAEMON=false
13 fi
14
15 PATH=/lib/mkinit/bin:/bin:/sbin:/usr/bin:/usr/sbin
16 export PATH
17
18 # Functions 
19 function usage {
20         echo 'usage: '$0' [options] [command]'
21         echo 
22         echo 'Options:'
23         echo '  -h,--help      Print usage information'
24         echo '  -t,--test      Fake all commands'
25         echo '  -d,--daemon    For spawning stdin listener'
26         echo ''
27         echo 'Command:'
28         echo '  boot           Execute boot-up procedures'
29         echo '  halt           Execute shutdown procedures'
30         echo '  reload         Re-execute init process'
31         echo '  mk <rule>      Execute mk rule'
32         echo '  start|stop|restart|zap|status <service>'
33         echo '                 Start,stop,restart,zap or query status of service'
34         echo '  eval <cmd>     Execute command in mkinit process'
35         exit
36 }
37
38 # Handle arguments
39 function doopts {
40         TMP=`getopt -n "$0" -o htd -l help,test,daemon -- "$@"`
41         [ $? != 0 ] &&
42                 usage
43         eval set -- "$TMP"
44
45         # Parse options
46         while [ ! "$1" == "--" ]; do
47                 case "$1" in
48                 -h|--help )   usage ;;
49                 -t|--test )   TESTING=true ;;
50                 -d|--daemon ) DAEMON=true ;;
51                 esac
52                 shift
53         done
54         shift
55         if [ "$*" ]; then
56                 COMMAND=($@)
57         fi
58
59         # Debugging output
60         if $TESTING; then
61                 echo 'Options'             1>&2
62                 echo '  TESTING:' $TESTING 1>&2
63                 echo '  DAEMON:'  $DAEMON  1>&2
64                 echo '  COMMAND:' $COMMAND 1>&2
65         fi
66 }
67
68 # Run mk on the init scripts
69 function runamk {
70         if $TESTING; then
71                 export P=echo
72                 export MKINIT_STATE=/tmp/mkinit_state
73                 mkdir -p $MKINIT_STATE
74         fi
75         /opt/plan9/bin/mk \
76                 -f /etc/init.mk \
77                 -i -k "$@"
78 }
79
80 # Process one command
81 function process {
82         cmd=$1
83         shift
84         echo mkinit -- running "$cmd" "$@"
85         case "$cmd" in
86         boot )
87                 runamk -a "$@"
88                 ;;
89         restart )
90                 if [ "$*" ]; then
91                         runamk "$@"-stop ||
92                                 runamk "$@"-zap
93                         runamk "$@"-start
94                 fi
95                 ;;
96         start|stop|zap|status )
97                 if [ "$*" ]; then
98                         runamk "$@-$cmd"
99                 fi
100                 ;;
101         reload )
102                 $TESTING && 
103                         opt=-t
104                 exec $0 -r $opt
105                 ;;
106         poweroff|reboot|kexec|halt)
107                 ( runamk "$cmd" "$@" & )
108                 ;;
109         eval )
110                 eval "$@"
111                 ;;
112         ?* )
113                 runamk "$cmd" "$@"
114                 ;;
115         esac
116 }
117
118 # Process arguments
119 doopts "$@"
120
121 # Run whatever was requested
122 process "${COMMAND[@]}"
123
124 # Fork console listener
125 while $DAEMON; do
126         while read -ep "mkinit> " line; do
127                 process $line
128                 history -s $line
129         done
130         $TESTING && exit
131         exec 0</dev/console 1>/dev/console 2>&1
132         echo "Respawning on /dev/console.."
133         sleep 1
134 done