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