softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontainer::Device Class Reference

Public Member Functions

 Device (std::string name="", int mode=-1)
 
 Device (const Device &dev)
 
bool parse (const json_t *element)
 Configures this device by parsing the supplied JSON configuration string. More...
 
bool activate (std::shared_ptr< ContainerAbstractInterface > container)
 Activates this device by running mknod and chmod commands which are run in the container. More...
 
void calculateDeviceMode (const int appliedMode)
 
const std::string getName ()
 
int getMode ()
 
bool getIsconfigured ()
 
void setMode (int _mode)
 

Detailed Description

Definition at line 29 of file devicenode.h.

Member Function Documentation

bool softwarecontainer::Device::parse ( const json_t *  element)

Configures this device by parsing the supplied JSON configuration string.

Parameters
elementA JSON configuration item.
Returns
false if an error was encountered while parsing, true otherwise.

Definition at line 39 of file devicenode.cpp.

References softwarecontainer::JSONParser::read().

Referenced by softwarecontainer::DeviceNodeGateway::readConfigElement().

40 {
41 
42  m_name = "";
43  if (!JSONParser::read(element, "name", m_name)) {
44  log_error() << "Key \"name\" missing or not a string in json configuration";
45  return false;
46  }
47 
48  m_mode = -1;
49 
50  // Mode is optional, i.e. do not do anything if it is not specified.
51  if (nullptr != json_object_get(element, "mode")) {
52  const bool modeParses = JSONParser::read(element, "mode", m_mode);
53 
54  if (!modeParses) {
55  log_error() << "Mode specified with bad format";
56  return false;
57  }
58  }
59 
60  return true;
61 }
static bool read(const json_t *element, const char *key, std::string &result)
Reads a string from a JSON Object.
Definition: jsonparser.cpp:51

Here is the call graph for this function:

Here is the caller graph for this function:

bool softwarecontainer::Device::activate ( std::shared_ptr< ContainerAbstractInterface container)

Activates this device by running mknod and chmod commands which are run in the container.

Returns
true upon success, false otherwise

Definition at line 63 of file devicenode.cpp.

64 {
65  if (!m_isConfigured) {
66  log_info() << "Mapping device " << m_name;
67  // Mount device in container
68  if (!container->mountDevice(m_name)) {
69  log_error() << "Unable to mount device " << m_name;
70  return false;
71  }
72 
73  // If mode is specified, try to set mode for the mounted device.
74  if (m_mode != -1) {
75  FunctionJob job(container, [&] () {
76  return chmod(m_name.c_str(), m_mode);
77  });
78  job.start();
79  job.wait();
80  if (job.isError()) {
81  log_error() << "Could not 'chmod " << m_mode
82  << "' the mounted device " << m_name;
83  return false;
84  }
85  }
86  m_isConfigured = true;
87  }
88  return true;
89 }

The documentation for this class was generated from the following files: