log.init 2.4 KB

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