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