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