Skip to content

Commit 1ce4403

Browse files
committed
fix(module): 防止 Sui shell server 孤儿进程残留
1 parent 9ee5529 commit 1ce4403

2 files changed

Lines changed: 76 additions & 8 deletions

File tree

module/src/main/cpp/main/sui_main.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919

2020
#include <cstdlib>
2121
#include <cstring>
22+
#include <csignal>
2223
#include <logging.h>
2324
#include <unistd.h>
2425
#include <sched.h>
2526
#include <app_process.h>
2627
#include <misc.h>
2728
#include <sys/stat.h>
29+
#include <sys/prctl.h>
2830
#include <fcntl.h>
2931
#include <selinux.h>
3032
#include <string>
@@ -283,6 +285,15 @@ static int sui_main(int argc, char** argv) {
283285

284286
if (pid == 0) {
285287
// Child process -> Shell Server
288+
if (prctl(PR_SET_PDEATHSIG, SIGKILL) != 0) {
289+
PLOGE("prctl PR_SET_PDEATHSIG");
290+
exit(EXIT_FAILURE);
291+
}
292+
if (getppid() == 1) {
293+
LOGW("shell server parent already exited");
294+
exit(EXIT_FAILURE);
295+
}
296+
286297
// uid 2000 cannot read /data/adb/modules/zygisk-sui/sui.dex or .so libraries
287298
const char* shell_dir = shell_dir_path.c_str();
288299
ensure_dir(shell_dir, 0755);

template/magisk_module/service.sh

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ print_log() {
2828
log -p i -t "$TAG" "$1"
2929
}
3030

31-
get_sui_pids() {
31+
get_sui_root_pids() {
3232
if command -v pidof >/dev/null 2>&1; then
3333
pidof sui 2>/dev/null
3434
return
@@ -37,8 +37,60 @@ get_sui_pids() {
3737
ps -A 2>/dev/null | awk '$NF == "sui" { print $2 }'
3838
}
3939

40-
is_sui_running() {
41-
[ -n "$(get_sui_pids)" ]
40+
get_sui_shell_pids() {
41+
if command -v pidof >/dev/null 2>&1; then
42+
pidof sui_shell 2>/dev/null
43+
return
44+
fi
45+
46+
ps -A 2>/dev/null | awk '$NF == "sui_shell" { print $2 }'
47+
}
48+
49+
count_pids() {
50+
pids="$1"
51+
if [ -z "$pids" ]; then
52+
echo 0
53+
return
54+
fi
55+
set -- $pids
56+
echo $#
57+
}
58+
59+
collect_sui_state() {
60+
sui_root_pids="$(get_sui_root_pids)"
61+
sui_shell_pids="$(get_sui_shell_pids)"
62+
sui_root_count="$(count_pids "$sui_root_pids")"
63+
sui_shell_count="$(count_pids "$sui_shell_pids")"
64+
}
65+
66+
is_sui_pair_healthy() {
67+
collect_sui_state
68+
[ "$sui_root_count" -eq 1 ] && [ "$sui_shell_count" -eq 1 ]
69+
}
70+
71+
kill_pid_list() {
72+
pids="$1"
73+
signal="$2"
74+
75+
if [ -z "$pids" ]; then
76+
return 0
77+
fi
78+
79+
# shellcheck disable=SC2086
80+
kill "$signal" $pids 2>/dev/null
81+
}
82+
83+
stop_sui_pair() {
84+
collect_sui_state
85+
86+
kill_pid_list "$sui_shell_pids" -TERM
87+
kill_pid_list "$sui_root_pids" -TERM
88+
sleep 1
89+
90+
collect_sui_state
91+
kill_pid_list "$sui_shell_pids" -KILL
92+
kill_pid_list "$sui_root_pids" -KILL
93+
sleep 1
4294
}
4395

4496
read_metadata() {
@@ -210,7 +262,7 @@ backoff_max=60
210262
interval=5
211263

212264
while true; do
213-
if is_sui_running; then
265+
if is_sui_pair_healthy; then
214266
if [ "$metadata_ready" -eq 0 ] && refresh_metadata; then
215267
metadata_ready=1
216268
fi
@@ -219,7 +271,13 @@ while true; do
219271
continue
220272
fi
221273

222-
print_log "Sui daemon is not running, restarting..."
274+
case "$sui_root_count:$sui_shell_count" in
275+
0:0) print_log "Sui root and shell servers are not running, restarting..." ;;
276+
0:*) print_log "Sui root server is not running, restarting pair..." ;;
277+
*:0) print_log "Sui shell server is not running, restarting pair..." ;;
278+
*) print_log "Sui process pair is inconsistent (root=$sui_root_pids shell=$sui_shell_pids), restarting..." ;;
279+
esac
280+
stop_sui_pair
223281
if [ "$metadata_ready" -eq 1 ] || refresh_metadata; then
224282
metadata_ready=1
225283
start_sui
@@ -228,9 +286,8 @@ while true; do
228286
fi
229287
sleep 2
230288

231-
if is_sui_running; then
232-
pids="$(get_sui_pids)"
233-
print_log "Sui daemon is running (pid: $pids)"
289+
if is_sui_pair_healthy; then
290+
print_log "Sui process pair is running (root=$sui_root_pids shell=$sui_shell_pids)"
234291
backoff=1
235292
sleep "$interval"
236293
continue

0 commit comments

Comments
 (0)