aboutsummaryrefslogtreecommitdiff
path: root/scripts/daemon.sh
blob: 30b3c5c00ce08b1e89031f70b71bfb15ee483c73 (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
#!/usr/bin/env sh
#
# Simple nDPId/nDPIsrvd start/stop script for testing purposes.
#

NROOT="${NROOT:-/tmp}"
NUSER="${NUSER:-$(id -u -n)}"
NSUFFIX="${NSUFFIX:-daemon-test}"
nDPId_THREADS="${nDPId_THREADS:-4}"
nDPId_ARGS="${nDPId_ARGS:-}"
nDPIsrvd_ARGS="${nDPIsrvd_ARGS:-}"

if [ x"${1}" = x -o x"${2}" = x ]; then
    printf '%s\n' "usage: ${0} [nDPId-path] [nDPIsrvd-path]" >&2
    printf '\n\t%s=%s\n' 'env NUSER'         "${NUSER}" >&2
    printf   '\t%s=%s\n' 'env NSUFFIX'       "${NSUFFIX}" >&2
    printf   '\t%s=%s\n' 'env nDPId_ARGS'    "${nDPId_ARGS}" >&2
    printf   '\t%s=%s\n' 'env nDPIsrvd_ARGS' "${nDPIsrvd_ARGS}" >&2
    exit 1
fi

RETVAL=0

if [ -r "${NROOT}/nDPId-${NSUFFIX}.pid" -o -r "${NROOT}/nDPIsrvd-${NSUFFIX}.pid" ]; then
    nDPId_PID="$(cat "${NROOT}/nDPId-${NSUFFIX}.pid" 2>/dev/null)"
    nDPIsrvd_PID="$(cat "${NROOT}/nDPIsrvd-${NSUFFIX}.pid" 2>/dev/null)"

    if [ x"${nDPId_PID}" != x ]; then
        sudo kill "${nDPId_PID}" 2>/dev/null || true

        MAX_TRIES=10
        while ps -p "${nDPId_PID}" > /dev/null; do
            test ${MAX_TRIES} -gt 0 || break
            sleep 1
            MAX_TRIES=$((MAX_TRIES - 1))
        done
        test ${MAX_TRIES} -eq 0 && { RETVAL=1; printf '%s\n' 'Error: nDPId not started' >&2; }

        rm -f "${NROOT}/nDPId-${NSUFFIX}.pid"
    else
        printf '%s\n' "${1} not started .." >&2
        RETVAL=1
    fi

    if [ x"${nDPIsrvd_PID}" != x ]; then
        kill "${nDPIsrvd_PID}" 2>/dev/null || true

        MAX_TRIES=10
        while ps -p "${nDPIsrvd_PID}" > /dev/null; do
            test ${MAX_TRIES} -gt 0 || break
            sleep 1
            MAX_TRIES=$((MAX_TRIES - 1))
        done
        test ${MAX_TRIES} -eq 0 && { RETVAL=1; printf '%s\n' 'Error: nDPIsrvd not started' >&2; }

        rm -f "${NROOT}/nDPIsrvd-${NSUFFIX}.pid" "${NROOT}/nDPIsrvd-${NSUFFIX}-collector.sock" "${NROOT}/nDPIsrvd-${NSUFFIX}-distributor.sock"
    else
        printf '%s\n' "${2} not started .." >&2
        RETVAL=1
    fi

    printf '%s\n' "daemons stopped" >&2
else
    set -x
    sudo ${2} -p "${NROOT}/nDPIsrvd-${NSUFFIX}.pid" -c "${NROOT}/nDPIsrvd-${NSUFFIX}-collector.sock" -s "${NROOT}/nDPIsrvd-${NSUFFIX}-distributor.sock" -d -u "${NUSER}" -L "${NROOT}/nDPIsrvd.log" ${nDPIsrvd_ARGS}
    test $? -eq 0 || RETVAL=1

    MAX_TRIES=10
    while [ ! -S "${NROOT}/nDPIsrvd-${NSUFFIX}-collector.sock" -a ${MAX_TRIES} -gt 0 ]; do
        sleep 0.5
        MAX_TRIES=$((MAX_TRIES - 1))
    done
    test ${MAX_TRIES} -eq 0 && { RETVAL=1; printf '%s\n' 'Error: nDPIsrvd collector socket not available' >&2; }

    MAX_TRIES=10
    while [ ! -S "${NROOT}/nDPIsrvd-${NSUFFIX}-distributor.sock" -a ${MAX_TRIES} -gt 0 ]; do
        sleep 0.5
        MAX_TRIES=$((MAX_TRIES - 1))
    done
    test ${MAX_TRIES} -eq 0 && { RETVAL=1; printf '%s\n' 'Error: nDPIsrvd distributor socket not available' >&2; }

    sudo chgrp "$(id -n -g "${NUSER}")" "${NROOT}/nDPIsrvd-${NSUFFIX}-collector.sock"
    test $? -eq 0 || RETVAL=1
    sudo chmod g+w "${NROOT}/nDPIsrvd-${NSUFFIX}-collector.sock"
    test $? -eq 0 || RETVAL=1
    sudo ${1} -p "${NROOT}/nDPId-${NSUFFIX}.pid" -c "${NROOT}/nDPIsrvd-${NSUFFIX}-collector.sock" -d -u "${NUSER}" -L "${NROOT}/nDPId.log" -o max-reader-threads=${nDPId_THREADS} ${nDPId_ARGS}
    test $? -eq 0 || RETVAL=1
    set +x
    printf '%s\n' "daemons started" >&2
    test ${RETVAL} -eq 0 && printf '%s\n' "You may now run examples e.g.: $(realpath --relative-to="$(pwd)" $(dirname "${0}")/../examples/py-flow-info/flow-info.py) --unix ${NROOT}/nDPIsrvd-${NSUFFIX}-distributor.sock"
fi

if [ ${RETVAL} -ne 0 ]; then
    test -r "${NROOT}/nDPIsrvd.log" && cat "${NROOT}/nDPIsrvd.log"
    test -r "${NROOT}/nDPId.log" && cat "${NROOT}/nDPId.log"
fi

exit ${RETVAL}