releasing package iodine version 0.6.0~rc1-16
[debian/iodine.git] / debian / iodine.iodined.init
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          iodined
4 # Required-Start:    $remote_fs $network $syslog $named
5 # Required-Stop:     $remote_fs $network $syslog
6 # Default-Start:     2 3 4 5
7 # Default-Stop:      0 1 6
8 # Short-Description: initscript for iodined
9 # Description:       initscript for iodined
10 ### END INIT INFO
11
12 # Author: gregor herrmann <gregor+debian@comodo.priv.at>
13
14 # Do NOT "set -e"
15
16 # PATH should only include /usr/* if it runs after the mountnfs.sh script
17 PATH=/sbin:/usr/sbin:/bin:/usr/bin
18 DESC="IP over DNS tunneling server"
19 NAME=iodined
20 DAEMON=/usr/sbin/$NAME
21 DEFAULT=iodine
22 DAEMON_ARGS=""
23 PIDFILE=/var/run/$NAME.pid
24 SCRIPTNAME=/etc/init.d/$NAME
25 CHROOTDIR=/var/run/iodine
26
27 # Exit if the package is not installed
28 [ -x "$DAEMON" ] || exit 0
29
30 # Load the VERBOSE setting and other rcS variables
31 . /lib/init/vars.sh
32
33 # Define LSB log_* functions.
34 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
35 . /lib/lsb/init-functions
36
37 # Get config
38 get_config() {
39         [ -r /etc/default/$DEFAULT ] && . /etc/default/$DEFAULT
40         if [ "$START_IODINED" != "true" ] ; then
41                 [ "$VERBOSE" != no ] && log_progress_msg "- automatic start disabled" && log_end_msg 0
42                 exit 0
43         else
44                 if [ -n "$IODINED_ARGS" ] && [ -n "$IODINED_PASSWORD" ] ; then
45                         DAEMON_ARGS="-u iodine -t $CHROOTDIR $IODINED_ARGS"
46                 else
47                         [ "$VERBOSE" != no ] && log_warning_msg "$NAME is not fully configured. Change this in /etc/default/$DEFAULT or run dpkg-reconfigure $DEFAULT."
48                         exit 0
49                 fi
50         fi
51 }
52
53 # chroot dir
54 check_chrootdir() {
55         if [ -d "$CHROOTDIR" ] || mkdir -p "$CHROOTDIR" ; then
56                 return 0
57         else
58                 [ "$VERBOSE" != no ] && log_failure_msg "$CHROOTDIR does not exist and can't be created."
59                 exit 0
60         fi
61 }       
62
63 #
64 # Function that starts the daemon/service
65 #
66 do_start()
67 {
68         # populate $DAEMON_ARGS
69         get_config
70         # check CHROOTDIR
71         check_chrootdir
72         # 
73         # Return
74         #   0 if daemon has been started
75         #   1 if daemon was already running
76         #   2 if daemon could not be started
77         start-stop-daemon --start --quiet --exec $DAEMON --test > /dev/null \
78                 || return 1
79         start-stop-daemon --start --quiet --exec $DAEMON -- $DAEMON_ARGS -P "$IODINED_PASSWORD" \
80                 || return 2
81         # Add code here, if necessary, that waits for the process to be ready
82         # to handle requests from services started subsequently which depend
83         # on this one.  As a last resort, sleep for some time.
84 }
85
86 #
87 # Function that stops the daemon/service
88 #
89 do_stop()
90 {
91         # Return
92         #   0 if daemon has been stopped
93         #   1 if daemon was already stopped
94         #   2 if daemon could not be stopped
95         #   other if a failure occurred
96         start-stop-daemon --stop --quiet --retry=TERM/5/KILL/5 --exec $DAEMON
97         RETVAL="$?"
98         [ "$RETVAL" = 2 ] && return 2
99         # Wait for children to finish too if this is a daemon that forks
100         # and if the daemon is only ever run from this initscript.
101         # If the above conditions are not satisfied then add some other code
102         # that waits for the process to drop all resources that could be
103         # needed by services started subsequently.  A last resort is to
104         # sleep for some time.
105         start-stop-daemon --stop --quiet --oknodo --retry=0/5/KILL/5 --exec $DAEMON
106         [ "$?" = 2 ] && return 2
107         # Many daemons don't delete their pidfiles when they exit.
108         rm -f $PIDFILE
109         return "$RETVAL"
110 }
111
112 case "$1" in
113   start)
114         [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
115         do_start
116         case "$?" in
117                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
118                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
119         esac
120         ;;
121   stop)
122         [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
123         do_stop
124         case "$?" in
125                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
126                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
127         esac
128         ;;
129   status)
130         status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
131         ;;
132   restart|force-reload)
133         log_daemon_msg "Restarting $DESC" "$NAME"
134         do_stop
135         case "$?" in
136           0|1)
137                 do_start
138                 case "$?" in
139                         0) log_end_msg 0 ;;
140                         1) log_end_msg 1 ;; # Old process is still running
141                         *) log_end_msg 1 ;; # Failed to start
142                 esac
143                 ;;
144           *)
145                 # Failed to stop
146                 log_end_msg 1
147                 ;;
148         esac
149         ;;
150   *)
151         echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
152         exit 3
153         ;;
154 esac
155
156 :