--port option only valid with --install option.

This commit is contained in:
Robin E. R. Davies
2025-09-19 08:02:14 -04:00
parent 0973efb025
commit 9470699527
+80 -60
View File
@@ -562,7 +562,6 @@ void InstallAudioService()
#endif #endif
} }
static bool IsP2pServiceEnabled() static bool IsP2pServiceEnabled()
{ {
WifiDirectConfigSettings settings; WifiDirectConfigSettings settings;
@@ -742,7 +741,7 @@ void SetVarPermissions(
catch (const std::exception &e) catch (const std::exception &e)
{ {
// spurious errors on pulse config files. // spurious errors on pulse config files.
// std::cout << "Error: failed to set permissions on file " << entry.path() << std::endl; // std::cout << "Error: failed to set permissions on file " << entry.path() << std::endl;
} }
} }
} }
@@ -926,26 +925,26 @@ void InstallPgpKey()
} }
} }
static uint32_t autoSelectPorts[] = {
static uint32_t autoSelectPorts[] = {
80, 80,
81, // unofficial alternate 81, // unofficial alternate
8080, // unofficial alternate 8080, // unofficial alternate
8081, // unofficial alternate 8081, // unofficial alternate
8008, // unofficial alternate 8008, // unofficial alternate
83 // Not official at all. 83 // Not official at all.
}; };
static bool isPortInUse(int port)
{
static bool isPortInUse(int port) {
int sockfd = socket(AF_INET, SOCK_STREAM, 0); int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) { if (sockfd < 0)
{
throw std::runtime_error(SS("Socket creation failed: " << strerror(errno))); throw std::runtime_error(SS("Socket creation failed: " << strerror(errno)));
} }
Finally ff{[sockfd]() { Finally ff{[sockfd]()
close(sockfd); {
}}; close(sockfd);
}};
struct sockaddr_in addr; struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
@@ -953,23 +952,27 @@ static bool isPortInUse(int port) {
addr.sin_port = htons(port); addr.sin_port = htons(port);
// Try to bind to the port // Try to bind to the port
int result = bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)); int result = bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
if (result < 0) { if (result < 0)
if (errno == EADDRINUSE) { {
return true; // Port is in use if (errno == EADDRINUSE)
} else { {
return true; // Port is in use
}
else
{
cout << "Warning: Unexpected error binding socket to port " << port << ": " << strerror(errno) << endl; cout << "Warning: Unexpected error binding socket to port " << port << ": " << strerror(errno) << endl;
return true; return true;
} }
} }
return false; // Port is free return false; // Port is free
} }
static std::string AutoSelectPort()
static std::string AutoSelectPort() { {
constexpr size_t MAX_PORT = sizeof(autoSelectPorts)/sizeof(autoSelectPorts[0]); constexpr size_t MAX_PORT = sizeof(autoSelectPorts) / sizeof(autoSelectPorts[0]);
int32_t port = -1; int32_t port = -1;
for (size_t i = 0; i < MAX_PORT; ++i) for (size_t i = 0; i < MAX_PORT; ++i)
@@ -980,29 +983,32 @@ static std::string AutoSelectPort() {
break; break;
} }
} }
if (port == -1) { if (port == -1)
{
cout << "Warning: Can't find an available HTTP port. Setting to port 80 anyway." << endl; cout << "Warning: Can't find an available HTTP port. Setting to port 80 anyway." << endl;
port = 80; port = 80;
} else if (port != 80) }
else if (port != 80)
{ {
cout << "Warning: Port 80 is already in use. Using port " << port << " instead." << endl; cout << "Warning: Port 80 is already in use. Using port " << port << " instead." << endl;
} }
return SS(port); return SS(port);
} }
bool SetWebServerPort(std::string portOption) bool SetWebServerPort(std::string portOption)
{ {
try { try
{
auto nPos = portOption.find_last_of(':'); auto nPos = portOption.find_last_of(':');
std::string strPort; std::string strPort;
if (nPos != std::string::npos) if (nPos != std::string::npos)
{ {
strPort = portOption.substr(nPos+1); strPort = portOption.substr(nPos + 1);
} else { }
else
{
strPort = portOption; strPort = portOption;
} }
std::istringstream ss(strPort); std::istringstream ss(strPort);
@@ -1021,7 +1027,8 @@ bool SetWebServerPort(std::string portOption)
StartService(true); StartService(true);
cout << "PiPedal web server is listening on port " << iPort << endl; cout << "PiPedal web server is listening on port " << iPort << endl;
} catch (const std::exception&e) }
catch (const std::exception &e)
{ {
cout << "Error: " << e.what() << endl; cout << "Error: " << e.what() << endl;
return false; return false;
@@ -1031,7 +1038,8 @@ bool SetWebServerPort(std::string portOption)
void CheckPreemptDynamicConfig() void CheckPreemptDynamicConfig()
{ {
try { try
{
BootConfig bootConfig; BootConfig bootConfig;
if (bootConfig.KernelType() == "PREEMPT_DYNAMIC") if (bootConfig.KernelType() == "PREEMPT_DYNAMIC")
@@ -1044,13 +1052,12 @@ void CheckPreemptDynamicConfig()
cout << endl; cout << endl;
} }
} }
}
} catch(const std::exception& /* ignore */) catch (const std::exception & /* ignore */)
{ {
} }
} }
void Install(const fs::path &programPrefix, const std::string endpointAddress) void Install(const fs::path &programPrefix, const std::string endpointAddress)
{ {
cout << "Configuring pipedal" << endl; cout << "Configuring pipedal" << endl;
@@ -1058,10 +1065,10 @@ void Install(const fs::path &programPrefix, const std::string endpointAddress)
if (!UsingNetworkManager()) if (!UsingNetworkManager())
{ {
cout << "Warning: The current OS is not using the NetworkManager network stack." << endl; cout << "Warning: The current OS is not using the NetworkManager network stack." << endl;
cout << " The PiPedal Auto-Hotspot feature will be disabled." << endl << endl; cout << " The PiPedal Auto-Hotspot feature will be disabled." << endl
<< endl;
cout << " Consult PiPedal documentation to find out how to correct this " << endl; cout << " Consult PiPedal documentation to find out how to correct this " << endl;
cout << " issue on Ubuntu Server." << endl; cout << " issue on Ubuntu Server." << endl;
} }
try try
{ {
@@ -1301,10 +1308,11 @@ void Install(const fs::path &programPrefix, const std::string endpointAddress)
if (endpointAddress.starts_with(ADDR_ANY)) if (endpointAddress.starts_with(ADDR_ANY))
{ {
cout << "PiPedal web server is listening on port " << endpointAddress.substr(ADDR_ANY.length()) << endl; cout << "PiPedal web server is listening on port " << endpointAddress.substr(ADDR_ANY.length()) << endl;
} else { }
else
{
cout << "PiPedal web server is listening on port " << endpointAddress << endl; cout << "PiPedal web server is listening on port " << endpointAddress << endl;
} }
} }
static std::string GetCurrentWebServicePort() static std::string GetCurrentWebServicePort()
@@ -1494,25 +1502,27 @@ void ListValidCountryCodes()
if (ccs.size() == 0) if (ccs.size() == 0)
{ {
cout << "No regulatory domains found." << endl; cout << "No regulatory domains found." << endl;
} else { }
using pair = std::pair<std::string,std::string>; else
{
using pair = std::pair<std::string, std::string>;
std::vector<pair> list; std::vector<pair> list;
for (auto &cc: ccs) for (auto &cc : ccs)
{ {
list.push_back(pair(cc.first,cc.second)); list.push_back(pair(cc.first, cc.second));
} }
auto collator = Locale::GetInstance()->GetCollator(); auto collator = Locale::GetInstance()->GetCollator();
auto compare = [&collator]( auto compare = [&collator](
pair &left, pair &left,
pair &right) pair &right)
{ {
return collator->Compare( return collator->Compare(
left.second, right.second) < 0; left.second, right.second) < 0;
}; };
std::sort(list.begin(), list.end(), compare); std::sort(list.begin(), list.end(), compare);
for (const auto &entry: list) for (const auto &entry : list)
{ {
cout << entry.first << " - " << entry.second << endl; cout << entry.first << " - " << entry.second << endl;
} }
@@ -1521,12 +1531,15 @@ void ListValidCountryCodes()
static std::string ProcessPortOption(std::string portOption) static std::string ProcessPortOption(std::string portOption)
{ {
if (portOption == "" || portOption == "0") // allow testing of port autodetect. if (portOption == "" || portOption == "0") // allow testing of port autodetect.
{ {
std::string t = GetCurrentWebServicePort(); std::string t = GetCurrentWebServicePort();
if (portOption != "0") {// allows easy debugging of port AutoSelectPort. if (portOption != "0")
{ // allows easy debugging of port AutoSelectPort.
portOption = t; portOption = t;
} else { }
else
{
portOption = ""; portOption = "";
} }
if (portOption == "") if (portOption == "")
@@ -1569,7 +1582,7 @@ int main(int argc, char **argv)
bool noWifi = false; bool noWifi = false;
parser.AddOption("--nosudo", &nosudo); // strictly a debugging aid. Run without sudo, until we fail because of permissions. parser.AddOption("--nosudo", &nosudo); // strictly a debugging aid. Run without sudo, until we fail because of permissions.
parser.AddOption("--list-wifi-country-codes",&list_wifi_country_codes); parser.AddOption("--list-wifi-country-codes", &list_wifi_country_codes);
parser.AddOption("--install", &install); parser.AddOption("--install", &install);
parser.AddOption("--uninstall", &uninstall); parser.AddOption("--uninstall", &uninstall);
parser.AddOption("--stop", &stop); parser.AddOption("--stop", &stop);
@@ -1589,15 +1602,17 @@ int main(int argc, char **argv)
parser.AddOption("--alsa-devices", &alsaDevices); parser.AddOption("--alsa-devices", &alsaDevices);
parser.AddOption("--alsa-device", &alsaDevice); parser.AddOption("--alsa-device", &alsaDevice);
parser.AddOption("--fix-permissions", &fix_permissions); parser.AddOption("--fix-permissions", &fix_permissions);
try try
{ {
parser.Parse(argc, (const char **)argv); parser.Parse(argc, (const char **)argv);
int actionCount = int actionCount =
(!alsaDevice.empty()) + alsaDevices + help + get_current_port + install + (!alsaDevice.empty()) + alsaDevices + help + get_current_port + install +
uninstall + stop + start + enable + disable + enable_hotspot + uninstall + stop + start + enable + disable + enable_hotspot +
disable_hotspot + restart + enable_p2p + disable_p2p + list_p2p_channels + disable_hotspot + restart + enable_p2p + disable_p2p + list_p2p_channels +
fix_permissions + list_wifi_country_codes; fix_permissions + list_wifi_country_codes;
if (actionCount == 0 && portOption.length() != 0) if (actionCount == 0 && portOption.length() != 0)
{ {
++actionCount; ++actionCount;
@@ -1615,6 +1630,12 @@ int main(int argc, char **argv)
throw std::runtime_error("No action provided."); throw std::runtime_error("No action provided.");
} }
if (portOption.length() != 0 && !install)
{
throw std::runtime_error("The --port option can only be specified in conjunction with the --install option.");
}
if ((!enable_p2p) && (!enable_hotspot) && (!list_p2p_channels)) if ((!enable_p2p) && (!enable_hotspot) && (!list_p2p_channels))
{ {
if (parser.Arguments().size() != 0) if (parser.Arguments().size() != 0)
@@ -1744,7 +1765,6 @@ int main(int argc, char **argv)
Install(prefix, portOption); Install(prefix, portOption);
FileSystemSync(); FileSystemSync();
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {