log.init 2.3 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_load_validate system system "$1" "$2" \
  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_load_validate system system "$1" "$2" \
  25. 'log_size:uinteger:0' \
  26. 'log_buffer_size:uinteger:0'
  27. }
  28. start_service_daemon()
  29. {
  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=64
  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 5 1 -1
  36. procd_close_instance
  37. }
  38. start_service_file()
  39. {
  40. PIDCOUNT="$(( ${PIDCOUNT} + 1))"
  41. local pid_file="/var/run/logread.${PIDCOUNT}.pid"
  42. [ "$2" = 0 ] || {
  43. echo "validation failed"
  44. return 1
  45. }
  46. [ -z "${log_file}" ] && return
  47. mkdir -p "$(dirname "${log_file}")"
  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. [ "$2" = 0 ] || {
  58. echo "validation failed"
  59. return 1
  60. }
  61. [ "${log_remote}" -ne 0 ] || return
  62. [ -z "${log_ip}" ] && return
  63. [ -z "${log_hostname}" ] && log_hostname=$(cat /proc/sys/kernel/hostname)
  64. procd_open_instance
  65. procd_set_param command "$PROG" -f -h "$log_hostname" -r "$log_ip" "${log_port}" -p "$pid_file"
  66. case "${log_proto}" in
  67. "udp") procd_append_param command -u;;
  68. "tcp") [ "${log_trailer_null}" -eq 1 ] && procd_append_param command -0;;
  69. esac
  70. [ -z "${log_prefix}" ] || procd_append_param command -P "${log_prefix}"
  71. procd_close_instance
  72. }
  73. service_triggers()
  74. {
  75. procd_add_reload_trigger "system"
  76. procd_add_validation validate_log_section
  77. }
  78. start_service()
  79. {
  80. config_load system
  81. config_foreach validate_log_daemon system start_service_daemon
  82. config_foreach validate_log_section system start_service_file
  83. config_foreach validate_log_section system start_service_remote
  84. }
  85. service_running() {
  86. procd_running log
  87. }