1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2009-2010 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 -q -K -s KILL -p "${pidfile}" -n "${NAME}"
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 grep -F -q -e "${PROG}" "/proc/${server}/cmdline" && {
107 append ignore "${server}"
112 # get all server pids that should be ignored
113 for server in `cat /var/run/${NAME}.*.pid`
115 append ignore "${server}"
118 # get all running pids and kill client connections
120 for pid in `pidof "${NAME}"`
122 # check if correct program, otherwise process next pid
123 grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || {
127 # check if pid should be ignored (servers, ourself)
129 for server in ${ignore}
131 if [ "${pid}" == "${server}" ]
137 [ "${skip}" -ne 0 ] && continue
140 echo "${initscript}: Killing ${pid}..."