Browse Source

Reformat shell scripts

Christoph Biedl 1 year ago
parent
commit
f1dad4da08
2 changed files with 85 additions and 81 deletions
  1. 13 14
      debian/softflowd-generator
  2. 72 67
      debian/softflowd.init

+ 13 - 14
debian/softflowd-generator

@@ -2,29 +2,28 @@
 
 set -eu
 
-SERVICEFILE="/lib/systemd/system/softflowd@.service"
-WANTDIR="$1/softflowd.service.wants"
+service_file="/lib/systemd/system/softflowd@.service"
+want_dir="$1/softflowd.service.wants"
 
-CONFIG_DIR=/etc/softflowd/
+config_dir=/etc/softflowd/
 
-if [ -d "$CONFIG_DIR" ] ; then
-    mkdir -p "$WANTDIR"
-    cd "$CONFIG_DIR"
-    for CONFIG in *.conf ; do
-        [ -f "$CONFIG" ] || continue
-        INSTANCE="$(systemd-escape "${CONFIG%%.conf}")"
-        LINK="$WANTDIR/softflowd@$INSTANCE.service"
+if [ -d "$config_dir" ]; then
+    mkdir -p "$want_dir"
+    cd "$config_dir"
+    for config in *.conf; do
+        [ -f "$config" ] || continue
+        instance="$(systemd-escape "${config%%.conf}")"
+        link="$want_dir/softflowd@$instance.service"
 
-        sh -n "$CONFIG_DIR$CONFIG" 2>/dev/null || continue
+        sh -n "$config_dir$config" 2>/dev/null || continue
 
         interface=
-        options=
 
-        . "$CONFIG_DIR$CONFIG"
+        . "$config_dir$config"
 
         [ "$interface" ] || continue
 
-        ln -s "$SERVICEFILE" "$LINK"
+        ln -s "$service_file" "$link"
     done
 fi
 

+ 72 - 67
debian/softflowd.init

@@ -12,21 +12,21 @@
 ### END INIT INFO
 
 PATH=/sbin:/usr/sbin:/bin:/usr/bin
-NAME=softflowd
-DESC='Flow-based network traffic analyser'
-SOFTFLOWD="/usr/sbin/$NAME"
-PIDDIR="/var/run/$NAME/"
-PRIVDROP_CHROOT_DIR='/var/run/softflowd/chroot'
+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
+[ -x "$softflowd" ] || exit 0
 
-if [ "$1" = 'start' ] ; then
-    mkdir -p "$PRIVDROP_CHROOT_DIR"
-    if [ -e /var/run/softflowd.ctl ] ; then
+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"
+    install -D -p /etc/protocols "$privdrop_chroot_dir/etc/protocols"
 fi
 
 # Load the VERBOSE setting and other rcS variables
@@ -41,34 +41,37 @@ fi
 #   0 if daemon has been started
 #   1 if daemon was already running
 #   2 if daemon could not be started
