package/base-files: move /etc/functions.sh to /lib/functions.sh
[openwrt.git] / package / base-files / files / etc / rc.common
1 #!/bin/sh
2 # Copyright (C) 2006-2011 OpenWrt.org
3
4 . $IPKG_INSTROOT/etc/functions.sh
5
6 initscript=$1
7 action=${2:-help}
8 shift 2
9
10 #
11 # service: simple wrapper around start-stop-daemon
12 #
13 # Usage: service ACTION EXEC ARGS...
14 #
15 # Action:
16 # -S,--start start EXEC, passing it ARGS as its arguments
17 # -K,--stop stop EXEC (send it a $SERVICE_SIG_STOP signal)
18 # -R,--reload reload EXEC (send it a $SERVICE_SIG_RELOAD signal)
19 #
20 # Environment variables used:
21 # SERVICE_DAEMONIZE run EXEC in background
22 # SERVICE_WRITE_PID create a pid file and use it
23 # SERVICE_USE_PID assume EXEC creates its own pid file and use it
24 # SERVICE_PID_FILE pid file to use (default to /var/run/EXEC.pid)
25 # SERVICE_SIG_RELOAD signal used for reloading
26 # SERVICE_SIG_STOP signal used for stopping
27 # SERVICE_UID user EXEC should be run as
28 # SERVICE_GID group EXEC should be run as
29 #
30 # SERVICE_DEBUG don't do anything, but show what would be done
31 # SERVICE_QUIET don't print anything
32 #
33
34 SERVICE_QUIET=1
35 SERVICE_SIG_RELOAD="HUP"
36 SERVICE_SIG_STOP="TERM"
37
38 service() {
39 local ssd
40 local ssd_pid_file
41 local ssd_sig
42 local ssd_start
43 ssd="${SERVICE_DEBUG:+echo }start-stop-daemon${SERVICE_QUIET:+ -q}"
44 case "$1" in
45 -S|--start|start)
46 ssd="$ssd -S"
47 ssd_start=1
48 ;;
49 -K|--stop|stop)
50 ssd="$ssd -K"
51 ssd_sig="$SERVICE_SIG_STOP"
52 ;;
53 -R|--reload|reload)
54 ssd="$ssd -K"
55 ssd_sig="$SERVICE_SIG_RELOAD"
56 ;;
57 *)
58 echo "ssd: unknow action '$1'" 1>&2
59 return 1
60 esac
61 shift
62 if [ -z "$1" ]; then
63 echo "ssd: missing arguments" 1>&2
64 return 1
65 fi
66 ssd="$ssd -x $1"
67 if [ -n "$SERVICE_PID_FILE$SERVICE_USE_PID$SERVICE_WRITE_PID" ]; then
68 ssd="$ssd -p ${SERVICE_PID_FILE:-/var/run/${1##*/}.pid}"
69 fi
70 ssd="$ssd${SERVICE_UID:+ -c $SERVICE_UID${SERVICE_GID:+:$SERVICE_GID}}"
71 if [ -n "$ssd_start" ]; then
72 ssd="$ssd${SERVICE_DAEMONIZE:+ -b}${SERVICE_WRITE_PID:+ -m}"
73 else
74 ssd="$ssd${ssd_sig:+ -s $ssd_sig}"
75 fi
76 shift
77 $ssd${1:+ -- "$@"}
78 }
79
80 service_start() {
81 service -S "$@"
82 }
83
84 service_stop() {
85 service -K "$@"
86 }
87
88 service_reload() {
89 service -R "$@"
90 }
91
92 start() {
93 return 0
94 }
95
96 stop() {
97 return 0
98 }
99
100 reload() {
101 return 1
102 }
103
104 restart() {
105 trap '' TERM
106 stop "$@"
107 start "$@"
108 }
109
110 boot() {
111 start "$@"
112 }
113
114 shutdown() {
115 stop
116 }
117
118 disable() {
119 name="$(basename "${initscript}")"
120 rm -f "$IPKG_INSTROOT"/etc/rc.d/S??$name
121 rm -f "$IPKG_INSTROOT"/etc/rc.d/K??$name
122 }
123
124 enable() {
125 name="$(basename "${initscript}")"
126 disable
127 [ -n "$START" -o -n "$STOP" ] || {
128 echo "/etc/init.d/$name does not have a START or STOP value"
129 return 1
130 }
131 [ "$START" ] && ln -s "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}"
132 [ "$STOP" ] && ln -s "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/K${STOP}${name##K[0-9][0-9]}"
133 }
134
135 enabled() {
136 name="$(basename "${initscript}")"
137 [ -x "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}" ]
138 }
139
140 depends() {
141 return 0
142 }
143
144 help() {
145 cat <<EOF
146 Syntax: $initscript [command]
147
148 Available commands:
149 start Start the service
150 stop Stop the service
151 restart Restart the service
152 reload Reload configuration files (or restart if that fails)
153 enable Enable service autostart
154 disable Disable service autostart
155 $EXTRA_HELP
156 EOF
157 }
158
159 . "$initscript"
160
161 ALL_COMMANDS="start stop reload restart boot shutdown enable disable enabled depends ${EXTRA_COMMANDS}"
162 list_contains ALL_COMMANDS "$action" || action=help
163 [ "$action" = "reload" ] && action='eval reload "$@" || restart "$@" && :'
164 $action "$@"
This page took 0.058053 seconds and 5 git commands to generate.