procd: rework trigger handling
[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 $fail_threshold $restart_timeout $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 # user info: array with 1 values $username
22 # pidfile: file name to write pid into
23 #
24 # No space separation is done for arrays/tables - use one function argument per command line argument
25 #
26 # procd_close_instance():
27 # Complete the instance being prepared
28 #
29 # procd_kill(service, [instance]):
30 # Kill a service instance (or all instances)
31 #
32
33 . $IPKG_INSTROOT/usr/share/libubox/jshn.sh
34
35 _PROCD_SERVICE=
36
37 _procd_call() {
38 local old_cb
39
40 json_set_namespace procd old_cb
41 "$@"
42 json_set_namespace $old_cb
43 }
44
45 _procd_wrapper() {
46 while [ -n "$1" ]; do
47 eval "$1() { _procd_call _$1 \"\$@\"; }"
48 shift
49 done
50 }
51
52 _procd_ubus_call() {
53 local cmd="$1"
54
55 [ -n "$PROCD_DEBUG" ] && json_dump >&2
56 ubus call service "$cmd" "$(json_dump)"
57 json_cleanup
58 }
59
60 _procd_open_service() {
61 local name="$1"
62 local script="$2"
63
64 _PROCD_SERVICE="$name"
65 _PROCD_INSTANCE_SEQ=0
66
67 json_init
68 json_add_string name "$name"
69 [ -n "$script" ] && json_add_string script "$script"
70 json_add_object instances
71 }
72
73 _procd_close_service() {
74 json_close_object
75 _procd_open_trigger
76 service_triggers
77 _procd_close_trigger
78 _procd_ubus_call set
79 }
80
81 _procd_add_array_data() {
82 while [ "$#" -gt 0 ]; do
83 json_add_string "" "$1"
84 shift
85 done
86 }
87
88 _procd_add_array() {
89 json_add_array "$1"
90 shift
91 _procd_add_array_data "$@"
92 json_close_array
93 }
94
95 _procd_add_table_data() {
96 while [ -n "$1" ]; do
97 local var="${1%%=*}"
98 local val="${1#*=}"
99 [ "$1" = "$val" ] && val=
100 json_add_string "$var" "$val"
101 shift
102 done
103 }
104
105 _procd_add_table() {
106 json_add_object "$1"
107 shift
108 _procd_add_table_data "$@"
109 json_close_object
110 }
111
112 _procd_open_instance() {
113 local name="$1"; shift
114
115 _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
116 name="${name:-instance$_PROCD_INSTANCE_SEQ}"
117 json_add_object "$name"
118 [ -n "$TRACE_SYSCALLS" ] && json_add_boolean trace "1"
119 }
120
121 _procd_open_trigger() {
122 let '_procd_trigger_open = _procd_trigger_open + 1'
123 [ "$_procd_trigger_open" -gt 1 ] && return
124 json_add_array "triggers"
125 }
126
127 _procd_close_trigger() {
128 let '_procd_trigger_open = _procd_trigger_open - 1'
129 [ "$_procd_trigger_open" -lt 1 ] || return
130 json_close_array
131 }
132
133 _procd_open_validate() {
134 json_select ..
135 json_add_array "validate"
136 }
137
138 _procd_close_validate() {
139 json_close_array
140 json_select triggers
141 }
142
143 _procd_add_jail() {
144 json_add_object "jail"
145 json_add_string name "$1"
146
147 shift
148
149 for a in $@; do
150 case $a in
151 log) json_add_boolean "log" "1";;
152 ubus) json_add_boolean "ubus" "1";;
153 procfs) json_add_boolean "procfs" "1";;
154 sysfs) json_add_boolean "sysfs" "1";;
155 ronly) json_add_boolean "ronly" "1";;
156 esac
157 done
158 json_add_object "mount"
159 json_close_object
160 json_close_object
161 }
162
163 _procd_add_jail_mount() {
164 local _json_no_warning=1
165
166 json_select "jail"
167 [ $? = 0 ] || return
168 json_select "mount"
169 [ $? = 0 ] || {
170 json_select ..
171 return
172 }
173 for a in $@; do
174 json_add_string "$a" "0"
175 done
176 json_select ..
177 json_select ..
178 }
179
180 _procd_add_jail_mount_rw() {
181 local _json_no_warning=1
182
183 json_select "jail"
184 [ $? = 0 ] || return
185 json_select "mount"
186 [ $? = 0 ] || {
187 json_select ..
188 return
189 }
190 for a in $@; do
191 json_add_string "$a" "1"
192 done
193 json_select ..
194 json_select ..
195 }
196
197 _procd_set_param() {
198 local type="$1"; shift
199
200 case "$type" in
201 env|data|limits)
202 _procd_add_table "$type" "$@"
203 ;;
204 command|netdev|file|respawn|watch)
205 _procd_add_array "$type" "$@"
206 ;;
207 error)
208 json_add_array "$type"
209 json_add_string "" "$@"
210 json_close_array
211 ;;
212 nice)
213 json_add_int "$type" "$1"
214 ;;
215 pidfile|user|seccomp|capabilities)
216 json_add_string "$type" "$1"
217 ;;
218 stdout|stderr|no_new_privs)
219 json_add_boolean "$type" "$1"
220 ;;
221 esac
222 }
223
224 _procd_add_interface_trigger() {
225 json_add_array
226 _procd_add_array_data "$1"
227 shift
228
229 json_add_array
230 _procd_add_array_data "if"
231
232 json_add_array
233 _procd_add_array_data "eq" "interface" "$1"
234 shift
235 json_close_array
236
237 json_add_array
238 _procd_add_array_data "run_script" "$@"
239 json_close_array
240
241 json_close_array
242 json_close_array
243 }
244
245 _procd_add_reload_interface_trigger() {
246 local script=$(readlink "$initscript")
247 local name=$(basename ${script:-$initscript})
248
249 _procd_open_trigger
250 _procd_add_interface_trigger "interface.*" $1 /etc/init.d/$name reload
251 _procd_close_trigger
252 }
253
254 _procd_add_config_trigger() {
255 json_add_array
256 _procd_add_array_data "$1"
257 shift
258
259 json_add_array
260 _procd_add_array_data "if"
261
262 json_add_array
263 _procd_add_array_data "eq" "package" "$1"
264 shift
265 json_close_array
266
267 json_add_array
268 _procd_add_array_data "run_script" "$@"
269 json_close_array
270
271 json_close_array
272
273 json_close_array
274 }
275
276 _procd_add_raw_trigger() {
277 json_add_array
278 _procd_add_array_data "$1"
279 shift
280 local timeout=$1
281 shift
282
283 json_add_array
284 json_add_array
285 _procd_add_array_data "run_script" "$@"
286 json_close_array
287 json_close_array
288
289 json_add_int "" "$timeout"
290
291 json_close_array
292 }
293
294 _procd_add_reload_trigger() {
295 local script=$(readlink "$initscript")
296 local name=$(basename ${script:-$initscript})
297 local file
298
299 _procd_open_trigger
300 for file in "$@"; do
301 _procd_add_config_trigger "config.change" "$file" /etc/init.d/$name reload
302 done
303 _procd_close_trigger
304 }
305
306 _procd_add_validation() {
307 _procd_open_validate
308 $@
309 _procd_close_validate
310 }
311
312 _procd_append_param() {
313 local type="$1"; shift
314 local _json_no_warning=1
315
316 json_select "$type"
317 [ $? = 0 ] || {
318 _procd_set_param "$type" "$@"
319 return
320 }
321 case "$type" in
322 env|data|limits)
323 _procd_add_table_data "$@"
324 ;;
325 command|netdev|file|respawn|watch)
326 _procd_add_array_data "$@"
327 ;;
328 error)
329 json_add_string "" "$@"
330 ;;
331 esac
332 json_select ..
333 }
334
335 _procd_close_instance() {
336 local respawn_vals
337 _json_no_warning=1
338 if json_select respawn ; then
339 json_get_values respawn_vals
340 if [ -z "$respawn_vals" ]; then
341 local respawn_retry=$(uci_get system.@service[0].respawn_retry)
342 _procd_add_array_data 3600 5 ${respawn_retry:-5}
343 fi
344 json_select ..
345 fi
346
347 json_close_object
348 }
349
350 _procd_add_instance() {
351 _procd_open_instance
352 _procd_set_param command "$@"
353 _procd_close_instance
354 }
355
356 _procd_kill() {
357 local service="$1"
358 local instance="$2"
359
360 json_init
361 [ -n "$service" ] && json_add_string name "$service"
362 [ -n "$instance" ] && json_add_string instance "$instance"
363 _procd_ubus_call delete
364 }
365
366 procd_open_data() {
367 local name="$1"
368 json_set_namespace procd __procd_old_cb
369 json_add_object data
370 }
371
372 procd_close_data() {
373 json_close_object
374 json_set_namespace $__procd_old_cb
375 }
376
377 _procd_set_config_changed() {
378 local package="$1"
379
380 json_init
381 json_add_string type config.change
382 json_add_object data
383 json_add_string package "$package"
384 json_close_object
385
386 ubus call service event "$(json_dump)"
387 }
388
389 procd_add_mdns_service() {
390 local service proto port
391 service=$1; shift
392 proto=$1; shift
393 port=$1; shift
394 json_add_object "${service}_$port"
395 json_add_string "service" "_$service._$proto.local"
396 json_add_int port "$port"
397 [ -n "$1" ] && {
398 json_add_array txt
399 for txt in $@; do json_add_string "" $txt; done
400 json_select ..
401 }
402 json_select ..
403 }
404
405 procd_add_mdns() {
406 procd_open_data
407 json_add_object "mdns"
408 procd_add_mdns_service $@
409 json_close_object
410 procd_close_data
411 }
412
413 uci_validate_section()
414 {
415 local _package="$1"
416 local _type="$2"
417 local _name="$3"
418 local _result
419 local _error
420 shift; shift; shift
421 _result=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null`
422 _error=$?
423 eval "$_result"
424 [ "$_error" = "0" ] || `/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null`
425 return $_error
426 }
427
428 _procd_wrapper \
429 procd_open_service \
430 procd_close_service \
431 procd_add_instance \
432 procd_add_raw_trigger \
433 procd_add_config_trigger \
434 procd_add_interface_trigger \
435 procd_add_reload_trigger \
436 procd_add_reload_interface_trigger \
437 procd_open_trigger \
438 procd_close_trigger \
439 procd_open_instance \
440 procd_close_instance \
441 procd_open_validate \
442 procd_close_validate \
443 procd_add_jail \
444 procd_add_jail_mount \
445 procd_add_jail_mount_rw \
446 procd_set_param \
447 procd_append_param \
448 procd_add_validation \
449 procd_set_config_changed \
450 procd_kill