-do_start () {
-    local INSTANCE="$1"
-    local CONFIG="$2"
+do_start() {
+    local instance config
+    instance="$1"
+    config="$2"
 
-    sh -n "$CONFIG" 2>/dev/null || return 2
+    sh -n "$config" 2>/dev/null || return 2
 
     interface=
     options=
 
-    . "$CONFIG"
+    . "$config"
 
     [ "$interface" ] || return 2
 
-    local PIDFILE="$PIDDIR$INSTANCE.pid"
-    local CTLSOCK="$PIDDIR$INSTANCE.ctl"
+    local pid_file ctl_sock
+    pid_file="$pid_dir$instance.pid"
+    ctl_sock="$pid_dir$instance.ctl"
     start-stop-daemon --start --quiet \
-        --pidfile "$PIDFILE" --exec "$SOFTFLOWD" --test > /dev/null \
-        || return 1
+        --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 \
-        --pidfile "$PIDFILE" \
-        --exec "$SOFTFLOWD" -- \
-        -p "$PIDFILE" -c "$CTLSOCK" \
-        -i "$interface" $options \
-        || return 2
+        --pid_file "$pid_file" \
+        --exec "$softflowd" -- \
+        -p "$pid_file" -c "$ctl_sock" \
+        -i "$interface" $options ||
+        return 2
     sleep 1
     start-stop-daemon --start --quiet \
-        --pidfile "$PIDFILE" --exec "$SOFTFLOWD" --test > /dev/null \
-        && return 2
+        --pid_file "$pid_file" --exec "$softflowd" --test >/dev/null &&
+        return 2
 }
 
 # Stop a softflowd instance
@@ -78,71 +81,74 @@ do_start () {
 #   1 if daemon was already stopped
 #   2 if daemon could not be stopped
 #   other if a failure occurred
-do_stop () {
-    local INSTANCE="$1"
+do_stop() {
+    local instance pid_file retval
+    instance="$1"
+    pid_file="$pid_dir/$instance.pid"
 
-    local PIDFILE="$PIDDIR/$INSTANCE.pid"
     start-stop-daemon --stop --quiet \
-        --retry=TERM/30/KILL/5 --pidfile "$PIDFILE" --name "$NAME"
-    RETVAL="$?"
-    [ "$RETVAL" = 2 ] && return 2
+        --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 "$PIDFILE"
-    return "$RETVAL"
+    rm -f "$pid_file"
+    return "$retval"
 }
 
-EXIT=0
+exit=0
 
-do_action () {
-    local CONFIG="$1"
+do_action() {
+    local config
+    config="$1"
 
-    INSTANCE="$(basename "${CONFIG%%.conf}")"
+    instance="$(basename "${config%%.conf}")"
 
-    case "$ACTION" in
+    case "$action" in
         start)
-            [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$INSTANCE"
-            do_start "$INSTANCE" "$CONFIG"
+            [ "$VERBOSE" != no ] && log_daemon_msg "Starting $description" "$instance"
+            do_start "$instance" "$config"
             case "$?" in
-                0|1)    [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+                0 | 1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                 2)
                     [ "$VERBOSE" != no ] && log_end_msg 1
-                    EXIT=1
+                    exit=1
                     ;;
             esac
             ;;
         stop)
-            [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$INSTANCE"
-            do_stop "$INSTANCE"
+            [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $description" "$instance"
+            do_stop "$instance"
             case "$?" in
-                0|1)    [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+                0 | 1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                 2)
                     [ "$VERBOSE" != no ] && log_end_msg 1
-                    EXIT=1
+                    exit=1
                     ;;
             esac
             ;;
         status)
-            status_of_proc -p "$PIDDIR/$INSTANCE.pid" "$SOFTFLOWD" "softflowd instance $INSTANCE" || EXIT=$?
+            status_of_proc -p "$pid_dir/$instance.pid" "$softflowd" "softflowd instance $instance" || exit=$?
             ;;
-        restart|force-reload)
-            log_daemon_msg "Restarting $DESC" "$INSTANCE"
-            do_stop "$INSTANCE"
+        restart | force-reload)
+            log_daemon_msg "Restarting $description" "$instance"
+            do_stop "$instance"
             case "$?" in
-                0|1)
-                    do_start "$INSTANCE" "$CONFIG"
+                0 | 1)
+                    do_start "$instance" "$config"
                     case "$?" in
-                        0)  log_end_msg 0 ;;
+                        0) log_end_msg 0 ;;
                         *)
                             # Old process is still running or
                             # failed to start
-                            log_end_msg 1 ;;
+                            log_end_msg 1
+                            ;;
                     esac
                     ;;
                 *)
                     # Failed to stop
                     log_end_msg 1
                     ;;
-                esac
+            esac
             ;;
         *)
             echo "Usage: /etc/init.d/softflowd {start|stop|status|restart|force-reload} [<instance> ...]" >&2
@@ -151,24 +157,23 @@ do_action () {
     esac
 }
 
-
-ACTION="$1"
+action="$1"
 shift
 
-if [ "$1" ] ; then
-    while [ "$1" ] ; do
-        CONFIG="/etc/softflowd/$1.conf"
-        if [ -f "$CONFIG" ] ; then
-            do_action "$CONFIG"
+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"
+    for config in /etc/softflowd/*.conf; do
+        if [ -f "$config" ]; then
+            do_action "$config"
         fi
     done
 fi
 
-exit $EXIT
+exit $exit