source: debian/i2p.init @ aa8fd85

Last change on this file since aa8fd85 was 627f707, checked in by kytv <kytv@…>, 5 years ago

debian: Add support for setting open file limits to initscript, add comment to explain how to do it with systemd

  • Property mode set to 100755
File size: 6.1 KB
Line 
1#!/bin/sh
2
3### BEGIN INIT INFO
4# Provides: i2p i2p-router
5# Required-Start: $remote_fs $syslog $named $network $time
6# Required-Stop: $remote_fs $syslog $named $network
7# Default-Start: 2 3 4 5
8# Default-Stop: 0 1 6
9# Short-Description: start and stop the i2p router
10# Description: i2p is a load-balanced unspoofable packet switching network
11### END INIT INFO
12
13# Check permissions
14if [ "`id -ur`" != '0' ]; then
15   echo 'ERROR: You must be root to start this service.' >&2
16   echo
17   exit 1
18fi
19
20PATH=/sbin:/usr/sbin:/bin:/usr/bin
21DESC="I2P Router"
22NAME="i2p"
23RUN="/var/run/$NAME"
24DAEMON="/usr/sbin/wrapper"
25PIDFILE="$RUN/$NAME.pid"
26JVMPIDFILE="$RUN/routerjvm.pid"
27I2P="/usr/share/i2p"
28I2PTEMP="/tmp/${NAME}-daemon"
29WRAPPERLOG="/var/log/i2p/wrapper.log"
30
31# Don't touch these, edit /etc/default/i2p
32RUN_DAEMON="False"
33NICE=0
34I2PUSER="i2psvc"
35
36I2P_ARGS="/etc/i2p/wrapper.config \
37 wrapper.java.additional.1=-DloggerFilenameOverride=/var/log/i2p/log-router-@.txt \
38 wrapper.java.additional.10=-Dwrapper.logfile=$WRAPPERLOG \
39 wrapper.java.additional.11=-Di2p.dir.pid=$RUN \
40 wrapper.java.additional.12=-Di2p.dir.temp=$I2PTEMP \
41 wrapper.logfile=$WRAPPERLOG \
42 wrapper.pidfile=$PIDFILE \
43 wrapper.java.pidfile=$JVMPIDFILE \
44 wrapper.daemonize=TRUE"
45
46. /lib/lsb/init-functions
47. /lib/init/vars.sh
48
49
50for ENV_FILE in /etc/environment /etc/default/locale; do
51    [ -r "$ENV_FILE" ] || continue
52    [ -s "$ENV_FILE" ] || continue
53
54    for var in LANG LANGUAGE LC_ALL LC_CTYPE; do
55        value=`egrep "^${var}=" "$ENV_FILE" | tail -n1 | cut -d= -f2`
56        [ -n "$value" ] && eval export $var=$value
57
58    done
59done
60
61# read config file
62[ -r /etc/default/$NAME ] && . /etc/default/$NAME
63
64if [ -z "$RUN_DAEMON" ]; then
65    echo "/etc/default/$NAME is not properly configured. Aborting." >&2
66    exit 1
67fi
68
69case "$CONFINE_WITH_APPARMOR" in
70    [NnFf]*)
71        USE_AA="no"
72        ;;
73    *)
74        USE_AA="yes"
75        ;;
76esac
77
78case "$RUN_DAEMON" in
79    [NnFf]*)
80        log_action_msg "$DESC daemon disabled in /etc/default/$NAME".
81        exit 0
82        ;;
83esac
84
85# We need the wrapper. If it's not found, abort.
86[ -x $DAEMON ] || exit 1
87
88# Even though this script doesn't call it, check for i2prouter.  If it's not
89# found, it probably means that the package isn't installed anymore.
90[ -x /usr/bin/i2prouter ] || exit 1
91
92do_start()
93{
94    [ ! -z $ULIMIT ] && ulimit -n $ULIMIT
95    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null 2>&1 \
96        || return 1
97    [ -d $RUN ] || mkdir $RUN > /dev/null 2>&1
98    [ -d $I2PTEMP ] || mkdir $I2PTEMP > /dev/null 2>&1
99    if [ -r $PIDFILE ]; then
100        PID="$(cat ${PIDFILE})"
101        if ! kill -0 $PID > /dev/null 2>&1; then
102                rm "$PIDFILE" "$JVMPIDFILE"
103        else
104                # we shouldn't get here, but...
105                echo "ERROR: Another instance of $DESC is already running." >&2
106                return 1
107        fi
108    fi
109    chown -Rf $I2PUSER:$I2PUSER  $I2PTEMP $RUN > /dev/null 2>&1
110    chown -f -R $I2PUSER:i2psvc /var/log/$NAME > /dev/null 2>&1
111    if [ "$USE_AA" = "yes" ] && \
112       [ -x /usr/sbin/aa-status ] && \
113       [ -x /usr/sbin/aa-exec ] && \
114       [ -e /etc/apparmor.d/system_i2p ] && \
115       /usr/sbin/aa-status --enabled ; then
116            AA="--startas /usr/sbin/aa-exec"
117            AA_ARGS="--profile=system_i2p -- $DAEMON"
118    else
119            AA=""
120            AA_ARGS=""
121    fi
122
123    TZ=UTC start-stop-daemon --start --quiet --chuid $I2PUSER --pidfile $PIDFILE $AA --exec $DAEMON --nicelevel $NICE -- \
124        $AA_ARGS $I2P_ARGS || return 2
125
126    # FIXME Temporary hack
127    # Files from /usr/share/i2p should have been copied, if need be, well before the 15 seconds are up.
128    if [ ! -f /var/lib/i2p/i2p-config/.perms ] && [ $I2PUSER = "i2psvc" ]; then
129        sleep 15
130        find /var/lib/i2p/ -type d -exec chmod 770 {} \;
131        find /var/lib/i2p/ -type f -exec chmod 660 {} \;
132        touch /var/lib/i2p/i2p-config/.perms
133    fi
134}
135
136do_stop()
137{
138    start-stop-daemon --stop --quiet --retry=TERM/60/KILL/20 --pidfile $PIDFILE -u $I2PUSER
139    RETVAL="$?"
140    [ "$RETVAL" = 2 ] && return 2
141
142    start-stop-daemon --stop --quiet --oknodo --retry=0/60/KILL/20 --exec $DAEMON
143
144    rm -rf "$I2PTEMP" > /dev/null 2>&1
145    [ -d "$RUN" ] && rm -f "$RUN/*" > /dev/null 2>&1
146    [ -d "$RUN" ] && rmdir "$RUN" > /dev/null 2>&1
147}
148
149do_dump()
150{
151    start-stop-daemon --stop --quiet -s 3 --pidfile $PIDFILE -u $I2PUSER
152    RETVAL="$?"
153    [ "$RETVAL" = 2 ] && return 2
154}
155
156do_graceful()
157{
158    start-stop-daemon --stop --quiet -s HUP --pidfile $PIDFILE -u $I2PUSER
159    [ "$RETVAL" = 2 ] && return 2
160}
161
162case "$1" in
163  start)
164        log_daemon_msg "Starting $DESC" "$NAME"
165        do_start
166        case "$?" in
167                0|1) log_end_msg 0 ;;
168                2) log_end_msg 1 ;;
169        esac
170        ;;
171  dump)
172        log_daemon_msg "Dumping $DESC" "$NAME"
173        do_dump
174        case "$?" in
175                0|1) log_end_msg 0 ;;
176                2) log_end_msg 1 ;;
177        esac
178        ;;
179  graceful)
180        log_daemon_msg "Attempting graceful shutdown of $DESC" "$NAME"
181        log_daemon_msg "(could take up to 11 minutes)"
182        do_graceful
183        case "$?" in
184                0|1) log_end_msg 0 ;;
185                2) log_end_msg 1 ;;
186        esac
187        ;;
188  stop)
189    log_daemon_msg "Stopping $DESC" "$NAME"
190        do_stop
191        case "$?" in
192                0|1) log_end_msg 0 ;;
193                2) log_end_msg 1 ;;
194        esac
195        ;;
196  status)
197       status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $?
198    ;;
199 restart|force-reload)
200        log_daemon_msg "Restarting $DESC" "$NAME"
201        do_stop
202        case "$?" in
203          0|1)
204                do_start
205                case "$?" in
206                        0) log_end_msg 0 ;;
207                        1) log_end_msg 1 ;; # Old process is still running
208                        *) log_end_msg 1 ;; # Failed to start
209                esac
210                ;;
211          *)
212                # Failed to stop
213                log_end_msg 1
214                ;;
215        esac
216        ;;
217*)
218    N=/etc/init.d/$NAME
219    echo "Usage: $N {start|stop|dump|graceful|status|restart|force-reload}" >&2
220    exit 3
221    ;;
222
223
224esac
Note: See TracBrowser for help on using the repository browser.