summarylogtreecommitdiffstats
path: root/i2prouter.sh
blob: f4e7ac9a411d0aa7560eca460a849a9e490ec1c9 (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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env bash

#-----------------------------------------------------------------------------
I2P_USER="i2p"
WRAPPER_CMD="/usr/bin/java-service-wrapper"
WRAPPER_CONF="/opt/i2p/wrapper.config"
PIDFILE="/run/i2p/i2p.pid"
TIMEOUT=30  #seconds
#-----------------------------------------------------------------------------

fail() {
    printf "\e[1;31m>>> ERROR:\033[0m %s\n" "$*"
    exit 1
}
debug() {
    printf "\e[1;33m>>> DEBUG:\033[0m %s\n" "$*"
}

check_user() {
    if [[ "$(id -un)" != "$I2P_USER" ]]; then
        #debug "current user: $(id -un)  dropping to user: $I2P_USER"
        #chmod 700 /opt/i2p/.{i2p,tmp}
        SCRIPT_PATH="$(cd $(dirname $0) && pwd)/$(basename $0)"
        su - "$I2P_USER" -c "${SCRIPT_PATH} $@"
        exit $?
    fi
}

init_vars() {
    [[ ! -r "$WRAPPER_CONF" ]] &&
        fail "Unable to read \$WRAPPER_CONF: ${WRAPPER_CONF}"
    [[ ! -x "$WRAPPER_CMD" ]] &&
        fail "Unable to find or execute \$WRAPPER_CMD: ${WRAPPER_CMD}"
    [[ ! $(grep -E ^I2P_USER $0) && "$EUID" = "0" ]] &&
        fail "Attempting to start as root! Please edit $(basename $0) and set the variable \$I2P_USER"
    [[ "$(id -un "$I2P_USER")" != "$I2P_USER" ]] &&
        fail "\$I2P_USER does not exist: $I2P_USER"
    COMMAND_LINE="\"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"i2prouter\" wrapper.name=\"i2prouter\" TZ=UTC"
}

get_wrapper_pid() {
    pgrep -u "$I2P_USER" -f 'wrapper.name=i2prouter'
}
get_pid() {
    pgrep -u "$I2P_USER" -f 'jar'
}

check_if_running() {
    unset pid
    if [[ -f "$PIDFILE" ]]; then
        if [[ -r "$PIDFILE" ]]; then
            pid=$(cat "$PIDFILE")
            #debug "pid:$pid  get_pid:$(get_pid)"
            if [[ ! "$pid" ]]; then
                pid=$(get_pid)
                if [[ ! "$pid" ]]; then
                    echo "Removing stale pid file: $PIDFILE"
                    rm -f "$PIDFILE"
                fi
            else
                [[ "$pid" != "$(get_pid)" ]] &&
                    fail "\$PIDFILE $PIDFILE differs from what is actually running!"
            fi
        else
            fail "Cannot read \$PIDFILE: $PIDFILE"
        fi
    fi
}

_console() {
    if [[ ! "$pid" ]]; then
        trap '' INT QUIT
        eval $COMMAND_LINE
        [[ $? != 0 ]] && fail "Failed to launch the wrapper!"
    else
        echo "I2P Router is already running! (pid: $pid)"
    fi
}

_start() {
    if [[ ! "$pid" ]]; then
        echo -n "Starting I2P Router"
        COMMAND_LINE+=" wrapper.daemonize=TRUE"
        eval $COMMAND_LINE
        [[ $? != 0 ]] && fail "Failed to launch the wrapper!"
        i=0
        while [[ ! "$pid" || $i < $TIMEOUT ]]; do
            echo -n "."
            sleep 1
            check_if_running
            ((i++))
        done
        [[ $(get_pid) ]] &&
            echo " done (pid $pid)" || fail "timeout: Failed to start wrapper!"
    else
        echo "I2P Router is already running! (pid: $pid)"
    fi
}

_restart() {
    [[ "$pid" ]] &&
        kill -USR1 $(get_wrapper_pid) || echo "I2P Router is not running"
}

_stop() {
    if [[ "$pid" ]]; then
        echo -n "Hard shutdown initiated"
        kill -TERM "$pid"
        [[ $? != 0 ]] && fail "Unable to stop I2P Router: kill -TERM $pid"
        i=0
        while [[ "$pid" || $i > $TIMEOUT ]]; do
            echo -n "."
            sleep 1
            [[ ! $(get_pid) ]] && unset pid
            ((i++))
        done
        if [[ "$pid" ]]; then
            fail "timeout: Failed to stop wrapper! (pid: $pid)"
        else
            echo " done"
            [[ "$1" = 'start' ]] && _start
        fi
    else
        echo "I2P Router is not running."
    fi
}

_graceful() {
    if [[ "$pid" ]]; then
        echo -n "Graceful shutdown initiated"
        kill -HUP "$pid"
        [[ $? != 0 ]] && fail "Unable to stop I2P Router."
        i=0
        while [[ "$pid" || $i > 660 ]]; do
            echo -n "."
            sleep 1
            [[ ! $(get_pid) ]] && unset pid
            ((i++))
        done
        if [[ "$pid" ]]; then
            fail "timeout: Took longer than 10m to stop. (pid: $pid)"
        else
            echo " done"
        fi
    else
        echo "I2P Router is not running."
    fi
}

_dump() {
    if [[ "$pid" ]]; then
        echo "Dumping threads..."
        kill -QUIT "$pid"
        [[ $? != 0 ]] &&
            fail "Failed to dump threads" || echo "Thread Dump is available in wrapper.log"
    else
        echo "I2P Router is not running."
    fi
}
#-----------------------------------------------------------------------------

[[ "$1" != @(console|start|stop|graceful|restart|dump) ]] && {
    echo "Usage: $(basename $0) [command]"
    echo
    echo "Commands:"
    echo "  console     Launch in the current console"
    echo "  start       Start in the background as a daemon process"
    echo "  stop        Stop if running as a daemon or in another console"
    echo "  graceful    Stop gracefully, may take up to 11 minutes for all tunnels to close"
    echo "  restart     Restart the JVM"
    echo "  dump        Request a Java thread dump if running"
    exit
}

check_user "$@"
check_if_running
init_vars

case "$1" in
     'console') _console
                ;;
       'start') _start
                ;;
        'stop') _stop
                ;;
    'graceful') _graceful
                ;;
     'restart') _restart
                ;;
        'dump') _dump
                ;;
esac