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