Persist all configuration settings across upgrades. Restart wifi-p2p if neccessary.

This commit is contained in:
Robin Davies
2024-08-27 14:55:48 -04:00
parent 722ef456bc
commit 06ebfb0bce
15 changed files with 340 additions and 201 deletions
+1
View File
@@ -571,6 +571,7 @@ add_executable(pipedalconfig
CommandLineParser.hpp
PiPedalException.hpp
ConfigMain.cpp
PiPedalConfiguration.hpp PiPedalConfiguration.cpp
JackServerSettings.hpp JackServerSettings.cpp
SystemConfigFile.hpp SystemConfigFile.cpp
WifiChannelSelectors.cpp WifiChannelSelectors.hpp
+88 -95
View File
@@ -38,6 +38,7 @@
#include <random>
#include "AudioConfig.hpp"
#include "WifiChannelSelectors.hpp"
#include "PiPedalConfiguration.hpp"
#include <grp.h>
#if JACK_HOST
@@ -54,6 +55,8 @@
using namespace std;
using namespace pipedal;
namespace fs = std::filesystem;
#define SERVICE_ACCOUNT_NAME "pipedal_d"
#define SERVICE_GROUP_NAME "pipedal_d"
@@ -80,12 +83,12 @@ using namespace pipedal;
#define REMOVE_OLD_SERVICE 0 // Grandfathering: whether to remove the old shutdown service (now pipedaladmind)
#define OLD_SHUTDOWN_SERVICE "pipedalshutdownd"
std::filesystem::path GetServiceFileName(const std::string &serviceName)
fs::path GetServiceFileName(const std::string &serviceName)
{
return std::filesystem::path(SERVICE_PATH) / (serviceName + ".service");
return fs::path(SERVICE_PATH) / (serviceName + ".service");
}
std::filesystem::path findOnPath(const std::string &command)
fs::path findOnPath(const std::string &command)
{
std::string path = getenv("PATH");
std::vector<std::string> paths;
@@ -98,8 +101,8 @@ std::filesystem::path findOnPath(const std::string &command)
pos = path.length();
}
std::string thisPath = path.substr(t, pos - t);
std::filesystem::path path = std::filesystem::path(thisPath) / command;
if (std::filesystem::exists(path))
fs::path path = fs::path(thisPath) / command;
if (fs::exists(path))
{
return path;
}
@@ -244,7 +247,7 @@ static void RemoveLine(const std::string &path, const std::string lineToRemove)
std::vector<std::string> lines;
try
{
if (std::filesystem::exists(path))
if (fs::exists(path))
{
{
ifstream f(path);
@@ -298,10 +301,10 @@ void InstallPamEnv()
#if INSTALL_JACK_SERVICE
std::string newLine = PAM_LINE;
std::vector<std::string> lines;
std::filesystem::path path = "/etc/security/pam_env.conf";
fs::path path = "/etc/security/pam_env.conf";
try
{
if (std::filesystem::exists(path))
if (fs::exists(path))
{
{
ifstream f(path);
@@ -355,9 +358,9 @@ void InstallLimits()
throw std::runtime_error("Failed to create audio service group.");
}
std::filesystem::path limitsConfig = "/etc/security/limits.d/audio.conf";
fs::path limitsConfig = "/etc/security/limits.d/audio.conf";
if (!std::filesystem::exists(limitsConfig))
if (!fs::exists(limitsConfig))
{
ofstream output(limitsConfig);
output << "# Realtime priority group used by pipedal audio (and also by jack)"
@@ -372,9 +375,9 @@ void InstallLimits()
#if JACK_HOST
void MaybeStartJackService()
{
std::filesystem::path drcFile = "/etc/jackdrc";
fs::path drcFile = "/etc/jackdrc";
if (std::filesystem::exists(drcFile) && std::filesystem::file_size(drcFile) != 0)
if (fs::exists(drcFile) && fs::file_size(drcFile) != 0)
{
sysExec(SYSTEMCTL_BIN " start jack");
}
@@ -466,23 +469,8 @@ void Uninstall()
{
try
{
try
{
if (IsP2pServiceEnabled())
{
WifiDirectConfigSettings settings;
settings.Load();
settings.enable_ = false;
settings.Save();
SetWifiDirectConfig(settings);
}
}
catch (const std::exception e)
{
std::cout << "Error: Unable to disable the Wiifi P2P service. " << e.what() << std::endl;
}
OnWifiUninstall(true);
OnWifiUninstall();
StopService();
DisableService();
@@ -494,7 +482,7 @@ void Uninstall()
try
{
std::filesystem::remove("/usr/bin/systemd/system/" OLD_SHUTDOWN_SERVICE ".service");
fs::remove("/usr/bin/systemd/system/" OLD_SHUTDOWN_SERVICE ".service");
}
catch (...)
{
@@ -502,21 +490,21 @@ void Uninstall()
try
{
std::filesystem::remove("/usr/bin/systemd/system/" ADMIN_SERVICE ".service");
fs::remove("/usr/bin/systemd/system/" ADMIN_SERVICE ".service");
}
catch (...)
{
}
try
{
std::filesystem::remove("/usr/bin/systemd/system/" PIPEDAL_NM_P2PD_SERVICE ".service");
fs::remove("/usr/bin/systemd/system/" PIPEDAL_NM_P2PD_SERVICE ".service");
}
catch (...)
{
}
try
{
std::filesystem::remove("/usr/bin/systemd/system/" PIPEDAL_P2PD_SERVICE ".service");
fs::remove("/usr/bin/systemd/system/" PIPEDAL_P2PD_SERVICE ".service");
}
catch (...)
{
@@ -524,7 +512,7 @@ void Uninstall()
try
{
std::filesystem::remove("/usr/bin/systemd/system/" PIPEDALD_SERVICE ".service");
fs::remove("/usr/bin/systemd/system/" PIPEDALD_SERVICE ".service");
}
catch (...)
{
@@ -532,7 +520,7 @@ void Uninstall()
try
{
std::filesystem::remove("/usr/bin/systemd/system/" JACK_SERVICE ".service");
fs::remove("/usr/bin/systemd/system/" JACK_SERVICE ".service");
}
catch (...)
{
@@ -596,13 +584,22 @@ static std::string RandomChars(int nchars)
return s.str();
}
static std::map<fs::path, fs::perms> sPermissionExceptions
({
{
"/var/pipedal/config/NetworkManagerP2P.json",
fs::perms::owner_read | fs::perms::owner_write | fs::perms::group_read | fs::perms::group_write
}
});
void SetVarPermissions(
const std::filesystem::path &path,
std::filesystem::perms directoryPermissions,
std::filesystem::perms filePermissions,
const fs::path &path,
fs::perms directoryPermissions,
fs::perms filePermissions,
uid_t uid, gid_t gid)
{
namespace fs = std::filesystem;
using namespace std::filesystem;
try {
if (fs::exists(path)) {
@@ -630,17 +627,22 @@ void SetVarPermissions(
}
} else {
fs::permissions(path,filePermissions, fs::perm_options::replace);
if (sPermissionExceptions.contains(path))
{
fs::permissions(path,sPermissionExceptions[path], fs::perm_options::replace);
} else {
fs::permissions(path,filePermissions, fs::perm_options::replace);
}
}
} else {
std::cout << "Error: Path does not exist: " << path << std::endl;
}
} catch (const std::filesystem::filesystem_error& e) {
} catch (const fs::filesystem_error& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
namespace fs = std::filesystem;
static void FixPermissions()
{
@@ -675,7 +677,7 @@ static void FixPermissions()
fs::perms::owner_read | fs::perms::owner_write |
fs::perms::group_read | fs::perms::group_write;
std::filesystem::path wpa_config_path{ "/etc/pipedal/config/wpa_supplicant/wpa_supplicant-pipedal.conf"};
fs::path wpa_config_path{ "/etc/pipedal/config/wpa_supplicant/wpa_supplicant-pipedal.conf"};
try {
fs::permissions(wpa_config_path,wpa_supplicant_perms, fs::perm_options::replace);
@@ -722,21 +724,40 @@ void CopyWpaSupplicantConfigFile()
try {
const char NM_SUPPLICANT_CONFIG_PATH[] = "/etc/pipedal/config/wpa_supplicant/wpa_supplicant-pipedal.conf";
const char NM_SUPPLICANT_CONFIG_SOURCE_PATH[] = "/etc/pipedal/config/templates/wpa_supplicant-pipedal.conf";
std::filesystem::path destinationPath = NM_SUPPLICANT_CONFIG_PATH;
std::filesystem::path sourcePath = NM_SUPPLICANT_CONFIG_SOURCE_PATH;
fs::path destinationPath = NM_SUPPLICANT_CONFIG_PATH;
fs::path sourcePath = NM_SUPPLICANT_CONFIG_SOURCE_PATH;
std::filesystem::create_directories(destinationPath.parent_path());
std::filesystem::copy_file(sourcePath,destinationPath,std::filesystem::copy_options::overwrite_existing);
fs::create_directories(destinationPath.parent_path());
fs::copy_file(sourcePath,destinationPath,fs::copy_options::overwrite_existing);
} catch (const std::exception&e)
{
cout << "ERROR: " << e.what() << endl;
}
}
void Install(const std::filesystem::path &programPrefix, const std::string endpointAddress)
void DeployVarConfig()
{
fs::path varConfig = "/var/pipedal/config/config.json";
if (!fs::exists(varConfig))
{
auto directory = varConfig.parent_path();
fs::create_directories(directory);
fs::path templateFile = "/etc/pipedal/config/templates/var_config.json";
try {
fs::copy(templateFile,varConfig);
} catch (const std::exception &e)
{
std::cout << "Error: Failed to create " << varConfig << std::endl;
}
}
}
void Install(const fs::path &programPrefix, const std::string endpointAddress)
{
cout << "Configuring pipedal" << endl;
try
{
DeployVarConfig();
if (sysExec(GROUPADD_BIN " -f " AUDIO_SERVICE_GROUP_NAME) != EXIT_SUCCESS)
{
throw std::runtime_error("Failed to create audio service group.");
@@ -812,8 +833,8 @@ void Install(const std::filesystem::path &programPrefix, const std::string endpo
// create and configure /var directory.
std::filesystem::path varDirectory("/var/pipedal");
std::filesystem::create_directory(varDirectory);
fs::path varDirectory("/var/pipedal");
fs::create_directory(varDirectory);
{
std::stringstream s;
@@ -841,8 +862,8 @@ void Install(const std::filesystem::path &programPrefix, const std::string endpo
if (authBindRequired)
{
std::filesystem::create_directories("/etc/authbind/byport");
std::filesystem::path portAuthFile = std::filesystem::path("/etc/authbind/byport") / strPort;
fs::create_directories("/etc/authbind/byport");
fs::path portAuthFile = fs::path("/etc/authbind/byport") / strPort;
{
// create it.
@@ -930,56 +951,28 @@ void Install(const std::filesystem::path &programPrefix, const std::string endpo
FixPermissions();
RestartService(false);
EnableService();
// Restart WiFi Direct if neccessary.
OnWifiReinstall();
}
catch (const std::exception &e)
{
// don't allow abnormal termination, which leaves the package in a state that's
// difficult to uninstall.
cout << "Error: " << e.what();
cout << " Run 'pipedalconfig --install' again to complete setup of PiPedal." << endl;
}
}
static std::string GetCurrentWebServicePort()
{
std::filesystem::path servicePath = GetServiceFileName(PIPEDALD_SERVICE);
try
{
if (std::filesystem::exists(servicePath))
{
{
ifstream f(servicePath);
if (!f.is_open())
{
throw std::runtime_error(SS("Can't open " << servicePath));
}
while (true)
{
std::string line;
if (f.eof())
break;
std::getline(f, line);
if (line.starts_with("ExecStart="))
{
auto startPos = line.find("-port ");
if (startPos != std::string::npos)
{
startPos = startPos + 6;
auto endPos = line.find(" -systemd");
if (endPos != std::string::npos)
{
std::string result = line.substr(startPos, endPos - startPos);
return result;
}
}
}
}
}
}
}
catch (const std::exception & /*ignored*/)
{
}
return "";
PiPedalConfiguration config;
config.Load("/etc/pipedal/config","");
std::ostringstream ss;
ss << config.GetSocketServerPort();
return ss.str();
}
static void PrintHelp()
@@ -1279,17 +1272,17 @@ int main(int argc, char **argv)
}
if (install)
{
std::filesystem::path prefix;
fs::path prefix;
if (prefixOption.length() != 0)
{
prefix = std::filesystem::path(prefixOption);
prefix = fs::path(prefixOption);
}
else
{
prefix = std::filesystem::path(argv[0]).parent_path().parent_path();
prefix = fs::path(argv[0]).parent_path().parent_path();
std::filesystem::path pipedalPath = prefix / "sbin" / "pipedald";
if (!std::filesystem::exists(pipedalPath))
fs::path pipedalPath = prefix / "sbin" / "pipedald";
if (!fs::exists(pipedalPath))
{
std::stringstream s;
s << "Can't find pipedald executable at " << pipedalPath << ". Try again using the -prefix option.";
+70 -2
View File
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Robin Davies
// Copyright (c) 2022-2024 Robin Davies
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
@@ -20,9 +20,76 @@
#include "pch.h"
#include "PiPedalConfiguration.hpp"
#include "json.hpp"
#include "ss.hpp"
#include "ServiceConfiguration.hpp"
using namespace pipedal;
namespace fs = std::filesystem;
void PiPedalConfiguration::Load(const std::filesystem::path &path, const std::filesystem::path &webRoot)
{
docRoot_ = path;
webRoot_ = webRoot;
// load installer defaults.
{
std::filesystem::path configPath = path / "config.json";
std::ifstream f(configPath);
if (!f.is_open())
{
std::stringstream s;
s << "Unable to open " << configPath;
throw PiPedalStateException(s.str());
}
json_reader reader(f);
reader.read(this);
}
// web port comes from service.conf
ServiceConfiguration serviceConfiguration;
serviceConfiguration.Load(fs::path(this->local_storage_path_) / "config" / "service.conf");
this->socketServerAddress_ = SS("0.0.0.0:" << serviceConfiguration.server_port);
// load any user-made settings
{
std::filesystem::path userPath = std::filesystem::path(this->local_storage_path_) / "config" / "config.json";
if (std::filesystem::exists(userPath))
{
std::ifstream f(userPath);
json_reader reader(f);
reader.read(this);
}
}
}
uint16_t PiPedalConfiguration::GetSocketServerPort() const
{
try
{
size_t pos = this->socketServerAddress_.find_last_of(':');
if (pos == std::string::npos)
{
throw std::exception();
}
std::string strPort = socketServerAddress_.substr(pos + 1);
unsigned long port = std::stoul(strPort);
if (port == 0)
{
throw std::exception();
}
if (port > std::numeric_limits<uint16_t>::max())
{
throw std::exception();
}
return (uint16_t)port;
}
catch (const std::exception &)
{
std::stringstream s;
s << "Invalid port number in '" << this->socketServerAddress_ << "'.";
throw PiPedalArgumentException(s.str());
}
}
JSON_MAP_BEGIN(PiPedalConfiguration)
JSON_MAP_REFERENCE(PiPedalConfiguration, lv2_path)
@@ -37,4 +104,5 @@ JSON_MAP_REFERENCE(PiPedalConfiguration, maxUploadSize)
JSON_MAP_REFERENCE(PiPedalConfiguration, accessPointGateway)
JSON_MAP_REFERENCE(PiPedalConfiguration, accessPointServerAddress)
JSON_MAP_REFERENCE(PiPedalConfiguration, isVst3Enabled)
JSON_MAP_REFERENCE(PiPedalConfiguration, end)
JSON_MAP_END()
+3 -44
View File
@@ -48,6 +48,7 @@ private:
std::string accessPointGateway_;
std::string accessPointServerAddress_;
bool isVst3Enabled_ = true;
bool end_ = false; // dummy target for /var/pipedal/config/config.json
public:
bool IsVst3Enabled() const { return isVst3Enabled_; }
@@ -57,26 +58,8 @@ public:
const std::filesystem::path& GetWebRoot() const {
return webRoot_;
}
void Load(const std::filesystem::path& path, const std::filesystem::path&webRoot) {
std::filesystem::path configPath = path / "config.json";
if (!std::filesystem::exists(configPath))
{
throw PiPedalException("File not found.");
}
std::ifstream f(configPath);
if (!f.is_open())
{
std::stringstream s;
s << "Unable to open " << configPath;
throw PiPedalStateException(s.str());
}
json_reader reader(f);
reader.read(this);
docRoot_ = path;
void Load(const std::filesystem::path& path, const std::filesystem::path&webRoot);
webRoot_ = webRoot;
}
const std::filesystem::path &GetDocRoot() const { return docRoot_; }
const std::string &GetLv2Path() const { return lv2_path_; }
const std::string &GetLocalStoragePath() const { return local_storage_path_; }
@@ -108,32 +91,8 @@ public:
return socketServerAddress_.substr(0,pos);
}
uint16_t GetSocketServerPort() const {
try {
size_t pos = this->socketServerAddress_.find_last_of(':');
if (pos == std::string::npos)
{
throw std::exception();
}
std::string strPort = socketServerAddress_.substr(pos+1);
unsigned long port = std::stoul(strPort);
if (port == 0)
{
throw std::exception();
}
if (port > std::numeric_limits<uint16_t>::max())
{
throw std::exception();
}
return (uint16_t) port;
} catch (const std::exception &)
{
std::stringstream s;
s << "Invalid port number in '" << this->socketServerAddress_ << "'.";
throw PiPedalArgumentException(s.str());
}
uint16_t GetSocketServerPort() const;
}
uint32_t GetThreads() const { return threads_; }
DECLARE_JSON_MAP(PiPedalConfiguration);
+25 -5
View File
@@ -549,13 +549,18 @@ void UninstallP2p()
restoreP2pDnsmasqConfFile();
restoreNetworkManagerP2pConfig();
sysExec(SYSTEMCTL_BIN " restart dhcpcd");
if (!UsingNetworkManager())
{
sysExec(SYSTEMCTL_BIN " restart dhcpcd");
} else {
sysExec(SYSTEMCTL_BIN " restart NetworkManager"); // bring up wlan0 wifi.
}
WifiDirectConfigSettings wifiDirectConfigSettings;
wifiDirectConfigSettings.Load();
wifiDirectConfigSettings.enable_ = false;
wifiDirectConfigSettings.Save();
sysExec(SYSTEMCTL_BIN " restart NetworkManager"); // bring up wlan0 wifi.
SetWifiDirectConfig(wifiDirectConfigSettings);
}
}
@@ -675,8 +680,15 @@ void pipedal::SetWifiDirectConfig(const WifiDirectConfigSettings &settings)
cout << e.what() << endl;
}
}
void pipedal::OnWifiUninstall()
void pipedal::OnWifiReinstall() {
WifiDirectConfigSettings settings;
settings.Load();
if (settings.enable_)
{
SetWifiDirectConfig(settings);
}
}
void pipedal::OnWifiUninstall(bool preserveState)
{
// intaller hook
if (IsApdInstalled())
@@ -685,7 +697,15 @@ void pipedal::OnWifiUninstall()
}
if (IsP2pInstalled())
{
WifiDirectConfigSettings settings;
settings.Load();
UninstallP2p();
// preserve the state for future installs.
if (preserveState)
{
settings.Save();
}
}
}
void pipedal::OnWifiInstallComplete()
+3 -1
View File
@@ -29,7 +29,9 @@ namespace pipedal {
void SetWifiConfig(const WifiConfigSettings&settings);
void SetWifiDirectConfig(const WifiDirectConfigSettings&settings);
void OnWifiUninstall();
void OnWifiUninstall(bool preserveState = false);
void OnWifiReinstall();
void OnWifiInstallComplete();
bool UsingNetworkManager();
+2 -2
View File
@@ -133,9 +133,9 @@ int main(int argc, char *argv[])
std::cout << "Usage: pipedald <doc_root> [<web_root>] [options...]\n\n"
<< "Options:\n"
<< " -systemd: Log to systemd journals instead of to the console.\n"
<< " -port: Port to listen on e.g. 0.0.0.0:80\n"
<< " -port: Port to listen on e.g. 80, or 0.0.0.0:80\n"
<< "Example:\n"
<< " pipedald /etc/pipedal/config /etc/pipedal/react -port 0.0.0.0:80 \n"
<< " pipedald /etc/pipedal/config /etc/pipedal/react -port 80 \n"
"\n"
"Description:\n\n"
" Configuration is read from <doc_root>/config.json\n"
+45
View File
@@ -0,0 +1,45 @@
{
// This file overrides default settings, as set by the installer in /etc/pipdal/config.json
//
// Changes made in this file will be preserved across upgrades.
//
// To override default settings, uncomment the setting line you want to modify.
//
// You should reboot after modifying any of these settings.
// Paths where PiPedal will look for LV2 Plugins.
// One or more directories, seperated by ':'
// "lv2_path": "/usr/lib/lv2:/usr/local/lib/lv2:/usr/modep/lv2",
// whether to lock process pages into memory. should be true unless running on a very memory constrained system.
// Setting to false may cause unpredictable audio dropouts.
// "mlock": true,
// Number of threads the web server should use.
// "threads" : 5,
// Whether to log individual http requests. (highly inadvisable, as requests are logged to the systemd log)
// "logHttpRequests": false,
// Log level for the web server
// { None=0,Error =1,Warning =2,Info = 3, Debug=4}
// "logLevel": 3,
// Maximum filesize to allow when uploading
// "maxUploadSize": 536870912 // 512MiB
// does nothing. avoids chasing ','s.
"end" : true
}