-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathtlp-rdw-nm.in
233 lines (192 loc) · 7.32 KB
/
tlp-rdw-nm.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/sh
# tlp-rdw - network manager dispatcher hook:
# enable/disable radios on ifup/ifdown
#
# Copyright (c) 2025 Thomas Koch <linrunner at gmx.net> and others.
# SPDX-License-Identifier: GPL-2.0-or-later
# --- Source libraries
for lib in @TLP_TLIB@/tlp-func-base @TLP_FLIB@/25-tlp-func-rf @TLP_FLIB@/30-tlp-func-rf-sw; do
# shellcheck disable=SC1090
. "$lib" || exit 70
done
# --- Functions
check_switch_lock() { # switch listed radio devices
# and time-lock them afterwards if actually switched
# $1: device type where the event originated -- do nothing if its time-locked
# $2: list of device types to switch
# $3: on/off
local sw_rc type
# quit if the originating *radio* device is time-locked (not LAN)
[ "$1" != "LAN" ] && check_timed_lock "${RDW_NM_LOCK}_$1" && return 1
for type in $2; do
if [ -n "$type" ] && [ "$type" != "$1" ]; then
# device type is valid and not the originating one
# --> do switch with state change lock
device_switch "$type" "$3" "${RDW_NM_LOCK}_${type}" "$RDW_NM_LOCKTIME"; sw_rc=$?
if [ "$sw_rc" = "4" ]; then
# switch failed, NetworkManager may be "asleep" -> schedule repeat:
# open a detached subshell, wait 2 secs and respawn ourselves,
# no more than two repetitions.
case "$repeats" in
"") repeats="2" ;;
2) repeats="1" ;;
*) repeats="" ;;
esac
if [ -n "$repeats" ]; then
echo_debug "nm" "+++ tlp_rdw_nm(${iface}).${action}.nm_seems_asleep: repeats=$repeats"
( sleep 2; $0 "$iface" "$action" "$repeats" < /dev/null > /dev/null ) &
do_exit 0
fi
fi
fi
done
return 0
}
save_iface_type () { # save interface type -- $1: interface; $2: type
# rc: 0=saved/1=error
[ -d "$NETD/$1" ] && { printf '%s\n' "$2" > "$RUNDIR/${1}.itype"; } 2> /dev/null
return $?
}
get_iface_type () { # get saved interface type -- $1: interface
# rc: 0=saved state found/1=not found
# retval: $itype
local rc
itype=$(read_sysf "$RUNDIR/${1}.itype"); rc=$?
rm -f "$RUNDIR/${1}.itype"
return $rc
}
echo_env () {
# record environment
if [ "$X_USB_ENV_TRACE" = "1" ]; then
echo_debug "nm" "tlp_rdw_nm.env: $(printenv)"
fi
}
# --- MAIN
# shellcheck disable=SC2034
_bgtask=1
# read configuration: quit on error, trace allowed
read_config 1 0
# quit if TLP disabled
check_tlp_enabled || do_exit 0
# quit if RDW disabled
check_run_flag "$RDW_KILL" && do_exit 0
add_sbin2path
# get args
iface="$1"
action="$2"
repeats="$3"
itype=""
case "$action" in
up|down) # interface up/down
# quit for invalid interfaces
if [ -z "$iface" ] || [ "$iface" = "none" ]; then
echo_debug "nm" "tlp_rdw_nm($iface).${action}.no_interface"
echo_env
do_exit 0
fi
# quit for virtual interfaces (up action)
if [ "$action" = "up" ] && readlink "$NETD/${iface}" | grep -q '/virtual/'; then
# save type for down action where $NETD/$iface won't be there anymore
save_iface_type "$iface" virtual
echo_debug "nm" "tlp_rdw_nm($iface).${action}.ignore_virtual"
echo_env
do_exit 0
fi
# get saved interface type (down action)
if [ "$action" = "down" ]; then
get_iface_type "$iface"
# quit for virtual interfaces
if [ "$itype" = "virtual" ]; then
echo_debug "nm" "tlp_rdw_nm($iface).${action}.ignore_virtual"
do_exit 0
fi
fi
echo_debug "nm" "+++ tlp_rdw_nm($iface).$action: repeats=$repeats"
echo_env
# shellcheck disable=SC2154
if [ -n "$_addpath" ]; then
# shellcheck disable=SC2154
echo_debug "path" "PATH=${_oldpath}[${_addpath}]"
else
# shellcheck disable=SC2154
echo_debug "path" "PATH=${_oldpath}"
fi
# determine interface type
if [ -n "$itype" ]; then
# saved type available (down action)
echo_debug "nm" "tlp_rdw_nm($iface).${action}: type=$itype [saved]"
elif cmd_exists "$NMCLI"; then
# no saved type but nmcli is available
# --> check if nmcli dev output matches interface
itype="$($NMCLI dev | awk '$1 ~ /^'"$iface"'$/ { print $2; }')"
if [ -z "$itype" ]; then
# iface is not found in nmcli dev output: many WWAN devices have
# different devices for control and the actual network connection
# --> check if interface matches a WWAN device
get_wwan_ifaces
# shellcheck disable=SC2154
if wordinlist "$iface" "$_wanifaces"; then
itype="wwan"
else
# fallback:
# if interface type detection with nmcli failed, then try to
# deduct it using interface name: it can happen if e.g.
# usb network card is unplugged
case "$iface" in
en* | eth*)
itype="ethernet"
;;
wl*)
itype="wifi"
;;
ww*)
itype="wwan"
;;
*)
itype="unknown"
;;
esac
fi
fi
# save interface type (up action)
[ "$action" = "up" ] && save_iface_type "$iface" "$itype"
echo_debug "nm" "tlp_rdw_nm($iface).${action}: type=$itype [nmcli]"
else
# nmcli is not available
itype="unknown"
echo_debug "nm" "tlp_rdw_nm($iface).${action}: type=$itype [none]"
fi
case "$action" in
up) # interface up, disable configured interfaces
case $itype in
*ethernet)
check_switch_lock LAN "$DEVICES_TO_DISABLE_ON_LAN_CONNECT" off
;;
*wireless|wifi)
check_switch_lock wifi "$DEVICES_TO_DISABLE_ON_WIFI_CONNECT" off
;;
gsm|wwan)
check_switch_lock wwan "$DEVICES_TO_DISABLE_ON_WWAN_CONNECT" off
;;
esac
;; # up
down) # interface down, enable configured interfaces
case $itype in
*ethernet)
check_switch_lock LAN "$DEVICES_TO_ENABLE_ON_LAN_DISCONNECT" on
;;
*wireless|wifi)
check_switch_lock wifi "$DEVICES_TO_ENABLE_ON_WIFI_DISCONNECT" on
;;
gsm|wwan)
check_switch_lock wwan "$DEVICES_TO_ENABLE_ON_WWAN_DISCONNECT" on
;;
esac
;; # down
esac
;; # up/down
*)
# other calls: do nothing
;;
esac # action
do_exit 0