Commit 4337bf4e authored by Alejandro Homs Puron's avatar Alejandro Homs Puron Committed by operator for beamline
Browse files

Add SystemCmd with previous CPUAffinity sudo management

parent e2b22ce3
......@@ -34,6 +34,34 @@ namespace lima
namespace SlsDetector
{
class SystemCmd
{
DEB_CLASS_NAMESPC(DebModCamera, "SystemCmd", "SlsDetector");
public:
SystemCmd(std::string cmd, std::string desc = "",
bool try_sudo = true, bool can_hide_out = true);
SystemCmd(const SystemCmd& o);
static void setUseSudo(bool use_sudo);
static bool getUseSudo();
std::ostream& args()
{ return m_args; }
int execute();
private:
void checkSudo();
static bool UseSudo;
std::string m_cmd;
std::string m_desc;
bool m_try_sudo;
bool m_can_hide_out;
std::ostringstream m_args;
};
class CPUAffinity
{
DEB_CLASS_NAMESPC(DebModCamera, "CPUAffinity", "SlsDetector");
......@@ -41,10 +69,6 @@ class CPUAffinity
CPUAffinity(uint64_t m = 0) : m_mask(internalMask(m))
{}
static void setUseSudo(bool use_sudo);
static bool getUseSudo();
static void checkSudo(std::string cmd, std::string desc = "");
static int getNbSystemCPUs(bool max_nb = false);
static int getNbHexDigits(bool max_nb = false)
......@@ -86,7 +110,6 @@ class CPUAffinity
bool applyWithNetDevSetter(const std::string& dev,
const std::string& queue) const;
static bool UseSudo;
static int findNbSystemCPUs();
static int findMaxNbSystemCPUs();
static std::string getNetDevSetterSudoDesc();
......
......@@ -125,15 +125,31 @@ namespace SlsDetector
#include "SlsDetectorCamera.h"
%End
class CPUAffinity
class SystemCmd
{
public:
CPUAffinity(unsigned long m = 0);
SystemCmd(std::string cmd, std::string desc = "",
bool try_sudo = true, bool can_hide_out = true);
SystemCmd(const SlsDetector::SystemCmd& o);
static void setUseSudo(bool use_sudo);
static bool getUseSudo();
static void checkSudo(std::string cmd, std::string desc = "");
void addArgs(std::string args);
%MethodCode
Py_BEGIN_ALLOW_THREADS
sipCpp->args() << *a0;
Py_END_ALLOW_THREADS
%End
int execute();
};
class CPUAffinity
{
public:
CPUAffinity(unsigned long m = 0);
static int getNbSystemCPUs(bool max_nb = false);
static int getNbHexDigits(bool max_nb = false);
static unsigned long allCPUs(bool max_nb = false);
......
......@@ -40,34 +40,46 @@ using namespace std;
using namespace lima;
using namespace lima::SlsDetector;
bool CPUAffinity::UseSudo = true;
bool SystemCmd::UseSudo = true;
void CPUAffinity::setUseSudo(bool use_sudo)
SystemCmd::SystemCmd(string cmd, string desc, bool try_sudo, bool can_hide_out)
: m_cmd(cmd), m_desc(desc), m_try_sudo(try_sudo),
m_can_hide_out(can_hide_out)
{
DEB_CONSTRUCTOR();
DEB_PARAM() << DEB_VAR4(m_cmd, m_desc, m_try_sudo, m_can_hide_out);
}
SystemCmd::SystemCmd(const SystemCmd& o)
: m_cmd(o.m_cmd), m_desc(o.m_desc), m_try_sudo(o.m_try_sudo),
m_can_hide_out(o.m_can_hide_out)
{
DEB_CONSTRUCTOR();
DEB_PARAM() << DEB_VAR4(m_cmd, m_desc, m_try_sudo, m_can_hide_out);
}
void SystemCmd::setUseSudo(bool use_sudo)
{
UseSudo = use_sudo;
}
bool CPUAffinity::getUseSudo()
bool SystemCmd::getUseSudo()
{
return UseSudo;
}
void CPUAffinity::checkSudo(string cmd, string desc)
void SystemCmd::checkSudo()
{
DEB_STATIC_FUNCT();
typedef map<string, bool> CacheMap;
static CacheMap cache_map;
CacheMap::iterator it = cache_map.find(cmd);
CacheMap::iterator it = cache_map.find(m_cmd);
if (it == cache_map.end()) {
ostringstream os;
os << "sudo -l " << cmd;
if (!DEB_CHECK_ANY(DebTypeTrace))
os << " > /dev/null 2>&1";
DEB_TRACE() << "executing: '" << os.str() << "'";
int ret = system(os.str().c_str());
bool ok = (ret == 0);
CacheMap::value_type entry(cmd, ok);
SystemCmd sudo("sudo", "", false);
sudo.args() << "-l " << m_cmd;
bool ok = (sudo.execute() == 0);
CacheMap::value_type entry(m_cmd, ok);
pair<CacheMap::iterator, bool> v = cache_map.insert(entry);
if (!v.second)
THROW_HW_ERROR(Error) << "Error inserting cache entry";
......@@ -80,16 +92,36 @@ void CPUAffinity::checkSudo(string cmd, string desc)
if (getlogin_r(user, sizeof(user)) != 0)
THROW_HW_ERROR(Error) << "Cannot get user login name";
DEB_ERROR() << "The command '" << cmd << "' is not allowed for " << user
<< " in the sudoers database. ";
DEB_ERROR() << "The command '" << m_cmd << "' is not allowed for "
<< user << " in the sudoers database. ";
DEB_ERROR() << "Check sudoers(5) man page and restart this process";
if (!desc.empty())
DEB_ERROR() << desc;
if (!m_desc.empty())
DEB_ERROR() << m_desc;
THROW_HW_ERROR(Error) << "Cannot execute sudo " << cmd << "! "
THROW_HW_ERROR(Error) << "Cannot execute sudo " << m_cmd << "! "
<< "See output for details";
}
int SystemCmd::execute()
{
DEB_MEMBER_FUNCT();
string args = m_args.str();
DEB_PARAM() << DEB_VAR4(m_cmd, args, m_try_sudo, m_can_hide_out);
ostringstream os;
if (m_try_sudo && getUseSudo()) {
checkSudo();
os << "sudo -n ";
}
os << m_cmd << " " << args;
if (!DEB_CHECK_ANY(DebTypeTrace) && m_can_hide_out)
os << " > /dev/null 2>&1";
DEB_TRACE() << "executing: '" << os.str() << "'";
int ret = system(os.str().c_str());
DEB_RETURN() << DEB_VAR1(ret);
return ret;
}
int CPUAffinity::findNbSystemCPUs()
{
DEB_STATIC_FUNCT();
......@@ -183,27 +215,18 @@ void CPUAffinity::applyWithTaskset(pid_t task, bool incl_threads) const
DEB_MEMBER_FUNCT();
DEB_PARAM() << DEB_VAR3(*this, task, incl_threads);
ostringstream os;
uint64_t mask = *this;
if (UseSudo) {
checkSudo("taskset");
os << "sudo -n ";
}
SystemCmd taskset("taskset");
const char *all_tasks_opt = incl_threads ? "-a " : "";
os << "taskset " << all_tasks_opt << "-p " << *this << " " << task;
if (!DEB_CHECK_ANY(DebTypeTrace))
os << " > /dev/null 2>&1";
DEB_TRACE() << "executing: '" << os.str() << "'";
int ret = system(os.str().c_str());
if (ret != 0) {
taskset.args() << all_tasks_opt << "-p " << *this << " " << task;
if (taskset.execute() != 0) {
const char *th = incl_threads ? "and threads " : "";
THROW_HW_ERROR(Error) << "Error setting task " << task
<< " " << th << "CPU affinity";
}
}
void CPUAffinity::applyWithSetAffinity(pid_t task, bool incl_threads)
const
void CPUAffinity::applyWithSetAffinity(pid_t task, bool incl_threads) const
{
DEB_MEMBER_FUNCT();
DEB_PARAM() << DEB_VAR3(*this, task, incl_threads);
......@@ -304,21 +327,11 @@ bool CPUAffinity::applyWithNetDevSetter(const string& dev,
DEB_MEMBER_FUNCT();
DEB_PARAM() << DEB_VAR2(dev, queue);
ostringstream os;
if (UseSudo) {
static string desc;
if (desc.empty())
desc = getNetDevSetterSudoDesc();
checkSudo(NetDevSetQueueRpsName, desc);
os << "sudo -n ";
}
os << NetDevSetQueueRpsName << " " << dev << " " << queue << " "
<< hex << "0x" << m_mask.to_ulong();
if (!DEB_CHECK_ANY(DebTypeTrace) && false)
os << " > /dev/null 2>&1";
DEB_TRACE() << "executing: '" << os.str() << "'";
int ret = system(os.str().c_str());
bool setter_ok = (ret == 0);
static string desc = getNetDevSetterSudoDesc();
SystemCmd setter(NetDevSetQueueRpsName, desc);
setter.args() << dev << " " << queue << " "
<< hex << "0x" << m_mask.to_ulong();
bool setter_ok = (setter.execute() == 0);
DEB_RETURN() << DEB_VAR1(setter_ok);
return setter_ok;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment