File tree Expand file tree Collapse file tree 1 file changed +47
-1
lines changed Expand file tree Collapse file tree 1 file changed +47
-1
lines changed Original file line number Diff line number Diff line change 2
2
# use sh rather than bash in order to work correctly on router device which has no bash installed.
3
3
set -e
4
4
5
+ child_pid=
6
+
7
+ func_trap () {
8
+ local signal=" $1 "
9
+ # we can't kill background child process with signal INT
10
+ if [[ " $signal " == INT ]]; then signal=TERM; fi
11
+ if [[ -n " $child_pid " ]]; then
12
+ kill -s " $signal " " $child_pid " || true
13
+ fi
14
+ }
15
+
16
+ trap_with_arg () {
17
+ local func sig
18
+ func=" $1 "
19
+ shift
20
+ for sig ; do
21
+ trap " $func $sig " " $sig "
22
+ done
23
+ }
24
+
25
+ has_builtin () {
26
+ [[ " $( command -v " $1 " ) " == " $1 " ]]
27
+ }
28
+
5
29
execute () {
6
30
local type bin key0 value0 key1 value1
7
31
@@ -130,7 +154,29 @@ execute() {
130
154
fi
131
155
132
156
unset SS_LOCAL_HOST SS_LOCAL_PORT SS_REMOTE_HOST SS_REMOTE_PORT SS_PLUGIN_OPTIONS
133
- " $bin " " $key0 " " $value0 " " $key1 " " $value1 " " $@ "
157
+ if has_builtin wait && has_builtin trap && has_builtin kill ; then
158
+ " $bin " " $key0 " " $value0 " " $key1 " " $value1 " " $@ " &
159
+ child_pid=$!
160
+ if [[ -z " $child_pid " ]]; then
161
+ echo Unknown error occur, cannot get process id of child process. >&2
162
+ exit 1
163
+ fi
164
+ # Send all signal to kcptun
165
+ trap_with_arg func_trap HUP INT QUIT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM
166
+ local wait_result=0
167
+ while true ; do
168
+ local value=0
169
+ wait " $child_pid " 2> /dev/null || value=$?
170
+ # 127 means this pid is not child process of our shell.
171
+ if [[ " $value " == 127 ]]; then break ; fi
172
+ wait_result=" $value "
173
+ if [[ " $value " == 0 ]]; then break ; fi
174
+ done
175
+ child_pid=
176
+ return $wait_result
177
+ else
178
+ " $bin " " $key0 " " $value0 " " $key1 " " $value1 " " $@ "
179
+ fi
134
180
}
135
181
136
182
findBinary
You can’t perform that action at this time.
0 commit comments