blob: 0f9405a2220007fc6225e3d3fa45dff5d6c578d3 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001# 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
45PROCD_RELOAD_DELAY=1000
46_PROCD_SERVICE=
47
48procd_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 type service_data >/dev/null 2>&1 && {
105 _procd_open_data
106 service_data
107 _procd_close_data
108 }
109 _procd_ubus_call ${1:-set}
110}
111
112_procd_add_array_data() {
113 while [ "$#" -gt 0 ]; do
114 json_add_string "" "$1"
115 shift
116 done
117}
118
119_procd_add_array() {
120 json_add_array "$1"
121 shift
122 _procd_add_array_data "$@"
123 json_close_array
124}
125
126_procd_add_table_data() {
127 while [ -n "$1" ]; do
128 local var="${1%%=*}"
129 local val="${1#*=}"
130 [ "$1" = "$val" ] && val=
131 json_add_string "$var" "$val"
132 shift
133 done
134}
135
136_procd_add_table() {
137 json_add_object "$1"
138 shift
139 _procd_add_table_data "$@"
140 json_close_object
141}
142
143_procd_open_instance() {
144 local name="$1"; shift
145
146 _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
147 name="${name:-instance$_PROCD_INSTANCE_SEQ}"
148 json_add_object "$name"
149 [ -n "$TRACE_SYSCALLS" ] && json_add_boolean trace "1"
150}
151
152_procd_open_trigger() {
153 let '_procd_trigger_open = _procd_trigger_open + 1'
154 [ "$_procd_trigger_open" -gt 1 ] && return
155 json_add_array "triggers"
156}
157
158_procd_close_trigger() {
159 let '_procd_trigger_open = _procd_trigger_open - 1'
160 [ "$_procd_trigger_open" -lt 1 ] || return
161 json_close_array
162}
163
164_procd_open_data() {
165 let '_procd_data_open = _procd_data_open + 1'
166 [ "$_procd_data_open" -gt 1 ] && return
167 json_add_object "data"
168}
169
170_procd_close_data() {
171 let '_procd_data_open = _procd_data_open - 1'
172 [ "$_procd_data_open" -lt 1 ] || return
173 json_close_object
174}
175
176_procd_open_validate() {
177 json_select ..
178 json_add_array "validate"
179}
180
181_procd_close_validate() {
182 json_close_array
183 json_select triggers
184}
185
186_procd_add_jail() {
187 json_add_object "jail"
188 json_add_string name "$1"
189
190 shift
191
192 for a in $@; do
193 case $a in
194 log) json_add_boolean "log" "1";;
195 ubus) json_add_boolean "ubus" "1";;
196 procfs) json_add_boolean "procfs" "1";;
197 sysfs) json_add_boolean "sysfs" "1";;
198 ronly) json_add_boolean "ronly" "1";;
199 requirejail) json_add_boolean "requirejail" "1";;
200 netns) json_add_boolean "netns" "1";;
201 userns) json_add_boolean "userns" "1";;
202 cgroupsns) json_add_boolean "cgroupsns" "1";;
203 esac
204 done
205 json_add_object "mount"
206 json_close_object
207 json_close_object
208}
209
210_procd_add_jail_mount() {
211 local _json_no_warning=1
212
213 json_select "jail"
214 [ $? = 0 ] || return
215 json_select "mount"
216 [ $? = 0 ] || {
217 json_select ..
218 return
219 }
220 for a in $@; do
221 json_add_string "$a" "0"
222 done
223 json_select ..
224 json_select ..
225}
226
227_procd_add_jail_mount_rw() {
228 local _json_no_warning=1
229
230 json_select "jail"
231 [ $? = 0 ] || return
232 json_select "mount"
233 [ $? = 0 ] || {
234 json_select ..
235 return
236 }
237 for a in $@; do
238 json_add_string "$a" "1"
239 done
240 json_select ..
241 json_select ..
242}
243
244_procd_set_param() {
245 local type="$1"; shift
246
247 case "$type" in
248 env|data|limits)
249 _procd_add_table "$type" "$@"
250 ;;
251 command|netdev|file|respawn|watch|watchdog)
252 _procd_add_array "$type" "$@"
253 ;;
254 error)
255 json_add_array "$type"
256 json_add_string "" "$@"
257 json_close_array
258 ;;
259 nice|term_timeout)
260 json_add_int "$type" "$1"
261 ;;
262 reload_signal)
263 json_add_int "$type" $(kill -l "$1")
264 ;;
265 pidfile|user|group|seccomp|capabilities|facility|\
266 extroot|overlaydir|tmpoverlaysize)
267 json_add_string "$type" "$1"
268 ;;
269 stdout|stderr|no_new_privs)
270 json_add_boolean "$type" "$1"
271 ;;
272 esac
273}
274
275_procd_add_timeout() {
276 [ "$PROCD_RELOAD_DELAY" -gt 0 ] && json_add_int "" "$PROCD_RELOAD_DELAY"
277 return 0
278}
279
280_procd_add_interface_trigger() {
281 json_add_array
282 _procd_add_array_data "$1"
283 shift
284
285 json_add_array
286 _procd_add_array_data "if"
287
288 json_add_array
289 _procd_add_array_data "eq" "interface" "$1"
290 shift
291 json_close_array
292
293 json_add_array
294 _procd_add_array_data "run_script" "$@"
295 json_close_array
296
297 json_close_array
298 _procd_add_timeout
299 json_close_array
300}
301
302_procd_add_reload_interface_trigger() {
303 local script=$(readlink "$initscript")
304 local name=$(basename ${script:-$initscript})
305
306 _procd_open_trigger
307 _procd_add_interface_trigger "interface.*" $1 /etc/init.d/$name reload
308 _procd_close_trigger
309}
310
311_procd_add_data_trigger() {
312 json_add_array
313 _procd_add_array_data "service.data.update"
314
315 json_add_array
316 _procd_add_array_data "if"
317
318 json_add_array
319 _procd_add_array_data "eq" "name" "$1"
320 shift
321 json_close_array
322
323 json_add_array
324 _procd_add_array_data "run_script" "$@"
325 json_close_array
326
327 json_close_array
328 _procd_add_timeout
329 json_close_array
330}
331
332_procd_add_reload_data_trigger() {
333 local script=$(readlink "$initscript")
334 local name=$(basename ${script:-$initscript})
335
336 _procd_open_trigger
337 _procd_add_data_trigger $1 /etc/init.d/$name reload
338 _procd_close_trigger
339}
340
341_procd_add_config_trigger() {
342 json_add_array
343 _procd_add_array_data "$1"
344 shift
345
346 json_add_array
347 _procd_add_array_data "if"
348
349 json_add_array
350 _procd_add_array_data "eq" "package" "$1"
351 shift
352 json_close_array
353
354 json_add_array
355 _procd_add_array_data "run_script" "$@"
356 json_close_array
357
358 json_close_array
359 _procd_add_timeout
360 json_close_array
361}
362
363_procd_add_mount_trigger() {
364 json_add_array
365 _procd_add_array_data "$1"
366 local action="$2"
367 local multi=0
368 shift ; shift
369
370 json_add_array
371 _procd_add_array_data "if"
372
373 if [ "$2" ]; then
374 json_add_array
375 _procd_add_array_data "or"
376 multi=1
377 fi
378
379 while [ "$1" ]; do
380 json_add_array
381 _procd_add_array_data "eq" "target" "$1"
382 shift
383 json_close_array
384 done
385
386 [ $multi = 1 ] && json_close_array
387
388 json_add_array
389 _procd_add_array_data "run_script" /etc/init.d/$name $action
390 json_close_array
391
392 json_close_array
393 _procd_add_timeout
394 json_close_array
395}
396
397_procd_add_action_mount_trigger() {
398 local action="$1"
399 shift
400 local mountpoints="$(procd_get_mountpoints "$@")"
401 [ "${mountpoints//[[:space:]]}" ] || return 0
402 local script=$(readlink "$initscript")
403 local name=$(basename ${script:-$initscript})
404
405 _procd_open_trigger
406 _procd_add_mount_trigger mount.add $action "$mountpoints"
407 _procd_close_trigger
408}
409
410procd_get_mountpoints() {
411 (
412 __procd_check_mount() {
413 local cfg="$1"
414 local path="${2%%/}/"
415 local target
416 config_get target "$cfg" target
417 target="${target%%/}/"
418 [ "$path" != "${path##$target}" ] && echo "${target%%/}"
419 }
420 local mpath
421 config_load fstab
422 for mpath in "$@"; do
423 config_foreach __procd_check_mount mount "$mpath"
424 done
425 ) | sort -u
426}
427
428_procd_add_restart_mount_trigger() {
429 _procd_add_action_mount_trigger restart "$@"
430}
431
432_procd_add_reload_mount_trigger() {
433 _procd_add_action_mount_trigger reload "$@"
434}
435
436_procd_add_raw_trigger() {
437 json_add_array
438 _procd_add_array_data "$1"
439 shift
440 local timeout=$1
441 shift
442
443 json_add_array
444 json_add_array
445 _procd_add_array_data "run_script" "$@"
446 json_close_array
447 json_close_array
448
449 json_add_int "" "$timeout"
450
451 json_close_array
452}
453
454_procd_add_reload_trigger() {
455 local script=$(readlink "$initscript")
456 local name=$(basename ${script:-$initscript})
457 local file
458
459 _procd_open_trigger
460 for file in "$@"; do
461 _procd_add_config_trigger "config.change" "$file" /etc/init.d/$name reload
462 done
463 _procd_close_trigger
464}
465
466_procd_add_validation() {
467 _procd_open_validate
468 $@
469 _procd_close_validate
470}
471
472_procd_append_param() {
473 local type="$1"; shift
474 local _json_no_warning=1
475
476 json_select "$type"
477 [ $? = 0 ] || {
478 _procd_set_param "$type" "$@"
479 return
480 }
481 case "$type" in
482 env|data|limits)
483 _procd_add_table_data "$@"
484 ;;
485 command|netdev|file|respawn|watch|watchdog)
486 _procd_add_array_data "$@"
487 ;;
488 error)
489 json_add_string "" "$@"
490 ;;
491 esac
492 json_select ..
493}
494
495_procd_close_instance() {
496 local respawn_vals
497 _json_no_warning=1
498 if json_select respawn ; then
499 json_get_values respawn_vals
500 if [ -z "$respawn_vals" ]; then
501 local respawn_threshold=$(uci_get system.@service[0].respawn_threshold)
502 local respawn_timeout=$(uci_get system.@service[0].respawn_timeout)
503 local respawn_retry=$(uci_get system.@service[0].respawn_retry)
504 _procd_add_array_data ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
505 fi
506 json_select ..
507 fi
508
509 json_close_object
510}
511
512_procd_add_instance() {
513 _procd_open_instance
514 _procd_set_param command "$@"
515 _procd_close_instance
516}
517
518procd_running() {
519 local service="$1"
520 local instance="${2:-*}"
521 [ "$instance" = "*" ] || instance="'$instance'"
522
523 json_init
524 json_add_string name "$service"
525 local running=$(_procd_ubus_call list | jsonfilter -l 1 -e "@['$service'].instances[$instance].running")
526
527 [ "$running" = "true" ]
528}
529
530_procd_kill() {
531 local service="$1"
532 local instance="$2"
533
534 json_init
535 [ -n "$service" ] && json_add_string name "$service"
536 [ -n "$instance" ] && json_add_string instance "$instance"
537 _procd_ubus_call delete
538}
539
540_procd_send_signal() {
541 local service="$1"
542 local instance="$2"
543 local signal="$3"
544
545 case "$signal" in
546 [A-Z]*) signal="$(kill -l "$signal" 2>/dev/null)" || return 1;;
547 esac
548
549 json_init
550 json_add_string name "$service"
551 [ -n "$instance" -a "$instance" != "*" ] && json_add_string instance "$instance"
552 [ -n "$signal" ] && json_add_int signal "$signal"
553 _procd_ubus_call signal
554}
555
556_procd_status() {
557 local service="$1"
558 local instance="$2"
559 local data state
560 local n_running=0
561 local n_stopped=0
562 local n_total=0
563
564 json_init
565 [ -n "$service" ] && json_add_string name "$service"
566
567 data=$(_procd_ubus_call list | jsonfilter -e '@["'"$service"'"]')
568 [ -z "$data" ] && { echo "inactive"; return 3; }
569
570 data=$(echo "$data" | jsonfilter -e '$.instances')
571 if [ -z "$data" ]; then
572 [ -z "$instance" ] && { echo "active with no instances"; return 0; }
573 data="[]"
574 fi
575
576 [ -n "$instance" ] && instance="\"$instance\"" || instance='*'
577
578 for state in $(jsonfilter -s "$data" -e '$['"$instance"'].running'); do
579 n_total=$((n_total + 1))
580 case "$state" in
581 false) n_stopped=$((n_stopped + 1)) ;;
582 true) n_running=$((n_running + 1)) ;;
583 esac
584 done
585
586 if [ $n_total -gt 0 ]; then
587 if [ $n_running -gt 0 ] && [ $n_stopped -eq 0 ]; then
588 echo "running"
589 return 0
590 elif [ $n_running -gt 0 ]; then
591 echo "running ($n_running/$n_total)"
592 return 0
593 else
594 echo "not running"
595 return 5
596 fi
597 else
598 echo "unknown instance $instance"
599 return 4
600 fi
601}
602
603procd_open_data() {
604 local name="$1"
605 json_set_namespace procd __procd_old_cb
606 json_add_object data
607}
608
609procd_close_data() {
610 json_close_object
611 json_set_namespace $__procd_old_cb
612}
613
614_procd_set_config_changed() {
615 local package="$1"
616
617 json_init
618 json_add_string type config.change
619 json_add_object data
620 json_add_string package "$package"
621 json_close_object
622
623 ubus call service event "$(json_dump)"
624}
625
626procd_add_mdns_service() {
627 local service proto port txt_count=0
628 service=$1; shift
629 proto=$1; shift
630 port=$1; shift
631 json_add_object "${service}_$port"
632 json_add_string "service" "_$service._$proto.local"
633 json_add_int port "$port"
634 for txt in "$@"; do
635 [ -z "$txt" ] && continue
636 txt_count=$((txt_count+1))
637 [ $txt_count -eq 1 ] && json_add_array txt
638 json_add_string "" "$txt"
639 done
640 [ $txt_count -gt 0 ] && json_select ..
641
642 json_select ..
643}
644
645procd_add_mdns() {
646 procd_open_data
647 json_add_object "mdns"
648 procd_add_mdns_service "$@"
649 json_close_object
650 procd_close_data
651}
652
653uci_validate_section()
654{
655 local _package="$1"
656 local _type="$2"
657 local _name="$3"
658 local _result
659 local _error
660 shift; shift; shift
661 _result=$(/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null)
662 _error=$?
663 eval "$_result"
664 [ "$_error" = "0" ] || $(/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null)
665 return $_error
666}
667
668uci_load_validate() {
669 local _package="$1"
670 local _type="$2"
671 local _name="$3"
672 local _function="$4"
673 local _option
674 local _result
675 shift; shift; shift; shift
676 for _option in "$@"; do
677 eval "local ${_option%%:*}"
678 done
679 uci_validate_section "$_package" "$_type" "$_name" "$@"
680 _result=$?
681 [ -n "$_function" ] || return $_result
682 eval "$_function \"\$_name\" \"\$_result\""
683}
684
685_procd_wrapper \
686 procd_open_service \
687 procd_close_service \
688 procd_add_instance \
689 procd_add_raw_trigger \
690 procd_add_config_trigger \
691 procd_add_interface_trigger \
692 procd_add_mount_trigger \
693 procd_add_reload_trigger \
694 procd_add_reload_data_trigger \
695 procd_add_reload_interface_trigger \
696 procd_add_action_mount_trigger \
697 procd_add_reload_mount_trigger \
698 procd_add_restart_mount_trigger \
699 procd_open_trigger \
700 procd_close_trigger \
701 procd_open_instance \
702 procd_close_instance \
703 procd_open_validate \
704 procd_close_validate \
705 procd_add_jail \
706 procd_add_jail_mount \
707 procd_add_jail_mount_rw \
708 procd_set_param \
709 procd_append_param \
710 procd_add_validation \
711 procd_set_config_changed \
712 procd_kill \
713 procd_send_signal