procd: Add wrapper for uci_validate_section()
[openwrt/openwrt.git] / package / system / ubox / files / log.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2013 OpenWrt.org
3
4 # start after and stop before networking
5 START=12
6 STOP=89
7 PIDCOUNT=0
8
9 USE_PROCD=1
10 PROG=/sbin/logread
11
12 validate_log_section()
13 {
14 uci_load_validate system system "$1" "$2" \
15 'log_file:string' \
16 'log_size:uinteger' \
17 'log_hostname:string' \
18 'log_ip:ipaddr' \
19 'log_remote:bool:1' \
20 'log_port:port:514' \
21 'log_proto:or("tcp", "udp"):udp' \
22 'log_trailer_null:bool:0' \
23 'log_prefix:string'
24 }
25
26 validate_log_daemon()
27 {
28 uci_load_validate system system "$1" "$2" \
29 'log_size:uinteger:0' \
30 'log_buffer_size:uinteger:0'
31 }
32
33 start_service_daemon()
34 {
35 [ $log_buffer_size -eq 0 -a $log_size -gt 0 ] && log_buffer_size=$log_size
36 [ $log_buffer_size -eq 0 ] && log_buffer_size=64
37 procd_open_instance
38 procd_set_param command "/sbin/logd"
39 procd_append_param command -S "${log_buffer_size}"
40 procd_set_param respawn 5 1 -1
41 procd_close_instance
42 }
43
44 start_service_file()
45 {
46 PIDCOUNT="$(( ${PIDCOUNT} + 1))"
47 local pid_file="/var/run/logread.${PIDCOUNT}.pid"
48
49 [ "$2" = 0 ] || {
50 echo "validation failed"
51 return 1
52 }
53 [ -z "${log_file}" ] && return
54
55 mkdir -p "$(dirname "${log_file}")"
56
57 procd_open_instance
58 procd_set_param command "$PROG" -f -F "$log_file" -p "$pid_file"
59 [ -n "${log_size}" ] && procd_append_param command -S "$log_size"
60 procd_close_instance
61 }
62
63 start_service_remote()
64 {
65 PIDCOUNT="$(( ${PIDCOUNT} + 1))"
66 local pid_file="/var/run/logread.${PIDCOUNT}.pid"
67
68 [ "$2" = 0 ] || {
69 echo "validation failed"
70 return 1
71 }
72 [ "${log_remote}" -ne 0 ] || return
73 [ -z "${log_ip}" ] && return
74 [ -z "${log_hostname}" ] && log_hostname=$(cat /proc/sys/kernel/hostname)
75
76 procd_open_instance
77 procd_set_param command "$PROG" -f -h "$log_hostname" -r "$log_ip" "${log_port}" -p "$pid_file"
78 case "${log_proto}" in
79 "udp") procd_append_param command -u;;
80 "tcp") [ "${log_trailer_null}" -eq 1 ] && procd_append_param command -0;;
81 esac
82 [ -z "${log_prefix}" ] || procd_append_param command -P "${log_prefix}"
83 procd_close_instance
84 }
85
86 service_triggers()
87 {
88 procd_add_reload_trigger "system"
89 procd_add_validation validate_log_section
90 }
91
92 start_service()
93 {
94 config_load system
95 config_foreach validate_log_daemon system start_service_daemon
96 config_foreach validate_log_section system start_service_file
97 config_foreach validate_log_section system start_service_remote
98 }