mirror of
https://github.com/2dust/v2rayN.git
synced 2025-10-26 10:14:42 +00:00
Fix TUN mode cleanup on Linux/macOS (#8202)
This commit is contained in:
parent
b2d6282755
commit
22c233f0cd
3 changed files with 30 additions and 4 deletions
|
|
@ -34,7 +34,7 @@ public class CoreAdminManager
|
||||||
StringBuilder sb = new();
|
StringBuilder sb = new();
|
||||||
sb.AppendLine("#!/bin/bash");
|
sb.AppendLine("#!/bin/bash");
|
||||||
var cmdLine = $"{fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetBinConfigPath(configPath).AppendQuotes())}";
|
var cmdLine = $"{fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetBinConfigPath(configPath).AppendQuotes())}";
|
||||||
sb.AppendLine($"sudo -S {cmdLine}");
|
sb.AppendLine($"exec sudo -S -- {cmdLine}");
|
||||||
var shFilePath = await FileManager.CreateLinuxShellFile("run_as_sudo.sh", sb.ToString(), true);
|
var shFilePath = await FileManager.CreateLinuxShellFile("run_as_sudo.sh", sb.ToString(), true);
|
||||||
|
|
||||||
var procService = new ProcessService(
|
var procService = new ProcessService(
|
||||||
|
|
|
||||||
|
|
@ -28,15 +28,15 @@ fi
|
||||||
kill_children() {
|
kill_children() {
|
||||||
local parent=$1
|
local parent=$1
|
||||||
local children=$(ps -o pid --no-headers --ppid "$parent")
|
local children=$(ps -o pid --no-headers --ppid "$parent")
|
||||||
|
|
||||||
# Output information about processes being terminated
|
# Output information about processes being terminated
|
||||||
echo "Processing children of PID: $parent..."
|
echo "Processing children of PID: $parent..."
|
||||||
|
|
||||||
# Process each child
|
# Process each child
|
||||||
for child in $children; do
|
for child in $children; do
|
||||||
# Recursively find and kill child's children first
|
# Recursively find and kill child's children first
|
||||||
kill_children "$child"
|
kill_children "$child"
|
||||||
|
|
||||||
# Force kill the child process
|
# Force kill the child process
|
||||||
echo "Terminating child process: $child"
|
echo "Terminating child process: $child"
|
||||||
kill -9 "$child" 2>/dev/null || true
|
kill -9 "$child" 2>/dev/null || true
|
||||||
|
|
@ -47,6 +47,18 @@ echo "============================================"
|
||||||
echo "Starting termination of process $PID and all its children"
|
echo "Starting termination of process $PID and all its children"
|
||||||
echo "============================================"
|
echo "============================================"
|
||||||
|
|
||||||
|
# Try graceful termination first
|
||||||
|
echo "Attempting graceful termination (SIGTERM) of PID: $PID"
|
||||||
|
kill -15 "$PID" 2>/dev/null || true
|
||||||
|
sleep 1
|
||||||
|
# If still running, fall back to kill_children
|
||||||
|
if ps -p $PID > /dev/null; then
|
||||||
|
echo "Process $PID did not exit after SIGTERM; proceeding with forced termination of its children and itself"
|
||||||
|
else
|
||||||
|
echo "Process $PID exited cleanly after SIGTERM"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
# Find and kill all child processes
|
# Find and kill all child processes
|
||||||
kill_children "$PID"
|
kill_children "$PID"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,20 @@ echo "============================================"
|
||||||
echo "Starting termination of process $PID and all its descendants"
|
echo "Starting termination of process $PID and all its descendants"
|
||||||
echo "============================================"
|
echo "============================================"
|
||||||
|
|
||||||
|
# Try graceful termination first
|
||||||
|
echo "Attempting graceful termination (SIGTERM) of PID: $PID"
|
||||||
|
kill -15 "$PID" 2>/dev/null || true
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
# If still running, fall back to kill_descendants
|
||||||
|
# Use the macOS-native 'kill -0' check
|
||||||
|
if kill -0 $PID 2>/dev/null; then
|
||||||
|
echo "Process $PID did not exit after SIGTERM; proceeding with forced termination of its descendants and itself"
|
||||||
|
else
|
||||||
|
echo "Process $PID exited cleanly after SIGTERM"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
# Find and kill all descendant processes
|
# Find and kill all descendant processes
|
||||||
kill_descendants "$PID"
|
kill_descendants "$PID"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue