source: debian/i2p.init @ 25f6c3d

Last change on this file since 25f6c3d was 29953ea, checked in by kytv <kytv@…>, 5 years ago

Debian: confine daemon with apparmor (ticket #1061)

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