log.init 2.2 KB

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