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