log.init 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. procd_open_instance
  51. procd_set_param command "$PROG" -f -F "$log_file" -p "$pid_file"
  52. [ -n "${log_size}" ] && procd_append_param command -S "$log_size"
  53. procd_close_instance
  54. }
  55. start_service_remote()
  56. {
  57. PIDCOUNT="$(( ${PIDCOUNT} + 1))"
  58. local pid_file="/var/run/logread.${PIDCOUNT}.pid"
  59. local log_ip log_port log_proto log_prefix log_remote log_trailer_null log_hostname
  60. validate_log_section "${1}" || {
  61. echo "validation failed"
  62. return 1
  63. }
  64. [ "${log_remote}" -ne 0 ] || return
  65. [ -z "${log_ip}" ] && return
  66. [ -z "${log_hostname}" ] && log_hostname=$(cat /proc/sys/kernel/hostname)
  67. procd_open_instance
  68. procd_set_param command "$PROG" -f -h "$log_hostname" -r "$log_ip" "${log_port}" -p "$pid_file"
  69. case "${log_proto}" in
  70. "udp") procd_append_param command -u;;
  71. "tcp") [ "${log_trailer_null}" -eq 1 ] && procd_append_param command -0;;
  72. esac
  73. [ -z "${log_prefix}" ] || procd_append_param command -P "${log_prefix}"
  74. procd_close_instance
  75. }
  76. service_triggers()
  77. {
  78. procd_add_reload_trigger "system"
  79. procd_add_validation validate_log_section
  80. }
  81. start_service()
  82. {
  83. config_load system
  84. config_foreach start_service_daemon system
  85. config_foreach start_service_file system
  86. config_foreach start_service_remote system
  87. }