0d029022ba0dd7e1bfff1c9eb21db6ff21931f0a
[openwrt/svn-archive/archive.git] / net / sslh / files / sslh.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2009 OpenWrt.org
3
4 NAME=sslh
5 PROG=/usr/sbin/sslh
6 START=95
7 PIDCOUNT=0
8
9 sslh_start()
10 {
11 local section="$1"
12
13 # check if section is enabled (default)
14 local enabled
15 config_get_bool enabled "${section}" enable 1
16 [ ${enabled} -eq 0 ] && return 1
17
18 # increase pid file count to handle multiple instances correctly
19 PIDCOUNT="$(( ${PIDCOUNT} + 1 ))"
20
21 # prepare parameters (initialise with pid file)
22 local args="-P /var/run/${NAME}.${PIDCOUNT}.pid"
23 local val
24 # A) listen parameter
25 config_get val "${section}" listen
26 [ ! -z ${val} ] && append args "-p ${val}"
27 # B) ssh parameter
28 config_get val "${section}" ssh
29 [ ! -z ${val} ] && append args "-s ${val}"
30 # C) ssl parameter
31 config_get val "${section}" ssl
32 [ ! -z ${val} ] && append args "-l ${val}"
33 # D) timeout (for ssh, then ssl is assumed)
34 config_get val "${section}" timeout
35 [ ! -z ${val} ] && append args "-t ${val}"
36 # E) verbose parameter
37 local verbosed
38 config_get_bool verbosed "${section}" verbose 0
39 [ ${verbosed} -ne 0 ] && append args "-v"
40
41 # execute program and return its exit code
42 [ ${verbosed} -ne 0 ] && echo "${NAME}: section ${section} starting ${PROG} ${args}"
43 ${PROG} ${args}
44 return $?
45 }
46
47 start()
48 {
49 config_load "${NAME}"
50 config_foreach sslh_start sslh
51 }
52
53 stop()
54 {
55 local pidfile
56 local rc=0
57
58 # killing all known processes
59 for pidfile in `ls /var/run/${NAME}.*.pid`
60 do
61 start-stop-daemon -K -s KILL -p "${pidfile}" -n "${NAME}"
62 [ $? -ne 0 ] && rc=1
63 rm -f "${pidfile}"
64 done
65
66 # kill orphaned processes
67 if [ ${rc} -ne 0 ]
68 then
69 echo "${NAME}: inconsistency in pid files killing all orphaned processes"
70 for pid in `pidof sslh`
71 do
72 # check if correct program
73 ps | grep ${pid} | grep ${PROG} >/dev/null
74 [ $? -ne 0 ] && continue
75
76 # kill process
77 echo "Killing ${pid}..."
78 kill -s KILL ${pid}
79 done
80 fi
81 }