]> Pileus Git - mkinit/blob - src/mkinit
0b0dd801810387e9f7cc0b040bebb4cdd3502964
[mkinit] / src / mkinit
1 #!/bin/bash
2
3 # Copyright (C) 2009 Andy Spencer
4 # See ../COPYING for terms
5
6 # GLobals
7 TEST=false
8 RELOAD=false
9 FIFO=/lib/mkinit/cmd
10 INITCTL=/dev/initctl
11 PATH=/lib/mkinit/bin:/bin:/sbin:/usr/bin:/usr/sbin
12 export PATH
13
14 # Functions 
15 function usage {
16         echo 'usage: '$0' [options] [command]'
17         echo 
18         echo 'Options:'
19         echo '  -h,--help    Print usage information'
20         echo '  -r,--relaod  Prevent spawning listeners when reloading'
21         echo '  -t,--test    Fake all commands'
22         echo
23         echo 'Command:'
24         echo '  boot         Execute boot-up procedures'
25         echo '  halt         Execute shutdown procedures'
26         echo '  reload       Re-execute init process'
27         echo '  mk <rule>    Execute mk rule'
28         echo '  start|stop|restart|zap|status <service>'
29         echo '               Start,stop,restart,zap or query status of service'
30         echo '  eval <cmd>   Execute command in mkinit process'
31         exit
32 }
33
34 # Run mk on the init scripts
35 function runamk {
36         $TEST && 
37                 export P=true
38         /usr/lib/plan9/bin/mk \
39                 -f /etc/init.mk \
40                 -i -k "$@"
41 }
42
43 # Process one command
44 function process {
45         cmd=$1
46         shift
47         arg=$*
48         case "$cmd" in
49         boot )
50                 echo
51                 echo mkinit -- booting
52                 runamk -a "$arg"
53                 ;;
54         restart )
55                 if [ "$arg" ]; then
56                         echo mkinit -- restarting $arg
57                         runamk "$arg"-stop
58                         runamk "$arg"-start
59                 fi
60                 ;;
61         start|stop|zap|status )
62                 if [ "$arg" ]; then
63                         echo mkinit -- ${cmd}ing $arg
64                         runamk "$arg-$cmd"
65                 fi
66                 ;;
67         mk|runlevel )
68                 if [ "$arg" ]; then
69                         [ "$cmd" = mk ] &&
70                                 echo mkinit -- running mk cmd [$arg]
71                         [ "$cmd" = runlevel ] &&
72                                 echo mkinit -- entering runlevel $arg
73                         runamk "$arg"
74                 fi
75                 ;;
76         reload )
77                 echo mkinit -- ${cmd}ing
78                 $TEST && 
79                         opt=-t
80                 exec $0 -r $opt
81                 ;;
82         eval )
83                 eval $arg
84                 ;;
85         ?* )
86                 echo mkinit -- unknown command [$cmd] [$arg]
87                 ;;
88         esac
89 }
90
91 # Handle arguments
92 TMP=`getopt -n "$0" \
93         --options     hrt \
94         --longoptions help,reload,test \
95         -- "$@"`
96 [ $? != 0 ] &&
97         usage
98 eval set -- "$TMP"
99 while [ ! "$1" == "--" ]; do
100         $TEST &&
101                 echo '$1=' $1
102         case "$1" in
103         -h|--help )
104                 usage
105                 ;;
106         -r|--reload )
107                 RELOAD=true
108                 ;;
109         -t|--test )
110                 TEST=true
111                 FIFO=/tmp/pipe
112                 INITCTL=/tmp/initctl
113                 trap "pkill -HUP -P $$" EXIT
114                 ;;
115         esac
116         shift
117 done
118 shift; cmd=$1
119 shift; arg=$*
120
121 # Debugging output
122 if [ "$TEST" ]; then
123         echo 'Options'
124         echo '  test:' $TEST
125         echo '  reload:' $RELOAD
126         echo '  cmd:' $cmd
127         echo '  arg:' $arg
128 fi
129
130 # Create fifos if they don't exist
131 test ! -e $FIFO &&
132         mkfifo $FIFO
133 test ! -e $INITCTL &&
134         mkfifo $INITCTL
135
136 # Initial boot-up
137 process $cmd $arg
138
139 # Fork listeners
140 if [ ! "$RELOAD" ]; then
141         # Fork /dev/initctl listener
142         ( exec 0<&- 1<&- 2<&-
143         initctld $INITCTL |
144         while read line; do
145                 echo $line > $FIFO
146         done ) &
147
148         # Fork console listener
149         # Readline uses stdin,stderr
150         ( exec 1<&-;
151         while true; do
152                 while read -e -p "mkinit> " line; do
153                         echo $line > $FIFO
154                         history -s $line
155                 done
156                 [ "$TEST" ] && break
157                 exec 0</dev/console 2>/dev/console
158                 echo "Respawning on /dev/console.." >&2
159                 sleep 1
160         done) <&0 &
161
162         # Close stdin, stderr
163         exec 0<&- 2<&-
164 fi
165
166 # Main loop
167 while true; do
168         while read line; do
169                 process $line
170         done < $FIFO
171 done