judge netns

This commit is contained in:
garywill 2024-02-25 10:00:00 +08:00
parent 1def3deb96
commit 8b57dcef1a
1 changed files with 16 additions and 5 deletions

View File

@ -854,7 +854,7 @@ pid_watchdog() {
} }
#======== #========
function get_pid_by_dbus_name() { get_pid_by_dbus_name() {
local DBUS_NAME="$1" local DBUS_NAME="$1"
local pid r local pid r
@ -866,6 +866,12 @@ function get_pid_by_dbus_name() {
echo "$pid" echo "$pid"
return $r return $r
} }
is_same_netns() {
local pid2="$1"
[[ ! -f /proc/$$/ns/net ]] && return 0 # no netns feature. treat as same
[[ "$(readlink /proc/$$/ns/net)" == "$(readlink /proc/$pid2/ns/net)" ]] && return 0
return 1
}
#----------------- #-----------------
@ -873,13 +879,16 @@ function get_pid_by_dbus_name() {
is_nm_running() { is_nm_running() {
NM_PID="$(get_pid_by_dbus_name "org.freedesktop.NetworkManager")" NM_PID="$(get_pid_by_dbus_name "org.freedesktop.NetworkManager")"
[[ ! -n "$NM_PID" ]] && return 1 [[ ! -n "$NM_PID" ]] && return 1 # not running
if (which nmcli >/dev/null 2>&1 ) && (nmcli -t -f RUNNING g 2>&1 | grep -E '^running$' >/dev/null 2>&1 ) ; then if (which nmcli >/dev/null 2>&1 ) && (nmcli -t -f RUNNING g 2>&1 | grep -E '^running$' >/dev/null 2>&1 ) ; then
if is_same_netns "$NM_PID"; then
return 0 return 0
else
return 1
fi fi
fi
NM_PID= # cancel value if treat as not running
return 1 # not running
} }
nm_knows() { nm_knows() {
@ -915,6 +924,8 @@ nm_restore_manage() {
sleep 0.5 sleep 0.5
fi fi
} }
#-------
#========= #=========
check_iptables() check_iptables()
{ {