| 1 |
/* |
| 2 |
* Copyright © 2016 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify it |
| 5 |
* under the terms of the GNU General Public License version 3, as published |
| 6 |
* by the Free Software Foundation. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, but |
| 9 |
* WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
* PURPOSE. See the GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Authors: |
| 17 |
* Ted Gould <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#pragma once |
| 21 |
|
| 22 |
#include "jobs-base.h" |
| 23 |
#include <chrono> |
| 24 |
#include <future> |
| 25 |
#include <gio/gio.h> |
| 26 |
#include <map> |
| 27 |
#include <memory> |
| 28 |
#include <mutex> |
| 29 |
#include <signal-unsubscriber.h> |
| 30 |
#include <lomiri/util/ResourcePtr.h> |
| 31 |
|
| 32 |
namespace lomiri |
| 33 |
{ |
| 34 |
namespace app_launch |
| 35 |
{ |
| 36 |
namespace jobs |
| 37 |
{ |
| 38 |
namespace manager |
| 39 |
{ |
| 40 |
|
| 41 |
class SystemD : public Base |
| 42 |
{ |
| 43 |
public: |
| 44 |
SystemD(const std::shared_ptr<Registry::Impl>& registry); |
| 45 |
virtual ~SystemD(); |
| 46 |
|
| 47 |
virtual std::shared_ptr<Application::Instance> launch( |
| 48 |
const AppID& appId, |
| 49 |
const std::string& job, |
| 50 |
const std::string& instance, |
| 51 |
const std::vector<Application::URL>& urls, |
| 52 |
launchMode mode, |
| 53 |
std::function<std::list<std::pair<std::string, std::string>>(void)>& getenv) override; |
| 54 |
virtual std::shared_ptr<Application::Instance> existing(const AppID& appId, |
| 55 |
const std::string& job, |
| 56 |
const std::string& instance, |
| 57 |
const std::vector<Application::URL>& urls) override; |
| 58 |
|
| 59 |
virtual std::list<std::string> runningAppIds(const std::list<std::string>& jobs) override; |
| 60 |
|
| 61 |
virtual std::vector<std::shared_ptr<instance::Base>> instances(const AppID& appID, const std::string& job) override; |
| 62 |
|
| 63 |
virtual core::Signal<const std::string&, const std::string&, const std::string&>& jobStarted() override; |
| 64 |
virtual core::Signal<const std::string&, const std::string&, const std::string&>& jobStopped() override; |
| 65 |
virtual core::Signal<const std::string&, const std::string&, const std::string&, Registry::FailureType>& jobFailed() |
| 66 |
override; |
| 67 |
|
| 68 |
static std::string userBusPath(); |
| 69 |
|
| 70 |
pid_t unitPrimaryPid(const AppID& appId, const std::string& job, const std::string& instance); |
| 71 |
std::vector<pid_t> unitPids(const AppID& appId, const std::string& job, const std::string& instance); |
| 72 |
void stopUnit(const AppID& appId, const std::string& job, const std::string& instance); |
| 73 |
|
| 74 |
private: |
| 75 |
std::string cgroup_root_; |
| 76 |
|
| 77 |
/** Connection to the User DBus bus */ |
| 78 |
std::shared_ptr<GDBusConnection> userbus_; |
| 79 |
/** Setup the bus and all the details in it */ |
| 80 |
void setupUserbus(const std::shared_ptr<Registry::Impl>& reg); |
| 81 |
|
| 82 |
core::Signal<const std::string&, const std::string&, const std::string&> sig_jobStarted; |
| 83 |
core::Signal<const std::string&, const std::string&, const std::string&> sig_jobStopped; |
| 84 |
core::Signal<const std::string&, const std::string&, const std::string&, Registry::FailureType> sig_jobFailed; |
| 85 |
|
| 86 |
ManagedDBusSignalConnection handle_reloading; /**< GDBus signal watcher handle for reloading signal */ |
| 87 |
ManagedDBusSignalConnection handle_unitNew; /**< GDBus signal watcher handle for the unit new signal */ |
| 88 |
ManagedDBusSignalConnection handle_unitRemoved; /**< GDBus signal watcher handle for the unit removed signal */ |
| 89 |
ManagedDBusSignalConnection handle_appFailed; /**< GDBus signal watcher handle for app failed signal */ |
| 90 |
|
| 91 |
bool noResetUnits_{false}; /**< Debug flag to avoid resetting the systemd units */ |
| 92 |
|
| 93 |
bool systemdIsReloading_{false}; /**< Keep track of systemd reloading, to elide other signals. */ |
| 94 |
|
| 95 |
std::once_flag |
| 96 |
flag_appFailed; /**< Variable to track to see if signal handlers are installed for application failed */ |
| 97 |
|
| 98 |
struct UnitInfo |
| 99 |
{ |
| 100 |
std::string appid; |
| 101 |
std::string job; |
| 102 |
std::string inst; |
| 103 |
|
| 104 |
bool operator<(const UnitInfo& b) const |
| 105 |
{ |
| 106 |
if (job != b.job) |
| 107 |
return job < b.job; |
| 108 |
|
| 109 |
if (appid != b.appid) |
| 110 |
return appid < b.appid; |
| 111 |
|
| 112 |
return inst < b.inst; |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
void getInitialUnits(const std::shared_ptr<GDBusConnection>& bus, const std::shared_ptr<GCancellable>& cancel); |
| 117 |
|
| 118 |
struct UnitData |
| 119 |
{ |
| 120 |
std::string jobpath; |
| 121 |
std::string unitpath; |
| 122 |
bool pendingRemoval{false}; |
| 123 |
}; |
| 124 |
|
| 125 |
std::map<UnitInfo, std::shared_ptr<UnitData>> unitPaths; |
| 126 |
UnitInfo parseUnit(const std::string& unit) const; |
| 127 |
std::string unitName(const UnitInfo& info) const; |
| 128 |
std::string unitPath(const UnitInfo& info); |
| 129 |
|
| 130 |
void systemdReloading(bool reloading); |
| 131 |
void unitNew(const std::string& name, const std::string& path, const std::shared_ptr<GDBusConnection>& bus); |
| 132 |
void unitRemoved(const std::string& name, const std::string& path); |
| 133 |
|
| 134 |
static std::string findEnv(const std::string& value, std::list<std::pair<std::string, std::string>>& env); |
| 135 |
static void removeEnv(const std::string& value, std::list<std::pair<std::string, std::string>>& env); |
| 136 |
static void copyEnv(const std::string& envname, std::list<std::pair<std::string, std::string>>& env); |
| 137 |
static void copyEnvByPrefix(const std::string& prefix, std::list<std::pair<std::string, std::string>>& env); |
| 138 |
static int envSize(std::list<std::pair<std::string, std::string>>& env); |
| 139 |
|
| 140 |
static std::vector<std::string> parseExec(std::list<std::pair<std::string, std::string>>& env); |
| 141 |
static void application_start_cb(GObject* obj, GAsyncResult* res, gpointer user_data); |
| 142 |
|
| 143 |
void resetUnit(const UnitInfo& info); |
| 144 |
}; |
| 145 |
|
| 146 |
} // namespace manager |
| 147 |
} // namespace jobs |
| 148 |
} // namespace app_launch |
| 149 |
} // namespace lomiri |