8549c40bdb59c1f5443208f6eb7c3ad0a7ac6444
[mirror/dsa-puppet.git] / modules / stunnel4 / files / etc-init.d-stunnel4
1 #! /bin/sh -e
2 ### BEGIN INIT INFO
3 # Provides:          stunnel4
4 # Required-Start:    $local_fs $remote_fs
5 # Required-Stop:     $local_fs $remote_fs
6 # Should-Start:      $syslog
7 # Should-Stop:       $syslog
8 # Default-Start:     2 3 4 5
9 # Default-Stop:      0 1 6
10 # Short-Description: Start or stop stunnel 4.x (SSL tunnel for network daemons)
11 ### END INIT INFO
12
13 DEFAULTPIDFILE="/var/run/stunnel4.pid"
14 DAEMON=/usr/bin/stunnel4
15 NAME=stunnel
16 DESC="SSL tunnels"
17 FILES="/etc/stunnel/*.conf"
18 OPTIONS=""
19 ENABLED=0
20
21 get_pids() {
22    local file=$1
23    if test -f $file; then
24      CHROOT=`grep "^chroot" $file|sed "s;.*= *;;"`
25      PIDFILE=`grep "^pid" $file|sed "s;.*= *;;"`
26      if [ "$PIDFILE" = "" ]; then
27        PIDFILE=$DEFAULTPIDFILE
28      fi
29      if test -f $CHROOT/$PIDFILE; then
30        cat $CHROOT/$PIDFILE
31      fi
32    fi
33 }
34
35 startdaemons() {
36   if ! [ -d /var/run/stunnel4 ]; then
37     rm -rf /var/run/stunnel4
38     install -d -o stunnel4 -g stunnel4 /var/run/stunnel4
39   fi
40   for file in $FILES; do 
41     if test -f $file; then
42       ARGS="$file $OPTIONS"
43       PROCLIST=`get_pids $file`
44       if [ "$PROCLIST" ] && kill -0 $PROCLIST 2>/dev/null; then
45         echo -n "[Already running: $file] "
46       elif $DAEMON $ARGS; then
47         echo -n "[Started: $file] "
48       else
49         echo "[Failed: $file]"
50         echo "You should check that you have specified the pid= in you configuration file"
51         exit 1
52       fi
53     fi
54   done;
55 }
56
57 killdaemons()
58 {
59   for file in $FILES; do
60     PROCLIST=`get_pids $file`
61     if [ "$PROCLIST" ] && kill -0 $PROCLIST 2>/dev/null; then
62        kill $PROCLIST
63        echo -n "[stopped: $file] "
64     fi
65   done
66 }
67
68 if [ "x$OPTIONS" != "x" ]; then
69   OPTIONS="-- $OPTIONS"
70 fi
71
72 test -f /etc/default/stunnel4 && . /etc/default/stunnel4
73 if [ "$ENABLED" = "0" ] ; then
74   echo "$DESC disabled, see /etc/default/stunnel4"
75   exit 0
76 fi
77
78 test -x $DAEMON || exit 0
79
80 set -e
81
82 case "$1" in
83   start)
84         echo -n "Starting $DESC: "
85         startdaemons
86         echo "$NAME."
87         ;;
88   stop)
89         echo -n "Stopping $DESC: "
90         killdaemons
91         echo "$NAME."
92         ;;
93 #force-reload does not send a SIGHUP, since SIGHUP is interpreted as a 
94 #quit signal by stunnel. I reported this problem to upstream authors.
95   force-reload|restart)
96         echo -n "Restarting $DESC: "
97         killdaemons
98         sleep 5
99         startdaemons
100         echo "$NAME."
101         ;;
102   *)
103         N=/etc/init.d/$NAME
104         echo "Usage: $N {start|stop|force-reload|restart}" >&2
105         exit 1
106         ;;
107 esac
108
109 exit 0