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