]> Pileus Git - mkinit/blob - src/mkinit
(no commit message)
[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         exec 1>/dev/console
49         case "$cmd" in
50         boot )
51                 echo
52                 echo mkinit -- booting
53                 runamk -a "$arg"
54                 ;;
55         restart )
56                 if [ "$arg" ]; then
57                         echo mkinit -- restarting $arg
58                         runamk "$arg"-stop
59                         runamk "$arg"-start
60                 fi
61                 ;;
62         start|stop|zap|status )
63                 if [ "$arg" ]; then
64                         echo mkinit -- ${cmd}ing $arg
65                         runamk "$arg-$cmd"
66                 fi
67                 ;;
68         mk|runlevel )
69                 if [ "$arg" ]; then
70                         [ "$cmd" = mk ] &&
71                                 echo mkinit -- running mk cmd [$arg]
72                         [ "$cmd" = runlevel ] &&
73                                 echo mkinit -- entering runlevel $arg
74                         runamk "$arg"
75                 fi
76                 ;;
77         reload )
78                 echo mkinit -- ${cmd}ing
79                 $TEST && 
80                         opt=-t
81                 exec $0 -r $opt
82                 ;;
83         eval )
84                 eval $arg
85                 ;;
86         ?* )
87                 echo mkinit -- unknown command [$cmd] [$arg]
88                 ;;
89         esac
90 }
91
92 # Telinit
93 if [ $$ != 1 -a $1 != "-t" ]; then
94         echo "$@" > $FIFO
95         exit
96 fi
97
98 # Handle arguments
99 TMP=`getopt -n "$0" \
100         --options     hrt \
101         --longoptions help,reload,test \
102         -- "$@"`
103 [ $? != 0 ] &&
104         usage
105 eval set -- "$TMP"
106 while [ ! "$1" == "--" ]; do
107         $TEST &&
108                 echo '$1=' $1
109         case "$1" in
110         -h|--help )
111                 usage
112                 ;;
113         -r|--reload )
114                 RELOAD=true
115                 ;;
116         -t|--test )
117                 TEST=true
118                 FIFO=/tmp/pipe
119                 INITCTL=/tmp/initctl
120                 trap "pkill -HUP -P $$" EXIT
121                 ;;
122         esac
123         shift
124 done
125 shift; cmd=$1
126 shift; arg=$*
127
128 # Debugging output
129 if $TEST; then
130         echo 'Options'
131         echo '  test:' $TEST
132         echo '  reload:' $RELOAD
133         echo '  cmd:' $cmd
134         echo '  arg:' $arg
135 fi
136
137 # Create fifos if they don't exist
138 test ! -e $FIFO &&
139         mkfifo $FIFO
140 test ! -e $INITCTL &&
141         mkfifo $INITCTL
142
143 # Initial boot-up
144 process $cmd $arg
145
146 # Fork listeners
147 if ! $RELOAD; then
148         # Fork /dev/initctl listener
149         ( exec 0<&- 1<&- 2<&-
150         initctld $INITCTL |
151         while read line; do
152                 echo $line > $FIFO
153         done ) &
154
155         # Fork console listener
156         # Readline uses stdin,stderr
157         ( exec 1<&-;
158         while true; do
159                 while read -e -p "mkinit> " line; do
160                         echo $line > $FIFO
161                         history -s $line
162                 done
163                 $TEST && break
164                 exec 0</dev/console 2>/dev/console
165                 echo "Respawning on /dev/console.." >&2
166                 sleep 1
167         done) <&0 &
168
169         # Close stdin, stderr
170         exec 0<&- 2<&-
171 fi
172
173 # Main loop
174 while true; do
175         while read line; do
176                 process $line
177         done < $FIFO
178 done