File: softflowd.init

package info (click to toggle)
softflowd 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 612 kB
  • sloc: ansic: 5,624; sh: 197; perl: 196; makefile: 26
file content (179 lines) | stat: -rw-r--r-- 4,901 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/sh

### BEGIN INIT INFO
# Provides:          softflowd
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Flow-based network traffic analyser
# Description:       Manage all softflowd exports defined in
#                    /etc/softflowd/*.conf
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
name=softflowd
description='Flow-based network traffic analyser'
softflowd="/usr/sbin/$name"
pid_dir="/var/run/$name/"
privdrop_chroot_dir='/var/run/softflowd/chroot'

[ -x "$softflowd" ] || exit 0

if [ "$1" = 'start' ]; then
    mkdir -p "$privdrop_chroot_dir"
    if [ -e /var/run/softflowd.ctl ]; then
        [ -L /var/run/softflowd.ctl ] || rm /var/run/softflowd.ctl
    fi
    [ -e /var/run/softflowd.ctl ] || ln -s /var/run/softflowd/default.ctl /var/run/softflowd.ctl
    install -D -p /etc/protocols "$privdrop_chroot_dir/etc/protocols"
fi

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB functions
. /lib/lsb/init-functions

# Start a softflowd instance
#
# Return
#   0 if daemon has been started
#   1 if daemon was already running
#   2 if daemon could not be started
do_start() {
    local instance config
    instance="$1"
    config="$2"

    sh -n "$config" 2>/dev/null || return 2

    interface=
    options=

    . "$config"

    [ "$interface" ] || return 2

    local pid_file ctl_sock
    pid_file="$pid_dir$instance.pid"
    ctl_sock="$pid_dir$instance.ctl"
    start-stop-daemon --start --quiet \
        --pid_file "$pid_file" --exec "$softflowd" --test >/dev/null ||
        return 1
    # shellcheck disable=SC2086 # We have to trust the config file
    start-stop-daemon --start --quiet \
        --pid_file "$pid_file" \
        --exec "$softflowd" -- \
        -p "$pid_file" -c "$ctl_sock" \
        -i "$interface" $options ||
        return 2
    sleep 1
    start-stop-daemon --start --quiet \
        --pid_file "$pid_file" --exec "$softflowd" --test >/dev/null &&
        return 2
}

# Stop a softflowd instance
#
# Return
#   0 if daemon has been stopped
#   1 if daemon was already stopped
#   2 if daemon could not be stopped
#   other if a failure occurred
do_stop() {
    local instance pid_file retval
    instance="$1"
    pid_file="$pid_dir/$instance.pid"

    start-stop-daemon --stop --quiet \
        --retry=TERM/30/KILL/5 --pid_file "$pid_file" --name "$name"
    retval="$?"
    [ "$retval" = 2 ] && return 2
    # Many daemons don't delete their pidfiles when they exit.
    rm -f "$pid_file"
    return "$retval"
}

exit=0

do_action() {
    local config
    config="$1"

    instance="$(basename "${config%%.conf}")"

    case "$action" in
        start)
            [ "$VERBOSE" != no ] && log_daemon_msg "Starting $description" "$instance"
            do_start "$instance" "$config"
            case "$?" in
                0 | 1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2)
                    [ "$VERBOSE" != no ] && log_end_msg 1
                    exit=1
                    ;;
            esac
            ;;
        stop)
            [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $description" "$instance"
            do_stop "$instance"
            case "$?" in
                0 | 1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2)
                    [ "$VERBOSE" != no ] && log_end_msg 1
                    exit=1
                    ;;
            esac
            ;;
        status)
            status_of_proc -p "$pid_dir/$instance.pid" "$softflowd" "softflowd instance $instance" || exit=$?
            ;;
        restart | force-reload)
            log_daemon_msg "Restarting $description" "$instance"
            do_stop "$instance"
            case "$?" in
                0 | 1)
                    do_start "$instance" "$config"
                    case "$?" in
                        0) log_end_msg 0 ;;
                        *)
                            # Old process is still running or
                            # failed to start
                            log_end_msg 1
                            ;;
                    esac
                    ;;
                *)
                    # Failed to stop
                    log_end_msg 1
                    ;;
            esac
            ;;
        *)
            echo "Usage: /etc/init.d/softflowd {start|stop|status|restart|force-reload} [<instance> ...]" >&2
            exit 3
            ;;
    esac
}

action="$1"
shift

if [ "$1" ]; then
    while [ "$1" ]; do
        config="/etc/softflowd/$1.conf"
        if [ -f "$config" ]; then
            do_action "$config"
        fi
        shift
    done
else
    for config in /etc/softflowd/*.conf; do
        if [ -f "$config" ]; then
            do_action "$config"
        fi
    done
fi

exit $exit