From 5c8f59952aabc160684adc39984588643ad1db15 Mon Sep 17 00:00:00 2001 From: stivius Date: Tue, 13 Apr 2021 17:39:54 +0300 Subject: [PATCH] fix: add current drive detection xibosignage#238 --- player/common/system/HardwareKeyGenerator.cpp | 45 ++++++++++++------- player/common/system/HardwareKeyGenerator.hpp | 7 ++- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/player/common/system/HardwareKeyGenerator.cpp b/player/common/system/HardwareKeyGenerator.cpp index 0afc6123a..f0fdc8d76 100644 --- a/player/common/system/HardwareKeyGenerator.cpp +++ b/player/common/system/HardwareKeyGenerator.cpp @@ -2,11 +2,14 @@ #include "common/Utils.hpp" #include "common/system/System.hpp" +#include "config/AppConfig.hpp" #include #include #include +namespace bp = boost::process; + HardwareKey HardwareKeyGenerator::generate() { auto key = cpuid() + static_cast(System::macAddress()) + volumeSerial(); @@ -37,35 +40,47 @@ inline void HardwareKeyGenerator::nativeCpuid(unsigned int* eax, std::string HardwareKeyGenerator::volumeSerial() { - namespace bp = boost::process; + const std::string SERIAL_PREFIX{"ID_SERIAL_SHORT"}; - bp::pipe volumeInfo; - bp::ipstream stream; + auto line = executeAndGrepFirstLine("udevadm info " + currentDrive(), SERIAL_PREFIX); - bp::child udevadm("udevadm info /dev/sda", bp::std_out > volumeInfo); - bp::child grep("grep ID_SERIAL_SHORT", bp::std_in stream); + return retrieveResult(std::regex{SERIAL_PREFIX + "=(.+)"}, line); +} - auto volumeSerial = retrieveVolumeSerial(stream); +std::string HardwareKeyGenerator::currentDrive() +{ + const std::string DEVICE_PREFIX{"/dev/"}; - udevadm.wait(); - grep.wait(); + auto line = executeAndGrepFirstLine("df " + AppConfig::playerBinary(), DEVICE_PREFIX); - return volumeSerial; + return DEVICE_PREFIX + retrieveResult(std::regex{DEVICE_PREFIX + "([^\\s]+)"}, line); } -std::string HardwareKeyGenerator::retrieveVolumeSerial(boost::process::ipstream& stream) +std::string HardwareKeyGenerator::retrieveResult(const std::regex& regex, const std::string& line) { const int SERIAL_CAPTURE_GROUP = 1; - const std::regex SERIAL_REGEX{"ID_SERIAL_SHORT=(.+)"}; - - std::string line; - std::getline(stream, line); std::smatch result; - if (std::regex_search(line, result, SERIAL_REGEX)) + if (std::regex_search(line, result, regex)) { return result[SERIAL_CAPTURE_GROUP].str(); } return {}; } + +std::string HardwareKeyGenerator::executeAndGrepFirstLine(const std::string& command, const std::string& grepSearch) +{ + bp::pipe pipe; + bp::ipstream stream; + + bp::child commandProcess(command, bp::std_out > pipe); + bp::child grepProcess("grep " + grepSearch, bp::std_in stream); + + commandProcess.wait(); + grepProcess.wait(); + + std::string line; + std::getline(stream, line); + return line; +} diff --git a/player/common/system/HardwareKeyGenerator.hpp b/player/common/system/HardwareKeyGenerator.hpp index 1c2047ef9..7894764b7 100644 --- a/player/common/system/HardwareKeyGenerator.hpp +++ b/player/common/system/HardwareKeyGenerator.hpp @@ -2,7 +2,8 @@ #include "common/system/HardwareKey.hpp" -#include +#include +#include #include class HardwareKeyGenerator @@ -15,5 +16,7 @@ class HardwareKeyGenerator static void nativeCpuid(unsigned int* eax, unsigned int* ebx, unsigned int* ecx, unsigned int* edx); static std::string macAddress(); static std::string volumeSerial(); - static std::string retrieveVolumeSerial(boost::process::ipstream& stream); + static std::string currentDrive(); + static std::string executeAndGrepFirstLine(const std::string& command, const std::string& grepSearch); + static std::string retrieveResult(const std::regex& regex, const std::string& line); };