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