| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of the Falcon Player (FPP) and is Copyright (C) | ||
| 3 | * 2013-2024 by the Falcon Player Developers. | ||
| 4 | * | ||
| 5 | * The Falcon Player (FPP) is free software, and is covered under | ||
| 6 | * multiple Open Source licenses. Please see the included 'LICENSES' | ||
| 7 | * file for descriptions of what files are covered by each license. | ||
| 8 | * | ||
| 9 | * This source file is covered under the LGPL v2.1 as described in the | ||
| 10 | * included LICENSE.LGPL file. | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include <arpa/inet.h> | ||
| 14 | #include <atomic> | ||
| 15 | #include <chrono> | ||
| 16 | #include <cstdio> | ||
| 17 | #include <ctime> | ||
| 18 | #include <filesystem> | ||
| 19 | #include <ifaddrs.h> | ||
| 20 | #include <list> | ||
| 21 | #include <map> | ||
| 22 | #include <net/if.h> | ||
| 23 | #include <netdb.h> | ||
| 24 | #include <netinet/in.h> | ||
| 25 | #include <set> | ||
| 26 | #include <sys/socket.h> | ||
| 27 | #include <sys/stat.h> | ||
| 28 | #include <sys/time.h> | ||
| 29 | #include <systemd/sd-daemon.h> | ||
| 30 | #include <thread> | ||
| 31 | #include <unistd.h> | ||
| 32 | #include <vector> | ||
| 33 | |||
| 34 | #include "FPPINIT.h" | ||
| 35 | |||
| 36 | // One single-quoted shell word: nothing inside single quotes is expanded. | ||
| 37 | // Every name below that reaches a shell goes through this, as well as the | ||
| 38 | // isShellSafeWord() checks the settings among them (TetherInterface, | ||
| 39 | // WifiRegulatoryDomain) already get: this runs as root, and one missed | ||
| 40 | // check should not be all that stands between a setting and the shell. | ||
| 41 | 822 | static std::string ShellSingleQuote(const std::string& v) { | |
| 42 |
1/2✓ Branch 0 taken 822 times.
✗ Branch 1 not taken.
|
822 | std::string out = "'"; |
| 43 |
2/2✓ Branch 0 taken 4908 times.
✓ Branch 1 taken 822 times.
|
5730 | for (char ch : v) { |
| 44 |
5/16✗ Branch 0 not taken.
✓ Branch 1 taken 4908 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4908 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4908 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4908 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 4908 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
14724 | out += (ch == '\'') ? std::string("'\\''") : std::string(1, ch); |
| 45 | } | ||
| 46 |
1/2✓ Branch 0 taken 822 times.
✗ Branch 1 not taken.
|
822 | out += "'"; |
| 47 | 822 | return out; | |
| 48 | ✗ | } | |
| 49 | |||
| 50 | 1542 | std::string FindTetherWIFIAdapater() { | |
| 51 | 1542 | std::string ret; | |
| 52 |
9/18✓ Branch 0 taken 1542 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1542 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 106 times.
✓ Branch 5 taken 1436 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 106 times.
✓ Branch 8 taken 1542 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1542 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 1436 times.
✓ Branch 13 taken 106 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
|
4626 | if (!getRawSetting("TetherInterface", ret) || ret.empty()) { |
| 53 | // see if we can find a non-associated interface | ||
| 54 |
5/8✓ Branch 0 taken 1436 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1436 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3202 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3202 times.
✓ Branch 7 taken 1436 times.
|
4638 | for (const auto& entry : std::filesystem::directory_iterator("/sys/class/net/")) { |
| 55 |
2/6✓ Branch 0 taken 3202 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3202 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
3202 | std::string dev = entry.path().filename(); |
| 56 |
4/6✓ Branch 0 taken 3202 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3202 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 613 times.
✓ Branch 5 taken 2589 times.
|
6404 | if (startsWith(dev, "wl")) { |
| 57 |
4/8✓ Branch 0 taken 613 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 613 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 613 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 613 times.
✗ Branch 7 not taken.
|
613 | std::string output = execAndReturn("/usr/sbin/iw dev " + ShellSingleQuote(dev) + " link"); |
| 58 |
3/6✓ Branch 0 taken 613 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 613 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 613 times.
|
1226 | if (contains(output, "Not connected")) { |
| 59 | ✗ | return dev; | |
| 60 | } | ||
| 61 |
1/2✓ Branch 0 taken 613 times.
✗ Branch 1 not taken.
|
613 | } |
| 62 |
3/6✓ Branch 0 taken 1436 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1436 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1436 times.
✗ Branch 5 not taken.
|
4638 | } |
| 63 | } | ||
| 64 | // Used unquoted in root commands; the UI only offers interface names. | ||
| 65 |
6/8✓ Branch 0 taken 106 times.
✓ Branch 1 taken 1436 times.
✓ Branch 2 taken 106 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 106 times.
✓ Branch 6 taken 1436 times.
✓ Branch 7 taken 106 times.
|
1542 | if (ret.empty() || !isShellSafeWord(ret)) { |
| 66 |
1/2✓ Branch 0 taken 1436 times.
✗ Branch 1 not taken.
|
1436 | ret = "wlan0"; |
| 67 | } | ||
| 68 | 1542 | return ret; | |
| 69 | 1542 | } | |
| 70 | |||
| 71 | 171 | static int getIntFromMap(const std::map<std::string, std::string>& m, const std::string& setting, int def) { | |
| 72 |
3/4✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 161 times.
|
171 | if (m.find(setting) != m.end()) { |
| 73 |
2/4✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 | std::string v = m.at(setting); |
| 74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (v.empty()) { |
| 75 | ✗ | return def; | |
| 76 | } | ||
| 77 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | return std::stoi(v); |
| 78 | 10 | } | |
| 79 | 161 | return def; | |
| 80 | } | ||
| 81 | // Boolean flags in interface.<if> are written by the UI as "1" (checked) or | ||
| 82 | // "" (unchecked); treat an API/hand-written "0"/"false" as off too rather than | ||
| 83 | // as "non-empty, therefore on". | ||
| 84 | 71 | static bool settingEnabled(const std::map<std::string, std::string>& m, const std::string& setting) { | |
| 85 |
1/2✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
|
71 | auto it = m.find(setting); |
| 86 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 21 times.
|
71 | if (it == m.end()) { |
| 87 | 50 | return false; | |
| 88 | } | ||
| 89 |
8/10✓ Branch 0 taken 19 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 10 times.
✓ Branch 9 taken 4 times.
|
21 | return !it->second.empty() && it->second != "0" && it->second != "false"; |
| 90 | } | ||
| 91 | // One wpa_supplicant network={} block. The network page takes a key as an | ||
| 92 | // 8-63 character passphrase or a 64 hex digit raw PSK; wpa_supplicant reads | ||
| 93 | // the raw PSK only unquoted (quoted, it is an over-long passphrase and the | ||
| 94 | // whole config fails to parse). SAE has no raw form, so such a key is WPA2 | ||
| 95 | // only. No key at all is an open network. | ||
| 96 | 33 | static std::string wpaNetworkBlock(const std::string& ssid, const std::string& key, bool wpa3, bool hidden, int priority) { | |
| 97 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
33 | bool rawPSK = key.size() == 64 && key.find_first_not_of("0123456789abcdefABCDEF") == std::string::npos; |
| 98 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
|
33 | std::string block = "\nnetwork={\n ssid=\"" + ssid + "\"\n"; |
| 99 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 31 times.
|
33 | if (key.empty()) { |
| 100 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | block += " key_mgmt=NONE\n"; |
| 101 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 27 times.
|
31 | } else if (rawPSK) { |
| 102 |
3/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
|
4 | block += " psk=" + key + "\n key_mgmt=WPA-PSK\n"; |
| 103 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 19 times.
|
27 | } else if (wpa3) { |
| 104 |
5/10✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
|
8 | block += " psk=\"" + key + "\"\n key_mgmt=SAE WPA-PSK\n sae_password=\"" + key + "\"\n"; |
| 105 | } else { | ||
| 106 |
3/6✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
|
19 | block += " psk=\"" + key + "\"\n key_mgmt=WPA-PSK\n"; |
| 107 | } | ||
| 108 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if (hidden) { |
| 109 | ✗ | block += " scan_ssid=1\n"; | |
| 110 | } | ||
| 111 |
3/6✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
|
66 | return block + " priority=" + std::to_string(priority) + "\n}\n\n"; |
| 112 | 33 | } | |
| 113 | 7 | static std::string netmaskToSubnet(const std::string& nm) { | |
| 114 | const std::map<std::string, std::string> NETMASK2SUBNET = { | ||
| 115 | { "255.255.255.252", "30" }, | ||
| 116 | { "255.255.255.248", "29" }, | ||
| 117 | { "255.255.255.240", "28" }, | ||
| 118 | { "255.255.255.224", "27" }, | ||
| 119 | { "255.255.255.192", "26" }, | ||
| 120 | { "255.255.255.128", "25" }, | ||
| 121 | { "255.255.255.0", "24" }, | ||
| 122 | { "255.255.254.0", "23" }, | ||
| 123 | { "255.255.252.0", "22" }, | ||
| 124 | { "255.255.248.0", "21" }, | ||
| 125 | { "255.255.240.0", "20" }, | ||
| 126 | { "255.255.224.0", "19" }, | ||
| 127 | { "255.255.192.0", "18" }, | ||
| 128 | { "255.255.128.0", "17" }, | ||
| 129 | { "255.255.0.0", "16" } | ||
| 130 |
3/6✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 105 times.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
119 | }; |
| 131 |
2/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 | if (NETMASK2SUBNET.contains(nm)) { |
| 132 |
2/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 | return NETMASK2SUBNET.at(nm); |
| 133 | } | ||
| 134 | ✗ | return "24"; | |
| 135 |
15/34✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 7 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 7 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 7 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 7 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 7 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 7 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 7 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 7 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 7 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 7 times.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
|
14 | } |
| 136 | |||
| 137 | 80 | static bool compareAndCopyFiles(std::map<std::string, std::string> filesNeeded) { | |
| 138 | 80 | bool changed = false; | |
| 139 |
2/2✓ Branch 0 taken 59 times.
✓ Branch 1 taken 80 times.
|
139 | for (auto& nf : filesNeeded) { |
| 140 |
2/4✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 59 times.
|
59 | if (FileExists(nf.first)) { |
| 141 | ✗ | std::string c = GetFileContents(nf.first); | |
| 142 | ✗ | if (c != nf.second) { | |
| 143 | ✗ | PutFileContents(nf.first, nf.second); | |
| 144 | ✗ | changed = true; | |
| 145 | } | ||
| 146 | ✗ | } else { | |
| 147 |
1/2✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
|
59 | PutFileContents(nf.first, nf.second); |
| 148 | 59 | changed = true; | |
| 149 | } | ||
| 150 | } | ||
| 151 | 80 | return changed; | |
| 152 | } | ||
| 153 | // A value for a WIFI: QR code field, where \ ; , : and " are escaped. | ||
| 154 | 26 | static std::string WifiQREscape(const std::string& v) { | |
| 155 | 26 | std::string out; | |
| 156 |
2/2✓ Branch 0 taken 208 times.
✓ Branch 1 taken 26 times.
|
234 | for (char ch : v) { |
| 157 |
5/10✓ Branch 0 taken 208 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 208 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 208 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 208 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 208 times.
|
208 | if (ch == '\\' || ch == ';' || ch == ',' || ch == ':' || ch == '"') { |
| 158 | ✗ | out += '\\'; | |
| 159 | } | ||
| 160 |
1/2✓ Branch 0 taken 208 times.
✗ Branch 1 not taken.
|
208 | out += ch; |
| 161 | } | ||
| 162 | 26 | return out; | |
| 163 | ✗ | } | |
| 164 | 13 | static std::string CreateHostAPDConfig(const std::string& interface) { | |
| 165 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
26 | std::string ssid = "FPP"; |
| 166 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
26 | std::string psk = "Christmas"; |
| 167 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
26 | std::string WifiRegulatoryDomain = "US"; |
| 168 |
2/4✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
26 | getRawSetting("WifiRegulatoryDomain", WifiRegulatoryDomain); |
| 169 |
2/4✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
26 | getRawSetting("TetherSSID", ssid); |
| 170 |
2/4✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 | getRawSetting("TetherPSK", psk); |
| 171 | |||
| 172 | 13 | std::string content; | |
| 173 |
5/10✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
|
13 | content.append("interface=").append(interface).append("\ndriver=nl80211\nwpa_passphrase=").append(psk).append("\nssid="); |
| 174 |
3/6✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
13 | content.append(ssid).append("\ncountry_code=").append(WifiRegulatoryDomain); |
| 175 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | content.append("\nhw_mode=g\nchannel=11\nmacaddr_acl=0\nauth_algs=3\nwpa=2\n"); |
| 176 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | content.append("wpa_key_mgmt=WPA-PSK\nwpa_pairwise=TKIP\nrsn_pairwise=CCMP\nwmm_enabled=0\nignore_broadcast_ssid=0\nwpa_group_rekey=3600\nwpa_gmk_rekey=86400\n"); |
| 177 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | content.append("reassociation_deadline=3000\npmk_r1_push=1\nft_over_ds=0\nbss_transition=1\n"); |
| 178 |
2/4✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 | if (FileExists("/usr/bin/qrencode")) { |
| 179 | // The SSID and PSK are single-quoted for the shell: inside the double | ||
| 180 | // quotes they used to sit in, "$(...)" in either setting ran as root. | ||
| 181 |
7/14✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 13 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 13 times.
✗ Branch 13 not taken.
|
13 | std::string payload = ShellSingleQuote("WIFI:T:WPA;S:" + WifiQREscape(ssid) + ";P:" + WifiQREscape(psk) + ";;"); |
| 182 | // Low error correction MAY result in a QR that can be pixel doubled which generally works better for phones to pick up | ||
| 183 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | std::string cmd = "/usr/bin/qrencode -t ASCII -m 0 -s 6 -l L -o "; |
| 184 |
5/10✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
|
13 | cmd.append(ShellSingleQuote(FPP_MEDIA_DIR + "/tmp/wifi-L.ascii")).append(" ").append(payload); |
| 185 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | exec(cmd); |
| 186 | // Generate a High error correction as well. If we cannot pixel double, this will give more info to the phone/device | ||
| 187 | |||
| 188 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | cmd = "/usr/bin/qrencode -t ASCII -m 3 -s 6 -l H -o "; |
| 189 |
5/10✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
|
13 | cmd.append(ShellSingleQuote(FPP_MEDIA_DIR + "/tmp/wifi-H.ascii")).append(" ").append(payload); |
| 190 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | exec(cmd); |
| 191 | 13 | } | |
| 192 | |||
| 193 | 13 | return content; | |
| 194 | 13 | } | |
| 195 | // The [DHCPServer] block for the tethering AP. The pool comes from | ||
| 196 | // config/interface.<iface> (the DHCP Pool Offset / Size fields on the Network | ||
| 197 | // page) whenever that file exists: maybeEnableTethering() rewrites this | ||
| 198 | // .network file *after* setupNetwork() has already generated it from the | ||
| 199 | // interface config, so hardcoding the pool here silently reverted whatever the | ||
| 200 | // user had configured -- a box set to offset 100 kept handing out addresses | ||
| 201 | // from .10 because the second write won. 10/100 remains the default for a | ||
| 202 | // board that has no interface config at all (fresh install, no /config dir). | ||
| 203 | 11 | static void AppendTetherDHCPServerSection(std::string& content, const std::string& interface) { | |
| 204 | 11 | int offset = 10; | |
| 205 | 11 | int poolSize = 100; | |
| 206 |
2/4✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
11 | const std::string interfaceConfigFile = FPP_MEDIA_DIR + "/config/interface." + interface; |
| 207 |
3/4✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 8 times.
|
11 | if (FileExists(interfaceConfigFile)) { |
| 208 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | auto interfaceSettings = loadSettingsFile(interfaceConfigFile); |
| 209 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
6 | offset = getIntFromMap(interfaceSettings, "DHCPOFFSET", offset); |
| 210 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | poolSize = getIntFromMap(interfaceSettings, "DHCPPOOLSIZE", poolSize); |
| 211 | 3 | } | |
| 212 | // PersistLeases=yes is already the default on Debian 13/trixie (systemd | ||
| 213 | // >= 256, via systemd-networkd-persistent-storage.service); set explicitly | ||
| 214 | // here for clarity and so behavior doesn't silently depend on an OS-level | ||
| 215 | // default we don't control. Harmlessly ignored as an unknown key on older | ||
| 216 | // systemd (verified: an unrecognized [DHCPServer] key logs a warning but | ||
| 217 | // doesn't break parsing). | ||
| 218 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | content.append("[DHCPServer]\n" |
| 219 | "PersistLeases=yes\n" | ||
| 220 | "PoolOffset=") | ||
| 221 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | .append(std::to_string(offset)) |
| 222 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | .append("\nPoolSize=") |
| 223 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
22 | .append(std::to_string(poolSize)) |
| 224 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | .append("\nEmitDNS=no\n\n"); |
| 225 |
5/8✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 10 times.
|
11 | if (FileExists(FPP_MEDIA_DIR + "/config/leases." + interface)) { |
| 226 |
4/8✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
|
1 | content.append(GetFileContents(FPP_MEDIA_DIR + "/config/leases." + interface)); |
| 227 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | content.append("\n"); |
| 228 | } | ||
| 229 | 11 | } | |
| 230 | 101 | static void unblockWifi() { | |
| 231 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 94 times.
|
101 | if (FileExists("/usr/sbin/rfkill")) { |
| 232 |
2/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
14 | exec("/usr/sbin/rfkill unblock wifi"); |
| 233 |
2/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
14 | exec("/usr/sbin/rfkill unblock all"); |
| 234 | } | ||
| 235 | 101 | } | |
| 236 | |||
| 237 | // Consumes the marker resetConfig.php drops for a "network" area reset. Must | ||
| 238 | // only be called from the boot "start" path (FPPINIT.cpp), NOT from | ||
| 239 | // setupNetwork() -- that function is also invoked live via `fppinit | ||
| 240 | // setupNetwork <iface>` (network.php's interface-save path) while | ||
| 241 | // systemd-networkd is already running, which would reintroduce the exact | ||
| 242 | // live-daemon race this is meant to avoid. Boot's "start" action runs before | ||
| 243 | // systemd-networkd.service is (re)started for this boot -- fppinit.service is | ||
| 244 | // ordered Before= it -- so calling this only from there is race-free by | ||
| 245 | // construction: the daemon that owns /var/lib/systemd/network/dhcp-server-lease/ | ||
| 246 | // isn't running yet. | ||
| 247 | // | ||
| 248 | // TODO(FPP11): this is one of several ad-hoc "do this on next boot" marker | ||
| 249 | // files scattered around FPP (see also /fpp_kiosk, fpp_expand_rootfs, | ||
| 250 | // /fppos_upgraded). Standardize these on a single settings-key convention | ||
| 251 | // instead of bare marker files. | ||
| 252 | 39 | void consumePendingDhcpLeaseReset() { | |
| 253 |
1/2✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
|
39 | std::string marker = FPP_MEDIA_DIR + "/config/dhcpLeaseResetPending"; |
| 254 |
3/4✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 1 times.
|
39 | if (!FileExists(marker)) { |
| 255 | 38 | return; | |
| 256 | } | ||
| 257 | // One JSON file per interface (not a per-lease directory), e.g. | ||
| 258 | // /var/lib/systemd/network/dhcp-server-lease/eth0 -- confirmed against a | ||
| 259 | // live device (systemd 257). Wipe all of them. | ||
| 260 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (DirectoryExists("/var/lib/systemd/network/dhcp-server-lease")) { |
| 261 |
5/8✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
|
3 | for (const auto& entry : std::filesystem::directory_iterator("/var/lib/systemd/network/dhcp-server-lease")) { |
| 262 | 2 | std::error_code ec; | |
| 263 | 2 | std::filesystem::remove(entry.path(), ec); | |
| 264 | 1 | } | |
| 265 | } | ||
| 266 | 1 | unlink(marker.c_str()); | |
| 267 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 38 times.
|
39 | } |
| 268 | |||
| 269 | // hostapd is up or on its way up or down. `systemctl is-active` prints one | ||
| 270 | // word; "inactive" contains "active", so compare the whole word. | ||
| 271 | 34 | static bool hostapdRunning() { | |
| 272 |
2/4✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
|
34 | std::string state = execAndReturn("/usr/bin/systemctl is-active hostapd"); |
| 273 |
1/2✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
|
34 | TrimWhiteSpace(state); |
| 274 |
9/16✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 30 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 30 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 30 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 30 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 30 times.
|
68 | return state == "active" || state == "activating" || state == "deactivating" || state == "reloading"; |
| 275 | 34 | } | |
| 276 | |||
| 277 | 80 | void setupNetwork(bool fullReload) { | |
| 278 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | unblockWifi(); |
| 279 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | auto dnsSettings = loadSettingsFile(FPP_MEDIA_DIR + "/config/dns"); |
| 280 | |||
| 281 | // Load global gateway configuration with migration support | ||
| 282 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | auto gatewaySettings = loadSettingsFile(FPP_MEDIA_DIR + "/config/gateway"); |
| 283 |
3/6✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
|
80 | std::string globalGateway = gatewaySettings["GATEWAY"]; |
| 284 | |||
| 285 | // Migration: If no global gateway exists, check for old per-interface gateways | ||
| 286 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | if (globalGateway.empty()) { |
| 287 | 80 | std::error_code cfgEc; | |
| 288 |
6/10✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 115 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 115 times.
✓ Branch 9 taken 80 times.
|
195 | for (const auto& entry : std::filesystem::directory_iterator(FPP_MEDIA_DIR + "/config", cfgEc)) { |
| 289 |
2/4✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✗ Branch 3 not taken.
|
115 | std::string filename = entry.path().filename(); |
| 290 |
4/6✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✓ Branch 5 taken 82 times.
|
230 | if (startsWith(filename, "interface.")) { |
| 291 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
|
33 | auto interfaceSettings = loadSettingsFile(entry.path()); |
| 292 |
3/6✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
|
33 | std::string oldGateway = interfaceSettings["GATEWAY"]; |
| 293 |
2/8✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 33 times.
|
33 | if (!oldGateway.empty() && oldGateway != "\"\"") { |
| 294 | // Found an old gateway setting - migrate it to global | ||
| 295 | ✗ | printf("FPP - Migrating gateway %s from %s to global configuration\n", | |
| 296 | oldGateway.c_str(), filename.c_str()); | ||
| 297 | |||
| 298 | // Save to global gateway file | ||
| 299 | ✗ | std::string gatewayContent = "GATEWAY=\"" + oldGateway + "\"\n"; | |
| 300 | ✗ | PutFileContents(FPP_MEDIA_DIR + "/config/gateway", gatewayContent); | |
| 301 | ✗ | globalGateway = oldGateway; | |
| 302 | |||
| 303 | // Remove old GATEWAY setting from interface file (optional cleanup) | ||
| 304 | ✗ | std::string interfaceContent = GetFileContents(entry.path()); | |
| 305 | ✗ | size_t pos = interfaceContent.find("GATEWAY="); | |
| 306 | ✗ | if (pos != std::string::npos) { | |
| 307 | ✗ | size_t endPos = interfaceContent.find('\n', pos); | |
| 308 | ✗ | if (endPos != std::string::npos) { | |
| 309 | ✗ | interfaceContent.erase(pos, endPos - pos + 1); | |
| 310 | ✗ | PutFileContents(entry.path(), interfaceContent); | |
| 311 | } | ||
| 312 | } | ||
| 313 | ✗ | break; // Use first gateway found | |
| 314 | ✗ | } | |
| 315 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
|
33 | } |
| 316 |
1/2✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
|
195 | } |
| 317 | } | ||
| 318 | |||
| 319 |
3/6✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
|
160 | std::string dns = dnsSettings["DNS1"]; |
| 320 |
4/8✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 80 times.
|
160 | if (dnsSettings["DNS2"] != "") { |
| 321 | ✗ | dns += ","; | |
| 322 | ✗ | dns += dnsSettings["DNS2"]; | |
| 323 | } | ||
| 324 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
160 | std::string WifiRegulatoryDomain = "US"; |
| 325 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | getRawSetting("WifiRegulatoryDomain", WifiRegulatoryDomain); |
| 326 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | if (isShellSafeWord(WifiRegulatoryDomain)) { |
| 327 |
3/6✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
|
80 | exec("/usr/sbin/iw reg set " + ShellSingleQuote(WifiRegulatoryDomain)); |
| 328 | } | ||
| 329 | |||
| 330 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | int tetherEnabled = getRawSettingInt("EnableTethering", 0); |
| 331 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | std::string tetherInterface = FindTetherWIFIAdapater(); |
| 332 | |||
| 333 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | const std::string dhcpProxyFile = FPP_MEDIA_DIR + "/config/dhcp-proxy-config.conf"; |
| 334 | 80 | std::string dhcpProxies; | |
| 335 | |||
| 336 | 80 | bool ipForward = false; | |
| 337 | 80 | bool hostapd = false; | |
| 338 | 80 | std::map<std::string, std::string> filesNeeded; | |
| 339 | 80 | std::list<std::string> filesToConsider; | |
| 340 | 80 | std::list<std::string> commandsToRun; | |
| 341 | 80 | std::list<std::string> postCommandsToRun; | |
| 342 | // error_code forms: a directory that isn't there (no wpasupplicant | ||
| 343 | // package, say) is simply empty, not an uncaught exception that ends | ||
| 344 | // fppinit before any of the network is configured. | ||
| 345 | 80 | std::error_code dirEc; | |
| 346 |
5/8✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 62 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 62 times.
✓ Branch 7 taken 80 times.
|
142 | for (const auto& entry : std::filesystem::directory_iterator("/etc/systemd/network", dirEc)) { |
| 347 |
2/4✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
|
62 | std::string dev = entry.path().filename(); |
| 348 |
8/30✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 62 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 62 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 62 times.
✓ Branch 16 taken 62 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 62 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 62 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
|
186 | if (startsWith(dev, "10-") && endsWith(dev, ".network")) { |
| 349 | ✗ | filesToConsider.emplace_back("/etc/systemd/network/" + dev); | |
| 350 | } | ||
| 351 | 142 | } | |
| 352 |
5/8✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 54 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 54 times.
✓ Branch 7 taken 80 times.
|
134 | for (const auto& entry : std::filesystem::directory_iterator("/etc/wpa_supplicant", dirEc)) { |
| 353 |
2/4✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
|
54 | std::string dev = entry.path().filename(); |
| 354 |
8/30✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 54 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 54 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 54 times.
✓ Branch 16 taken 54 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 54 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 54 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
|
162 | if (startsWith(dev, "wpa_supplicant-wl") && endsWith(dev, ".conf")) { |
| 355 | ✗ | filesToConsider.emplace_back("/etc/wpa_supplicant/" + dev); | |
| 356 | } | ||
| 357 | 134 | } | |
| 358 |
3/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 79 times.
|
80 | if (FileExists("/etc/hostapd/hostapd.conf")) { |
| 359 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | filesToConsider.emplace_back("/etc/hostapd/hostapd.conf"); |
| 360 | } | ||
| 361 |
5/8✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 79 times.
✓ Branch 7 taken 1 times.
|
80 | if (std::filesystem::exists(FPP_MEDIA_DIR + "/config")) { |
| 362 |
6/10✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 79 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 79 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 115 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 115 times.
✓ Branch 9 taken 79 times.
|
194 | for (const auto& entry : std::filesystem::directory_iterator(FPP_MEDIA_DIR + "/config")) { |
| 363 |
2/4✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✗ Branch 3 not taken.
|
115 | std::string dev = entry.path().filename(); |
| 364 |
4/6✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✓ Branch 5 taken 82 times.
|
230 | if (startsWith(dev, "interface.")) { |
| 365 | 33 | bool validConfig = true; | |
| 366 |
3/6✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
|
33 | auto interfaceSettings = loadSettingsFile(FPP_MEDIA_DIR + "/config/" + dev); |
| 367 |
1/2✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
|
33 | std::string interface = dev.substr(10); |
| 368 | |||
| 369 |
1/2✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
|
33 | std::string content = "[Match]\nName="; |
| 370 | 33 | std::string addressLines; | |
| 371 | |||
| 372 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
|
66 | int DHCPSERVER = getIntFromMap(interfaceSettings, "DHCPSERVER", 0); |
| 373 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
|
66 | int DHCPOFFSET = getIntFromMap(interfaceSettings, "DHCPOFFSET", 100); |
| 374 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
|
66 | int DHCPPOOLSIZE = getIntFromMap(interfaceSettings, "DHCPPOOLSIZE", 50); |
| 375 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
|
66 | int ROUTEMETRIC = getIntFromMap(interfaceSettings, "ROUTEMETRIC", 0); |
| 376 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
|
33 | int IPFORWARDING = getIntFromMap(interfaceSettings, "IPFORWARDING", 0); |
| 377 | |||
| 378 |
13/20✓ Branch 0 taken 3 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 30 times.
✓ Branch 12 taken 3 times.
✓ Branch 13 taken 30 times.
✓ Branch 14 taken 2 times.
✓ Branch 15 taken 31 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
39 | if (DHCPSERVER == 1 && interfaceSettings["PROTO"] != "dhcp") { |
| 379 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | std::string address = interfaceSettings["ADDRESS"]; |
| 380 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | address = address.substr(0, address.find_last_of(".")); |
| 381 |
5/10✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
|
2 | dhcpProxies += "RewriteRule ^" + address + ".([0-9:]*)$ http://" + address + ".$1/ [P,L]\n"; |
| 382 |
5/10✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
|
2 | dhcpProxies += "RewriteRule ^" + address + ".([0-9:]*)/(.*)$ http://" + address + ".$1/$2 [P,L]\n\n"; |
| 383 | 2 | } | |
| 384 | |||
| 385 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
|
33 | content.append(interface).append("\nType="); |
| 386 |
4/6✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 11 times.
|
66 | if (startsWith(interface, "wl")) { |
| 387 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | content.append("wlan\n\n"); |
| 388 | } else { | ||
| 389 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | content.append("ether\n\n"); |
| 390 | } | ||
| 391 |
1/2✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
|
33 | content.append("[Network]\n"); |
| 392 |
4/8✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 33 times.
|
66 | if (dnsSettings["DNS1"] != "") { |
| 393 | ✗ | content.append("DNS=").append(dnsSettings["DNS1"]).append("\n"); | |
| 394 | } | ||
| 395 |
4/8✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 33 times.
|
66 | if (dnsSettings["DNS2"] != "") { |
| 396 | ✗ | content.append("DNS=").append(dnsSettings["DNS2"]).append("\n"); | |
| 397 | } | ||
| 398 |
5/8✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 26 times.
✓ Branch 7 taken 7 times.
|
66 | if (interfaceSettings["PROTO"] == "dhcp") { |
| 399 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | addressLines = "DHCP=yes\n"; |
| 400 | 26 | DHCPSERVER = 0; | |
| 401 | } else { | ||
| 402 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | addressLines = "Address="; |
| 403 |
9/18✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 7 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 7 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 7 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 7 times.
✗ Branch 17 not taken.
|
28 | addressLines.append(interfaceSettings["ADDRESS"]).append("/").append(netmaskToSubnet(interfaceSettings["NETMASK"])).append("\n"); |
| 404 | } | ||
| 405 |
4/6✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 11 times.
|
66 | if (startsWith(interface, "wl")) { |
| 406 | // wifi settings | ||
| 407 |
5/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 20 times.
|
22 | if (tetherEnabled == 1 && interface == tetherInterface) { |
| 408 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
6 | filesNeeded["/etc/hostapd/hostapd.conf"] = CreateHostAPDConfig(interface); |
| 409 | 2 | DHCPSERVER = 1; | |
| 410 | 2 | hostapd = true; | |
| 411 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | addressLines.append("Address=192.168.8.1/24\n"); |
| 412 |
4/6✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✓ Branch 5 taken 1 times.
|
40 | } else if (!interfaceSettings["SSID"].empty()) { |
| 413 |
2/4✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
|
38 | bool wpa3 = settingEnabled(interfaceSettings, "WPA3"); |
| 414 |
2/4✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
|
38 | bool backupWpa3 = settingEnabled(interfaceSettings, "BACKUPWPA3"); |
| 415 |
1/2✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
|
19 | std::string wpa = "ctrl_interface=/var/run/wpa_supplicant\nctrl_interface_group=0\nupdate_config=1\ncountry="; |
| 416 |
1/2✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
|
19 | wpa.append(WifiRegulatoryDomain); |
| 417 | // Protected Management Frames (802.11w) as a GLOBAL "optional" | ||
| 418 | // (pmf=1), never as a per-network ieee80211w=1. The difference | ||
| 419 | // matters: for the global default wpa_supplicant checks whether | ||
| 420 | // the driver advertises a BIP cipher and silently skips PMF if | ||
| 421 | // not, but a per-network ieee80211w is honoured blindly. The | ||
| 422 | // brcmfmac firmware on the Pi Zero W (BCM43430; the Pi 3B shares | ||
| 423 | // the chip) has no MFP support, so with ieee80211w=1 it negotiated | ||
| 424 | // PMF with any PMF-capable AP (most Wi-Fi 6 routers), failed to | ||
| 425 | // install the IGTK ("WPA: Failed to configure IGTK to the driver") | ||
| 426 | // and dropped an otherwise-good association - looked like a bad | ||
| 427 | // password. On boards whose driver does advertise a BIP cipher | ||
| 428 | // (BCM43455, rtw88, ...) nothing changes: SAE still negotiates | ||
| 429 | // PMF and WPA2 APs set to PMF-required still work. | ||
| 430 | // This mirrors what NetworkManager does on stock Raspberry Pi | ||
| 431 | // OS. GitHub issue #2953. | ||
| 432 |
1/2✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
|
19 | wpa.append("\npmf=1\n"); |
| 433 |
4/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 11 times.
|
19 | if (wpa3 || backupWpa3) { |
| 434 | // 2 = hash-to-element AND hunting-and-pecking. With 1 (H2E only) | ||
| 435 | // wpa_supplicant skips every BSS of an AP that lacks H2E ("skip - | ||
| 436 | // SAE H2E required, but not supported by the AP"), so the WPA3 | ||
| 437 | // checkbox could never connect to such an AP on any board. | ||
| 438 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | wpa.append("sae_pwe=2\n"); |
| 439 | } | ||
| 440 |
6/14✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 19 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 19 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 19 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
57 | wpa.append(wpaNetworkBlock(interfaceSettings["SSID"], interfaceSettings["PSK"], wpa3, |
| 441 |
2/4✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
|
38 | settingEnabled(interfaceSettings, "HIDDEN"), 100)); |
| 442 |
16/32✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 14 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 14 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 14 times.
✓ Branch 15 taken 5 times.
✓ Branch 16 taken 14 times.
✓ Branch 17 taken 5 times.
✓ Branch 18 taken 19 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 19 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 14 times.
✓ Branch 23 taken 5 times.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
|
85 | if (!interfaceSettings["BACKUPSSID"].empty() && interfaceSettings["BACKUPSSID"] != "\"\"") { |
| 443 |
6/14✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 14 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
42 | wpa.append(wpaNetworkBlock(interfaceSettings["BACKUPSSID"], interfaceSettings["BACKUPPSK"], backupWpa3, |
| 444 |
2/4✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
42 | settingEnabled(interfaceSettings, "BACKUPHIDDEN"), 90)); |
| 445 | } | ||
| 446 |
4/8✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 19 times.
✗ Branch 7 not taken.
|
19 | filesNeeded["/etc/wpa_supplicant/wpa_supplicant-" + interface + ".conf"] = wpa; |
| 447 | // Queue these unconditionally rather than gating them on | ||
| 448 | // `systemctl is-active`/`is-enabled` queries: during early | ||
| 449 | // boot the systemd manager is saturated (dbus etc. take many | ||
| 450 | // seconds to come up), so those synchronous queries block for | ||
| 451 | // ~9s and stall fppinit -- and this wpa_supplicant@ branch | ||
| 452 | // only runs once a wifi interface exists. The commands run | ||
| 453 | // only if the wpa config actually changed (see commandsToRun | ||
| 454 | // handling below), and `enable` + `reload-or-restart` are both | ||
| 455 | // idempotent, so the pre-checks bought nothing but the stall. | ||
| 456 |
3/6✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
|
19 | commandsToRun.emplace_back("systemctl enable \"wpa_supplicant@" + interface + ".service\" &"); |
| 457 |
3/6✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
|
19 | commandsToRun.emplace_back("systemctl reload-or-restart \"wpa_supplicant@" + interface + ".service\" &"); |
| 458 |
3/6✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
|
19 | postCommandsToRun.emplace_back("systemctl reload-or-restart \"wpa_supplicant@" + interface + ".service\" &"); |
| 459 | 19 | } else { | |
| 460 | 1 | validConfig = false; | |
| 461 | } | ||
| 462 | } | ||
| 463 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 29 times.
|
33 | if (DHCPSERVER) { |
| 464 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | content.append("DHCPServer=yes\n"); |
| 465 | } | ||
| 466 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if (IPFORWARDING == 1) { |
| 467 | ✗ | content.append("IPForward=yes\n"); | |
| 468 | ✗ | ipForward = true; | |
| 469 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 32 times.
|
33 | } else if (IPFORWARDING == 2) { |
| 470 | // systemd-networkd might not have masquerade support compiled in, we'll just do it manually | ||
| 471 | 1 | ipForward = true; | |
| 472 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | exec("/usr/sbin/nft add table nat"); |
| 473 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | exec("/usr/sbin/nft 'add chain nat postrouting { type nat hook postrouting priority 100 ; }'"); |
| 474 |
4/8✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
|
1 | exec("/usr/sbin/nft add rule nat postrouting oif " + ShellSingleQuote(interface) + " masquerade"); |
| 475 | } | ||
| 476 |
1/2✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
|
33 | content.append(addressLines); |
| 477 | // some of the FPP7 images don't support this setting. They use older gcc | ||
| 478 | // Pi Zero 2 W has issues with IgnoreCarrierLoss causing network visibility problems (Issue #2487) | ||
| 479 | #ifdef PLATFORM_PI | ||
| 480 |
3/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
2 | if (!isPiZero2W()) { |
| 481 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | content.append("IgnoreCarrierLoss=5s\n"); |
| 482 | } | ||
| 483 | #else | ||
| 484 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | content.append("IgnoreCarrierLoss=5s\n"); |
| 485 | #endif | ||
| 486 |
1/2✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
|
33 | content.append("\n"); |
| 487 | |||
| 488 | // Apply global gateway to static interfaces only | ||
| 489 |
4/20✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 33 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 33 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 33 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
33 | if (!globalGateway.empty() && interfaceSettings["PROTO"] != "dhcp") { |
| 490 | ✗ | content.append("[Route]\nGateway=").append(globalGateway).append("\n"); | |
| 491 | ✗ | if (ROUTEMETRIC != 0) { | |
| 492 | ✗ | content.append("Metric=").append(std::to_string(ROUTEMETRIC)).append("\n"); | |
| 493 | } | ||
| 494 | } | ||
| 495 |
1/2✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
|
33 | content.append("\n"); |
| 496 |
5/8✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 26 times.
✓ Branch 7 taken 7 times.
|
66 | if (interfaceSettings["PROTO"] == "dhcp") { |
| 497 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | content.append("[DHCPv4]\nClientIdentifier=mac\nUseDomains=true\n"); |
| 498 | // FPP owns the hostname (checkHostName writes /etc/hostname), so | ||
| 499 | // there is nothing to learn from the lease. Leaving this at its | ||
| 500 | // default of yes makes networkd call org.freedesktop.hostname1 the | ||
| 501 | // moment the lease lands, which D-Bus-activates systemd-hostnamed, | ||
| 502 | // which in turn activates polkit to authorize the call. Measured on | ||
| 503 | // a single-core AM335x board that chain cost ~21s of boot (4.3s | ||
| 504 | // hostnamed + 16.8s polkitd) for a hostname that was already | ||
| 505 | // correct; nothing else on an FPP box uses polkit at all. | ||
| 506 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | content.append("UseHostname=false\n"); |
| 507 | // Only use NTP from DHCP if explicitly enabled | ||
| 508 | 26 | std::string useNTPFromDHCP; | |
| 509 |
2/4✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
|
26 | getRawSetting("UseNTPFromDHCP", useNTPFromDHCP); |
| 510 |
3/4✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 2 times.
|
26 | if (useNTPFromDHCP != "1") { |
| 511 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | content.append("UseNTP=no\n"); |
| 512 | } | ||
| 513 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
33 | } else if (ROUTEMETRIC != 0) { |
| 514 | ✗ | content.append("[DHCPv4]\n"); | |
| 515 | } | ||
| 516 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if (ROUTEMETRIC != 0) { |
| 517 | ✗ | content.append("RouteMetric=").append(interfaceSettings["ROUTEMETRIC"]).append("\n\n"); | |
| 518 | ✗ | content.append("[IPv6AcceptRA]\nRouteMetric=").append(interfaceSettings["ROUTEMETRIC"]).append("\n\n"); | |
| 519 | } else { | ||
| 520 |
1/2✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
|
33 | content.append("\n"); |
| 521 | } | ||
| 522 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 29 times.
|
33 | if (DHCPSERVER == 1) { |
| 523 | // PersistLeases=yes is already the default on Debian 13/trixie | ||
| 524 | // (systemd >= 256, via systemd-networkd-persistent-storage.service); | ||
| 525 | // set explicitly here for clarity and so behavior doesn't silently | ||
| 526 | // depend on an OS-level default we don't control. Harmlessly ignored | ||
| 527 | // as an unknown key on older systemd (verified: an unrecognized | ||
| 528 | // [DHCPServer] key logs a warning but doesn't break parsing). | ||
| 529 |
5/10✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
|
4 | content.append("[DHCPServer]\nPersistLeases=yes\nDefaultLeaseTimeSec=8h\nMaxLeaseTimeSec=8h\nPoolOffset=").append(std::to_string(DHCPOFFSET)).append("\nPoolSize=").append(std::to_string(DHCPPOOLSIZE)).append("\n"); |
| 530 |
3/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
|
8 | if (!dnsSettings["DNS1"].empty()) { |
| 531 | ✗ | content.append("EmitDNS=yes\nDNS=").append(dnsSettings["DNS1"]).append("\n"); | |
| 532 | } | ||
| 533 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | content.append("\n"); |
| 534 |
5/8✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 2 times.
|
4 | if (FileExists(FPP_MEDIA_DIR + "/config/leases." + interface)) { |
| 535 |
4/8✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
|
2 | content.append(GetFileContents(FPP_MEDIA_DIR + "/config/leases." + interface)); |
| 536 | } | ||
| 537 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | content.append("\n"); |
| 538 | } | ||
| 539 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 times.
|
33 | if (validConfig) { |
| 540 |
4/8✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 32 times.
✗ Branch 7 not taken.
|
32 | filesNeeded["/etc/systemd/network/10-" + interface + ".network"] = content; |
| 541 | } | ||
| 542 | 33 | } | |
| 543 | 194 | } | |
| 544 | } | ||
| 545 | |||
| 546 | // If tethering is explicitly enabled (==1) but no interface configs set it up, | ||
| 547 | // configure it now (handles fresh installs with no /config directory) | ||
| 548 |
4/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 75 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
|
80 | if (tetherEnabled == 1 && !hostapd) { |
| 549 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
9 | filesNeeded["/etc/hostapd/hostapd.conf"] = CreateHostAPDConfig(tetherInterface); |
| 550 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | std::string content = "[Match]\nName="; |
| 551 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | content.append(tetherInterface).append("\nType=wlan\n\n" |
| 552 | "[Network]\n" | ||
| 553 | "DHCP=no\n" | ||
| 554 | "Address=192.168.8.1/24\n" | ||
| 555 | "DHCPServer=yes\n\n"); | ||
| 556 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | AppendTetherDHCPServerSection(content, tetherInterface); |
| 557 |
4/8✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
|
3 | filesNeeded["/etc/systemd/network/10-" + tetherInterface + ".network"] = content; |
| 558 | 3 | hostapd = true; | |
| 559 | 3 | } | |
| 560 | |||
| 561 | 80 | bool reloadApache = false; | |
| 562 |
5/8✓ Branch 0 taken 78 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 78 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 80 times.
|
80 | if (dhcpProxies.empty() && FileExists(dhcpProxyFile)) { |
| 563 | ✗ | unlink(dhcpProxyFile.c_str()); | |
| 564 | ✗ | reloadApache = true; | |
| 565 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 78 times.
|
80 | } else if (!dhcpProxies.empty()) { |
| 566 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | PutFileContents(dhcpProxyFile, dhcpProxies); |
| 567 | 2 | reloadApache = true; | |
| 568 | } | ||
| 569 | |||
| 570 | // FPP's NTP daemon is chrony. Large-offset stepping (ntpd's "-g") is handled | ||
| 571 | // by the "makestep" directive in /etc/chrony/chrony.conf, so there is nothing | ||
| 572 | // to inject per-boot. Here we only reconcile DHCP-provided NTP servers with | ||
| 573 | // the UseNTPFromDHCP setting: the networkd-dispatcher chrony hook drops any | ||
| 574 | // DHCP servers into /run/chrony-dhcp (consumed via "sourcedir" in | ||
| 575 | // chrony.conf) when the setting is enabled. When it's disabled, make sure | ||
| 576 | // none linger from a previous configuration. chrony is started after | ||
| 577 | // fpp_postnetwork, so clearing the dir now is enough -- no reload needed. | ||
| 578 | 80 | std::string useNTPFromDHCP; | |
| 579 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | getRawSetting("UseNTPFromDHCP", useNTPFromDHCP); |
| 580 |
3/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✓ Branch 3 taken 2 times.
|
80 | if (useNTPFromDHCP != "1") { |
| 581 |
2/4✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
|
156 | exec("/usr/bin/rm -f /run/chrony-dhcp/*.sources 2>/dev/null"); |
| 582 | } | ||
| 583 | |||
| 584 | // Auto-detect default gateway as a fallback NTP server so that chrony has | ||
| 585 | // a reachable peer when internet pool servers are blocked (e.g. behind | ||
| 586 | // double NAT / a gateway that drops NTP replies -- see issue #2736). LAN | ||
| 587 | // traffic to the gateway does not cross any NAT boundary. chrony gracefully | ||
| 588 | // ignores non-responsive servers, so this is safe even if the gateway does | ||
| 589 | // not run NTP. | ||
| 590 | { | ||
| 591 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
160 | std::string gw = execAndReturn( |
| 592 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | "/usr/bin/ip route show default 2>/dev/null | /usr/bin/awk '{print $3}'"); |
| 593 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | TrimWhiteSpace(gw); |
| 594 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 22 times.
|
80 | if (!gw.empty()) { |
| 595 | struct sockaddr_in sa; | ||
| 596 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | if (inet_pton(AF_INET, gw.c_str(), &(sa.sin_addr)) == 1) { |
| 597 |
2/4✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 58 times.
✗ Branch 3 not taken.
|
58 | std::string chronyConf = GetFileContents("/etc/chrony/chrony.conf"); |
| 598 |
3/4✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 1 times.
|
58 | if (chronyConf.find("server " + gw) == std::string::npos) { |
| 599 | chronyConf += "\n# Auto-detected default gateway fallback\nserver " | ||
| 600 |
3/6✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 57 times.
✗ Branch 5 not taken.
|
57 | + gw + " minpoll 10 maxpoll 12 iburst\n"; |
| 601 |
2/4✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
|
114 | PutFileContents("/etc/chrony/chrony.conf", chronyConf); |
| 602 |
2/4✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
|
57 | execbg("/usr/bin/systemctl reload-or-restart chrony.service &"); |
| 603 |
1/2✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
|
57 | printf("FPP - Added default gateway %s as fallback NTP server\n", |
| 604 | gw.c_str()); | ||
| 605 | } | ||
| 606 | 58 | } | |
| 607 | } | ||
| 608 | 80 | } | |
| 609 | 80 | bool changed = false; | |
| 610 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 80 times.
|
81 | for (auto& ftc : filesToConsider) { |
| 611 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (filesNeeded.find(ftc) == filesNeeded.end()) { |
| 612 | // No longer used/needed, remove it | ||
| 613 | 1 | unlink(ftc.c_str()); | |
| 614 | 1 | changed = true; | |
| 615 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
2 | if (startsWith(ftc, "/etc/wpa_supplicant/")) { |
| 616 | ✗ | std::string intf = ftc.substr(ftc.find("-") + 1); | |
| 617 | ✗ | intf = intf.substr(0, intf.find(".")); | |
| 618 | ✗ | execbg("/usr/bin/systemctl disable wpa_supplicant@" + intf + ".service &"); | |
| 619 | ✗ | } | |
| 620 | } | ||
| 621 | } | ||
| 622 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | changed |= compareAndCopyFiles(filesNeeded); |
| 623 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 44 times.
|
80 | if (changed) { |
| 624 | 36 | bool localFullReload = false; | |
| 625 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | printf("Need to restart/reload stuff.\n"); |
| 626 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 5 times.
|
36 | if (!hostapd) { |
| 627 | // Out of AP mode before the Wi-Fi client starts: while hostapd | ||
| 628 | // still holds the radio, a wpa_supplicant started beside it often | ||
| 629 | // fails to associate, and leaving AP mode from the UI then needed | ||
| 630 | // a power cycle (#2825). | ||
| 631 |
6/10✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 31 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 28 times.
|
124 | if (contains(execAndReturn("/usr/bin/systemctl is-enabled hostapd"), "enabled")) { |
| 632 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | execbg("/usr/bin/systemctl disable hostapd.service &"); |
| 633 | 3 | localFullReload = true; | |
| 634 | } | ||
| 635 |
3/4✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 28 times.
|
31 | if (hostapdRunning()) { |
| 636 | // Bounded: an unresponsive hostapd (driver/dbus hiccup) must not be | ||
| 637 | // able to wedge fppinit indefinitely - this runs synchronously in | ||
| 638 | // the interactive "Restart Network" request (network_apply_interface), | ||
| 639 | // so a hang here blocks that whole HTTP request and can make the | ||
| 640 | // rest of the appliance look unresponsive until it clears. | ||
| 641 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | exec("/usr/bin/timeout 10 /usr/bin/systemctl stop hostapd.service"); |
| 642 |
3/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
|
3 | if (hostapdRunning()) { |
| 643 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | printf("FPP - hostapd did not stop within 10s, killing it\n"); |
| 644 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | exec("/usr/bin/systemctl kill --signal=SIGKILL hostapd.service"); |
| 645 | } | ||
| 646 | 3 | localFullReload = true; | |
| 647 | } | ||
| 648 |
2/4✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
|
62 | exec("rm -f /home/fpp/media/tmp/wifi-*.ascii"); |
| 649 | } | ||
| 650 | // These end in "&" and must not be waited for: through exec()'s popen the | ||
| 651 | // backgrounded systemctl keeps the pipe open, so exec() blocks until it | ||
| 652 | // exits. A start job for an adapter that is not plugged in then held | ||
| 653 | // fppinit -- and systemd-networkd, ordered after it -- until the job | ||
| 654 | // timed out. | ||
| 655 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 36 times.
|
74 | for (auto& c : commandsToRun) { |
| 656 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | printf(" %s\n", c.c_str()); |
| 657 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | execbg(c); |
| 658 | } | ||
| 659 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 3 times.
|
36 | if (fullReload) { |
| 660 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
|
66 | exec("/usr/bin/systemctl daemon-reload"); |
| 661 | } | ||
| 662 |
2/4✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
|
36 | execbg("/usr/bin/systemctl reload-or-restart systemd-networkd.service &"); |
| 663 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 31 times.
|
36 | if (hostapd) { |
| 664 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | unblockWifi(); |
| 665 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
10 | execbg("/usr/bin/systemctl reload-or-restart hostapd.service &"); |
| 666 | } | ||
| 667 |
4/4✓ Branch 0 taken 33 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 30 times.
|
36 | if (fullReload && localFullReload) { |
| 668 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
6 | exec("/usr/bin/systemctl daemon-reload"); |
| 669 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | exec("/usr/bin/systemctl reload-or-restart systemd-networkd.service"); |
| 670 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
|
5 | for (auto& c : postCommandsToRun) { |
| 671 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | printf(" %s\n", c.c_str()); |
| 672 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | execbg(c); |
| 673 | } | ||
| 674 | } | ||
| 675 | } | ||
| 676 | |||
| 677 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | printf("FPP - Setting max IGMP memberships\n"); |
| 678 | // rmem_max is the ceiling a plain SO_RCVBUF request is clamped to. The e131 | ||
| 679 | // bridge input sockets ask for far more than the old 384KB when many | ||
| 680 | // universes are configured (and use SO_RCVBUFFORCE where possible), so raise | ||
| 681 | // the ceiling to 8MB to cover the burst of a several-hundred-universe frame. | ||
| 682 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
|
80 | exec("/usr/sbin/sysctl -w net/core/rmem_max=8388608 net/core/wmem_max=393216 net/ipv4/igmp_max_memberships=512 > /dev/null 2>&1"); |
| 683 | |||
| 684 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | printf("FPP - Configuring ethernet interfaces for show traffic\n"); |
| 685 |
5/8✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 160 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 160 times.
✓ Branch 7 taken 80 times.
|
240 | for (const auto& entry : std::filesystem::directory_iterator("/sys/class/net/")) { |
| 686 |
2/4✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 160 times.
✗ Branch 3 not taken.
|
160 | std::string dev = entry.path().filename(); |
| 687 |
3/4✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
✓ Branch 3 taken 86 times.
|
160 | if (IsShowEthernetInterface(dev)) { |
| 688 |
3/4✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
✓ Branch 3 taken 18 times.
|
74 | if (InterfaceHasRoutableIPv4(dev)) { |
| 689 | // fq is required for the per-socket pacing the UDP channel | ||
| 690 | // outputs use, and is a good general-purpose default even | ||
| 691 | // when pacing is off (whether to pace is decided per | ||
| 692 | // destination at runtime). Only useful on interfaces that | ||
| 693 | // can carry routed UDP, so an address-less interface | ||
| 694 | // dedicated to a raw layer-2 output (ColorLight) keeps its | ||
| 695 | // default qdisc. Not applied to wifi (mac80211 has its own | ||
| 696 | // internal queueing), and the helper refuses to replace an | ||
| 697 | // admin-installed custom qdisc. | ||
| 698 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | InstallShowTrafficQdisc(dev); |
| 699 | } | ||
| 700 | // Energy Efficient Ethernet causes latency spikes and drops with | ||
| 701 | // bursty traffic on several NICs (including the Pi's) | ||
| 702 |
4/8✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 74 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 74 times.
✗ Branch 7 not taken.
|
74 | exec("/usr/sbin/ethtool --set-eee " + ShellSingleQuote(dev) + " eee off > /dev/null 2>&1"); |
| 703 | } | ||
| 704 | 240 | } | |
| 705 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 79 times.
|
80 | if (ipForward) { |
| 706 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | exec("/usr/sbin/sysctl net.ipv4.ip_forward=1"); |
| 707 | } else { | ||
| 708 |
2/4✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 79 times.
✗ Branch 3 not taken.
|
158 | exec("/usr/sbin/sysctl net.ipv4.ip_forward=0"); |
| 709 | } | ||
| 710 |
20/34✓ Branch 0 taken 2 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 1 times.
✓ Branch 12 taken 2 times.
✓ Branch 13 taken 78 times.
✓ Branch 14 taken 2 times.
✓ Branch 15 taken 78 times.
✓ Branch 16 taken 2 times.
✓ Branch 17 taken 78 times.
✓ Branch 18 taken 2 times.
✓ Branch 19 taken 78 times.
✓ Branch 20 taken 2 times.
✓ Branch 21 taken 78 times.
✓ Branch 22 taken 1 times.
✓ Branch 23 taken 79 times.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
|
88 | if (reloadApache && (!contains(execAndReturn("/usr/bin/systemctl is-active apache2"), "inactive"))) { |
| 711 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | exec("/usr/bin/systemctl reload-or-restart apache2"); |
| 712 | } | ||
| 713 | 80 | } | |
| 714 | |||
| 715 | // Whether an IPv4 address is one that reaches a real network. Excludes the | ||
| 716 | // board's own USB gadget and tether addresses, and 169.254/16: networkd falls | ||
| 717 | // back to link-local while DHCP is still pending (LinkLocalAddressing=yes), and | ||
| 718 | // the probe can finish before a slow DHCP server answers, so treating it as | ||
| 719 | // "the address" started fppd on it ahead of the real lease. | ||
| 720 | 144 | static bool isUpstreamAddress(const std::string& addr) { | |
| 721 |
12/26✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 144 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 144 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 144 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 144 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 144 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 144 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 143 times.
✓ Branch 17 taken 1 times.
✓ Branch 18 taken 144 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 144 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
|
432 | return !startsWith(addr, "169.254.") && addr != "192.168.6.2" && addr != "192.168.7.2" && addr != "192.168.8.1"; |
| 722 | } | ||
| 723 | |||
| 724 | // Check if there are any network interfaces that could potentially receive NTP time | ||
| 725 | // Returns true if there's at least one interface with a "real" IP address that could reach NTP servers | ||
| 726 | // Excludes loopback, USB gadget, and tethering interfaces | ||
| 727 | 1629 | static bool hasNetworkInterfaceForNTP() { | |
| 728 | // If EnableTethering is explicitly set to 1, user expects to use tethering | ||
| 729 | // which means no external network - skip NTP wait | ||
| 730 |
2/4✓ Branch 0 taken 1629 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1629 times.
✗ Branch 3 not taken.
|
1629 | int tetheringEnabled = getRawSettingInt("EnableTethering", 0); |
| 731 |
2/2✓ Branch 0 taken 187 times.
✓ Branch 1 taken 1442 times.
|
1629 | if (tetheringEnabled == 1) { |
| 732 |
1/2✓ Branch 0 taken 187 times.
✗ Branch 1 not taken.
|
187 | printf("FPP - Tethering is enabled, skipping NTP time wait\n"); |
| 733 | 187 | return false; | |
| 734 | } | ||
| 735 | |||
| 736 | 1442 | struct ifaddrs* ifAddrStruct = NULL; | |
| 737 | 1442 | struct ifaddrs* ifa = NULL; | |
| 738 | 1442 | void* tmpAddrPtr = NULL; | |
| 739 | 1442 | bool hasValidIP = false; | |
| 740 | |||
| 741 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1442 times.
|
1442 | if (getifaddrs(&ifAddrStruct) != 0) { |
| 742 | // If we can't get interface info, assume we might have network | ||
| 743 | ✗ | return true; | |
| 744 | } | ||
| 745 | |||
| 746 |
2/2✓ Branch 0 taken 1748 times.
✓ Branch 1 taken 1384 times.
|
3132 | for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) { |
| 747 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1748 times.
|
1748 | if (!ifa->ifa_addr) { |
| 748 | 1504 | continue; | |
| 749 | } | ||
| 750 | |||
| 751 |
1/2✓ Branch 0 taken 1748 times.
✗ Branch 1 not taken.
|
1820 | std::string nm = ifa->ifa_name; |
| 752 | // Skip loopback and USB gadget interfaces (usb0, usb1, etc.) | ||
| 753 |
12/20✓ Branch 0 taken 1748 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 244 times.
✓ Branch 3 taken 1504 times.
✓ Branch 4 taken 244 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 244 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 244 times.
✓ Branch 10 taken 244 times.
✓ Branch 11 taken 1504 times.
✓ Branch 12 taken 244 times.
✓ Branch 13 taken 1504 times.
✓ Branch 14 taken 1504 times.
✓ Branch 15 taken 244 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
2236 | if (nm == "lo" || startsWith(nm, "usb")) { |
| 754 | 1504 | continue; | |
| 755 | } | ||
| 756 | |||
| 757 | // Only check IPv4 addresses | ||
| 758 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 186 times.
|
244 | if (ifa->ifa_addr->sa_family == AF_INET) { |
| 759 | 58 | tmpAddrPtr = &((struct sockaddr_in*)ifa->ifa_addr)->sin_addr; | |
| 760 | char addressBuffer[INET_ADDRSTRLEN]; | ||
| 761 | 58 | inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); | |
| 762 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | std::string addr = addressBuffer; |
| 763 | |||
| 764 | // Skip USB gadget, tethering and link-local addresses | ||
| 765 |
2/4✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 58 times.
|
58 | if (!isUpstreamAddress(addr)) { |
| 766 | ✗ | continue; | |
| 767 | } | ||
| 768 | |||
| 769 | // Found a valid IP that could potentially reach NTP | ||
| 770 | 58 | hasValidIP = true; | |
| 771 | 58 | break; | |
| 772 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | } |
| 773 |
3/3✓ Branch 0 taken 186 times.
✓ Branch 1 taken 1504 times.
✓ Branch 2 taken 58 times.
|
1748 | } |
| 774 | |||
| 775 |
1/2✓ Branch 0 taken 1442 times.
✗ Branch 1 not taken.
|
1442 | if (ifAddrStruct != NULL) { |
| 776 | 1442 | freeifaddrs(ifAddrStruct); | |
| 777 | } | ||
| 778 | |||
| 779 | // If no valid IP found, check if any "real" interfaces exist with link up | ||
| 780 | // An interface must have carrier (link) to potentially get DHCP | ||
| 781 |
2/2✓ Branch 0 taken 1384 times.
✓ Branch 1 taken 58 times.
|
1442 | if (!hasValidIP) { |
| 782 | // Re-scan to check if interfaces exist with carrier | ||
| 783 |
1/2✓ Branch 0 taken 1384 times.
✗ Branch 1 not taken.
|
1384 | if (getifaddrs(&ifAddrStruct) == 0) { |
| 784 |
1/2✓ Branch 0 taken 1384 times.
✗ Branch 1 not taken.
|
1384 | std::string tetherInterface = FindTetherWIFIAdapater(); |
| 785 |
2/2✓ Branch 0 taken 1496 times.
✓ Branch 1 taken 1380 times.
|
2876 | for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) { |
| 786 |
1/2✓ Branch 0 taken 1496 times.
✗ Branch 1 not taken.
|
1568 | std::string nm = ifa->ifa_name; |
| 787 | // Skip loopback and USB gadget interfaces | ||
| 788 |
12/20✓ Branch 0 taken 1496 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✓ Branch 3 taken 1384 times.
✓ Branch 4 taken 112 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 112 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 112 times.
✓ Branch 10 taken 112 times.
✓ Branch 11 taken 1384 times.
✓ Branch 12 taken 112 times.
✓ Branch 13 taken 1384 times.
✓ Branch 14 taken 1384 times.
✓ Branch 15 taken 112 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
1720 | if (nm == "lo" || startsWith(nm, "usb")) { |
| 789 | 1384 | continue; | |
| 790 | } | ||
| 791 | // Skip the interface designated for tethering | ||
| 792 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | if (nm == tetherInterface) { |
| 793 | ✗ | continue; | |
| 794 | } | ||
| 795 | // Check for "real" network interface names | ||
| 796 |
5/24✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 112 times.
✓ Branch 10 taken 112 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 112 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 112 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
336 | if (startsWith(nm, "eth") || startsWith(nm, "wlan") || |
| 797 |
4/28✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 112 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 112 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 112 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 112 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
|
112 | startsWith(nm, "en") || startsWith(nm, "wl") || |
| 798 |
7/34✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 112 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 112 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 112 times.
✗ Branch 20 not taken.
✓ Branch 21 taken 112 times.
✗ Branch 22 not taken.
✓ Branch 23 taken 112 times.
✗ Branch 24 not taken.
✓ Branch 25 taken 112 times.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
|
224 | startsWith(nm, "br") || startsWith(nm, "bond")) { |
| 799 | // Check carrier state via sysfs - most reliable across all drivers | ||
| 800 | // /sys/class/net/<iface>/carrier returns 1 if link, 0 if no link | ||
| 801 |
2/4✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
|
112 | std::string carrierPath = "/sys/class/net/" + nm + "/carrier"; |
| 802 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | std::string carrier = GetFileContents(carrierPath); |
| 803 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | TrimWhiteSpace(carrier); |
| 804 |
3/4✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 108 times.
|
112 | if (carrier == "1") { |
| 805 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | printf("FPP - Interface %s has carrier, will wait for NTP\n", nm.c_str()); |
| 806 | 4 | hasValidIP = true; | |
| 807 | 4 | break; | |
| 808 | } | ||
| 809 |
4/4✓ Branch 0 taken 108 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 4 times.
|
116 | } |
| 810 |
3/3✓ Branch 0 taken 108 times.
✓ Branch 1 taken 1384 times.
✓ Branch 2 taken 4 times.
|
1496 | } |
| 811 |
1/2✓ Branch 0 taken 1384 times.
✗ Branch 1 not taken.
|
1384 | if (ifAddrStruct != NULL) { |
| 812 | 1384 | freeifaddrs(ifAddrStruct); | |
| 813 | } | ||
| 814 | 1384 | } | |
| 815 | } | ||
| 816 | 1442 | return hasValidIP; | |
| 817 | } | ||
| 818 | |||
| 819 | 37 | void handleBootDelay() { | |
| 820 |
2/4✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
|
37 | int i = getRawSettingInt("bootDelay", -1); |
| 821 |
1/2✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
|
37 | const std::string delayFile = FPP_MEDIA_DIR + "/tmp/boot_delay"; |
| 822 |
1/2✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
|
37 | const std::string skipFile = FPP_MEDIA_DIR + "/tmp/boot_delay_skip"; |
| 823 | |||
| 824 | // bootDelay=0 means no delay at all - clean up any flag file and return immediately | ||
| 825 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 21 times.
|
37 | if (i == 0) { |
| 826 | 16 | unlink(delayFile.c_str()); | |
| 827 | 16 | unlink(skipFile.c_str()); | |
| 828 | 16 | return; | |
| 829 | } | ||
| 830 | |||
| 831 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
|
21 | if (i > 0) { |
| 832 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | printf("FPP - Sleeping for %d seconds\n", i); |
| 833 | // Create flag file with start time and duration for UI countdown | ||
| 834 | 1 | time_t startTime = time(nullptr); | |
| 835 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | std::string flagContent = std::to_string(startTime) + "," + std::to_string(i); |
| 836 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | PutFileContents(delayFile, flagContent); |
| 837 | // Notify systemd we're starting the delay and extend timeout | ||
| 838 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | sd_notify(0, "STATUS=Boot delay in progress"); |
| 839 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | sd_notifyf(0, "EXTEND_TIMEOUT_USEC=%llu", (unsigned long long)(i + 30) * 1000000); |
| 840 | |||
| 841 | // Sleep in 100ms increments to allow UI to show countdown and respond to skip | ||
| 842 | 1 | int remainingMs = i * 1000; | |
| 843 | 1 | int watchdogCounter = 0; | |
| 844 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | while (remainingMs > 0) { |
| 845 | // Check for skip request from UI every iteration | ||
| 846 |
3/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 11 times.
|
12 | if (FileExists(skipFile)) { |
| 847 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | printf("FPP - Boot delay skip requested by user\n"); |
| 848 | 1 | unlink(skipFile.c_str()); | |
| 849 | 1 | break; | |
| 850 | } | ||
| 851 | |||
| 852 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 853 | 11 | remainingMs -= 100; | |
| 854 | 11 | watchdogCounter++; | |
| 855 | |||
| 856 | // Notify systemd watchdog every 10 seconds (100 iterations) | ||
| 857 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (watchdogCounter >= 100) { |
| 858 | ✗ | sd_notify(0, "WATCHDOG=1"); | |
| 859 | ✗ | watchdogCounter = 0; | |
| 860 | } | ||
| 861 | } | ||
| 862 | // Remove flag files when delay completes | ||
| 863 | 1 | unlink(delayFile.c_str()); | |
| 864 | 1 | unlink(skipFile.c_str()); | |
| 865 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
21 | } else if (i == -1) { |
| 866 | // Auto mode: nothing to wait on here. The is-there-an-NTP-capable-network | ||
| 867 | // decision and the quick SNTP set both happen in handleTimeSyncWait(), | ||
| 868 | // which runs on the network thread after waitForInterfacesUp() has | ||
| 869 | // already waited for link/IP properly. This used to pre-poll up to 5s | ||
| 870 | // for an interface, but that only serialized boot ahead of the | ||
| 871 | // concurrent audio/network threads and its verdict was recomputed | ||
| 872 | // later anyway. Just clean up any stale flag files. | ||
| 873 | 20 | unlink(delayFile.c_str()); | |
| 874 | 20 | unlink(skipFile.c_str()); | |
| 875 | } | ||
| 876 |
4/4✓ Branch 0 taken 21 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 16 times.
|
53 | } |
| 877 | |||
| 878 | // Minimal SNTP (RFC 4330) client: query an NTP server over UDP and step the | ||
| 879 | // system clock to its transmit timestamp (second precision). This one-shot set | ||
| 880 | // runs during boot so we never block ~20s waiting for the chrony daemon to step | ||
| 881 | // the clock; chrony refines it to sub-millisecond afterwards. Note the socket is | ||
| 882 | // left unbound, so the query sources from a random ephemeral port -- which (like | ||
| 883 | // chrony itself) dodges routers/ISP gateways that drop NTP replies to source | ||
| 884 | // port 123. Must run as root (clock_settime needs CAP_SYS_TIME). Returns true if | ||
| 885 | // the clock was set. | ||
| 886 | 36 | static bool quickSntpSet(const std::string& host, int timeoutSecs) { | |
| 887 | 36 | struct addrinfo hints {}; | |
| 888 | 36 | hints.ai_family = AF_INET; | |
| 889 | 36 | hints.ai_socktype = SOCK_DGRAM; | |
| 890 | 36 | struct addrinfo* res = nullptr; | |
| 891 |
6/8✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 34 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 34 times.
✓ Branch 7 taken 2 times.
|
36 | if (getaddrinfo(host.c_str(), "123", &hints, &res) != 0 || res == nullptr) { |
| 892 | 34 | return false; | |
| 893 | } | ||
| 894 | 2 | bool ok = false; | |
| 895 | 2 | int sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | |
| 896 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (sock >= 0) { |
| 897 | 2 | struct timeval tv { timeoutSecs, 0 }; | |
| 898 | 2 | setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | |
| 899 | 2 | setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); | |
| 900 | 2 | unsigned char pkt[48] = { 0 }; | |
| 901 | 2 | pkt[0] = 0x1b; // LI=0, VN=3, Mode=3 (client) | |
| 902 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (sendto(sock, pkt, sizeof(pkt), 0, res->ai_addr, res->ai_addrlen) == (ssize_t)sizeof(pkt) |
| 903 |
4/8✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
|
2 | && recv(sock, pkt, sizeof(pkt), 0) == (ssize_t)sizeof(pkt)) { |
| 904 | ✗ | int stratum = pkt[1]; | |
| 905 | // Transmit timestamp: 32-bit seconds since 1900, big-endian, at offset 40. | ||
| 906 | ✗ | uint32_t ntpSecs = ((uint32_t)pkt[40] << 24) | ((uint32_t)pkt[41] << 16) | ((uint32_t)pkt[42] << 8) | (uint32_t)pkt[43]; | |
| 907 | // stratum 0 == "kiss o' death"/invalid; 16+ == unsynchronized. | ||
| 908 | // 2208988800 = seconds between the 1900 (NTP) and 1970 (Unix) epochs. | ||
| 909 | ✗ | if (stratum > 0 && stratum < 16 && ntpSecs > 2208988800U) { | |
| 910 | ✗ | struct timespec ts { (time_t)(ntpSecs - 2208988800U), 0 }; | |
| 911 | ✗ | ok = (clock_settime(CLOCK_REALTIME, &ts) == 0); | |
| 912 | } | ||
| 913 | } | ||
| 914 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | close(sock); |
| 915 | } | ||
| 916 | 2 | freeaddrinfo(res); | |
| 917 | 2 | return ok; | |
| 918 | } | ||
| 919 | |||
| 920 | // Ensure the clock is sane before fppd starts - called AFTER waitForInterfacesUp | ||
| 921 | // so we know network is available. On RTC-less boards the clock boots stale (at | ||
| 922 | // the image build date), which would make schedules and logs wrong. Rather than | ||
| 923 | // block boot for ~20s waiting for the chrony daemon to step the clock, do a | ||
| 924 | // quick one-shot SNTP set now and continue; the daemon refines it afterwards. | ||
| 925 | 35 | void handleTimeSyncWait() { | |
| 926 |
2/4✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
|
35 | int i = getRawSettingInt("bootDelay", -1); |
| 927 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 20 times.
|
35 | if (i != -1) { |
| 928 | // Only do time sync wait in auto mode | ||
| 929 | 17 | return; | |
| 930 | } | ||
| 931 | |||
| 932 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | const std::string delayFile = FPP_MEDIA_DIR + "/tmp/boot_delay"; |
| 933 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | const std::string skipFile = FPP_MEDIA_DIR + "/tmp/boot_delay_skip"; |
| 934 | |||
| 935 | // Check if there are any network interfaces that could get NTP time | ||
| 936 | // If not, skip - no point trying NTP on a device with no network | ||
| 937 |
3/4✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 18 times.
|
20 | if (!hasNetworkInterfaceForNTP()) { |
| 938 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | printf("FPP - No network interface found, skipping NTP time set\n"); |
| 939 | 2 | unlink(delayFile.c_str()); | |
| 940 | 2 | unlink(skipFile.c_str()); | |
| 941 | 2 | return; | |
| 942 | } | ||
| 943 | |||
| 944 | struct stat attr; | ||
| 945 | 18 | stat("/etc/fpp/rfs_version", &attr); | |
| 946 | 18 | time_t fileTime = attr.st_ctime; | |
| 947 | 18 | time_t currentTime = time(nullptr); | |
| 948 | |||
| 949 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (difftime(fileTime, currentTime) <= 0) { |
| 950 | // Clock is already at or past the image build date - nothing to do. | ||
| 951 | ✗ | unlink(delayFile.c_str()); | |
| 952 | ✗ | unlink(skipFile.c_str()); | |
| 953 | ✗ | return; | |
| 954 | } | ||
| 955 | |||
| 956 | // Clock is stale (no RTC). Do a fast SNTP set against the configured server | ||
| 957 | // and move on -- do NOT block on full NTP convergence. Use the first | ||
| 958 | // pool/server from chrony.conf so a site-local time server is honoured; fall | ||
| 959 | // back to FPP's pool. | ||
| 960 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
36 | std::string ntpHost = execAndReturn( |
| 961 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | "/usr/bin/awk '/^(pool|server)[[:space:]]/ { print $2; exit }' /etc/chrony/chrony.conf 2>/dev/null"); |
| 962 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | TrimWhiteSpace(ntpHost); |
| 963 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1 times.
|
18 | if (ntpHost.empty()) { |
| 964 |
1/2✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
|
17 | ntpHost = "falconplayer.pool.ntp.org"; |
| 965 | } | ||
| 966 | |||
| 967 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | printf("FPP - System clock is stale; doing a quick SNTP set from %s\n", ntpHost.c_str()); |
| 968 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | sd_notify(0, "STATUS=Setting system clock via SNTP"); |
| 969 | 18 | bool set = false; | |
| 970 |
3/4✓ Branch 0 taken 36 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
|
54 | for (int attempt = 0; attempt < 2 && !set; ++attempt) { |
| 971 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | set = quickSntpSet(ntpHost, 2); |
| 972 | } | ||
| 973 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (set) { |
| 974 | ✗ | currentTime = time(nullptr); | |
| 975 | struct tm tmNow; | ||
| 976 | ✗ | localtime_r(¤tTime, &tmNow); | |
| 977 | char buffer[26]; | ||
| 978 | ✗ | strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", &tmNow); | |
| 979 | ✗ | printf("FPP - Clock set via SNTP to %s; chrony will refine it in the background\n", buffer); | |
| 980 | } else { | ||
| 981 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | printf("FPP - Quick SNTP set failed (no time server reachable yet); continuing, chrony will sync when available\n"); |
| 982 | } | ||
| 983 | 18 | unlink(delayFile.c_str()); | |
| 984 | 18 | unlink(skipFile.c_str()); | |
| 985 |
4/4✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 2 times.
|
22 | } |
| 986 | |||
| 987 | // Restart the supplicant of every WiFi client that is enabled but not running. | ||
| 988 | // Its boot-time start job can give up before it could run (JobTimeoutSec in | ||
| 989 | // etc/systemd/wpa_supplicant@.service.d counts from when it was queued, and a | ||
| 990 | // slow boot used it all up: issue #3178), and nothing else retries it. Each | ||
| 991 | // client has a wpa_supplicant-<if>.conf; the tether AP has none. | ||
| 992 | 37 | void checkWLANInterface() { | |
| 993 | 37 | std::error_code ec; | |
| 994 |
5/8✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 37 times.
|
45 | for (const auto& entry : std::filesystem::directory_iterator("/etc/wpa_supplicant", ec)) { |
| 995 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | std::string name = entry.path().filename(); |
| 996 |
11/30✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 8 times.
✓ Branch 12 taken 8 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 8 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 8 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 8 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 8 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
|
40 | if (!startsWith(name, "wpa_supplicant-wl") || !endsWith(name, ".conf")) { |
| 997 | ✗ | continue; | |
| 998 | } | ||
| 999 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | std::string intf = name.substr(15, name.size() - 15 - 5); |
| 1000 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
8 | if (!isShellSafeWord(intf)) { |
| 1001 | ✗ | continue; | |
| 1002 | } | ||
| 1003 |
4/6✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 7 times.
|
8 | if (!FileExists("/sys/class/net/" + intf)) { |
| 1004 | // Not plugged in: a restart would only time out again, and the | ||
| 1005 | // udev rule starts it if the adapter turns up. | ||
| 1006 | 1 | continue; | |
| 1007 | } | ||
| 1008 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | std::string unit = "wpa_supplicant@" + intf; |
| 1009 |
2/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 | std::string state = execAndReturn("/usr/bin/systemctl is-active " + unit); |
| 1010 |
14/28✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 3 times.
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 3 times.
✓ Branch 14 taken 7 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 7 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 3 times.
✓ Branch 19 taken 4 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
|
40 | if ((contains(state, "inactive") || contains(state, "failed")) && |
| 1011 |
16/28✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 4 times.
✓ Branch 11 taken 3 times.
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 3 times.
✓ Branch 14 taken 4 times.
✓ Branch 15 taken 3 times.
✓ Branch 16 taken 4 times.
✓ Branch 17 taken 3 times.
✓ Branch 18 taken 4 times.
✓ Branch 19 taken 3 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
|
15 | contains(execAndReturn("/usr/bin/systemctl is-enabled " + unit), "enabled")) { |
| 1012 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | printf("Need to restart %s\n", unit.c_str()); |
| 1013 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | execbg("systemctl restart \"" + unit + ".service\" &"); |
| 1014 | } | ||
| 1015 |
4/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 1 times.
|
46 | } |
| 1016 | 37 | } | |
| 1017 | |||
| 1018 | // Build the spoken "I Have Found The Following I P Addresses, ..." string from | ||
| 1019 | // the current non-tether IPv4 addresses. Returns "" if none are present yet. | ||
| 1020 | 232 | static std::string buildIPAnnounceString() { | |
| 1021 |
1/2✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
|
232 | std::string announce = "I Have Found The Following I P Addresses"; |
| 1022 | 232 | bool found = false; | |
| 1023 | 232 | struct ifaddrs* ifAddrStruct = NULL; | |
| 1024 | 232 | struct ifaddrs* ifa = NULL; | |
| 1025 | 232 | void* tmpAddrPtr = NULL; | |
| 1026 | 232 | getifaddrs(&ifAddrStruct); | |
| 1027 |
2/2✓ Branch 0 taken 821 times.
✓ Branch 1 taken 232 times.
|
1053 | for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) { |
| 1028 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 821 times.
|
821 | if (!ifa->ifa_addr) { |
| 1029 | 433 | continue; | |
| 1030 | } | ||
| 1031 |
1/2✓ Branch 0 taken 821 times.
✗ Branch 1 not taken.
|
821 | std::string nm = ifa->ifa_name; |
| 1032 |
16/30✓ Branch 0 taken 821 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 821 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 816 times.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 816 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 816 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 428 times.
✓ Branch 11 taken 388 times.
✓ Branch 12 taken 816 times.
✓ Branch 13 taken 5 times.
✓ Branch 14 taken 816 times.
✓ Branch 15 taken 5 times.
✓ Branch 16 taken 821 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 821 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 433 times.
✓ Branch 21 taken 388 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
|
4095 | if (startsWith(nm, "usb") || startsWith(nm, "lo")) { |
| 1033 | 433 | continue; | |
| 1034 | } | ||
| 1035 |
2/2✓ Branch 0 taken 47 times.
✓ Branch 1 taken 341 times.
|
388 | if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4 |
| 1036 | // is a valid IP4 Address | ||
| 1037 | 47 | tmpAddrPtr = &((struct sockaddr_in*)ifa->ifa_addr)->sin_addr; | |
| 1038 | char addressBuffer[INET_ADDRSTRLEN]; | ||
| 1039 | 47 | inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); | |
| 1040 |
1/2✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
|
47 | std::string addr = addressBuffer; |
| 1041 |
3/4✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 1 times.
|
47 | if (isUpstreamAddress(addr)) { |
| 1042 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | printf("FPP - Found %s IP Address %s\n", ifa->ifa_name, addressBuffer); |
| 1043 |
3/6✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 46 times.
✗ Branch 5 not taken.
|
46 | announce += ", " + std::string(addressBuffer); |
| 1044 | 46 | found = true; | |
| 1045 | } | ||
| 1046 | 47 | } | |
| 1047 |
2/2✓ Branch 0 taken 388 times.
✓ Branch 1 taken 433 times.
|
821 | } |
| 1048 |
1/2✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
|
232 | if (ifAddrStruct != NULL) { |
| 1049 | 232 | freeifaddrs(ifAddrStruct); | |
| 1050 | } | ||
| 1051 |
6/10✓ Branch 0 taken 44 times.
✓ Branch 1 taken 188 times.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 188 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 188 times.
✓ Branch 7 taken 44 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
652 | return found ? announce : ""; |
| 1052 | 232 | } | |
| 1053 | |||
| 1054 | // A USB network adapter can enumerate, bind, and bring up its netdev, and then | ||
| 1055 | // have the USB link itself fail at the protocol layer: every register access | ||
| 1056 | // returns -EPROTO, the radio never associates, and the board runs perfectly | ||
| 1057 | // while being completely unreachable. Measured at roughly 5% of boots on one | ||
| 1058 | // adapter/controller pairing, and the box stays dark until someone walks out | ||
| 1059 | // to it. | ||
| 1060 | // | ||
| 1061 | // Nothing recovers this in software. Tried on hardware and rejected: unbind and | ||
| 1062 | // rebind on the usb driver (re-enumerates, then fails its descriptor reads and | ||
| 1063 | // never returns), disabling and re-enabling the root-hub port (leaves it "not | ||
| 1064 | // attached"), and unbinding the host controller (brings the controller back but | ||
| 1065 | // not the device). Even a physical replug does not reliably fix it. A reboot | ||
| 1066 | // does, so that is what this does. | ||
| 1067 | // | ||
| 1068 | // The whole difficulty is firing only for this failure and nothing else. All of | ||
| 1069 | // the following must hold: | ||
| 1070 | // - a USB-attached network interface exists and has no carrier | ||
| 1071 | // - no other interface has an address, so we are not already reachable | ||
| 1072 | // - tethering is not the configured intent | ||
| 1073 | // - this boot's kernel log shows USB protocol errors | ||
| 1074 | // That last condition is what separates a wedged adapter from an ordinary "no | ||
| 1075 | // access point in range", "wrong passphrase", or "AP is down" -- none of which a | ||
| 1076 | // reboot would fix, and all of which it would hide. | ||
| 1077 | static constexpr int USB_WEDGE_MAX_REBOOTS = 2; | ||
| 1078 | |||
| 1079 | 46 | static std::string usbWedgeStateFile() { | |
| 1080 | // /var/tmp, not media/tmp (fppinit.service wipes that on stop, and this has | ||
| 1081 | // to survive exactly the reboot it is counting) and not media/config (this | ||
| 1082 | // is transient state, and a restored config backup must not carry a stale | ||
| 1083 | // count into an unrelated box). | ||
| 1084 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
92 | return "/var/tmp/fpp-usbnet-wedge-reboots"; |
| 1085 | } | ||
| 1086 | |||
| 1087 | // Attempts made recently. Deliberately expires: a box power-cycled hours later | ||
| 1088 | // should get a fresh pair of tries rather than inheriting a stale count. | ||
| 1089 | 2 | static int usbWedgeRebootCount() { | |
| 1090 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | std::string c = GetFileContents(usbWedgeStateFile()); |
| 1091 | 2 | int count = 0; | |
| 1092 | 2 | long long when = 0; | |
| 1093 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | if (c.empty() || sscanf(c.c_str(), "%d %lld", &count, &when) != 2) { |
| 1094 | 2 | return 0; | |
| 1095 | } | ||
| 1096 | ✗ | if ((((long long)time(nullptr)) - when) > 3600) { | |
| 1097 | ✗ | return 0; | |
| 1098 | } | ||
| 1099 | ✗ | return count; | |
| 1100 | 2 | } | |
| 1101 | |||
| 1102 | 42 | static void clearUsbWedgeRebootCount() { | |
| 1103 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | unlink(usbWedgeStateFile().c_str()); |
| 1104 | 42 | } | |
| 1105 | |||
| 1106 | // USB port ids (e.g. "1-1.2") of USB-attached network interfaces that have no | ||
| 1107 | // carrier. An interface that has carrier is working even if DHCP has not | ||
| 1108 | // finished, and is none of our business. | ||
| 1109 | 15 | static std::set<std::string> carrierlessUsbNetDeviceIds() { | |
| 1110 | 15 | std::set<std::string> ids; | |
| 1111 | 15 | std::error_code dirEc; | |
| 1112 |
5/8✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 38 times.
✓ Branch 7 taken 15 times.
|
53 | for (const auto& entry : std::filesystem::directory_iterator("/sys/class/net", dirEc)) { |
| 1113 |
2/4✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
|
38 | std::string ifName = entry.path().filename(); |
| 1114 |
12/20✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 23 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 23 times.
✓ Branch 10 taken 23 times.
✓ Branch 11 taken 15 times.
✓ Branch 12 taken 23 times.
✓ Branch 13 taken 15 times.
✓ Branch 14 taken 15 times.
✓ Branch 15 taken 23 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
84 | if (ifName == "lo" || startsWith(ifName, "usb")) { |
| 1115 | 15 | continue; // usb0/usb1 are the board's own gadget interfaces, not adapters | |
| 1116 | } | ||
| 1117 |
3/6✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
|
23 | std::string carrier = GetFileContents(entry.path().string() + "/carrier"); |
| 1118 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | TrimWhiteSpace(carrier); |
| 1119 |
3/4✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 12 times.
|
23 | if (carrier == "1") { |
| 1120 | 11 | continue; | |
| 1121 | } | ||
| 1122 | 12 | std::error_code ec; | |
| 1123 |
3/6✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
12 | std::filesystem::path dev = std::filesystem::canonical(entry.path() / "device", ec); |
| 1124 |
8/12✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 9 times.
✓ Branch 8 taken 9 times.
✓ Branch 9 taken 3 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
12 | if (ec || dev.string().find("/usb") == std::string::npos) { |
| 1125 | 9 | continue; // onboard MAC; a reboot is not indicated for it | |
| 1126 | } | ||
| 1127 | // .../usb1/1-1/1-1.2/1-1.2:1.0 -- the interface node's parent is the USB | ||
| 1128 | // device, and its name is the port id the kernel logs errors against. | ||
| 1129 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | std::string usbId = dev.parent_path().filename(); |
| 1130 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | if (!usbId.empty() && usbId.find(':') == std::string::npos) { |
| 1131 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | ids.insert(usbId); |
| 1132 | } | ||
| 1133 |
6/6✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 35 times.
|
82 | } |
| 1134 | 15 | return ids; | |
| 1135 | ✗ | } | |
| 1136 | |||
| 1137 | 3 | static bool anyInterfaceHasAddress() { | |
| 1138 | 3 | struct ifaddrs* addrs = nullptr; | |
| 1139 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (getifaddrs(&addrs) != 0) { |
| 1140 | ✗ | return true; // cannot tell: assume reachable and do not reboot | |
| 1141 | } | ||
| 1142 | 3 | bool found = false; | |
| 1143 |
3/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
6 | for (struct ifaddrs* ifa = addrs; ifa && !found; ifa = ifa->ifa_next) { |
| 1144 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET) { |
| 1145 | 3 | continue; | |
| 1146 | } | ||
| 1147 | ✗ | std::string nm = ifa->ifa_name; | |
| 1148 | ✗ | if (nm == "lo" || startsWith(nm, "usb")) { | |
| 1149 | ✗ | continue; | |
| 1150 | } | ||
| 1151 | ✗ | uint32_t a = ntohl(((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr); | |
| 1152 | ✗ | if ((a >> 16) == 0xA9FE) { | |
| 1153 | ✗ | continue; // 169.254/16 link-local is not a usable address | |
| 1154 | } | ||
| 1155 | ✗ | found = true; | |
| 1156 | ✗ | } | |
| 1157 | 3 | freeifaddrs(addrs); | |
| 1158 | 3 | return found; | |
| 1159 | } | ||
| 1160 | |||
| 1161 | // The signature of a link that has failed below the driver: -EPROTO/-ETIMEDOUT | ||
| 1162 | // against the adapter's own USB port id. | ||
| 1163 | // | ||
| 1164 | // Anchoring on the port id is what keeps a flaky USB stick from rebooting a box | ||
| 1165 | // whose WiFi is merely out of range -- both conditions would otherwise be true | ||
| 1166 | // at once. Every line the kernel and the driver emit for this failure carries | ||
| 1167 | // the id, in both forms: | ||
| 1168 | // usb 1-1.2: device descriptor read/64, error -71 | ||
| 1169 | // rtw88_8822bu 1-1.2:1.0: write register 0x5 failed with -71 | ||
| 1170 | // Thresholded because one retried transfer is normal; a real wedge produces | ||
| 1171 | // thousands. | ||
| 1172 | 3 | static bool kernelLogShowsUsbProtocolFailure(const std::set<std::string>& usbIds) { | |
| 1173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (usbIds.empty()) { |
| 1174 | ✗ | return false; | |
| 1175 | } | ||
| 1176 | 3 | std::string ids; | |
| 1177 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | for (const auto& id : usbIds) { |
| 1178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!ids.empty()) { |
| 1179 | ✗ | ids += "|"; | |
| 1180 | } | ||
| 1181 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
|
12 | for (char c : id) { // the ids contain '.', which must not be a wildcard |
| 1182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (c == '.') { |
| 1183 | ✗ | ids += "\\."; | |
| 1184 | } else { | ||
| 1185 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | ids += c; |
| 1186 | } | ||
| 1187 | } | ||
| 1188 | } | ||
| 1189 | std::string out = execAndReturn( | ||
| 1190 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
6 | "/bin/dmesg 2>/dev/null | /bin/grep -cE '(" + ids + "):.*(error -(71|110)" + |
| 1191 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | "|failed with -(71|110)|fail, status: -(71|110))'"); |
| 1192 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | TrimWhiteSpace(out); |
| 1193 | 3 | return atoi(out.c_str()) >= 4; | |
| 1194 | 3 | } | |
| 1195 | |||
| 1196 | // Set once a reboot has been asked for, so the rest of postNetwork can stop | ||
| 1197 | // doing work for a boot that is about to be thrown away. | ||
| 1198 | static std::atomic<bool> s_usbWedgeRebootPending{false}; | ||
| 1199 | |||
| 1200 | 37 | bool usbWedgeRebootPending() { | |
| 1201 | 37 | return s_usbWedgeRebootPending.load(); | |
| 1202 | } | ||
| 1203 | |||
| 1204 | // Returns true if a reboot was requested. Does NOT block waiting for it. | ||
| 1205 | // | ||
| 1206 | // fppd's start depends on fpp_postnetwork, so anything that stalls this service | ||
| 1207 | // past its TimeoutStartSec takes fppd down with it. /usr/sbin/reboot is a | ||
| 1208 | // symlink to systemctl, which asks logind over D-Bus to set a wall message; | ||
| 1209 | // on an early-boot box that activation can time out. Calling it synchronously | ||
| 1210 | // hung this service for 37+ seconds until systemd killed it -- the reboot never | ||
| 1211 | // happened AND fppd never started, which is worse than the wedge being fixed. | ||
| 1212 | // So: go straight to the system manager (no logind), don't wait for the job, | ||
| 1213 | // and background the call so it cannot stall this thread no matter what. | ||
| 1214 | 16 | static bool rebootIfUsbNetWedged() { | |
| 1215 |
3/6✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 16 times.
|
32 | if (getRawSettingInt("AutoRebootOnUSBNetworkFailure", 1) == 0) { |
| 1216 | ✗ | return false; | |
| 1217 | } | ||
| 1218 |
4/6✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 15 times.
|
32 | if (getRawSettingInt("EnableTethering", 0) == 1) { |
| 1219 | 1 | return false; // no upstream network is expected; being without one is the plan | |
| 1220 | } | ||
| 1221 |
1/2✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
|
15 | std::set<std::string> stalled = carrierlessUsbNetDeviceIds(); |
| 1222 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
|
15 | if (stalled.empty()) { |
| 1223 | 12 | return false; // no USB adapter in trouble, so a reboot cannot be the answer | |
| 1224 | } | ||
| 1225 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (anyInterfaceHasAddress()) { |
| 1226 | ✗ | return false; // reachable some other way; never reboot a box that is fine | |
| 1227 | } | ||
| 1228 |
3/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
|
3 | if (!kernelLogShowsUsbProtocolFailure(stalled)) { |
| 1229 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | printf("FPP - USB network adapter has no carrier, but its USB link is healthy " |
| 1230 | "(no access point in range?); not rebooting\n"); | ||
| 1231 | 1 | return false; | |
| 1232 | } | ||
| 1233 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | int count = usbWedgeRebootCount(); |
| 1234 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (count >= USB_WEDGE_MAX_REBOOTS) { |
| 1235 | ✗ | printf("FPP - USB network adapter is wedged, but %d reboots have not cleared it; " | |
| 1236 | "staying up rather than looping\n", | ||
| 1237 | count); | ||
| 1238 | ✗ | return false; | |
| 1239 | } | ||
| 1240 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | printf("FPP - USB network adapter is wedged: USB protocol errors, no carrier, and no " |
| 1241 | "other network. Rebooting (attempt %d of %d).\n", | ||
| 1242 | count + 1, USB_WEDGE_MAX_REBOOTS); | ||
| 1243 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | PutFileContents(usbWedgeStateFile(), |
| 1244 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
4 | std::to_string(count + 1) + " " + std::to_string((long long)time(nullptr))); |
| 1245 | 2 | sync(); | |
| 1246 | 2 | s_usbWedgeRebootPending = true; | |
| 1247 | // Forced reboot, not a graceful one. The condition being recovered from is a | ||
| 1248 | // wedged USB stack, which is precisely what stops a graceful shutdown from | ||
| 1249 | // finishing: tasks block in uninterruptible sleep on the dead device, so | ||
| 1250 | // neither SIGTERM nor SIGKILL clears them. Measured on hardware, an ordinary | ||
| 1251 | // "systemctl start reboot.target" began stopping units 8s after this point | ||
| 1252 | // and then sat there for 37 minutes, the controller logging | ||
| 1253 | // "musb-hdrc: VBUS_ERROR in a_wait_bcon" while it churned -- the board was | ||
| 1254 | // unreachable that whole time, which is the outage this is supposed to end. | ||
| 1255 | // sync() above is what makes skipping the unmounts acceptable; the sysrq | ||
| 1256 | // fallback covers systemctl itself being unable to run. | ||
| 1257 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | execbg("( /bin/systemctl --force --force reboot" |
| 1258 | " || { echo 1 > /proc/sys/kernel/sysrq; echo b > /proc/sysrq-trigger; } ) &"); | ||
| 1259 | 2 | return true; | |
| 1260 | 15 | } | |
| 1261 | |||
| 1262 | // WiFi interfaces with client settings, which may still associate and get an | ||
| 1263 | // address. Read from the config, so it includes adapters that are not present. | ||
| 1264 | 86 | static std::set<std::string> wifiClientInterfaces() { | |
| 1265 | 86 | std::set<std::string> ret; | |
| 1266 |
2/4✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
✗ Branch 3 not taken.
|
86 | int tetherEnabled = getRawSettingInt("EnableTethering", 0); |
| 1267 | 86 | std::string tetherInterface; | |
| 1268 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 79 times.
|
86 | if (tetherEnabled == 1) { |
| 1269 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | tetherInterface = FindTetherWIFIAdapater(); // its settings describe the AP, not a client |
| 1270 | } | ||
| 1271 | 86 | std::error_code ec; | |
| 1272 |
6/10✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 86 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 50 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 50 times.
✓ Branch 9 taken 86 times.
|
136 | for (const auto& entry : std::filesystem::directory_iterator(FPP_MEDIA_DIR + "/config", ec)) { |
| 1273 |
2/4✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
|
50 | std::string name = entry.path().filename(); |
| 1274 |
13/24✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
✓ Branch 5 taken 25 times.
✓ Branch 6 taken 25 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 22 times.
✓ Branch 10 taken 25 times.
✓ Branch 11 taken 25 times.
✓ Branch 12 taken 50 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 50 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 28 times.
✓ Branch 17 taken 22 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
150 | if (!startsWith(name, "interface.wl") || name.substr(10) == tetherInterface) { |
| 1275 | 28 | continue; | |
| 1276 | } | ||
| 1277 |
2/4✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
|
22 | auto settings = loadSettingsFile(entry.path()); |
| 1278 |
12/32✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 22 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 22 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 22 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 22 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 22 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 22 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 22 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 22 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 22 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
|
110 | if (!settings["SSID"].empty() && settings["SSID"] != "\"\"") { |
| 1279 |
2/4✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
|
22 | ret.insert(name.substr(10)); |
| 1280 | } | ||
| 1281 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 28 times.
|
136 | } |
| 1282 | // A hand-written supplicant config counts too | ||
| 1283 |
5/8✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 72 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 72 times.
✓ Branch 7 taken 86 times.
|
158 | for (const auto& entry : std::filesystem::directory_iterator("/etc/wpa_supplicant", ec)) { |
| 1284 |
2/4✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
|
72 | std::string name = entry.path().filename(); |
| 1285 |
15/30✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 48 times.
✓ Branch 6 taken 24 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 24 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 24 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 24 times.
✓ Branch 13 taken 48 times.
✓ Branch 14 taken 24 times.
✓ Branch 15 taken 48 times.
✓ Branch 16 taken 72 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 72 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 24 times.
✓ Branch 21 taken 48 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
|
264 | if (startsWith(name, "wpa_supplicant-wl") && endsWith(name, ".conf")) { |
| 1286 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
24 | ret.insert(name.substr(15, name.size() - 15 - 5)); |
| 1287 | } | ||
| 1288 | 158 | } | |
| 1289 | 86 | return ret; | |
| 1290 | 86 | } | |
| 1291 | |||
| 1292 | // An address can only come from a wired interface once it has carrier (static | ||
| 1293 | // configs are not applied without it), or from a present WiFi adapter with | ||
| 1294 | // client settings. With neither, a first boot would sit out the whole wait | ||
| 1295 | // before tethering comes up, so for a board with no wired port and no | ||
| 1296 | // configured WiFi adapter, the wait ends once the set of interfaces has held | ||
| 1297 | // still for a moment -- enough for USB adapters enumerating one after another | ||
| 1298 | // behind a hub (seen ~1s apart). | ||
| 1299 | // | ||
| 1300 | // A wired port without carrier is never written off early: carrier cannot tell | ||
| 1301 | // "no cable" from "still autonegotiating", which took 5.1s on a Pi 5 against a | ||
| 1302 | // gigabit switch. Giving up there started tethering, then tore it down when | ||
| 1303 | // DHCP landed a few seconds later. | ||
| 1304 | // | ||
| 1305 | // A configured WiFi adapter that is absent is not worth waiting for: postNetwork | ||
| 1306 | // runs after network.target, which wpa_supplicant@<if> orders itself before, so | ||
| 1307 | // by now its start job has either seen the adapter appear or given up on it | ||
| 1308 | // (JobTimeoutSec in etc/systemd/wpa_supplicant@.service.d). If it turns up | ||
| 1309 | // later, udev starts its supplicant then. | ||
| 1310 | class UpstreamLinkWatch { | ||
| 1311 | public: | ||
| 1312 | static constexpr int SETTLE_ITERATIONS = 20; // x 200ms | ||
| 1313 | |||
| 1314 | enum class State { | ||
| 1315 | NONE, // nothing can get an address | ||
| 1316 | POSSIBLE, // something may still get link | ||
| 1317 | LINK // something has link; its address may be about to land | ||
| 1318 | }; | ||
| 1319 | |||
| 1320 | 86 | explicit UpstreamLinkWatch(std::set<std::string> wifiClients) : | |
| 1321 | 86 | wifiClients(std::move(wifiClients)) {} | |
| 1322 | |||
| 1323 | 1565 | State check(int count) { | |
| 1324 | 1565 | std::set<std::string> state; | |
| 1325 | 1565 | bool possible = false; | |
| 1326 | 1565 | std::error_code ec; | |
| 1327 |
5/8✓ Branch 0 taken 1565 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1565 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3571 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3573 times.
✓ Branch 7 taken 1563 times.
|
5136 | for (const auto& entry : std::filesystem::directory_iterator("/sys/class/net", ec)) { |
| 1328 |
2/4✓ Branch 0 taken 3573 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3573 times.
✗ Branch 3 not taken.
|
3573 | std::string dev = entry.path().filename(); |
| 1329 |
21/36✓ Branch 0 taken 3573 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2010 times.
✓ Branch 3 taken 1563 times.
✓ Branch 4 taken 2010 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2010 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2010 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2010 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2010 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 2010 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 1596 times.
✓ Branch 17 taken 414 times.
✓ Branch 18 taken 2010 times.
✓ Branch 19 taken 1563 times.
✓ Branch 20 taken 2010 times.
✓ Branch 21 taken 1563 times.
✓ Branch 22 taken 2010 times.
✓ Branch 23 taken 1563 times.
✓ Branch 24 taken 2010 times.
✓ Branch 25 taken 1563 times.
✓ Branch 26 taken 3159 times.
✓ Branch 27 taken 414 times.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
|
7593 | if (dev == "lo" || startsWith(dev, "usb") || !FileExists(entry.path().string() + "/device")) { |
| 1330 | 3159 | continue; // loopback, the board's own gadget links, virtual devices | |
| 1331 | } | ||
| 1332 |
3/6✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 414 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 414 times.
✗ Branch 5 not taken.
|
414 | std::string flags = GetFileContents(entry.path().string() + "/flags"); |
| 1333 | 414 | bool up = strtol(flags.c_str(), nullptr, 16) & IFF_UP; | |
| 1334 |
3/6✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 414 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 414 times.
✗ Branch 5 not taken.
|
414 | state.insert(dev + (up ? ":up" : ":down")); |
| 1335 |
12/22✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 414 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 414 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 312 times.
✓ Branch 7 taken 102 times.
✓ Branch 8 taken 312 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 137 times.
✓ Branch 11 taken 175 times.
✓ Branch 12 taken 414 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 414 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 137 times.
✓ Branch 17 taken 277 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
|
414 | if (FileExists(entry.path().string() + "/wireless") && !wifiClients.count(dev)) { |
| 1336 | 137 | continue; // no client config, so it can only ever be the tether AP | |
| 1337 | } | ||
| 1338 |
3/6✓ Branch 0 taken 277 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 277 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 277 times.
✗ Branch 5 not taken.
|
277 | std::string carrier = GetFileContents(entry.path().string() + "/carrier"); |
| 1339 |
1/2✓ Branch 0 taken 277 times.
✗ Branch 1 not taken.
|
277 | TrimWhiteSpace(carrier); |
| 1340 |
3/4✓ Branch 0 taken 277 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 275 times.
|
277 | if (carrier == "1") { |
| 1341 | 2 | return State::LINK; | |
| 1342 | } | ||
| 1343 | 275 | possible = true; | |
| 1344 |
14/14✓ Branch 0 taken 275 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 275 times.
✓ Branch 3 taken 137 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 275 times.
✓ Branch 6 taken 3296 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 1563 times.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 1563 times.
✓ Branch 11 taken 2 times.
✓ Branch 12 taken 1563 times.
✓ Branch 13 taken 2 times.
|
5283 | } |
| 1345 |
3/4✓ Branch 0 taken 1563 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 1551 times.
|
1563 | if (state != lastState) { |
| 1346 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | lastState = state; |
| 1347 | 12 | lastChange = count; | |
| 1348 | } | ||
| 1349 |
8/8✓ Branch 0 taken 1288 times.
✓ Branch 1 taken 275 times.
✓ Branch 2 taken 64 times.
✓ Branch 3 taken 1224 times.
✓ Branch 4 taken 62 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1561 times.
✓ Branch 7 taken 2 times.
|
1563 | if (possible || lastState.empty() || (count - lastChange) < SETTLE_ITERATIONS) { |
| 1350 | 1561 | return State::POSSIBLE; | |
| 1351 | } | ||
| 1352 | 2 | return State::NONE; | |
| 1353 | 1565 | } | |
| 1354 | |||
| 1355 | private: | ||
| 1356 | std::set<std::string> wifiClients; | ||
| 1357 | std::set<std::string> lastState; | ||
| 1358 | int lastChange = 0; | ||
| 1359 | }; | ||
| 1360 | |||
| 1361 | 86 | bool waitForInterfacesUp(int timeOut, bool allowUsbRecovery) { | |
| 1362 | 86 | int count = 0; | |
| 1363 |
1/2✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
|
86 | UpstreamLinkWatch linkWatch(wifiClientInterfaces()); |
| 1364 | // If no network interfaces have carrier/link, don't wait for IP address - likely no network available and no point waiting for DHCP/NTP | ||
| 1365 |
3/4✓ Branch 0 taken 1609 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1565 times.
✓ Branch 3 taken 44 times.
|
1609 | while (!hasNetworkInterfaceForNTP()) { |
| 1366 |
1/2✓ Branch 0 taken 1565 times.
✗ Branch 1 not taken.
|
1565 | UpstreamLinkWatch::State link = linkWatch.check(count); |
| 1367 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1563 times.
|
1565 | if (link == UpstreamLinkWatch::State::LINK) { |
| 1368 | // Has link but no address yet: DHCP gets the rest of the timeOut | ||
| 1369 | // below rather than being cut off at the halfway mark. | ||
| 1370 | 2 | break; | |
| 1371 | } | ||
| 1372 | 1563 | bool giveUp = count >= (timeOut / 2); // spend half the timeOut waiting for interfaces to have link, then give up | |
| 1373 |
4/4✓ Branch 0 taken 1525 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1523 times.
|
1563 | if (!giveUp && link == UpstreamLinkWatch::State::NONE) { |
| 1374 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | printf("FPP - No wired port and no configured WiFi adapter present; nothing can get an address\n"); |
| 1375 | 2 | giveUp = true; | |
| 1376 | } | ||
| 1377 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1523 times.
|
1563 | if (giveUp) { |
| 1378 | // Before writing the network off, check for a USB adapter whose USB | ||
| 1379 | // link has died, which only a reboot clears. | ||
| 1380 | // | ||
| 1381 | // Opt-in, and only the boot path opts in. The tether callers below | ||
| 1382 | // are driven by networkd-dispatcher on carrier changes, and rebooting | ||
| 1383 | // from a carrier-change handler is a boot loop with extra steps. | ||
| 1384 |
7/8✓ Branch 0 taken 16 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 14 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 38 times.
|
40 | if (allowUsbRecovery && rebootIfUsbNetWedged()) { |
| 1385 | // Reboot is queued. Return at once and let the caller skip the | ||
| 1386 | // rest of postNetwork: this service is on fppd's dependency | ||
| 1387 | // chain, so overrunning its start timeout here would fail fppd | ||
| 1388 | // for the few seconds of life this boot has left. | ||
| 1389 | 2 | return false; | |
| 1390 | } | ||
| 1391 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | printf("FPP - No network interfaces with link detected after waiting for %d ms, skipping IP wait\n", count * 200); |
| 1392 | 38 | return false; | |
| 1393 | } | ||
| 1394 |
1/2✓ Branch 0 taken 1523 times.
✗ Branch 1 not taken.
|
1523 | std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 1395 | 1523 | ++count; | |
| 1396 | } | ||
| 1397 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | printf("FPP - Waited for %0.1f seconds for network interfaces to have link...\n", (((float)count) * 0.2)); |
| 1398 | 46 | std::string announce; | |
| 1399 | do { | ||
| 1400 |
1/2✓ Branch 0 taken 214 times.
✗ Branch 1 not taken.
|
214 | announce = buildIPAnnounceString(); |
| 1401 |
2/2✓ Branch 0 taken 172 times.
✓ Branch 1 taken 42 times.
|
214 | if (announce.empty()) { |
| 1402 |
1/2✓ Branch 0 taken 172 times.
✗ Branch 1 not taken.
|
172 | std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 1403 | 172 | ++count; | |
| 1404 | } | ||
| 1405 |
6/6✓ Branch 0 taken 172 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 168 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 168 times.
✓ Branch 5 taken 46 times.
|
214 | } while (announce.empty() && (count < timeOut)); |
| 1406 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 42 times.
|
46 | if (announce.empty()) { |
| 1407 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | printf("FPP - Could not get a valid IP address\n"); |
| 1408 | 4 | return false; | |
| 1409 | } | ||
| 1410 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | printf("FPP - Waited for %0.1f seconds for IP address\n", (((float)count) * 0.2f)); |
| 1411 | // Network came up, so whatever the last wedge was, it is over. Clearing here | ||
| 1412 | // rather than letting the count expire keeps the next occurrence entitled to | ||
| 1413 | // its full pair of attempts. | ||
| 1414 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | clearUsbWedgeRebootCount(); |
| 1415 | 42 | return true; | |
| 1416 | 86 | } | |
| 1417 | |||
| 1418 | // Announce the device's IP address(es) over audio with flite + pw-play. Invoked | ||
| 1419 | // from fpp-announce-ip.service (ordered after fpp_postnetwork and the PipeWire | ||
| 1420 | // services) rather than inline in postNetwork, so the CPU-heavy flite synthesis | ||
| 1421 | // doesn't compete with the tail of postNetwork / fppd startup on single-core | ||
| 1422 | // boards. Network is already up by the time the service runs. | ||
| 1423 | 2 | void announceIPAddresses() { | |
| 1424 |
8/20✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 2 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
6 | if (getRawSettingInt("disableIPAnnouncement", 0) || !FileExists("/usr/bin/flite")) { |
| 1425 | ✗ | return; | |
| 1426 | } | ||
| 1427 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | std::string announce = buildIPAnnounceString(); |
| 1428 | // postNetwork's IP wait can time out with the DHCP lease landing moments | ||
| 1429 | // later; this service is off fppd's critical path, so give the address a | ||
| 1430 | // bounded chance to appear rather than silently skipping the announcement. | ||
| 1431 |
5/6✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✓ Branch 5 taken 2 times.
|
18 | for (int i = 0; announce.empty() && i < 75; ++i) { |
| 1432 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 1433 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | announce = buildIPAnnounceString(); |
| 1434 | } | ||
| 1435 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (announce.empty()) { |
| 1436 | ✗ | printf("FPP - announceIP: no usable IP address to announce\n"); | |
| 1437 | ✗ | return; | |
| 1438 | } | ||
| 1439 | // flite is an ALSA client, so playing directly (flite -t) would route | ||
| 1440 | // through the pipewire-alsa plugin + /root/.asoundrc -- the fragile ALSA | ||
| 1441 | // chain we're retiring. Instead render to a WAV and play it through PipeWire | ||
| 1442 | // natively with pw-play. pw-play needs the runtime env vars to find FPP's | ||
| 1443 | // PipeWire instance at /run/pipewire-fpp. /run/fppd is created by setupAudio. | ||
| 1444 | // Both halves are hard-capped. A PipeWire graph that cannot reach a working | ||
| 1445 | // sink does not fail pw-play -- the node parks in error and the stream never | ||
| 1446 | // starts *or* ends, so pw-play blocks in poll() indefinitely. Unbounded, that | ||
| 1447 | // turns a cosmetic audio problem into an unbootable box. The real fix is | ||
| 1448 | // upstream (FPP now stops WirePlumber from creating a second node fighting | ||
| 1449 | // for the same PCM), but nothing about announcing an IP address is worth | ||
| 1450 | // risking a hung boot service over, so cap it here too. | ||
| 1451 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | const std::string announceWav = "/run/fppd/ip-announce.wav"; |
| 1452 | std::string fliteCmd = | ||
| 1453 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
4 | "/usr/bin/timeout 60 /usr/bin/flite -voice awb -t \"" + announce + "\" -o " + announceWav + |
| 1454 | " && PIPEWIRE_RUNTIME_DIR=/run/pipewire-fpp XDG_RUNTIME_DIR=/run/pipewire-fpp" | ||
| 1455 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | " /usr/bin/timeout 60 /usr/bin/pw-play " + |
| 1456 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | announceWav; |
| 1457 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | exec(fliteCmd); |
| 1458 | 2 | unlink(announceWav.c_str()); // don't leave the rendered WAV behind in /run | |
| 1459 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | } |
| 1460 | // Turn off 802.11 power saving on every wireless interface. A show controller | ||
| 1461 | // wants its network responsive, not its radio dozing between beacons; the power | ||
| 1462 | // saved is irrelevant on a mains-powered board. | ||
| 1463 | // | ||
| 1464 | // Was dead code until now -- written but never called, from the commit that | ||
| 1465 | // introduced it -- so this had simply never run. The drivers in use happen to | ||
| 1466 | // default power_save off, which is why nobody noticed; this makes it true by | ||
| 1467 | // intent rather than by luck, and covers an adapter whose driver defaults the | ||
| 1468 | // other way. | ||
| 1469 | 35 | void disableWLANPowerManagement() { | |
| 1470 | 35 | struct ifaddrs* ifAddrStruct = NULL; | |
| 1471 | 35 | getifaddrs(&ifAddrStruct); | |
| 1472 | // getifaddrs yields one entry per address family, so a single interface shows | ||
| 1473 | // up as AF_PACKET plus AF_INET plus AF_INET6. Collect names first: otherwise | ||
| 1474 | // this forks iw two or three times per adapter for no reason. | ||
| 1475 | 35 | std::set<std::string> wireless; | |
| 1476 |
2/2✓ Branch 0 taken 134 times.
✓ Branch 1 taken 35 times.
|
169 | for (struct ifaddrs* ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) { |
| 1477 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 134 times.
|
134 | if (!ifa->ifa_addr) { |
| 1478 | ✗ | continue; | |
| 1479 | } | ||
| 1480 |
1/2✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
|
268 | std::string nm = ifa->ifa_name; |
| 1481 |
4/6✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 134 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 126 times.
|
268 | if (startsWith(nm, "wl")) { |
| 1482 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | wireless.insert(nm); |
| 1483 | } | ||
| 1484 | 134 | } | |
| 1485 |
1/2✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
|
35 | if (ifAddrStruct != NULL) { |
| 1486 | 35 | freeifaddrs(ifAddrStruct); | |
| 1487 | } | ||
| 1488 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 35 times.
|
37 | for (const auto& nm : wireless) { |
| 1489 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | printf("FPP - Disabling power saving for %s\n", nm.c_str()); |
| 1490 |
4/8✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
|
2 | exec("/usr/sbin/iw dev " + ShellSingleQuote(nm) + " set power_save off 2>/dev/null > /dev/null"); |
| 1491 | } | ||
| 1492 | 35 | } | |
| 1493 | |||
| 1494 | 81 | void maybeEnableTethering() { | |
| 1495 |
2/4✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
✗ Branch 3 not taken.
|
81 | int te = getRawSettingInt("EnableTethering", 0); |
| 1496 |
2/2✓ Branch 0 taken 69 times.
✓ Branch 1 taken 12 times.
|
81 | if (te == 0) { |
| 1497 | 69 | bool found = false; | |
| 1498 | 69 | struct ifaddrs* ifAddrStruct = NULL; | |
| 1499 | 69 | struct ifaddrs* ifa = NULL; | |
| 1500 | 69 | void* tmpAddrPtr = NULL; | |
| 1501 | 69 | getifaddrs(&ifAddrStruct); | |
| 1502 |
2/2✓ Branch 0 taken 234 times.
✓ Branch 1 taken 69 times.
|
303 | for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) { |
| 1503 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 234 times.
|
234 | if (!ifa->ifa_addr) { |
| 1504 | 141 | continue; | |
| 1505 | } | ||
| 1506 |
1/2✓ Branch 0 taken 234 times.
✗ Branch 1 not taken.
|
236 | std::string nm = ifa->ifa_name; |
| 1507 |
13/30✓ Branch 0 taken 234 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 234 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 234 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 234 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 234 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 141 times.
✓ Branch 11 taken 93 times.
✓ Branch 12 taken 234 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 234 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 234 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 234 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 141 times.
✓ Branch 21 taken 93 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
|
1170 | if (startsWith(nm, "usb") || startsWith(nm, "lo")) { |
| 1508 | 141 | continue; | |
| 1509 | } | ||
| 1510 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 54 times.
|
93 | if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4 |
| 1511 | // is a valid IP4 Address | ||
| 1512 | 39 | tmpAddrPtr = &((struct sockaddr_in*)ifa->ifa_addr)->sin_addr; | |
| 1513 | char addressBuffer[INET_ADDRSTRLEN]; | ||
| 1514 | 39 | inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); | |
| 1515 |
1/2✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
|
39 | std::string addr = addressBuffer; |
| 1516 |
2/4✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
|
39 | if (isUpstreamAddress(addr)) { |
| 1517 | 39 | found = true; | |
| 1518 | } | ||
| 1519 | 39 | } | |
| 1520 |
2/2✓ Branch 0 taken 93 times.
✓ Branch 1 taken 141 times.
|
234 | } |
| 1521 |
1/2✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
|
69 | if (ifAddrStruct != NULL) { |
| 1522 | 69 | freeifaddrs(ifAddrStruct); | |
| 1523 | } | ||
| 1524 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 39 times.
|
69 | if (!found) { |
| 1525 | // Before enabling tethering, check if any wired interface has carrier/link. | ||
| 1526 | // If a cable is connected, DHCP will likely succeed shortly - don't enable tethering. | ||
| 1527 | // This matches the original bash MaybeEnableTethering logic that checked ethtool link. | ||
| 1528 | 30 | bool hasWiredLink = false; | |
| 1529 |
5/8✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 63 times.
✓ Branch 7 taken 7 times.
|
70 | for (const auto& entry : std::filesystem::directory_iterator("/sys/class/net/")) { |
| 1530 |
2/4✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
|
63 | std::string dev = entry.path().filename(); |
| 1531 |
29/58✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 37 times.
✓ Branch 5 taken 26 times.
✓ Branch 6 taken 37 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 37 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 37 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 37 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 37 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 37 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 37 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 37 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 37 times.
✓ Branch 24 taken 37 times.
✓ Branch 25 taken 26 times.
✓ Branch 26 taken 37 times.
✓ Branch 27 taken 26 times.
✓ Branch 28 taken 37 times.
✓ Branch 29 taken 26 times.
✓ Branch 30 taken 37 times.
✓ Branch 31 taken 26 times.
✓ Branch 32 taken 37 times.
✓ Branch 33 taken 26 times.
✓ Branch 34 taken 37 times.
✓ Branch 35 taken 26 times.
✓ Branch 36 taken 63 times.
✗ Branch 37 not taken.
✓ Branch 38 taken 63 times.
✗ Branch 39 not taken.
✓ Branch 40 taken 26 times.
✓ Branch 41 taken 37 times.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 53 not taken.
✗ Branch 54 not taken.
✗ Branch 55 not taken.
✗ Branch 56 not taken.
✗ Branch 57 not taken.
|
411 | if (startsWith(dev, "eth") || startsWith(dev, "en") || startsWith(dev, "br") || startsWith(dev, "bond")) { |
| 1532 |
2/4✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
|
26 | std::string carrierPath = "/sys/class/net/" + dev + "/carrier"; |
| 1533 |
2/4✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
|
26 | if (FileExists(carrierPath)) { |
| 1534 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | std::string carrier = GetFileContents(carrierPath); |
| 1535 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | TrimWhiteSpace(carrier); |
| 1536 |
3/4✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 3 times.
|
26 | if (carrier == "1") { |
| 1537 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | printf("FPP - Wired interface %s has link, not enabling tethering\n", dev.c_str()); |
| 1538 | 23 | hasWiredLink = true; | |
| 1539 | 23 | break; | |
| 1540 | } | ||
| 1541 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 23 times.
|
26 | } |
| 1542 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 23 times.
|
26 | } |
| 1543 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 23 times.
|
93 | } |
| 1544 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 7 times.
|
30 | if (hasWiredLink) { |
| 1545 | // A wired interface has link - DHCP should complete soon. | ||
| 1546 | // Don't enable tethering; the routable.d/check_tether dispatcher | ||
| 1547 | // will clean up tethering if it was enabled from a previous boot. | ||
| 1548 | 23 | return; | |
| 1549 | } | ||
| 1550 | // Check if the tether interface has a wifi client configuration | ||
| 1551 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | std::string tetherInterface = FindTetherWIFIAdapater(); |
| 1552 |
2/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 | std::string interfaceConfigFile = FPP_MEDIA_DIR + "/config/interface." + tetherInterface; |
| 1553 |
3/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 6 times.
|
7 | if (FileExists(interfaceConfigFile)) { |
| 1554 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | auto interfaceSettings = loadSettingsFile(interfaceConfigFile); |
| 1555 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
2 | if (!interfaceSettings["SSID"].empty()) { |
| 1556 | // WiFi client config exists - give it time to connect and get DHCP | ||
| 1557 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | printf("FPP - %s has WiFi SSID configured, waiting up to 12s for connection and DHCP...\n", tetherInterface.c_str()); |
| 1558 | 1 | int waitCount = 0; | |
| 1559 | 1 | bool gotIP = false; | |
| 1560 | 1 | bool connected = false; | |
| 1561 |
4/6✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
|
25 | while (waitCount < 24 && !gotIP && !connected) { // 24 * 500ms = 12 seconds |
| 1562 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 1563 | // Check for IP address | ||
| 1564 | 24 | struct ifaddrs* ifAddrCheck = NULL; | |
| 1565 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (getifaddrs(&ifAddrCheck) == 0) { |
| 1566 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
|
48 | for (struct ifaddrs* ifa = ifAddrCheck; ifa != NULL; ifa = ifa->ifa_next) { |
| 1567 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
|
24 | if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { |
| 1568 | ✗ | std::string ifname = ifa->ifa_name; | |
| 1569 | ✗ | if (ifname == tetherInterface) { | |
| 1570 | ✗ | void* tmpAddrPtr = &((struct sockaddr_in*)ifa->ifa_addr)->sin_addr; | |
| 1571 | char addressBuffer[INET_ADDRSTRLEN]; | ||
| 1572 | ✗ | inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); | |
| 1573 | ✗ | std::string addr = addressBuffer; | |
| 1574 | ✗ | if (!contains(addr, "169.254.")) { // Ignore link-local | |
| 1575 | ✗ | printf("FPP - %s got IP address %s\n", tetherInterface.c_str(), addr.c_str()); | |
| 1576 | ✗ | gotIP = true; | |
| 1577 | ✗ | break; | |
| 1578 | } | ||
| 1579 | ✗ | } | |
| 1580 | ✗ | } | |
| 1581 | } | ||
| 1582 | 24 | freeifaddrs(ifAddrCheck); | |
| 1583 | } | ||
| 1584 | // Also check if connected to AP (even without IP yet) | ||
| 1585 |
3/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 11 times.
|
24 | if (!gotIP && waitCount > 10) { // After 5 seconds, start checking connection status |
| 1586 |
4/8✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
|
13 | std::string iwOutput = execAndReturn("/usr/sbin/iw " + ShellSingleQuote(tetherInterface) + " link"); |
| 1587 |
3/6✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 13 times.
|
26 | if (contains(iwOutput, "Connected to")) { |
| 1588 | ✗ | printf("FPP - %s connected to WiFi, keeping WiFi config active\n", tetherInterface.c_str()); | |
| 1589 | ✗ | connected = true; | |
| 1590 | } | ||
| 1591 | 13 | } | |
| 1592 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
24 | if (gotIP || connected) |
| 1593 | break; | ||
| 1594 | 24 | waitCount++; | |
| 1595 | } | ||
| 1596 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (!gotIP && !connected) { |
| 1597 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | printf("FPP - %s no IP or connection after 12s, enabling tethering\n", tetherInterface.c_str()); |
| 1598 | 1 | te = 1; | |
| 1599 | } | ||
| 1600 | } else { | ||
| 1601 | // Interface config exists but no SSID - enable tethering | ||
| 1602 | ✗ | te = 1; | |
| 1603 | } | ||
| 1604 | 1 | } else { | |
| 1605 | // did not find an ip address and no wifi client config exists | ||
| 1606 | 6 | te = 1; | |
| 1607 | } | ||
| 1608 | 7 | } | |
| 1609 | } | ||
| 1610 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | std::string tetherInterface = FindTetherWIFIAdapater(); |
| 1611 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 44 times.
|
58 | if (te == 1) { |
| 1612 |
4/6✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 8 times.
|
14 | if (!FileExists("/sys/class/net/" + tetherInterface)) { |
| 1613 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | printf("No interface for tethering.\n"); |
| 1614 | 6 | te = 2; | |
| 1615 | } | ||
| 1616 | } | ||
| 1617 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 50 times.
|
58 | if (te == 1) { |
| 1618 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | std::string c = CreateHostAPDConfig(tetherInterface); |
| 1619 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | PutFileContents("/etc/hostapd/hostapd.conf", c); |
| 1620 | |||
| 1621 | // Remove wpa_supplicant config if it exists (switching from client to AP mode) | ||
| 1622 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | std::string wpaConfig = "/etc/wpa_supplicant/wpa_supplicant-" + tetherInterface + ".conf"; |
| 1623 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
8 | if (FileExists(wpaConfig)) { |
| 1624 | ✗ | printf("FPP - Removing wpa_supplicant config for %s to enable tethering\n", tetherInterface.c_str()); | |
| 1625 | ✗ | unlink(wpaConfig.c_str()); | |
| 1626 | ✗ | exec("/usr/bin/systemctl stop " + ShellSingleQuote("wpa_supplicant@" + tetherInterface + ".service")); | |
| 1627 | ✗ | exec("/usr/bin/systemctl disable " + ShellSingleQuote("wpa_supplicant@" + tetherInterface + ".service")); | |
| 1628 | } | ||
| 1629 | |||
| 1630 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | std::string content = "[Match]\nName="; |
| 1631 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | content.append(tetherInterface).append("\nType=wlan\n\n" |
| 1632 | "[Network]\n" | ||
| 1633 | "DHCP=no\n" | ||
| 1634 | "Address=192.168.8.1/24\n" | ||
| 1635 | "DHCPServer=yes\n\n"); | ||
| 1636 | |||
| 1637 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | AppendTetherDHCPServerSection(content, tetherInterface); |
| 1638 |
3/6✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
8 | PutFileContents("/etc/systemd/network/10-" + tetherInterface + ".network", content); |
| 1639 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | unblockWifi(); |
| 1640 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | exec("/usr/bin/systemctl reload-or-restart systemd-networkd.service"); |
| 1641 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | unblockWifi(); |
| 1642 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
16 | exec("/usr/bin/systemctl reload-or-restart hostapd.service"); |
| 1643 | // Only the boot-time symlink is wanted here. Plain "enable" also syncs | ||
| 1644 | // hostapd's SysV init script through update-rc.d and then reloads the | ||
| 1645 | // manager: measured 4.75s on a PocketBeagle2 even when already enabled, | ||
| 1646 | // on fppd's startup path. systemd ignores the init script since a native | ||
| 1647 | // unit exists, and nothing needs a reload to see a new wants link. | ||
| 1648 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | exec("SYSTEMCTL_SKIP_SYSV=1 /usr/bin/systemctl enable --no-reload hostapd.service"); |
| 1649 | 8 | } | |
| 1650 | 58 | } | |
| 1651 | 82 | void detectNetworkModules() { | |
| 1652 | 82 | std::string content; | |
| 1653 |
5/8✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 82 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 176 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 176 times.
✓ Branch 7 taken 82 times.
|
258 | for (const auto& entry : std::filesystem::directory_iterator("/sys/class/net/")) { |
| 1654 |
2/4✓ Branch 0 taken 176 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
✗ Branch 3 not taken.
|
176 | const std::string filename = entry.path().filename(); |
| 1655 |
2/4✓ Branch 0 taken 176 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
✗ Branch 3 not taken.
|
176 | std::string modName = "/sys/class/net/" + filename + "/device/driver/module"; |
| 1656 |
3/4✓ Branch 0 taken 176 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 175 times.
|
176 | if (FileExists(modName)) { |
| 1657 | char buf[256]; | ||
| 1658 | 1 | size_t l = readlink(modName.c_str(), buf, sizeof(buf)); | |
| 1659 | 1 | buf[l] = 0; | |
| 1660 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | std::string mod = buf; |
| 1661 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | mod = mod.substr(mod.rfind("/") + 1); |
| 1662 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | content += mod; |
| 1663 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | content += "\n"; |
| 1664 | 1 | } | |
| 1665 | 258 | } | |
| 1666 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 81 times.
|
82 | if (!content.empty()) { |
| 1667 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | PutFileContents("/etc/modules-load.d/fpp-network.conf", content); |
| 1668 | // PutFileContents("/home/fpp/media/config/fpp-network-modules.conf", content); | ||
| 1669 | } | ||
| 1670 | 82 | } | |
| 1671 | 74 | void removeDummyInterface() { | |
| 1672 |
2/4✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
|
74 | exec("ip link delete dummy0 > /dev/null 2>&1"); |
| 1673 | 74 | } | |
| 1674 |