Loading config.h.in +2 −1 Changes for config.h.in: 2 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ #define SYSLITE_UNIT_DIR "@SYSLITE_UNIT_DIR@" #define SYSLITE_DEFAULT_TARGET "@SYSLITE_DEFAULT_TARGET@" #define SYSLITE_TTY_COUNT @SYSLITE_TTY_COUNT@ #define SYSLITE_HOSTNAME_FILE "/etc/hostname" #define SYSLITE_HOSTNAME_FILE "/etc/hostname.yaml" #define SYSLITE_HOSTNAME_LEGACY "/etc/hostname" #define SYSLITE_JOURNAL "@SYSLITE_JOURNAL@" #define SYSLITE_VERSION "@PROJECT_VERSION@" src/syslite.cpp +30 −12 Changes for src/syslite.cpp: 30 added lines, 12 removed lines. Original line number Diff line number Diff line Loading @@ -31,7 +31,7 @@ namespace syslite { Init::~Init() {} bool Init::loadConfig(const std::string &) { // Configuration is compile-time (config.h). Hostname from /etc/hostname. // Configuration is compile-time (config.h). Hostname from /etc/hostname.yaml. // Register TTYs based on compiled-in count. // On a live ISO (squashfs boot), run the installer on tty1. bool &liveMode = _liveMode; Loading Loading @@ -102,19 +102,37 @@ namespace syslite { std::filesystem::remove(SYSLITE_SHUTDOWN_FILE, ec); } void Init::setHostname() { if (_hostname.empty()) { // Read from /etc/hostname std::ifstream f("/etc/hostname"); if (f.is_open()) { std::getline(f, _hostname); // Trim whitespace while (!_hostname.empty() && (_hostname.back() == '\n' || _hostname.back() == '\r' || _hostname.back() == ' ')) _hostname.pop_back(); // "hostname: <name>" from /etc/hostname.yaml (written by webctl and the // installer); a plain /etc/hostname as long as there's none static std::string readHostname() { auto trim = [](std::string s) { size_t b = s.find_first_not_of(" \t\r\n"); if (b == std::string::npos) return std::string(); size_t e = s.find_last_not_of(" \t\r\n"); s = s.substr(b, e - b + 1); if (s.size() >= 2 && (s[0] == '"' || s[0] == '\'') && s.back() == s[0]) s = s.substr(1, s.size() - 2); return s; }; std::string line; std::ifstream y(SYSLITE_HOSTNAME_FILE); if (y.is_open()) { while (std::getline(y, line)) { if (line.rfind("hostname:", 0) != 0) continue; std::string v = line.substr(9); auto hash = v.find(" #"); if (hash != std::string::npos) v.erase(hash); return trim(v); } return ""; } std::ifstream f(SYSLITE_HOSTNAME_LEGACY); if (f.is_open() && std::getline(f, line)) return trim(line); return ""; } void Init::setHostname() { if (_hostname.empty()) _hostname = readHostname(); if (!_hostname.empty()) { sethostname(_hostname.c_str(), _hostname.size()); Loading Loading
config.h.in +2 −1 Changes for config.h.in: 2 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ #define SYSLITE_UNIT_DIR "@SYSLITE_UNIT_DIR@" #define SYSLITE_DEFAULT_TARGET "@SYSLITE_DEFAULT_TARGET@" #define SYSLITE_TTY_COUNT @SYSLITE_TTY_COUNT@ #define SYSLITE_HOSTNAME_FILE "/etc/hostname" #define SYSLITE_HOSTNAME_FILE "/etc/hostname.yaml" #define SYSLITE_HOSTNAME_LEGACY "/etc/hostname" #define SYSLITE_JOURNAL "@SYSLITE_JOURNAL@" #define SYSLITE_VERSION "@PROJECT_VERSION@"
src/syslite.cpp +30 −12 Changes for src/syslite.cpp: 30 added lines, 12 removed lines. Original line number Diff line number Diff line Loading @@ -31,7 +31,7 @@ namespace syslite { Init::~Init() {} bool Init::loadConfig(const std::string &) { // Configuration is compile-time (config.h). Hostname from /etc/hostname. // Configuration is compile-time (config.h). Hostname from /etc/hostname.yaml. // Register TTYs based on compiled-in count. // On a live ISO (squashfs boot), run the installer on tty1. bool &liveMode = _liveMode; Loading Loading @@ -102,19 +102,37 @@ namespace syslite { std::filesystem::remove(SYSLITE_SHUTDOWN_FILE, ec); } void Init::setHostname() { if (_hostname.empty()) { // Read from /etc/hostname std::ifstream f("/etc/hostname"); if (f.is_open()) { std::getline(f, _hostname); // Trim whitespace while (!_hostname.empty() && (_hostname.back() == '\n' || _hostname.back() == '\r' || _hostname.back() == ' ')) _hostname.pop_back(); // "hostname: <name>" from /etc/hostname.yaml (written by webctl and the // installer); a plain /etc/hostname as long as there's none static std::string readHostname() { auto trim = [](std::string s) { size_t b = s.find_first_not_of(" \t\r\n"); if (b == std::string::npos) return std::string(); size_t e = s.find_last_not_of(" \t\r\n"); s = s.substr(b, e - b + 1); if (s.size() >= 2 && (s[0] == '"' || s[0] == '\'') && s.back() == s[0]) s = s.substr(1, s.size() - 2); return s; }; std::string line; std::ifstream y(SYSLITE_HOSTNAME_FILE); if (y.is_open()) { while (std::getline(y, line)) { if (line.rfind("hostname:", 0) != 0) continue; std::string v = line.substr(9); auto hash = v.find(" #"); if (hash != std::string::npos) v.erase(hash); return trim(v); } return ""; } std::ifstream f(SYSLITE_HOSTNAME_LEGACY); if (f.is_open() && std::getline(f, line)) return trim(line); return ""; } void Init::setHostname() { if (_hostname.empty()) _hostname = readHostname(); if (!_hostname.empty()) { sethostname(_hostname.c_str(), _hostname.size()); Loading