summaryrefslogtreecommitdiffstats
path: root/network/sshguard/rc.sshguard
blob: c08033b3c7d0a8b08925d089825c7e51e21ed903 (plain)
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
#!/bin/bash

declare -r daemon_name=sshguard
declare -r daemon_prog=/usr/sbin/sshguard

################################################################################
the_daemon()
################################################################################
{
    daemon --name "${daemon_name}" "${@}"
}

################################################################################
the_damon_start()
################################################################################
{
    if ! the_daemon --running; then
        echo "Starting ${daemon_name}:  ${daemon_prog}"

        the_daemon -- ${daemon_prog}
    else
        echo "${daemon_name} seems to be already running."
    fi
}

################################################################################
the_damon_stop()
################################################################################
{
    if the_daemon --running; then
        echo "Stopping ${daemon_name}."

        the_daemon --stop
    else
        echo "${daemon_name} does not seem to be running."
    fi
}

################################################################################
the_damon_restart()
################################################################################
{
    if the_daemon --running; then
        the_damon_stop
        the_daemon_wait_stopped
        the_damon_start
    else
        echo "${daemon_name} does not seem to be running."
    fi
}

################################################################################
the_daemon_wait_stopped()
################################################################################
{
    # All time values given in a unit of 0.1 second.

    local -r timeout=50
    local delay=1
    local delay_sum=0
    local -r test_expr=(the_daemon --running)

    # Poll (with timeout) for the daemon to exit.

    while "${test_expr[@]}" && [[ ${delay_sum} -lt ${timeout} ]]; do
        sleep $(echo "${delay} / 10.0" | bc -l)
        delay_sum=$((delay_sum + delay))

        # Double the delay in each iteration to lower the CPU use.

        delay=$((delay * 2))

        # Adjust next's iteration delay prevent waiting longer than _timeout_
        # in case the time already waited and the delay to be waited in
        # the next iteration would be greater than the requested _timeout_.

        if [[ $((delay_sum + delay)) -gt ${timeout} ]]; then
            delay=$((timeout - delay_sum))
        fi
    done

    if "${test_expr[@]}"; then
        echo "Timeout waiting for ${daemon_name} to stop."

        exit 1
    fi
}

case "${1}" in
start)
    the_damon_start
;;

stop)
    the_damon_stop
;;

restart)
    the_damon_restart
;;

*)
    echo "usage: ${0} start|stop|restart"
;;
esac