procd: add a PROCD_DEBUG variable that will dump the ubus calls from init scripts...
[openwrt/openwrt.git] / package / system / procd / files / procd.sh
1 # procd API:
2 #
3 # procd_open_service(name, [script]):
4 # Initialize a new procd command message containing a service with one or more instances
5 #
6 # procd_close_service()
7 # Send the command message for the service
8 #
9 # procd_open_instance([name]):
10 # Add an instance to the service described by the previous procd_open_service call
11 #
12 # procd_set_param(type, [value...])
13 # Available types:
14 # command: command line (array).
15 # respawn info: array with 3 values $restart_timeout $fail_hreshold $max_fail
16 # env: environment variable (passed to the process)
17 # data: arbitrary name/value pairs for detecting config changes (table)
18 # file: configuration files (array)
19 # netdev: bound network device (detects ifindex changes)
20 # limits: resource limits (passed to the process)
21 #
22 # No space separation is done for arrays/tables - use one function argument per command line argument
23 #
24 # procd_close_instance():
25 # Complete the instance being prepared
26 #
27 # procd_kill(service, [instance]):
28 # Kill a service instance (or all instances)
29 #
30
31 . $IPKG_INSTROOT/usr/share/libubox/jshn.sh
32
33 _PROCD_SERVICE=
34
35 _procd_call() {
36 local old_cb
37
38 json_set_namespace procd old_cb
39 "$@"
40 json_set_namespace $old_cb
41 }
42
43 _procd_wrapper() {
44 while [ -n "$1" ]; do
45 eval "$1() { _procd_call _$1 \"\$@\"; }"
46 shift
47 done
48 }
49
50 _procd_ubus_call() {
51 local cmd="$1"
52
53 [ -n "$PROCD_DEBUG" ] && json_dump >&2
54 ubus call service "$cmd" "$(json_dump)"
55 json_cleanup
56 }
57
58 _procd_open_service() {
59 local name="$1"
60 local script="$2"
61
62 _PROCD_SERVICE="$name"
63 _PROCD_INSTANCE_SEQ=0
64
65 json_init
66 json_add_string name "$name"
67 [ -n "$script" ] && json_add_string script "$script"
68 json_add_object instances
69 }
70
71 _procd_close_service() {
72 json_close_object
73 service_triggers
74 _procd_ubus_call set
75 }
76
77 _procd_add_array_data() {
78 while [ -n "$1" ]; do
79 json_add_string "" "$1"
80 shift
81 done
82 }
83
84 _procd_add_array() {
85 json_add_array "$1"
86 shift
87 _procd_add_array_data "$@"
88 json_close_array
89 }
90
91 _procd_add_table_data() {
92 while [ -n "$1" ]; do
93 local var="${1%%=*}"
94 local val="${1#*=}"
95 [[ "$1" == "$val" ]] && val=
96 json_add_string "$var" "$val"
97 shift
98 done
99 }
100
101 _procd_add_table() {
102 json_add_object "$1"
103 shift
104 _procd_add_table_data "$@"
105 json_close_object
106 }
107
108 _procd_open_instance() {
109 local name="$1"; shift
110
111 _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
112 name="${name:-instance$_PROCD_INSTANCE_SEQ}"
113 json_add_object "$name"
114 }
115
116 _procd_open_trigger() {
117 json_add_array "triggers"
118 }
119
120 _procd_open_validate() {
121 json_add_array "validate"
122 }
123
124 _procd_set_param() {
125 local type="$1"; shift
126
127 case "$type" in
128 env|data|limits)
129 _procd_add_table "$type" "$@"
130 ;;
131 command|netdev|file|respawn)
132 _procd_add_array "$type" "$@"
133 ;;
134 nice)
135 json_add_int "$type" "$1"
136 ;;
137 esac
138 }
139
140 _procd_add_config_trigger() {
141 json_add_array
142 _procd_add_array_data "$1"
143 shift
144
145 json_add_array
146 _procd_add_array_data "if"
147
148 json_add_array
149 _procd_add_array_data "eq" "package" "$1"
150 shift
151 json_close_array
152
153 json_add_array
154 _procd_add_array_data "run_script" "$@"
155 json_close_array
156
157 json_close_array
158
159 json_close_array
160 }
161
162 _procd_add_reload_trigger() {
163 local script=$(readlink "$initscript")
164 local name=$(basename ${script:-$initscript})
165
166 _procd_open_trigger
167 _procd_add_config_trigger "config.change" $1 /etc/init.d/$name reload
168 _procd_close_trigger
169 }
170
171 _procd_add_validation() {
172 _procd_open_validate
173 $@
174 _procd_close_validate
175 }
176
177 _procd_append_param() {
178 local type="$1"; shift
179
180 json_select "$type"
181 case "$type" in
182 env|data|limits)
183 _procd_add_table_data "$@"
184 ;;
185 command|netdev|file|respawn)
186 _procd_add_array_data "$@"
187 ;;
188 esac
189 json_select ..
190 }
191
192 _procd_close_instance() {
193 json_close_object
194 }
195
196 _procd_close_trigger() {
197 json_close_array
198 }
199
200 _procd_close_validate() {
201 json_close_array
202 }
203
204 _procd_add_instance() {
205 _procd_open_instance
206 _procd_set_param command "$@"
207 _procd_close_instance
208 }
209
210 _procd_kill() {
211 local service="$1"
212 local instance="$2"
213
214 json_init
215 [ -n "$service" ] && json_add_string name "$service"
216 [ -n "$instance" ] && json_add_string instance "$instance"
217 _procd_ubus_call delete
218 }
219
220 uci_validate_section()
221 {
222 local package="$1"
223 local type="$2"
224 local name="$3"
225 local error
226 shift; shift; shift
227 local result=`/sbin/validate_data "$package" "$type" "$name" "$@" 2> /dev/null`
228 error=$?
229 eval "$result"
230 [ "$error" = "0" ] || `/sbin/validate_data "$package" "$type" "$name" "$@" 1> /dev/null`
231 return $error
232 }
233
234 _procd_wrapper \
235 procd_open_service \
236 procd_close_service \
237 procd_add_instance \
238 procd_add_config_trigger \
239 procd_add_reload_trigger \
240 procd_open_trigger \
241 procd_close_trigger \
242 procd_open_instance \
243 procd_close_instance \
244 procd_open_validate \
245 procd_close_validate \
246 procd_set_param \
247 procd_append_param \
248 procd_add_validation \
249 procd_kill