1
0

softflowd.init 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. DESC='Flow-based network traffic analyser'
  15. SOFTFLOWD="/usr/sbin/$NAME"
  16. PIDDIR="/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="$1"
  39. local CONFIG="$2"
  40. sh -n "$CONFIG" 2>/dev/null || return 2
  41. interface=
  42. options=
  43. . "$CONFIG"
  44. [ "$interface" ] || return 2
  45. local PIDFILE="$PIDDIR$INSTANCE.pid"
  46. local CTLSOCK="$PIDDIR$INSTANCE.ctl"
  47. start-stop-daemon --start --quiet \
  48. --pidfile "$PIDFILE" --exec "$SOFTFLOWD" --test > /dev/null \
  49. || return 1
  50. start-stop-daemon --start --quiet \
  51. --pidfile "$PIDFILE" \
  52. --exec "$SOFTFLOWD" -- \
  53. -p "$PIDFILE" -c "$CTLSOCK" \
  54. -i "$interface" $options \
  55. || return 2
  56. sleep 1
  57. start-stop-daemon --start --quiet \
  58. --pidfile "$PIDFILE" --exec "$SOFTFLOWD" --test > /dev/null \
  59. && return 2
  60. }
  61. # Stop a softflowd instance
  62. #
  63. # Return
  64. # 0 if daemon has been stopped
  65. # 1 if daemon was already stopped
  66. # 2 if daemon could not be stopped
  67. # other if a failure occurred
  68. do_stop () {
  69. local INSTANCE="$1"
  70. local PIDFILE="$PIDDIR/$INSTANCE.pid"
  71. start-stop-daemon --stop --quiet \
  72. --retry=TERM/30/KILL/5 --pidfile "$PIDFILE" --name "$NAME"
  73. RETVAL="$?"
  74. [ "$RETVAL" = 2 ] && return 2
  75. # Many daemons don't delete their pidfiles when they exit.
  76. rm -f "$PIDFILE"
  77. return "$RETVAL"
  78. }
  79. EXIT=0
  80. do_action () {
  81. local CONFIG="$1"
  82. INSTANCE="$(basename "${CONFIG%%.conf}")"
  83. case "$ACTION" in
  84. start)
  85. [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$INSTANCE"
  86. do_start "$INSTANCE" "$CONFIG"
  87. case "$?" in
  88. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  89. 2)
  90. [ "$VERBOSE" != no ] && log_end_msg 1
  91. EXIT=1
  92. ;;
  93. esac
  94. ;;
  95. stop)
  96. [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$INSTANCE"
  97. do_stop "$INSTANCE"
  98. case "$?" in
  99. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  100. 2)
  101. [ "$VERBOSE" != no ] && log_end_msg 1
  102. EXIT=1
  103. ;;
  104. esac
  105. ;;
  106. status)
  107. status_of_proc -p "$PIDDIR/$INSTANCE.pid" "$SOFTFLOWD" "softflowd instance $INSTANCE" || EXIT=$?
  108. ;;
  109. restart|force-reload)
  110. log_daemon_msg "Restarting $DESC" "$INSTANCE"
  111. do_stop "$INSTANCE"
  112. case "$?" in
  113. 0|1)
  114. do_start "$INSTANCE" "$CONFIG"
  115. case "$?" in
  116. 0) log_end_msg 0 ;;
  117. *)
  118. # Old process is still running or
  119. # failed to start
  120. log_end_msg 1 ;;
  121. esac
  122. ;;
  123. *)
  124. # Failed to stop
  125. log_end_msg 1
  126. ;;
  127. esac
  128. ;;
  129. *)
  130. echo "Usage: /etc/init.d/softflowd {start|stop|status|restart|force-reload} [<instance> ...]" >&2
  131. exit 3
  132. ;;
  133. esac
  134. }
  135. ACTION="$1"
  136. shift
  137. if [ "$1" ] ; then
  138. while [ "$1" ] ; do
  139. CONFIG="/etc/softflowd/$1.conf"
  140. if [ -f "$CONFIG" ] ; then
  141. do_action "$CONFIG"
  142. fi
  143. shift
  144. done
  145. else
  146. for CONFIG in /etc/softflowd/*.conf ; do
  147. if [ -f "$CONFIG" ] ; then
  148. do_action "$CONFIG"
  149. fi
  150. done
  151. fi
  152. exit $EXIT