base-files: run readlink on initscript name before opening services
[openwrt.git] / package / base-files / files / etc / rc.common
1 #!/bin/sh
2 # Copyright (C) 2006-2012 OpenWrt.org
3
4 . $IPKG_INSTROOT/lib/functions.sh
5 . $IPKG_INSTROOT/lib/functions/service.sh
6
7 initscript=$1
8 action=${2:-help}
9 shift 2
10
11 start() {
12         return 0
13 }
14
15 stop() {
16         return 0
17 }
18
19 reload() {
20         return 1
21 }
22
23 restart() {
24         trap '' TERM
25         stop "$@"
26         start "$@"
27 }
28
29 boot() {
30         start "$@"
31 }
32
33 shutdown() {
34         stop
35 }
36
37 disable() {
38         name="$(basename "${initscript}")"
39         rm -f "$IPKG_INSTROOT"/etc/rc.d/S??$name
40         rm -f "$IPKG_INSTROOT"/etc/rc.d/K??$name
41 }
42
43 enable() {
44         name="$(basename "${initscript}")"
45         disable
46         [ -n "$START" -o -n "$STOP" ] || {
47                 echo "/etc/init.d/$name does not have a START or STOP value"
48                 return 1
49         }
50         [ "$START" ] && ln -s "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}"
51         [ "$STOP"  ] && ln -s "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/K${STOP}${name##K[0-9][0-9]}"
52 }
53
54 enabled() {
55         name="$(basename "${initscript}")"
56         [ -x "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}" ]
57 }
58
59 depends() {
60         return 0
61 }
62
63 help() {
64         cat <<EOF
65 Syntax: $initscript [command]
66
67 Available commands:
68         start   Start the service
69         stop    Stop the service
70         restart Restart the service
71         reload  Reload configuration files (or restart if that fails)
72         enable  Enable service autostart
73         disable Disable service autostart
74 $EXTRA_HELP
75 EOF
76 }
77
78 # for procd
79 start_service() {
80         return 0
81 }
82
83 stop_service() {
84         return 0
85 }
86
87 ${INIT_TRACE:+set -x}
88
89 . "$initscript"
90
91 [ -n "$USE_PROCD" ] && {
92         . $IPKG_INSTROOT/lib/functions/procd.sh
93         basescript=$(readlink "$initscript")
94         rc_procd() {
95                 procd_open_service "$(basename ${basescript:-$initscript})" "$initscript"
96                 "$@"
97                 procd_close_service
98         }
99
100         start() {
101                 rc_procd start_service "$@"
102         }
103
104         stop() {
105                 procd_kill "$(basename ${basescript:-$initscript})" "$1"
106         }
107
108         reload() {
109                 start
110         }
111 }
112
113 ALL_COMMANDS="start stop reload restart boot shutdown enable disable enabled depends ${EXTRA_COMMANDS}"
114 list_contains ALL_COMMANDS "$action" || action=help
115 [ "$action" = "reload" ] && action='eval reload "$@" || restart "$@" && :'
116 $action "$@"