softwarecontainer  0.18.0-739e8d7 2017-05-04
waylandgateway.cpp
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  */
19 
20 #include <string>
21 #include <unistd.h>
22 #include "waylandgateway.h"
23 
24 namespace softwarecontainer {
25 
26 // These lines are needed in order to define the fields, which otherwise would
27 // yield linker errors.
28 constexpr const char *WaylandGateway::ENABLED_FIELD;
29 constexpr const char *WaylandGateway::SOCKET_FILE_NAME;
30 constexpr const char *WaylandGateway::WAYLAND_RUNTIME_DIR_VARIABLE_NAME;
31 
32 WaylandGateway::WaylandGateway(std::shared_ptr<ContainerAbstractInterface> container) :
33  Gateway(ID, container, true /*this GW is dynamic*/),
34  m_enabled(false),
35  m_activatedOnce(false)
36 {
37 }
38 
39 WaylandGateway::~WaylandGateway()
40 {
41 }
42 
43 bool WaylandGateway::readConfigElement(const json_t *element)
44 {
45  bool configValue = false;
46 
47  if (!JSONParser::read(element, ENABLED_FIELD, configValue)) {
48  log_error() << "Key " << ENABLED_FIELD << " missing or not bool in json configuration";
49  return false;
50  }
51 
52  if (!m_enabled) {
53  m_enabled = configValue;
54  }
55 
56  return true;
57 }
58 
59 bool WaylandGateway::activateGateway()
60 {
61  if (!m_enabled) {
62  log_info() << "Wayland gateway disabled";
63  return true;
64  }
65 
66  /* If we are enabled and have been activated once already, we shouldn't do anything more
67  since bind-mounting again would fail and the whitelisting principle means
68  that we never need to do more than what is already done in this state, e.g.
69  'enabled' is the most permissive state. */
70  if (m_enabled && m_activatedOnce) {
71  log_info() << "Ignoring redundant activation";
72  return true;
73  }
74 
75  bool hasWayland = false;
76  std::string dir = Glib::getenv(WAYLAND_RUNTIME_DIR_VARIABLE_NAME, hasWayland);
77  if (!hasWayland) {
78  log_error() << "Should enable wayland gateway, but " << WAYLAND_RUNTIME_DIR_VARIABLE_NAME << " is not defined";
79  return false;
80  }
81 
82  std::shared_ptr<ContainerAbstractInterface> container = getContainer();
83 
84  log_info() << "enabling Wayland gateway. Socket dir:" << dir;
85  std::string pathInHost = buildPath(dir, SOCKET_FILE_NAME);
86  std::string pathInContainer = buildPath("/gateways", SOCKET_FILE_NAME);
87 
88  if (!container->bindMountInContainer(pathInHost, pathInContainer, false)) {
89  log_error() << "Could not bind mount the wayland socket into the container";
90  return false;
91  }
92 
93  std::string socketDir = parentPath(pathInContainer);
94  container->setEnvironmentVariable(WAYLAND_RUNTIME_DIR_VARIABLE_NAME, socketDir);
95 
96  m_activatedOnce = true;
97 
98  return true;
99 }
100 
101 bool WaylandGateway::teardownGateway()
102 {
103  return true;
104 }
105 
106 } // namespace softwarecontainer
bool readConfigElement(const json_t *element)
Gateway specific parsing of config elements.
std::shared_ptr< ContainerAbstractInterface > getContainer()
Get a handle to the associated container.
Definition: gateway.cpp:128
Developers guide to adding a config item:
static bool read(const json_t *element, const char *key, std::string &result)
Reads a string from a JSON Object.
Definition: jsonparser.cpp:51