Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add current drive detection xibosignage#238 #239

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions player/common/system/HardwareKeyGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

#include "common/Utils.hpp"
#include "common/system/System.hpp"
#include "config/AppConfig.hpp"

#include <boost/process/child.hpp>
#include <boost/process/io.hpp>
#include <regex>

namespace bp = boost::process;

HardwareKey HardwareKeyGenerator::generate()
{
auto key = cpuid() + static_cast<std::string>(System::macAddress()) + volumeSerial();
Expand Down Expand Up @@ -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<volumeInfo, bp::std_out> 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<pipe, bp::std_out> stream);

commandProcess.wait();
grepProcess.wait();

std::string line;
std::getline(stream, line);
return line;
}
7 changes: 5 additions & 2 deletions player/common/system/HardwareKeyGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#include "common/system/HardwareKey.hpp"

#include <boost/process/io.hpp>
#include <ios>
#include <regex>
#include <string>

class HardwareKeyGenerator
Expand All @@ -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);
};