softwarecontainer  0.18.0-739e8d7 2017-05-04
dbusgateway.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 "gateway/dbus/dbusgateway.h"
21 #include "gateway/gatewayparsererror.h"
22 
23 namespace softwarecontainer {
24 
25 DBusGateway::DBusGateway(const std::string &gatewayDir,
26  std::shared_ptr<ContainerAbstractInterface> container) :
27  Gateway(ID, container),
28  sessionBus(DBusGatewayInstance::ProxyType::SessionProxy, gatewayDir, container),
29  systemBus(DBusGatewayInstance::ProxyType::SystemProxy, gatewayDir, container)
30 {
31 }
32 
33 DBusGateway::~DBusGateway()
34 {
35 }
36 
37 bool DBusGateway::setConfig(const json_t *config)
38 {
39  try {
40  log_debug() << "Setting config for dbus session bus";
41  bool sessionBusParseResult = sessionBus.setConfig(config);
42 
43  log_debug() << "Setting config for dbus system bus";
44  bool systemBusParseResult = systemBus.setConfig(config);
45 
46  if (!sessionBusParseResult && !systemBusParseResult) {
47  log_error() << "Neither session nor system bus could use the given config";
48  return false;
49  }
50  } catch (GatewayParserError &err) {
51  log_error() << "Bad config in one of the gateway configurations";
52  return false;
53  }
54 
55  return true;
56 }
57 
59 {
60  bool sessionBusActivationResult = false;
61  if (sessionBus.isConfigured()) {
62  log_debug() << "Activating dbus session bus";
63  sessionBusActivationResult = sessionBus.activate();
64  }
65 
66  bool systemBusActivationResult = false;
67  if (systemBus.isConfigured()) {
68  log_debug() << "Activating dbus system bus";
69  sessionBusActivationResult = systemBus.activate();
70  }
71 
72  if (!sessionBusActivationResult && !systemBusActivationResult) {
73  log_error() << "Neither dbus session bus nor dbus system bus could be activated";
74  return false;
75  }
76  return true;
77 }
78 
80 {
81  bool sessionBusTeardownResult;
82  if (sessionBus.isActivated()) {
83  log_debug() << "Tearing down dbus session bus";
84  sessionBusTeardownResult = sessionBus.teardown();
85  } else {
86  sessionBusTeardownResult = false;
87  }
88 
89  bool systemBusTeardownResult;
90  if (systemBus.isActivated()) {
91  log_debug() << "Tearing down dbus system bus";
92  systemBusTeardownResult = systemBus.teardown();
93  } else {
94  systemBusTeardownResult = false;
95  }
96 
97  if (!sessionBusTeardownResult && !systemBusTeardownResult) {
98  log_error() << "Neither dbus session bus nor dbus system bus could be tore down";
99  return false;
100  }
101 
102  return true;
103 }
104 
106 {
107  return sessionBus.isConfigured() || systemBus.isConfigured();
108 }
109 
111 {
112  return sessionBus.isActivated() || systemBus.isActivated();
113 }
114 
115 } // namespace softwarecontainer
virtual bool isActivated() override
Is the gateway activated or not?
virtual bool isConfigured() override
Is the gateway configured or not?
virtual bool teardown() override
Restore system to the state prior to launching of gateway.
Definition: dbusgateway.cpp:79
virtual bool setConfig(const json_t *config) override
Sets config for both dbus session instances.
Definition: dbusgateway.cpp:37
Developers guide to adding a config item:
virtual bool activate() override
Activates both dbus session instances.
Definition: dbusgateway.cpp:58