log.init 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_ip:ipaddr' \
  15. 'log_remote:bool:1' \
  16. 'log_port:port:514' \
  17. 'log_proto:or("tcp", "udp"):udp' \
  18. 'log_prefix:string'
  19. }
  20. validate_log_daemon()
  21. {
  22. uci_validate_section system system "${1}" \
  23. 'log_size:uinteger:0' \
  24. 'log_buffer_size:uinteger:0'
  25. }
  26. start_service_daemon()
  27. {
  28. local log_buffer_size log_size
  29. validate_log_daemon "${1}"
  30. [ $log_buffer_size -eq 0 -a $log_size -gt 0 ] && log_buffer_size=$log_size
  31. [ $log_buffer_size -eq 0 ] && log_buffer_size=16
  32. procd_open_instance
  33. procd_set_param command "/sbin/logd"
  34. procd_append_param command -S "${log_buffer_size}"
  35. procd_set_param respawn
  36. procd_close_instance
  37. }
  38. start_service_file()
  39. {
  40. PIDCOUNT="$(( ${PIDCOUNT} + 1))"
  41. local pid_file="/var/run/logread.${PIDCOUNT}.pid"
  42. local log_file log_size
  43. validate_log_section "${1}" || {
  44. echo "validation failed"
  45. return 1
  46. }
  47. [ -z "${log_file}" ] && return
  48. procd_open_instance
  49. procd_set_param command "$PROG" -f -F "$log_file" -p "$pid_file"
  50. [ -n "${log_size}" ] && procd_append_param command -S "$log_size"
  51. procd_close_instance
  52. }
  53. start_service_remote()
  54. {
  55. PIDCOUNT="$(( ${PIDCOUNT} + 1))"
  56. local pid_file="/var/run/logread.${PIDCOUNT}.pid"
  57. local log_ip log_port log_proto log_prefix log_remote
  58. validate_log_section "${1}" || {
  59. echo "validation failed"
  60. return 1
  61. }
  62. [ "${log_remote}" -ne 0 ] || return
  63. [ -z "${log_ip}" ] && return
  64. procd_open_instance
  65. procd_set_param command "$PROG" -f -r "$log_ip" "${log_port}" -p "$pid_file"
  66. [ "${log_proto}" != "udp" ] || procd_append_param command -u
  67. [ -z "${log_prefix}" ] || procd_append_param command -P "${log_prefix}"
  68. procd_close_instance
  69. }
  70. service_triggers()
  71. {
  72. procd_add_reload_trigger "system"
  73. procd_add_validation validate_log_section
  74. }
  75. start_service()
  76. {
  77. config_load system
  78. config_foreach start_service_daemon system
  79. config_foreach start_service_file system
  80. config_foreach start_service_remote system
  81. }