softflowd.init 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: softflowd
  4. # Required-Start: $network $remote_fs $syslog
  5. # Required-Stop: $network $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Flow-based network traffic analyser
  9. # Description: Manage all softflowd exports defined in
  10. # /etc/softflowd/*.conf
  11. ### END INIT INFO
  12. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  13. name=softflowd
  14. description='Flow-based network traffic analyser'
  15. softflowd="/usr/sbin/$name"
  16. pid_dir="/var/run/$name/"
  17. privdrop_chroot_dir='/var/run/softflowd/chroot'
  18. [ -x "$softflowd" ] || exit 0
  19. if [ "$1" = 'start' ]; then
  20. mkdir -p "$privdrop_chroot_dir"
  21. if [ -e /var/run/softflowd.ctl ]; then
  22. [ -L /var/run/softflowd.ctl ] || rm /var/run/softflowd.ctl
  23. fi
  24. [ -e /var/run/softflowd.ctl ] || ln -s /var/run/softflowd/default.ctl /var/run/softflowd.ctl
  25. install -D -p /etc/protocols "$privdrop_chroot_dir/etc/protocols"
  26. fi
  27. # Load the VERBOSE setting and other rcS variables
  28. . /lib/init/vars.sh
  29. # Define LSB functions
  30. . /lib/lsb/init-functions
  31. # Start a softflowd instance
  32. #
  33. # Return
  34. # 0 if daemon has been started
  35. # 1 if daemon was already running
  36. # 2 if daemon could not be started
  37. do_start() {
  38. local instance config
  39. instance="$1"
  40. config="$2"
  41. sh -n "$config" 2>/dev/null || return 2
  42. interface=
  43. options=
  44. . "$config"
  45. [ "$interface" ] || return 2
  46. local pid_file ctl_sock
  47. pid_file="$pid_dir$instance.pid"
  48. ctl_sock="$pid_dir$instance.ctl"
  49. start-stop-daemon --start --quiet \
  50. --pid_file "$pid_file" --exec "$softflowd" --test >/dev/null ||
  51. return 1
  52. # shellcheck disable=SC2086 # We have to trust the config file
  53. start-stop-daemon --start --quiet \
  54. --pid_file "$pid_file" \
  55. --exec "$softflowd" -- \
  56. -p "$pid_file" -c "$ctl_sock" \
  57. -i "$interface" $options ||
  58. return 2
  59. sleep 1
  60. start-stop-daemon --start --quiet \
  61. --pid_file "$pid_file" --exec "$softflowd" --test >/dev/null &&
  62. return 2
  63. }
  64. # Stop a softflowd instance
  65. #
  66. # Return
  67. # 0 if daemon has been stopped
  68. # 1 if daemon was already stopped
  69. # 2 if daemon could not be stopped
  70. # other if a failure occurred
  71. do_stop() {
  72. local instance pid_file retval
  73. instance="$1"
  74. pid_file="$pid_dir/$instance.pid"
  75. start-stop-daemon --stop --quiet \
  76. --retry=TERM/30/KILL/5 --pid_file "$pid_file" --name "$name"
  77. retval="$?"
  78. [ "$retval" = 2 ] && return 2
  79. # Many daemons don't delete their pidfiles when they exit.
  80. rm -f "$pid_file"
  81. return "$retval"
  82. }
  83. exit=0
  84. do_action() {
  85. local config
  86. config="$1"
  87. instance="$(basename "${config%%.conf}")"
  88. case "$action" in
  89. start)
  90. [ "$VERBOSE" != no ] && log_daemon_msg "Starting $description" "$instance"
  91. do_start "$instance" "$config"
  92. case "$?" in
  93. 0 | 1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  94. 2)
  95. [ "$VERBOSE" != no ] && log_end_msg 1
  96. exit=1
  97. ;;
  98. esac
  99. ;;
  100. stop)
  101. [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $description" "$instance"
  102. do_stop "$instance"
  103. case "$?" in
  104. 0 | 1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  105. 2)
  106. [ "$VERBOSE" != no ] && log_end_msg 1
  107. exit=1
  108. ;;
  109. esac
  110. ;;
  111. status)
  112. status_of_proc -p "$pid_dir/$instance.pid" "$softflowd" "softflowd instance $instance" || exit=$?
  113. ;;
  114. restart | force-reload)
  115. log_daemon_msg "Restarting $description" "$instance"
  116. do_stop "$instance"
  117. case "$?" in
  118. 0 | 1)
  119. do_start "$instance" "$config"
  120. case "$?" in
  121. 0) log_end_msg 0 ;;
  122. *)
  123. # Old process is still running or
  124. # failed to start
  125. log_end_msg 1
  126. ;;
  127. esac
  128. ;;
  129. *)
  130. # Failed to stop
  131. log_end_msg 1
  132. ;;
  133. esac
  134. ;;
  135. *)
  136. echo "Usage: /etc/init.d/softflowd {start|stop|status|restart|force-reload} [<instance> ...]" >&2
  137. exit 3
  138. ;;
  139. esac
  140. }
  141. action="$1"
  142. shift
  143. if [ "$1" ]; then
  144. while [ "$1" ]; do
  145. config="/etc/softflowd/$1.conf"
  146. if [ -f "$config" ]; then
  147. do_action "$config"
  148. fi
  149. shift
  150. done
  151. else
  152. for config in /etc/softflowd/*.conf; do
  153. if [ -f "$config" ]; then
  154. do_action "$config"
  155. fi
  156. done
  157. fi
  158. exit $exit