ubox: make logging code honour the hostname properly
[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_validate_section system system "${1}" \
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_validate_section system system "${1}" \
29                 'log_size:uinteger:0' \
30                 'log_buffer_size:uinteger:0'
31 }
32
33 start_service_daemon()
34 {
35         local log_buffer_size log_size
36         validate_log_daemon "${1}"
37         [ $log_buffer_size -eq 0 -a $log_size -gt 0 ] && log_buffer_size=$log_size
38         [ $log_buffer_size -eq 0 ] && log_buffer_size=16
39         procd_open_instance
40         procd_set_param command "/sbin/logd"
41         procd_append_param command -S "${log_buffer_size}"
42         procd_set_param respawn
43         procd_close_instance
44 }
45
46 start_service_file()
47 {
48         PIDCOUNT="$(( ${PIDCOUNT} + 1))"
49         local pid_file="/var/run/logread.${PIDCOUNT}.pid"
50         local log_file log_size
51
52         validate_log_section "${1}" || {
53                 echo "validation failed"
54                 return 1
55         }
56         [ -z "${log_file}" ] && return
57
58         procd_open_instance
59         procd_set_param command "$PROG" -f -F "$log_file" -p "$pid_file"
60         [ -n "${log_size}" ] && procd_append_param command -S "$log_size"
61         procd_close_instance
62 }
63
64 start_service_remote()
65 {
66         PIDCOUNT="$(( ${PIDCOUNT} + 1))"
67         local pid_file="/var/run/logread.${PIDCOUNT}.pid"
68         local log_ip log_port log_proto log_prefix log_remote log_trailer_null log_hostname
69
70         validate_log_section "${1}" || {
71                 echo "validation failed"
72                 return 1
73         }
74         [ "${log_remote}" -ne 0 ] || return
75         [ -z "${log_ip}" ] && return
76         [ -z "${log_hostname}" ] && log_hostname=$(cat /proc/sys/kernel/hostname)
77
78         procd_open_instance
79         procd_set_param command "$PROG" -f -h "$log_hostname" -r "$log_ip" "${log_port}" -p "$pid_file"
80         case "${log_proto}" in
81                 "udp") procd_append_param command -u;;
82                 "tcp") [ "${log_trailer_null}" -eq 1 ] && procd_append_param command -0;;
83         esac
84         [ -z "${log_prefix}" ] || procd_append_param command -P "${log_prefix}"
85         procd_close_instance
86 }
87
88 service_triggers()
89 {
90         procd_add_reload_trigger "system"
91         procd_add_validation validate_log_section
92 }
93
94 start_service()
95 {
96         config_load system
97         config_foreach start_service_daemon system
98         config_foreach start_service_file system
99         config_foreach start_service_remote system
100 }