1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2009 OpenWrt.org
8 EXTRA_COMMANDS
="killclients"
9 EXTRA_HELP
=" killclients Kill ${NAME} processes except servers and yourself"
15 # check if section is enabled (default)
17 config_get_bool enabled
"${section}" enable 1
18 [ "${enabled}" -eq 0 ] && return 1
20 # increase pid file count to handle multiple instances correctly
21 PIDCOUNT
="$(( ${PIDCOUNT} + 1 ))"
23 # prepare parameters (initialise with pid file)
24 local args
="-P /var/run/${NAME}.${PIDCOUNT}.pid"
27 config_get val
"${section}" listen
28 [ -n "${val}" ] && append args
"-p ${val}"
30 config_get val
"${section}" ssh
31 [ -n "${val}" ] && append args
"-s ${val}"
33 config_get val
"${section}" ssl
34 [ -n "${val}" ] && append args
"-l ${val}"
35 # D) timeout (for ssh, then ssl is assumed)
36 config_get val
"${section}" timeout
37 [ -n "${val}" ] && append args
"-t ${val}"
38 # E) verbose parameter
40 config_get_bool verbosed
"${section}" verbose
0
41 [ "${verbosed}" -ne 0 ] && append args
"-v"
43 # execute program and return its exit code
44 [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} started via ${PROG} ${args}"
52 config_foreach sslh_start sslh
60 # killing all server processes
61 for pidfile
in `ls /var/run/${NAME}.*.pid`
63 start-stop-daemon
-K -s KILL
-p "${pidfile}" -n "${NAME}" >/dev
/null
67 [ -z "${pidfile}" ] && echo "${initscript}: no pid files, if you get problems with start then try killclients"
68 [ ${rc} -ne 0 ] && echo "${initscript}: inconsistency in pid files, if you get problems with start then try killclients"
80 # if this script is run from inside a client session, then ignore that session
82 while [ "${pid}" -ne 0 ]
84 # get parent process id
85 pid
=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
86 [ "${pid}" -eq 0 ] && break
88 # check if pid is connected to a client connection
89 # a) get established connection for pid
90 connection
=`netstat -tupn 2>/dev/null | sed "s/[ ]\+/ /g" | grep -e "ESTABLISHED ${pid}/"`
91 [ -z "${connection}" ] && continue
92 # get connection details for foreign address
93 proto
=`echo ${connection} | cut -d ' ' -f 1`
94 address
=`echo ${connection} | cut -d ' ' -f 5`
96 # b) get pid for foreign address, only possible if foreign address is from this machine itself
97 connection
=`netstat -tupn 2>/dev/null | sed "s/[ ]\+/ /g" | grep -e "^${proto}.*${address}.*ESTABLISHED.*/${NAME}"`
98 [ -z "${connection}" ] && continue
99 # check that the local address (field 4) corresponds to the foreign address of the previous connection
100 server
=`echo ${connection} | cut -d ' ' -f 4`
101 [ "${server}" != "${address}" ] && continue
102 # get pid from connection
103 server
=`echo ${connection} | cut -d ' ' -f 7 | cut -d '/' -f 1`
105 # check if client connection
106 ps |
grep -e "^[ ]*${server} " |
grep -e "${PROG}" >/dev
/null
109 append ignore
"${server}"
114 # get all server pids that should be ignored
115 for server
in `cat /var/run/${NAME}.*.pid`
117 append ignore
"${server}"
120 # get all running pids and kill client connections
122 for pid
in `pidof "${NAME}"`
124 # check if correct program
125 ps |
grep -e "^[ ]*${pid} " |
grep -e "${PROG}" >/dev
/null
126 [ $?
-ne 0 ] && continue
128 # check if pid should be ignored (servers, ourself)
130 for server
in ${ignore}
132 if [ "${pid}" == "${server}" ]
138 [ "${skip}" -ne 0 ] && continue
141 echo "${initscript}: Killing ${pid}..."