big svn cleanup
[anytun.git] / src / openvpn / gentoo / openvpn.init
blob99fa8b287c2bec7bcc291e4a13772b0c51f8f17e
1 #!/sbin/runscript
3 # OpenVPN start/stop script
4 # Adapted to Gentoo by James Yonan
6 # Originally Contributed to the OpenVPN project by
7 # Douglas Keller <doug@voidstar.dyndns.org>
8 # 2002.05.15
10 # This script does the following:
12 # - Starts an openvpn process for each .conf file it finds in
13 #   /etc/openvpn.
15 # - If /etc/openvpn/xxx.sh exists for a xxx.conf file then it executes
16 #   it before starting openvpn (useful for doing openvpn --mktun...).
18 # - In addition to start/stop you can do:
20 #   service openvpn reload - SIGHUP
21 #   service openvpn reopen - SIGUSR1
22 #   service openvpn status - SIGUSR2
24 # Location of openvpn binary
25 openvpn=/usr/local/sbin/openvpn
27 # PID directory
28 piddir=/var/run/openvpn
30 # Our working directory (.conf files should be here)
31 work=/etc/openvpn
33 # Our options
34 opts="start stop restart condrestart"
36 depend() {
37     need net
38     use dns
41 start() {
42     ebegin "Starting OpenVPN"
44     # Load the TUN/TAP module
45     /sbin/modprobe tun >/dev/null 2>&1
47     if [ ! -d  $piddir ]; then
48         mkdir $piddir
49     fi
51     cd $work
53     # Start every .conf in $work and run .sh if exists
54     local errors=0
55     local successes=0
56     local retstatus=0
57     for c in `/bin/ls *.conf 2>/dev/null`; do
58         bn=${c%%.conf}
59         if [ -f "$bn.sh" ]; then
60             . $bn.sh
61         fi
62         rm -f $piddir/$bn.pid
63         $openvpn --daemon openvpn-$bn --writepid $piddir/$bn.pid --config $c --cd $work
64         if [ $? = 0 ]; then
65             successes=1
66         else
67             errors=1
68         fi
69     done
71     # Decide status based on errors/successes.
72     # If at least one tunnel succeeded, we return success.
73     # If some tunnels succeeded and some failed, we return
74     #   success but give a warning.
75     if [ $successes = 1 ]; then
76         if [ $errors = 1 ]; then
77             ewarn "Note: At least one OpenVPN tunnel failed to start"
78         fi
79     else
80         retstatus=1
81         if [ $errors = 0 ]; then
82             ewarn "Note: No OpenVPN configuration files were found in $work"
83         fi
84     fi
85     eend $retstatus "Error starting OpenVPN"
88 stop() {
89     ebegin "Stopping OpenVPN"
90     for pidf in `/bin/ls $piddir/*.pid 2>/dev/null`; do
91         if [ -s $pidf ]; then
92             kill `cat $pidf` >/dev/null 2>&1
93         fi
94         rm -f $pidf
95     done
96     eend 0
99 # this should really be in runscript.sh
100 started() {
101     if [ -L "${svcdir}/started/${myservice}" ]; then
102         return 1
103     else
104         return 0
105     fi
108 # attempt to restart ONLY if we are already started
109 condrestart() {
110     started || restart