]> Pileus Git - mkinit/blob - src/mkinit
mkinit
[mkinit] / src / mkinit
1 #!/bin/sh
2
3 # Copyright (C) 2009 Andy Spencer
4 # See ../COPYING for terms
5
6 function usage {
7 cat - <<EOF
8 usage: $0 [options] [command]
9
10 Options:
11   -h,--help    Print usage information
12   -t,--test    Fake all commands
13
14 Command:
15   boot         Execute boot-up procedures
16   halt         Execute shutdown procedures
17   reload       Re-execute init process
18   mk <rule>    Execute mk rule
19   start|stop|restart|zap|status <service>
20                Start,stop,restart,zap or query status of service
21   eval <cmd>   Execute command in mkinit process
22 EOF
23 exit
24 }
25
26 function runamk {
27         [ "$TEST" ] && export P=true
28         /usr/lib/plan9/bin/mk \
29                 -f /etc/init.mk \
30                 -i -k "$@"
31 }
32
33 # Handle arguments
34 TEMP=`getopt -n "$0" \
35         --options     th \
36         --longoptions test,help \
37         -- "$@"`
38 [ $? != 0 ] && usage
39 eval set -- "$TEMP"
40 while true; do
41         [ "$TEST" ] && echo "\$1=$1"
42         case "$1" in
43         -h|--help   ) usage ;;
44         -t|--test   ) TEST=true; shift ;;
45         --          ) shift; cmd="$1";
46                       shift; args="$@";
47                       break ;;
48         *           ) ;;
49         esac
50 done
51 if [ "$TEST" ]; then
52         echo "Options"
53         echo "  test=$TEST"
54         echo "  cmd=$cmd"
55         echo "  args=$args"
56 fi
57
58 # Main loop
59 while true; do
60         case "$cmd" in
61         boot )
62                 echo
63                 echo "mkinit -- booting"
64                 if runamk -a "$args" && ! [ "$TEST" ]; then
65                         # booted successuflly, redirect input
66                         echo "skipping redirect"
67                         #pipe=/lib/mkinit/cmd
68                         #[ -p pipe ] || mkfifo $pipe
69                         #exec 0< $pipe
70                 fi
71                 ;;
72         halt|reboot|poweroff )
73                 echo "mkinit -- ${cmd}ing"
74                 runamk "halt"
75                 if [ "$args" -o "$cmd" = reboot -o "$cmd" = poweroff ]; then
76                         # mk handles syncing and logging message
77                         ${TEST:+echo} $cmd -ndf $args
78                 fi
79                 ;;
80         restart )
81                 if [ "$args" ]; then
82                         echo "mkinit -- restarting $args"
83                         runamk "stop-$args"
84                         runamk "start-$args"
85                 fi
86                 ;;
87         start|stop|zap|status )
88                 if [ "$args" ]; then
89                         echo "mkinit -- ${cmd}ing $args"
90                         runamk "${cmd}-$args"
91                 fi
92                 ;;
93         mk )
94                 if [ "$args" ]; then
95                         echo "mkinit -- running mk cmd [$args]"
96                         runamk "$args"
97                 fi
98                 ;;
99         reload )
100                 exec /sbin/mkinit ${TEST:+"-t"}
101                 ;;
102         eval )
103                 $args
104                 ;;
105         ?* )
106                 echo "unknown command [$cmd $args]"
107                 ;;
108         esac
109         read -e -p "mkinit> " cmd args
110         history -s $cmd $args
111 done