Upstream refresh for v1.5.0-rc1 : Upstream 19.07 : 4fb6b8c553f692eeb5bcb203e0f8ee8df0...
[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: $username to run service as
22 #     group: $groupname to run service as
23 #     pidfile: file name to write pid into
24 #     stdout: boolean whether to redirect commands stdout to syslog (default: 0)
25 #     stderr: boolean whether to redirect commands stderr to syslog (default: 0)
26 #     facility: syslog facility used when logging to syslog (default: daemon)
27 #
28 #   No space separation is done for arrays/tables - use one function argument per command line argument
29 #
30 # procd_close_instance():
31 #   Complete the instance being prepared
32 #
33 # procd_running(service, [instance]):
34 #   Checks if service/instance is currently running
35 #
36 # procd_kill(service, [instance]):
37 #   Kill a service instance (or all instances)
38 #
39 # procd_send_signal(service, [instance], [signal])
40 #   Send a signal to a service instance (or all instances)
41 #
42
43 . "$IPKG_INSTROOT/usr/share/libubox/jshn.sh"
44
45 PROCD_RELOAD_DELAY=1000
46 _PROCD_SERVICE=
47
48 procd_lock() {
49         local basescript=$(readlink "$initscript")
50         local service_name="$(basename ${basescript:-$initscript})"
51
52         flock -n 1000 &> /dev/null
53         if [ "$?" != "0" ]; then
54                 exec 1000>"$IPKG_INSTROOT/var/lock/procd_${service_name}.lock"
55                 flock 1000
56                 if [ "$?" != "0" ]; then
57                         logger "warning: procd flock for $service_name failed"
58                 fi
59         fi
60 }
61
62 _procd_call() {
63         local old_cb
64
65         json_set_namespace procd old_cb
66         "$@"
67         json_set_namespace $old_cb
68 }
69
70 _procd_wrapper() {
71         procd_lock
72         while [ -n "$1" ]; do
73                 eval "$1() { _procd_call _$1 \"\$@\"; }"
74                 shift
75         done
76 }
77
78 _procd_ubus_call() {
79         local cmd="$1"
80
81         [ -n "$PROCD_DEBUG" ] && json_dump >&2
82         ubus call service "$cmd" "$(json_dump)"
83         json_cleanup
84 }
85
86 _procd_open_service() {
87         local name="$1"
88         local script="$2"
89
90         _PROCD_SERVICE="$name"
91         _PROCD_INSTANCE_SEQ=0
92
93         json_init
94         json_add_string name "$name"
95         [ -n "$script" ] && json_add_string script "$script"
96         json_add_object instances
97 }
98
99 _procd_close_service() {
100         json_close_object
101         _procd_open_trigger
102         service_triggers
103         _procd_close_trigger
104         _procd_open_data
105         service_data
106         _procd_close_data
107         _procd_ubus_call ${1:-set}
108 }
109
110 _procd_add_array_data() {
111         while [ "$#" -gt 0 ]; do
112                 json_add_string "" "$1"
113                 shift
114         done
115 }
116
117 _procd_add_array() {
118         json_add_array "$1"
119         shift
120         _procd_add_array_data "$@"
121         json_close_array
122 }
123
124 _procd_add_table_data() {
125         while [ -n "$1" ]; do
126                 local var="${1%%=*}"
127                 local val="${1#*=}"
128                 [ "$1" = "$val" ] && val=
129                 json_add_string "$var" "$val"
130                 shift
131         done
132 }
133
134 _procd_add_table() {
135         json_add_object "$1"
136         shift
137         _procd_add_table_data "$@"
138         json_close_object
139 }
140
141 _procd_open_instance() {
142         local name="$1"; shift
143
144         _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
145         name="${name:-instance$_PROCD_INSTANCE_SEQ}"
146         json_add_object "$name"
147         [ -n "$TRACE_SYSCALLS" ] && json_add_boolean trace "1"
148 }
149
150 _procd_open_trigger() {
151         let '_procd_trigger_open = _procd_trigger_open + 1'
152         [ "$_procd_trigger_open" -gt 1 ] && return
153         json_add_array "triggers"
154 }
155
156 _procd_close_trigger() {
157         let '_procd_trigger_open = _procd_trigger_open - 1'
158         [ "$_procd_trigger_open" -lt 1 ] || return
159         json_close_array
160 }
161
162 _procd_open_data() {
163         let '_procd_data_open = _procd_data_open + 1'
164         [ "$_procd_data_open" -gt 1 ] && return
165         json_add_object "data"
166 }
167
168 _procd_close_data() {
169         let '_procd_data_open = _procd_data_open - 1'
170         [ "$_procd_data_open" -lt 1 ] || return
171         json_close_object
172 }
173
174 _procd_open_validate() {
175         json_select ..
176         json_add_array "validate"
177 }
178
179 _procd_close_validate() {
180         json_close_array
181         json_select triggers
182 }
183
184 _procd_add_jail() {
185         json_add_object "jail"
186         json_add_string name "$1"
187
188         shift
189         
190         for a in $@; do
191                 case $a in
192                 log)    json_add_boolean "log" "1";;
193                 ubus)   json_add_boolean "ubus" "1";;
194                 procfs) json_add_boolean "procfs" "1";;
195                 sysfs)  json_add_boolean "sysfs" "1";;
196                 ronly)  json_add_boolean "ronly" "1";;
197                 esac
198         done
199         json_add_object "mount"
200         json_close_object
201         json_close_object
202 }
203
204 _procd_add_jail_mount() {
205         local _json_no_warning=1
206
207         json_select "jail"
208         [ $? = 0 ] || return
209         json_select "mount"
210         [ $? = 0 ] || {
211                 json_select ..
212                 return
213         }
214         for a in $@; do
215                 json_add_string "$a" "0"
216         done
217         json_select ..
218         json_select ..
219 }
220
221 _procd_add_jail_mount_rw() {
222         local _json_no_warning=1
223
224         json_select "jail"
225         [ $? = 0 ] || return
226         json_select "mount"
227         [ $? = 0 ] || {
228                 json_select ..
229                 return
230         }
231         for a in $@; do
232                 json_add_string "$a" "1"
233         done
234         json_select ..
235         json_select ..
236 }
237
238 _procd_set_param() {
239         local type="$1"; shift
240
241         case "$type" in
242                 env|data|limits)
243                         _procd_add_table "$type" "$@"
244                 ;;
245                 command|netdev|file|respawn|watch)
246                         _procd_add_array "$type" "$@"
247                 ;;
248                 error)
249                         json_add_array "$type"
250                         json_add_string "" "$@"
251                         json_close_array
252                 ;;
253                 nice|term_timeout)
254                         json_add_int "$type" "$1"
255                 ;;
256                 reload_signal)
257                         json_add_int "$type" $(kill -l "$1")
258                 ;;
259                 pidfile|user|group|seccomp|capabilities|facility)
260                         json_add_string "$type" "$1"
261                 ;;
262                 stdout|stderr|no_new_privs)
263                         json_add_boolean "$type" "$1"
264                 ;;
265         esac
266 }
267
268 _procd_add_timeout() {
269         [ "$PROCD_RELOAD_DELAY" -gt 0 ] && json_add_int "" "$PROCD_RELOAD_DELAY"
270         return 0
271 }
272
273 _procd_add_interface_trigger() {
274         json_add_array
275         _procd_add_array_data "$1"
276         shift
277
278         json_add_array
279         _procd_add_array_data "if"
280
281         json_add_array
282         _procd_add_array_data "eq" "interface" "$1"
283         shift
284         json_close_array
285
286         json_add_array
287         _procd_add_array_data "run_script" "$@"
288         json_close_array
289
290         json_close_array
291         _procd_add_timeout
292         json_close_array
293 }
294
295 _procd_add_reload_interface_trigger() {
296         local script=$(readlink "$initscript")
297         local name=$(basename ${script:-$initscript})
298
299         _procd_open_trigger
300         _procd_add_interface_trigger "interface.*" $1 /etc/init.d/$name reload
301         _procd_close_trigger
302 }
303
304 _procd_add_config_trigger() {
305         json_add_array
306         _procd_add_array_data "$1"
307         shift
308
309         json_add_array
310         _procd_add_array_data "if"
311
312         json_add_array
313         _procd_add_array_data "eq" "package" "$1"
314         shift
315         json_close_array
316
317         json_add_array
318         _procd_add_array_data "run_script" "$@"
319         json_close_array
320
321         json_close_array
322         _procd_add_timeout
323         json_close_array
324 }
325
326 _procd_add_raw_trigger() {
327         json_add_array
328         _procd_add_array_data "$1"
329         shift
330         local timeout=$1
331         shift
332
333         json_add_array
334         json_add_array
335         _procd_add_array_data "run_script" "$@"
336         json_close_array
337         json_close_array
338
339         json_add_int "" "$timeout"
340
341         json_close_array
342 }
343
344 _procd_add_reload_trigger() {
345         local script=$(readlink "$initscript")
346         local name=$(basename ${script:-$initscript})
347         local file
348
349         _procd_open_trigger
350         for file in "$@"; do
351                 _procd_add_config_trigger "config.change" "$file" /etc/init.d/$name reload
352         done
353         _procd_close_trigger
354 }
355
356 _procd_add_validation() {
357         _procd_open_validate
358         $@
359         _procd_close_validate
360 }
361
362 _procd_append_param() {
363         local type="$1"; shift
364         local _json_no_warning=1
365
366         json_select "$type"
367         [ $? = 0 ] || {
368                 _procd_set_param "$type" "$@"
369                 return
370         }
371         case "$type" in
372                 env|data|limits)
373                         _procd_add_table_data "$@"
374                 ;;
375                 command|netdev|file|respawn|watch)
376                         _procd_add_array_data "$@"
377                 ;;
378                 error)
379                         json_add_string "" "$@"
380                 ;;
381         esac
382         json_select ..
383 }
384
385 _procd_close_instance() {
386         local respawn_vals
387         _json_no_warning=1
388         if json_select respawn ; then
389                 json_get_values respawn_vals
390                 if [ -z "$respawn_vals" ]; then
391                         local respawn_threshold=$(uci_get system.@service[0].respawn_threshold)
392                         local respawn_timeout=$(uci_get system.@service[0].respawn_timeout)
393                         local respawn_retry=$(uci_get system.@service[0].respawn_retry)
394                         _procd_add_array_data ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
395                 fi
396                 json_select ..
397         fi
398
399         json_close_object
400 }
401
402 _procd_add_instance() {
403         _procd_open_instance
404         _procd_set_param command "$@"
405         _procd_close_instance
406 }
407
408 procd_running() {
409         local service="$1"
410         local instance="${2:-instance1}"
411         local running
412
413         json_init
414         json_add_string name "$service"
415         running=$(_procd_ubus_call list | jsonfilter -e "@.$service.instances.${instance}.running")
416
417         [ "$running" = "true" ]
418 }
419
420 _procd_kill() {
421         local service="$1"
422         local instance="$2"
423
424         json_init
425         [ -n "$service" ] && json_add_string name "$service"
426         [ -n "$instance" ] && json_add_string instance "$instance"
427         _procd_ubus_call delete
428 }
429
430 _procd_send_signal() {
431         local service="$1"
432         local instance="$2"
433         local signal="$3"
434
435         case "$signal" in
436                 [A-Z]*) signal="$(kill -l "$signal" 2>/dev/null)" || return 1;;
437         esac
438
439         json_init
440         json_add_string name "$service"
441         [ -n "$instance" -a "$instance" != "*" ] && json_add_string instance "$instance"
442         [ -n "$signal" ] && json_add_int signal "$signal"
443         _procd_ubus_call signal
444 }
445
446 procd_open_data() {
447         local name="$1"
448         json_set_namespace procd __procd_old_cb
449         json_add_object data
450 }
451
452 procd_close_data() {
453         json_close_object
454         json_set_namespace $__procd_old_cb
455 }
456
457 _procd_set_config_changed() {
458         local package="$1"
459
460         json_init
461         json_add_string type config.change
462         json_add_object data
463         json_add_string package "$package"
464         json_close_object
465
466         ubus call service event "$(json_dump)"
467 }
468
469 procd_add_mdns_service() {
470         local service proto port
471         service=$1; shift
472         proto=$1; shift
473         port=$1; shift
474         json_add_object "${service}_$port"
475         json_add_string "service" "_$service._$proto.local"
476         json_add_int port "$port"
477         [ -n "$1" ] && {
478                 json_add_array txt
479                 for txt in "$@"; do json_add_string "" "$txt"; done
480                 json_select ..
481         }
482         json_select ..
483 }
484
485 procd_add_mdns() {
486         procd_open_data
487         json_add_object "mdns"
488         procd_add_mdns_service "$@"
489         json_close_object
490         procd_close_data
491 }
492
493 uci_validate_section()
494 {
495         local _package="$1"
496         local _type="$2"
497         local _name="$3"
498         local _result
499         local _error
500         shift; shift; shift
501         _result=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null`
502         _error=$?
503         eval "$_result"
504         [ "$_error" = "0" ] || `/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null`
505         return $_error
506 }
507
508 uci_load_validate() {
509         local _package="$1"
510         local _type="$2"
511         local _name="$3"
512         local _function="$4"
513         local _option
514         local _result
515         shift; shift; shift; shift
516         for _option in "$@"; do
517                 eval "local ${_option%%:*}"
518         done
519         uci_validate_section "$_package" "$_type" "$_name" "$@"
520         _result=$?
521         [ -n "$_function" ] || return $_result
522         eval "$_function \"\$_name\" \"\$_result\""
523 }
524
525 _procd_wrapper \
526         procd_open_service \
527         procd_close_service \
528         procd_add_instance \
529         procd_add_raw_trigger \
530         procd_add_config_trigger \
531         procd_add_interface_trigger \
532         procd_add_reload_trigger \
533         procd_add_reload_interface_trigger \
534         procd_open_trigger \
535         procd_close_trigger \
536         procd_open_instance \
537         procd_close_instance \
538         procd_open_validate \
539         procd_close_validate \
540         procd_add_jail \
541         procd_add_jail_mount \
542         procd_add_jail_mount_rw \
543         procd_set_param \
544         procd_append_param \
545         procd_add_validation \
546         procd_set_config_changed \
547         procd_kill \
548         procd_send_signal