Skip to content

Commit 325d94c

Browse files
committed
Improved the devd files further setup-nic.py and auto-switch.py.
Added link-up.py to handle the linkup for ethernet with devd.
1 parent 7848325 commit 325d94c

File tree

6 files changed

+107
-12
lines changed

6 files changed

+107
-12
lines changed

NetworkMgr/configuration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ def update_system(self):
474474
defaultrouter_line = f'defaultrouter="{defaultrouter}"\n'
475475
self.remove_rc_conf_line(defaultrouter_line)
476476
restart_card_network(nic)
477+
# sometimes the inet address isn't available immediately after dhcp is enabled.
478+
start_static_network(nic, inet, netmask)
477479
wait_inet(nic)
478480
restart_rounting_and_dhcp(nic)
479481

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def datafilelist(installbase, sourcebase):
2828

2929
networkmgr_share = [
3030
'src/auto-switch.py',
31+
'src/link-up.py',
3132
'src/setup-nic.py'
3233
]
3334

src/auto-switch.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,28 @@
1818

1919
default_nic = Popen(
2020
'netstat -rn | grep default',
21+
stdout=PIPE,
2122
shell=True,
2223
universal_newlines=True
2324
).stdout.read()
2425

26+
nics = Popen(
27+
['ifconfig', '-l', 'ether'],
28+
stdout=PIPE,
29+
close_fds=True,
30+
universal_newlines=True
31+
)
32+
33+
nics_left_over = nics.stdout.read().replace(nic, '').strip()
34+
nic_list = sorted(re.sub(not_nics_regex, '', nics_left_over).strip().split())
35+
2536
# Stop the script if the nic is not valid or not in the default route.
2637
if re.search(not_nics_regex, nic):
2738
exit(0)
2839
elif nic not in default_nic:
2940
exit(0)
41+
elif not nic_list:
42+
exit(0)
3043

3144
nic_ifconfig = Popen(
3245
['ifconfig', nic],
@@ -35,6 +48,13 @@
3548
universal_newlines=True
3649
).stdout.read()
3750

51+
dhcp = Popen(
52+
['sysrc', '-n', f'ifconfig_{nic}'],
53+
stdout=PIPE,
54+
close_fds=True,
55+
universal_newlines=True
56+
).stdout.read()
57+
3858
active_status = (
3959
'status: active' in nic_ifconfig,
4060
'status: associated' in nic_ifconfig
@@ -46,4 +66,23 @@
4666
# or associated.
4767
if not any(active_status):
4868
os.system(f'service netif stop {nic}')
49-
os.system('service routing restart')
69+
if dhcp.strip() == 'DHCP':
70+
for current_nic in nic_list:
71+
output = Popen(
72+
['ifconfig', current_nic],
73+
stdout=PIPE,
74+
close_fds=True,
75+
universal_newlines=True
76+
)
77+
nic_ifconfig = output.stdout.read()
78+
status_types = [
79+
'active',
80+
'associated',
81+
]
82+
found_status = re.search(f"status: ({'|'.join(status_types)})", nic_ifconfig)
83+
found_inet = re.search("inet(\s|6)", nic_ifconfig)
84+
if found_status and found_inet:
85+
os.system(f'service dhclient restart {current_nic}')
86+
break
87+
else:
88+
os.system('service routing restart')

src/link-up.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/local/bin/python3
2+
3+
import os
4+
import re
5+
import sys
6+
from subprocess import Popen, PIPE, run
7+
8+
args = sys.argv
9+
if len(args) != 2:
10+
exit(1)
11+
nic = args[1]
12+
13+
not_nics_regex = "(enc|lo|fwe|fwip|tap|plip|pfsync|pflog|ipfw|tun|sl|faith|" \
14+
"ppp|bridge|wg|wlan)[0-9]+|vm-[a-z]+"
15+
16+
# Stop the script if the nic is not valid.
17+
if re.search(not_nics_regex, nic):
18+
exit(0)
19+
20+
dhcp = Popen(
21+
['sysrc', '-n', f'ifconfig_{nic}'],
22+
stdout=PIPE,
23+
close_fds=True,
24+
universal_newlines=True
25+
).stdout.read()
26+
27+
if os.path.exists(f'/tmp/network-{nic}'):
28+
network = open(f'/tmp/network-{nic}', 'r').read()
29+
if 'attached' in network:
30+
if dhcp.strip() == 'DHCP':
31+
Popen(f'service dhclient quietstart {nic}', shell=True)
32+
else:
33+
Popen(f'service routing restart', shell=True)
34+
with open(f'/tmp/network-{nic}', 'w') as network:
35+
network.writelines(f'linked')
36+
exit(0)
37+
38+
nic_ifconfig = Popen(
39+
['ifconfig', nic],
40+
stdout=PIPE,
41+
close_fds=True,
42+
universal_newlines=True
43+
).stdout.read()
44+
45+
if 'inet ' in nic_ifconfig:
46+
Popen(
47+
f'service routing restart ; '
48+
f'service dhclient restart {nic}',
49+
shell=True
50+
)
51+
else:
52+
Popen(
53+
f'service netif start {nic} ; '
54+
'sleep 1 ; '
55+
f'service routing restart ; '
56+
f'service dhclient restart {nic}',
57+
shell=True
58+
)

src/networkmgr.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ notify 100 {
1010
match "system" "IFNET";
1111
match "type" "LINK_UP";
1212
media-type "ethernet";
13-
action "/usr/local/share/networkmgr/setup-nic.py $subsystem";
13+
action "/usr/local/share/networkmgr/link-up.py $subsystem";
1414
};
1515

1616
notify 100 {

src/setup-nic.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,12 @@ def file_content(paths):
5454
with rc_conf.open('a') as rc:
5555
rc.writelines(f'wlans_{nic}="wlan{wlanNum}"\n')
5656
rc.writelines(f'ifconfig_wlan{wlanNum}="WPA DHCP"\n')
57-
Popen(f'/etc/pccard_ether {nic} startchildren', shell=True)
58-
57+
break
5958
else:
6059
if f'ifconfig_{nic}=' not in rc_conf_content:
6160
with rc_conf.open('a') as rc:
6261
rc.writelines(f'ifconfig_{nic}="DHCP"\n')
63-
Popen('/etc/pccard_ether {nic} startchildren', shell=True)
64-
else:
65-
Popen(
66-
f'service netif start {nic} ; '
67-
f'service dhclient start {nic} ; '
68-
f'service routing restart',
69-
shell=True
70-
)
62+
with open(f'/tmp/network-{nic}', 'w') as network:
63+
network.writelines(f'attached')
64+
65+
Popen(f'/etc/pccard_ether {nic} startchildren', shell=True)

0 commit comments

Comments
 (0)