--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
+51 -31
View File
@@ -562,7 +562,6 @@ void InstallAudioService()
#endif #endif
} }
static bool IsP2pServiceEnabled() static bool IsP2pServiceEnabled()
{ {
WifiDirectConfigSettings settings; WifiDirectConfigSettings settings;
@@ -926,7 +925,6 @@ void InstallPgpKey()
} }
} }
static uint32_t autoSelectPorts[] = { static uint32_t autoSelectPorts[] = {
80, 80,
81, // unofficial alternate 81, // unofficial alternate
@@ -936,14 +934,15 @@ static uint32_t autoSelectPorts[] = {
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;
@@ -955,10 +954,14 @@ static bool isPortInUse(int 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) { {
if (errno == EADDRINUSE)
{
return true; // Port is in use return true; // Port is in use
} else { }
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;
} }
@@ -967,8 +970,8 @@ static bool isPortInUse(int port) {
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;
@@ -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,7 +1502,9 @@ void ListValidCountryCodes()
if (ccs.size() == 0) if (ccs.size() == 0)
{ {
cout << "No regulatory domains found." << endl; cout << "No regulatory domains found." << endl;
} else { }
else
{
using pair = std::pair<std::string, std::string>; using pair = std::pair<std::string, std::string>;
std::vector<pair> list; std::vector<pair> list;
for (auto &cc : ccs) for (auto &cc : ccs)
@@ -1524,9 +1534,12 @@ 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 == "")
@@ -1589,6 +1602,7 @@ 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);
@@ -1598,6 +1612,7 @@ int main(int argc, char **argv)
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)
{ {