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