| 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 |
#include <algorithm> |
| 21 |
#include <array> |
| 22 |
#include <cerrno> |
| 23 |
#include <cstring> |
| 24 |
#include <numeric> |
| 25 |
#include <lomiri/util/GObjectMemory.h> |
| 26 |
#include <lomiri/util/GlibMemory.h> |
| 27 |
#include <lomiri/util/ResourcePtr.h> |
| 28 |
|
| 29 |
#include "application-impl-base.h" |
| 30 |
#include "helper-impl.h" |
| 31 |
#include "jobs-base.h" |
| 32 |
#include "jobs-systemd.h" |
| 33 |
#include "jobs-posix.h" |
| 34 |
#include "registry-impl.h" |
| 35 |
#include "string-util.h" |
| 36 |
|
| 37 |
using namespace lomiri::util; |
| 38 |
|
| 39 |
namespace lomiri |
| 40 |
{ |
| 41 |
namespace app_launch |
| 42 |
{ |
| 43 |
namespace jobs |
| 44 |
{ |
| 45 |
namespace manager |
| 46 |
{ |
| 47 |
|
| 48 |
Base::Base(const std::shared_ptr<Registry::Impl>& registry) |
| 49 |
: registry_{registry} |
| 50 |
, allApplicationJobs_{"application-click", "application-legacy", "application-snap"} |
| 51 |
{ |
| 52 |
} |
| 53 |
|
| 54 |
Base::~Base() |
| 55 |
{ |
| 56 |
} |
| 57 |
|
| 58 |
/** Should determine which jobs backend to use, but we only have |
| 59 |
one right now. */ |
| 60 |
std::shared_ptr<Base> Base::determineFactory(const std::shared_ptr<Registry::Impl>& registry) |
| 61 |
{ |
| 62 |
#if USE_SYSTEMD |
| 63 |
g_debug("Building a systemd jobs manager"); |
| 64 |
return std::make_shared<jobs::manager::SystemD>(registry); |
| 65 |
#else |
| 66 |
g_debug("Building a POSIX jobs manager"); |
| 67 |
return std::make_shared<jobs::manager::POSIX>(registry); |
| 68 |
#endif |
| 69 |
} |
| 70 |
|
| 71 |
const std::list<std::string>& Base::getAllApplicationJobs() const |
| 72 |
{ |
| 73 |
return allApplicationJobs_; |
| 74 |
} |
| 75 |
|
| 76 |
core::Signal<const std::shared_ptr<Application>&, const std::shared_ptr<Application::Instance>&>& Base::appStarted() |
| 77 |
{ |
| 78 |
std::call_once(flag_appStarted, [this]() { |
| 79 |
jobStarted().connect([this](const std::string& job, const std::string& appid, const std::string& instanceid) { |
| 80 |
if (std::find(allApplicationJobs_.begin(), allApplicationJobs_.end(), job) == allApplicationJobs_.end()) |
| 81 |
{ |
| 82 |
/* Not an application, different signal */ |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
try |
| 87 |
{ |
| 88 |
auto reg = getReg(); |
| 89 |
auto appId = reg->find(appid); |
| 90 |
auto app = reg->createApp(appId); |
| 91 |
auto inst = std::dynamic_pointer_cast<app_impls::Base>(app)->findInstance(instanceid); |
| 92 |
|
| 93 |
sig_appStarted(app, inst); |
| 94 |
} |
| 95 |
catch (std::runtime_error& e) |
| 96 |
{ |
| 97 |
g_warning("Error in appStarted signal from job: %s", e.what()); |
| 98 |
} |
| 99 |
}); |
| 100 |
}); |
| 101 |
|
| 102 |
return sig_appStarted; |
| 103 |
} |
| 104 |
|
| 105 |
core::Signal<const std::shared_ptr<Application>&, const std::shared_ptr<Application::Instance>&>& Base::appStopped() |
| 106 |
{ |
| 107 |
std::call_once(flag_appStopped, [this]() { |
| 108 |
jobStopped().connect([this](const std::string& job, const std::string& appid, const std::string& instanceid) { |
| 109 |
if (std::find(allApplicationJobs_.begin(), allApplicationJobs_.end(), job) == allApplicationJobs_.end()) |
| 110 |
{ |
| 111 |
/* Not an application, different signal */ |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
try |
| 116 |
{ |
| 117 |
auto reg = getReg(); |
| 118 |
auto appId = reg->find(appid); |
| 119 |
auto app = reg->createApp(appId); |
| 120 |
auto inst = std::dynamic_pointer_cast<app_impls::Base>(app)->findInstance(instanceid); |
| 121 |
|
| 122 |
sig_appStopped(app, inst); |
| 123 |
} |
| 124 |
catch (std::runtime_error& e) |
| 125 |
{ |
| 126 |
g_warning("Error in appStopped signal from job: %s", e.what()); |
| 127 |
} |
| 128 |
}); |
| 129 |
}); |
| 130 |
|
| 131 |
return sig_appStopped; |
| 132 |
} |
| 133 |
|
| 134 |
core::Signal<const std::shared_ptr<Application>&, const std::shared_ptr<Application::Instance>&, Registry::FailureType>& |
| 135 |
Base::appFailed() |
| 136 |
{ |
| 137 |
std::call_once(flag_appFailed, [this]() { |
| 138 |
jobFailed().connect([this](const std::string& job, const std::string& appid, const std::string& instanceid, |
| 139 |
Registry::FailureType reason) { |
| 140 |
if (std::find(allApplicationJobs_.begin(), allApplicationJobs_.end(), job) == allApplicationJobs_.end()) |
| 141 |
{ |
| 142 |
/* Not an application, different signal */ |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
try |
| 147 |
{ |
| 148 |
auto reg = getReg(); |
| 149 |
auto appId = reg->find(appid); |
| 150 |
auto app = reg->createApp(appId); |
| 151 |
auto inst = std::dynamic_pointer_cast<app_impls::Base>(app)->findInstance(instanceid); |
| 152 |
|
| 153 |
sig_appFailed(app, inst, reason); |
| 154 |
} |
| 155 |
catch (std::runtime_error& e) |
| 156 |
{ |
| 157 |
g_warning("Error in appFailed signal from job: %s", e.what()); |
| 158 |
} |
| 159 |
}); |
| 160 |
}); |
| 161 |
|
| 162 |
return sig_appFailed; |
| 163 |
} |
| 164 |
|
| 165 |
/** Structure to track the data needed for upstart events. This cleans |
| 166 |
up the lifecycle as we're passing this as a pointer through the |
| 167 |
GLib calls. */ |
| 168 |
struct upstartEventData |
| 169 |
{ |
| 170 |
/** Keeping a weak pointer because the handle is held by |
| 171 |
the registry implementation. */ |
| 172 |
std::weak_ptr<Registry::Impl> weakReg; |
| 173 |
}; |
| 174 |
|
| 175 |
/** Core handler for pause and resume events. Includes turning the GVariant |
| 176 |
pid list into a std::vector and getting the application object. */ |
| 177 |
void Base::pauseEventEmitted(core::Signal<const std::shared_ptr<Application>&, |
| 178 |
const std::shared_ptr<Application::Instance>&, |
| 179 |
const std::vector<pid_t>&>& signal, |
| 180 |
const std::shared_ptr<GVariant>& params, |
| 181 |
const std::shared_ptr<Registry::Impl>& reg) |
| 182 |
{ |
| 183 |
std::vector<pid_t> pids; |
| 184 |
auto vappid = unique_glib(g_variant_get_child_value(params.get(), 0)); |
| 185 |
auto vinstid = unique_glib(g_variant_get_child_value(params.get(), 1)); |
| 186 |
auto vpids = unique_glib(g_variant_get_child_value(params.get(), 2)); |
| 187 |
guint64 pid; |
| 188 |
GVariantIter thispid; |
| 189 |
g_variant_iter_init(&thispid, vpids.get()); |
| 190 |
|
| 191 |
while (g_variant_iter_loop(&thispid, "t", &pid)) |
| 192 |
{ |
| 193 |
pids.emplace_back(pid); |
| 194 |
} |
| 195 |
|
| 196 |
auto cappid = g_variant_get_string(vappid.get(), NULL); |
| 197 |
auto cinstid = g_variant_get_string(vinstid.get(), NULL); |
| 198 |
|
| 199 |
auto appid = reg->find(cappid); |
| 200 |
auto app = reg->createApp(appid); |
| 201 |
auto inst = std::dynamic_pointer_cast<app_impls::Base>(app)->findInstance(cinstid); |
| 202 |
|
| 203 |
signal(app, inst, pids); |
| 204 |
|
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
/** Grab the signal object for application paused. If we're not already listing for |
| 209 |
those signals this sets up a listener for them. */ |
| 210 |
core::Signal<const std::shared_ptr<Application>&, |
| 211 |
const std::shared_ptr<Application::Instance>&, |
| 212 |
const std::vector<pid_t>&>& |
| 213 |
Base::appPaused() |
| 214 |
{ |
| 215 |
std::call_once(flag_appPaused, [this]() { |
| 216 |
auto reg = getReg(); |
| 217 |
reg->thread.executeOnThread<bool>([this, reg]() { |
| 218 |
upstartEventData* data = new upstartEventData{reg}; |
| 219 |
|
| 220 |
handle_appPaused = managedDBusSignalConnection( |
| 221 |
g_dbus_connection_signal_subscribe(reg->_dbus.get(), /* bus */ |
| 222 |
nullptr, /* sender */ |
| 223 |
"com.lomiri.LomiriAppLaunch", /* interface */ |
| 224 |
"ApplicationPaused", /* signal */ |
| 225 |
"/", /* path */ |
| 226 |
nullptr, /* arg0 */ |
| 227 |
G_DBUS_SIGNAL_FLAGS_NONE, |
| 228 |
[](GDBusConnection*, const gchar*, const gchar*, const gchar*, |
| 229 |
const gchar*, GVariant* params, gpointer user_data) -> void { |
| 230 |
auto data = reinterpret_cast<upstartEventData*>(user_data); |
| 231 |
auto reg = data->weakReg.lock(); |
| 232 |
|
| 233 |
if (!reg) |
| 234 |
{ |
| 235 |
g_warning("Registry object invalid!"); |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
auto sparams = share_glib(g_variant_ref(params)); |
| 240 |
auto manager = std::dynamic_pointer_cast<Base>(reg->jobs()); |
| 241 |
manager->pauseEventEmitted(manager->sig_appPaused, sparams, reg); |
| 242 |
}, /* callback */ |
| 243 |
data, /* user data */ |
| 244 |
[](gpointer user_data) { |
| 245 |
auto data = reinterpret_cast<upstartEventData*>(user_data); |
| 246 |
delete data; |
| 247 |
}), /* user data destroy */ |
| 248 |
reg->_dbus); |
| 249 |
|
| 250 |
return true; |
| 251 |
}); |
| 252 |
}); |
| 253 |
|
| 254 |
return sig_appPaused; |
| 255 |
} |
| 256 |
|
| 257 |
/** Grab the signal object for application resumed. If we're not already listing for |
| 258 |
those signals this sets up a listener for them. */ |
| 259 |
core::Signal<const std::shared_ptr<Application>&, |
| 260 |
const std::shared_ptr<Application::Instance>&, |
| 261 |
const std::vector<pid_t>&>& |
| 262 |
Base::appResumed() |
| 263 |
{ |
| 264 |
std::call_once(flag_appResumed, [this]() { |
| 265 |
auto reg = getReg(); |
| 266 |
reg->thread.executeOnThread<bool>([this, reg]() { |
| 267 |
upstartEventData* data = new upstartEventData{reg}; |
| 268 |
|
| 269 |
handle_appResumed = managedDBusSignalConnection( |
| 270 |
g_dbus_connection_signal_subscribe(reg->_dbus.get(), /* bus */ |
| 271 |
nullptr, /* sender */ |
| 272 |
"com.lomiri.LomiriAppLaunch", /* interface */ |
| 273 |
"ApplicationResumed", /* signal */ |
| 274 |
"/", /* path */ |
| 275 |
nullptr, /* arg0 */ |
| 276 |
G_DBUS_SIGNAL_FLAGS_NONE, |
| 277 |
[](GDBusConnection*, const gchar*, const gchar*, const gchar*, |
| 278 |
const gchar*, GVariant* params, gpointer user_data) -> void { |
| 279 |
auto data = reinterpret_cast<upstartEventData*>(user_data); |
| 280 |
auto reg = data->weakReg.lock(); |
| 281 |
|
| 282 |
if (!reg) |
| 283 |
{ |
| 284 |
g_warning("Registry object invalid!"); |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
auto sparams = share_glib(g_variant_ref(params)); |
| 289 |
auto manager = std::dynamic_pointer_cast<Base>(reg->jobs()); |
| 290 |
manager->pauseEventEmitted(manager->sig_appResumed, sparams, |
| 291 |
reg); |
| 292 |
}, /* callback */ |
| 293 |
data, /* user data */ |
| 294 |
[](gpointer user_data) { |
| 295 |
auto data = reinterpret_cast<upstartEventData*>(user_data); |
| 296 |
delete data; |
| 297 |
}), /* user data destroy */ |
| 298 |
reg->_dbus); |
| 299 |
|
| 300 |
return true; |
| 301 |
}); |
| 302 |
}); |
| 303 |
|
| 304 |
return sig_appResumed; |
| 305 |
} |
| 306 |
|
| 307 |
core::Signal<const std::shared_ptr<Helper>&, const std::shared_ptr<Helper::Instance>&>& Base::helperStarted( |
| 308 |
Helper::Type type) |
| 309 |
{ |
| 310 |
try |
| 311 |
{ |
| 312 |
return *sig_helpersStarted.at(type.value()); |
| 313 |
} |
| 314 |
catch (std::out_of_range& e) |
| 315 |
{ |
| 316 |
jobStarted().connect( |
| 317 |
[this, type](const std::string& job, const std::string& appid, const std::string& instanceid) { |
| 318 |
if (job != type.value()) |
| 319 |
{ |
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
try |
| 324 |
{ |
| 325 |
auto reg = getReg(); |
| 326 |
auto appId = reg->find(appid); |
| 327 |
auto helper = reg->createHelper(type, appId, reg); |
| 328 |
auto inst = std::dynamic_pointer_cast<helper_impls::Base>(helper)->existingInstance(instanceid); |
| 329 |
|
| 330 |
(*sig_helpersStarted.at(type.value()))(helper, inst); |
| 331 |
} |
| 332 |
catch (...) |
| 333 |
{ |
| 334 |
g_warning("Unable to emit signal for helper type: %s", type.value().c_str()); |
| 335 |
} |
| 336 |
}); |
| 337 |
|
| 338 |
sig_helpersStarted.emplace( |
| 339 |
type.value(), |
| 340 |
std::make_shared<core::Signal<const std::shared_ptr<Helper>&, const std::shared_ptr<Helper::Instance>&>>()); |
| 341 |
} |
| 342 |
|
| 343 |
return *sig_helpersStarted.at(type.value()); |
| 344 |
} |
| 345 |
|
| 346 |
core::Signal<const std::shared_ptr<Helper>&, const std::shared_ptr<Helper::Instance>&>& Base::helperStopped( |
| 347 |
Helper::Type type) |
| 348 |
{ |
| 349 |
try |
| 350 |
{ |
| 351 |
return *sig_helpersStopped.at(type.value()); |
| 352 |
} |
| 353 |
catch (std::out_of_range& e) |
| 354 |
{ |
| 355 |
jobStopped().connect( |
| 356 |
[this, type](const std::string& job, const std::string& appid, const std::string& instanceid) { |
| 357 |
if (job != type.value()) |
| 358 |
{ |
| 359 |
return; |
| 360 |
} |
| 361 |
|
| 362 |
try |
| 363 |
{ |
| 364 |
auto reg = getReg(); |
| 365 |
auto appId = lomiri::app_launch::AppID::parse(appid); |
| 366 |
auto helper = reg->createHelper(type, appId, reg); |
| 367 |
auto inst = std::dynamic_pointer_cast<helper_impls::Base>(helper)->existingInstance(instanceid); |
| 368 |
|
| 369 |
(*sig_helpersStopped.at(type.value()))(helper, inst); |
| 370 |
} |
| 371 |
catch (...) |
| 372 |
{ |
| 373 |
g_warning("Unable to emit signal for helper type: %s", type.value().c_str()); |
| 374 |
} |
| 375 |
}); |
| 376 |
|
| 377 |
sig_helpersStopped.emplace( |
| 378 |
type.value(), |
| 379 |
std::make_shared<core::Signal<const std::shared_ptr<Helper>&, const std::shared_ptr<Helper::Instance>&>>()); |
| 380 |
} |
| 381 |
|
| 382 |
return *sig_helpersStopped.at(type.value()); |
| 383 |
} |
| 384 |
|
| 385 |
core::Signal<const std::shared_ptr<Helper>&, const std::shared_ptr<Helper::Instance>&, Registry::FailureType>& |
| 386 |
Base::helperFailed(Helper::Type type) |
| 387 |
{ |
| 388 |
try |
| 389 |
{ |
| 390 |
return *sig_helpersFailed.at(type.value()); |
| 391 |
} |
| 392 |
catch (std::out_of_range& e) |
| 393 |
{ |
| 394 |
jobFailed().connect([this, type](const std::string& job, const std::string& appid, |
| 395 |
const std::string& instanceid, Registry::FailureType reason) { |
| 396 |
if (job != type.value()) |
| 397 |
{ |
| 398 |
return; |
| 399 |
} |
| 400 |
|
| 401 |
try |
| 402 |
{ |
| 403 |
auto reg = getReg(); |
| 404 |
auto appId = lomiri::app_launch::AppID::parse(appid); |
| 405 |
auto helper = reg->createHelper(type, appId, reg); |
| 406 |
auto inst = std::dynamic_pointer_cast<helper_impls::Base>(helper)->existingInstance(instanceid); |
| 407 |
|
| 408 |
(*sig_helpersFailed.at(type.value()))(helper, inst, reason); |
| 409 |
} |
| 410 |
catch (...) |
| 411 |
{ |
| 412 |
g_warning("Unable to emit signal for helper type: %s", type.value().c_str()); |
| 413 |
} |
| 414 |
}); |
| 415 |
|
| 416 |
sig_helpersFailed.emplace( |
| 417 |
type.value(), |
| 418 |
std::make_shared<core::Signal<const std::shared_ptr<Helper>&, const std::shared_ptr<Helper::Instance>&, |
| 419 |
Registry::FailureType>>()); |
| 420 |
} |
| 421 |
|
| 422 |
return *sig_helpersFailed.at(type.value()); |
| 423 |
} |
| 424 |
|
| 425 |
/** Take the GVariant of parameters and turn them into an application and |
| 426 |
and instance. Easier to read in the smaller function */ |
| 427 |
std::tuple<std::shared_ptr<Application>, std::shared_ptr<Application::Instance>> Base::managerParams( |
| 428 |
const std::shared_ptr<GVariant>& params, const std::shared_ptr<Registry::Impl>& reg) |
| 429 |
{ |
| 430 |
std::shared_ptr<Application> app; |
| 431 |
std::shared_ptr<Application::Instance> instance; |
| 432 |
|
| 433 |
const gchar* cappid = nullptr; |
| 434 |
const gchar* cinstid = nullptr; |
| 435 |
g_variant_get(params.get(), "(&s&s)", &cappid, &cinstid); |
| 436 |
|
| 437 |
auto appid = reg->find(cappid); |
| 438 |
app = reg->createApp(appid); |
| 439 |
|
| 440 |
/* TODO Instance */ |
| 441 |
|
| 442 |
return std::make_tuple(app, instance); |
| 443 |
} |
| 444 |
|
| 445 |
/** Used to store data for manager based signal handlers. Has a link to the |
| 446 |
registry and the callback to use in a C++ style. */ |
| 447 |
struct managerEventData |
| 448 |
{ |
| 449 |
/* Keeping a weak pointer because the handle is held by |
| 450 |
the registry implementation. */ |
| 451 |
std::weak_ptr<Registry::Impl> weakReg; |
| 452 |
std::function<void(const std::shared_ptr<Registry::Impl>& reg, |
| 453 |
const std::shared_ptr<Application>& app, |
| 454 |
const std::shared_ptr<Application::Instance>& instance, |
| 455 |
const std::shared_ptr<GDBusConnection>&, |
| 456 |
const std::string&, |
| 457 |
const std::shared_ptr<GVariant>&)> |
| 458 |
func; |
| 459 |
}; |
| 460 |
|
| 461 |
/** Register for a signal for the manager. All of the signals needed this same |
| 462 |
code so it got pulled out into a function. Takes the same of the signal, the registry |
| 463 |
that we're using and a function to call after we've messaged all the parameters |
| 464 |
into being something C++-ish. */ |
| 465 |
guint Base::managerSignalHelper(const std::string& signalname, |
| 466 |
std::function<void(const std::shared_ptr<Registry::Impl>& reg, |
| 467 |
const std::shared_ptr<Application>& app, |
| 468 |
const std::shared_ptr<Application::Instance>& instance, |
| 469 |
const std::shared_ptr<GDBusConnection>&, |
| 470 |
const std::string&, |
| 471 |
const std::shared_ptr<GVariant>&)> responsefunc) |
| 472 |
{ |
| 473 |
auto reg = getReg(); |
| 474 |
managerEventData* focusdata = new managerEventData{reg, responsefunc}; |
| 475 |
|
| 476 |
return g_dbus_connection_signal_subscribe( |
| 477 |
reg->_dbus.get(), /* bus */ |
| 478 |
nullptr, /* sender */ |
| 479 |
"com.lomiri.LomiriAppLaunch", /* interface */ |
| 480 |
signalname.c_str(), /* signal */ |
| 481 |
"/", /* path */ |
| 482 |
nullptr, /* arg0 */ |
| 483 |
G_DBUS_SIGNAL_FLAGS_NONE, |
| 484 |
[](GDBusConnection* cconn, const gchar* csender, const gchar*, const gchar*, const gchar*, GVariant* params, |
| 485 |
gpointer user_data) -> void { |
| 486 |
auto data = reinterpret_cast<managerEventData*>(user_data); |
| 487 |
auto reg = data->weakReg.lock(); |
| 488 |
|
| 489 |
if (!reg) |
| 490 |
{ |
| 491 |
g_warning("Registry object invalid!"); |
| 492 |
return; |
| 493 |
} |
| 494 |
|
| 495 |
/* If we're still conneted and the manager has been cleared |
| 496 |
we'll just be a no-op */ |
| 497 |
auto ljobs = std::dynamic_pointer_cast<Base>(reg->jobs()); |
| 498 |
if (!ljobs->manager_) |
| 499 |
{ |
| 500 |
return; |
| 501 |
} |
| 502 |
|
| 503 |
try |
| 504 |
{ |
| 505 |
auto vparams = share_glib(g_variant_ref(params)); |
| 506 |
auto conn = share_gobject(G_DBUS_CONNECTION(g_object_ref(cconn))); |
| 507 |
std::string sender = csender; |
| 508 |
std::shared_ptr<Application> app; |
| 509 |
std::shared_ptr<Application::Instance> instance; |
| 510 |
|
| 511 |
std::tie(app, instance) = managerParams(vparams, reg); |
| 512 |
|
| 513 |
data->func(reg, app, instance, conn, sender, vparams); |
| 514 |
} |
| 515 |
catch (std::runtime_error& e) |
| 516 |
{ |
| 517 |
g_warning("Unable to call signal handler for manager signal: %s", e.what()); |
| 518 |
} |
| 519 |
}, |
| 520 |
focusdata, |
| 521 |
[](gpointer user_data) { |
| 522 |
auto data = reinterpret_cast<managerEventData*>(user_data); |
| 523 |
delete data; |
| 524 |
}); /* user data destroy */ |
| 525 |
} |
| 526 |
|
| 527 |
/** Set the manager for the registry. This includes tracking the pointer |
| 528 |
as well as setting up the signals to call back into the manager. The |
| 529 |
signals are only setup once per registry even if the manager is cleared |
| 530 |
and changed again. They will just be no-op's in those cases. |
| 531 |
*/ |
| 532 |
void Base::setManager(std::shared_ptr<Registry::Manager> manager) |
| 533 |
{ |
| 534 |
if (manager_) |
| 535 |
{ |
| 536 |
throw std::runtime_error("Already have a manager and trying to set another"); |
| 537 |
} |
| 538 |
|
| 539 |
g_debug("Setting a new manager"); |
| 540 |
manager_ = manager; |
| 541 |
|
| 542 |
std::call_once(flag_managerSignals, [this]() { |
| 543 |
auto reg = getReg(); |
| 544 |
|
| 545 |
if (!reg->thread.executeOnThread<bool>([this, reg]() { |
| 546 |
handle_managerSignalFocus = managedDBusSignalConnection( |
| 547 |
managerSignalHelper( |
| 548 |
"LomiriFocusRequest", |
| 549 |
[](const std::shared_ptr<Registry::Impl>& reg, const std::shared_ptr<Application>& app, |
| 550 |
const std::shared_ptr<Application::Instance>& instance, |
| 551 |
const std::shared_ptr<GDBusConnection>& conn, const std::string& sender, |
| 552 |
const std::shared_ptr<GVariant>& params) { |
| 553 |
/* Nothing to do today */ |
| 554 |
std::dynamic_pointer_cast<Base>(reg->jobs()) |
| 555 |
->manager_->focusRequest(app, instance, [](bool response) { |
| 556 |
/* NOTE: We have no |
| 557 |
clue what thread |
| 558 |
this is gonna be |
| 559 |
executed on, but |
| 560 |
since we're just |
| 561 |
talking to the |
| 562 |
GDBus |
| 563 |
thread it isn't an |
| 564 |
issue today. Be |
| 565 |
careful in |
| 566 |
changing |
| 567 |
this code. */ |
| 568 |
}); |
| 569 |
}), |
| 570 |
reg->_dbus); |
| 571 |
handle_managerSignalStarting = managedDBusSignalConnection( |
| 572 |
managerSignalHelper( |
| 573 |
"LomiriStartingBroadcast", |
| 574 |
[](const std::shared_ptr<Registry::Impl>& reg, const std::shared_ptr<Application>& app, |
| 575 |
const std::shared_ptr<Application::Instance>& instance, |
| 576 |
const std::shared_ptr<GDBusConnection>& conn, const std::string& sender, |
| 577 |
const std::shared_ptr<GVariant>& params) { |
| 578 |
|
| 579 |
std::dynamic_pointer_cast<Base>(reg->jobs()) |
| 580 |
->manager_->startingRequest(app, instance, [conn, sender, params](bool response) { |
| 581 |
/* NOTE: We have no clue what thread this is gonna be |
| 582 |
executed on, but since we're just talking to the GDBus |
| 583 |
thread it isn't an issue today. Be careful in changing |
| 584 |
this code. */ |
| 585 |
if (response) |
| 586 |
{ |
| 587 |
g_dbus_connection_emit_signal(conn.get(), sender.c_str(), /* destination */ |
| 588 |
"/", /* path */ |
| 589 |
"com.lomiri.LomiriAppLaunch", /* interface */ |
| 590 |
"LomiriStartingSignal", /* signal */ |
| 591 |
params.get(), /* params, the same */ |
| 592 |
nullptr); /* error */ |
| 593 |
} |
| 594 |
}); |
| 595 |
}), |
| 596 |
reg->_dbus); |
| 597 |
handle_managerSignalResume = managedDBusSignalConnection( |
| 598 |
managerSignalHelper( |
| 599 |
"LomiriResumeRequest", |
| 600 |
[](const std::shared_ptr<Registry::Impl>& reg, const std::shared_ptr<Application>& app, |
| 601 |
const std::shared_ptr<Application::Instance>& instance, |
| 602 |
const std::shared_ptr<GDBusConnection>& conn, const std::string& sender, |
| 603 |
const std::shared_ptr<GVariant>& params) { |
| 604 |
std::dynamic_pointer_cast<Base>(reg->jobs()) |
| 605 |
->manager_->resumeRequest(app, instance, [conn, sender, params](bool response) { |
| 606 |
/* NOTE: We have no clue what thread this is gonna be |
| 607 |
executed on, but since we're just talking to the GDBus |
| 608 |
thread it isn't an issue today. Be careful in changing |
| 609 |
this code. */ |
| 610 |
if (response) |
| 611 |
{ |
| 612 |
g_dbus_connection_emit_signal(conn.get(), sender.c_str(), /* destination */ |
| 613 |
"/", /* path */ |
| 614 |
"com.lomiri.LomiriAppLaunch", /* interface */ |
| 615 |
"LomiriResumeResponse", /* signal */ |
| 616 |
params.get(), /* params, the same */ |
| 617 |
nullptr); /* error */ |
| 618 |
} |
| 619 |
}); |
| 620 |
}), |
| 621 |
reg->_dbus); |
| 622 |
|
| 623 |
return true; |
| 624 |
})) |
| 625 |
{ |
| 626 |
g_warning("Unable to install manager signals"); |
| 627 |
} |
| 628 |
}); |
| 629 |
} |
| 630 |
|
| 631 |
/** Clear the manager pointer */ |
| 632 |
void Base::clearManager() |
| 633 |
{ |
| 634 |
g_debug("Clearing the manager"); |
| 635 |
manager_.reset(); |
| 636 |
} |
| 637 |
|
| 638 |
/** Get application objects for all of the applications based |
| 639 |
on the appids associated with the application jobs */ |
| 640 |
std::list<std::shared_ptr<Application>> Base::runningApps() |
| 641 |
{ |
| 642 |
auto reg = getReg(); |
| 643 |
auto appids = runningAppIds(allApplicationJobs_); |
| 644 |
|
| 645 |
std::list<std::shared_ptr<Application>> apps; |
| 646 |
for (const auto& appid : appids) |
| 647 |
{ |
| 648 |
auto id = reg->find(appid); |
| 649 |
if (id.empty()) |
| 650 |
{ |
| 651 |
g_debug("Unable to handle AppID: %s", appid.c_str()); |
| 652 |
continue; |
| 653 |
} |
| 654 |
|
| 655 |
try |
| 656 |
{ |
| 657 |
apps.emplace_back(reg->createApp(id)); |
| 658 |
} |
| 659 |
catch (std::runtime_error& e) |
| 660 |
{ |
| 661 |
g_debug("Error adding appid '%s' to running apps list: %s", appid.c_str(), e.what()); |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
return apps; |
| 666 |
} |
| 667 |
|
| 668 |
/** Get application objects for all of the applications based |
| 669 |
on the appids associated with the application jobs */ |
| 670 |
std::list<std::shared_ptr<Helper>> Base::runningHelpers(const Helper::Type& type) |
| 671 |
{ |
| 672 |
auto reg = getReg(); |
| 673 |
auto appids = runningAppIds({type.value()}); |
| 674 |
|
| 675 |
std::list<std::shared_ptr<Helper>> helpers; |
| 676 |
for (const auto& appid : appids) |
| 677 |
{ |
| 678 |
auto id = reg->find(appid); |
| 679 |
if (id.empty()) |
| 680 |
{ |
| 681 |
g_debug("Unable to handle AppID: %s", appid.c_str()); |
| 682 |
continue; |
| 683 |
} |
| 684 |
|
| 685 |
helpers.emplace_back(reg->createHelper(type, id, reg)); |
| 686 |
} |
| 687 |
|
| 688 |
return helpers; |
| 689 |
} |
| 690 |
|
| 691 |
} // namespace manager |
| 692 |
|
| 693 |
namespace instance |
| 694 |
{ |
| 695 |
|
| 696 |
Base::Base(const AppID& appId, |
| 697 |
const std::string& job, |
| 698 |
const std::string& instance, |
| 699 |
const std::vector<Application::URL>& urls, |
| 700 |
const std::shared_ptr<Registry::Impl>& registry) |
| 701 |
: appId_(appId) |
| 702 |
, job_(job) |
| 703 |
, instance_(instance) |
| 704 |
, urls_(urls) |
| 705 |
, registry_(registry) |
| 706 |
{ |
| 707 |
} |
| 708 |
|
| 709 |
/** Checks to see if we have a primary PID for the instance */ |
| 710 |
bool Base::isRunning() |
| 711 |
{ |
| 712 |
return primaryPid() != 0; |
| 713 |
} |
| 714 |
|
| 715 |
/** Looks at the PIDs in the instance cgroup and checks to see if @pid |
| 716 |
is in the set. |
| 717 |
|
| 718 |
@param pid PID to look for |
| 719 |
*/ |
| 720 |
bool Base::hasPid(pid_t pid) |
| 721 |
{ |
| 722 |
auto vpids = pids(); |
| 723 |
bool hasit = std::find(vpids.begin(), vpids.end(), pid) != vpids.end(); |
| 724 |
g_debug("Checking for PID %d on AppID '%s' result: %s", pid, std::string(appId_).c_str(), hasit ? "YES" : "NO"); |
| 725 |
return hasit; |
| 726 |
} |
| 727 |
|
| 728 |
/** Pauses this application by sending SIGSTOP to all the PIDs in the |
| 729 |
cgroup and tells Zeitgeist that we've left the application. */ |
| 730 |
void Base::pause() |
| 731 |
{ |
| 732 |
g_debug("Pausing application: %s", std::string(appId_).c_str()); |
| 733 |
registry_->zgSendEvent(appId_, ZEITGEIST_ZG_LEAVE_EVENT); |
| 734 |
|
| 735 |
auto pids = forAllPids([this](pid_t pid) { |
| 736 |
auto oomval = oom::paused(); |
| 737 |
g_debug("Pausing PID: %d (%d)", pid, int(oomval)); |
| 738 |
signalToPid(pid, SIGSTOP); |
| 739 |
oomValueToPid(pid, oomval); |
| 740 |
}); |
| 741 |
|
| 742 |
pidListToDbus(registry_, appId_, instance_, pids, "ApplicationPaused"); |
| 743 |
} |
| 744 |
|
| 745 |
/** Resumes this application by sending SIGCONT to all the PIDs in the |
| 746 |
cgroup and tells Zeitgeist that we're accessing the application. */ |
| 747 |
void Base::resume() |
| 748 |
{ |
| 749 |
g_debug("Resuming application: %s", std::string(appId_).c_str()); |
| 750 |
registry_->zgSendEvent(appId_, ZEITGEIST_ZG_ACCESS_EVENT); |
| 751 |
|
| 752 |
auto pids = forAllPids([this](pid_t pid) { |
| 753 |
auto oomval = oom::focused(); |
| 754 |
g_debug("Resuming PID: %d (%d)", pid, int(oomval)); |
| 755 |
signalToPid(pid, SIGCONT); |
| 756 |
oomValueToPid(pid, oomval); |
| 757 |
}); |
| 758 |
|
| 759 |
pidListToDbus(registry_, appId_, instance_, pids, "ApplicationResumed"); |
| 760 |
} |
| 761 |
|
| 762 |
/** Focuses this application by sending SIGCONT to all the PIDs in the |
| 763 |
cgroup and tells the Shell to focus the application. */ |
| 764 |
void Base::focus() |
| 765 |
{ |
| 766 |
g_debug("Focusing application: %s", std::string(appId_).c_str()); |
| 767 |
|
| 768 |
GError* error = nullptr; |
| 769 |
GVariantBuilder params; |
| 770 |
g_variant_builder_init(¶ms, G_VARIANT_TYPE_TUPLE); |
| 771 |
g_variant_builder_add_value(¶ms, g_variant_new_string(std::string(appId_).c_str())); |
| 772 |
g_variant_builder_add_value(¶ms, g_variant_new_string(instance_.c_str())); |
| 773 |
g_dbus_connection_emit_signal(registry_->_dbus.get(), /* bus */ |
| 774 |
nullptr, /* destination */ |
| 775 |
"/", /* path */ |
| 776 |
"com.lomiri.LomiriAppLaunch", /* interface */ |
| 777 |
"LomiriFocusRequest", /* signal */ |
| 778 |
g_variant_builder_end(¶ms), /* params */ |
| 779 |
&error); /* error */ |
| 780 |
|
| 781 |
if (error != nullptr) |
| 782 |
{ |
| 783 |
g_warning("Unable to emit signal 'LomiriFocusRequest' for appid '%s': '%s'", std::string(appId_).c_str(), |
| 784 |
error->message); |
| 785 |
g_error_free(error); |
| 786 |
} |
| 787 |
else |
| 788 |
{ |
| 789 |
g_debug("Emitted 'LomiriFocusRequest' to DBus"); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
/** Go through the list of PIDs calling a function and handling |
| 794 |
the issue with getting PIDs being a racey condition. |
| 795 |
|
| 796 |
\param eachPid Function to run on each PID |
| 797 |
*/ |
| 798 |
std::vector<pid_t> Base::forAllPids(std::function<void(pid_t)> eachPid) |
| 799 |
{ |
| 800 |
std::set<pid_t> seenPids; |
| 801 |
bool added = true; |
| 802 |
|
| 803 |
while (added) |
| 804 |
{ |
| 805 |
added = false; |
| 806 |
auto pidlist = pids(); |
| 807 |
for (auto pid : pidlist) |
| 808 |
{ |
| 809 |
if (seenPids.insert(pid).second) |
| 810 |
{ |
| 811 |
eachPid(pid); |
| 812 |
added = true; |
| 813 |
} |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
return std::vector<pid_t>(seenPids.begin(), seenPids.end()); |
| 818 |
} |
| 819 |
|
| 820 |
/** Send a signal that we've change the application. Do this on the |
| 821 |
registry thread in an idle so that we don't block anyone. |
| 822 |
|
| 823 |
\param pids List of PIDs to turn into variants to send |
| 824 |
\param signal Name of the DBus signal to send |
| 825 |
*/ |
| 826 |
void Base::pidListToDbus(const std::shared_ptr<Registry::Impl>& reg, |
| 827 |
const AppID& appid, |
| 828 |
const std::string& instanceid, |
| 829 |
const std::vector<pid_t>& pids, |
| 830 |
const std::string& signal) |
| 831 |
{ |
| 832 |
GVariantUPtr vpids; |
| 833 |
|
| 834 |
if (pids.empty()) |
| 835 |
{ |
| 836 |
vpids = unique_glib(g_variant_ref_sink(g_variant_new_array(G_VARIANT_TYPE_UINT64, nullptr, 0))); |
| 837 |
} |
| 838 |
else |
| 839 |
{ |
| 840 |
GVariantBuilder builder; |
| 841 |
g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY); |
| 842 |
|
| 843 |
for (auto pid : pids) |
| 844 |
{ |
| 845 |
g_variant_builder_add_value(&builder, g_variant_new_uint64(pid)); |
| 846 |
} |
| 847 |
|
| 848 |
vpids = unique_glib(g_variant_ref_sink(g_variant_builder_end(&builder))); |
| 849 |
} |
| 850 |
|
| 851 |
GVariantBuilder params; |
| 852 |
g_variant_builder_init(¶ms, G_VARIANT_TYPE_TUPLE); |
| 853 |
g_variant_builder_add_value(¶ms, g_variant_new_string(std::string(appid).c_str())); |
| 854 |
g_variant_builder_add_value(¶ms, g_variant_new_string(instanceid.c_str())); |
| 855 |
g_variant_builder_add_value(¶ms, vpids.get()); |
| 856 |
|
| 857 |
GError* error = nullptr; |
| 858 |
g_dbus_connection_emit_signal(reg->_dbus.get(), /* bus */ |
| 859 |
nullptr, /* destination */ |
| 860 |
"/", /* path */ |
| 861 |
"com.lomiri.LomiriAppLaunch", /* interface */ |
| 862 |
signal.c_str(), /* signal */ |
| 863 |
g_variant_builder_end(¶ms), /* params, the same */ |
| 864 |
&error); /* error */ |
| 865 |
|
| 866 |
if (error != nullptr) |
| 867 |
{ |
| 868 |
g_warning("Unable to emit signal '%s' for appid '%s': %s", signal.c_str(), std::string(appid).c_str(), |
| 869 |
error->message); |
| 870 |
g_error_free(error); |
| 871 |
} |
| 872 |
else |
| 873 |
{ |
| 874 |
g_debug("Emitted '%s' to DBus", signal.c_str()); |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
/** Sets the OOM adjustment by getting the list of PIDs and writing |
| 879 |
the value to each of their files in proc |
| 880 |
|
| 881 |
\param score OOM Score to set |
| 882 |
*/ |
| 883 |
void Base::setOomAdjustment(const oom::Score score) |
| 884 |
{ |
| 885 |
forAllPids([this, &score](pid_t pid) { oomValueToPid(pid, score); }); |
| 886 |
} |
| 887 |
|
| 888 |
/** Figures out the path to the primary PID of the application and |
| 889 |
then reads its OOM adjustment file. */ |
| 890 |
oom::Score Base::getOomAdjustment() |
| 891 |
{ |
| 892 |
auto pid = primaryPid(); |
| 893 |
if (pid == 0) |
| 894 |
{ |
| 895 |
throw std::runtime_error("No PID for application: " + std::string(appId_)); |
| 896 |
} |
| 897 |
|
| 898 |
auto path = pidToOomPath(pid); |
| 899 |
GError* error = nullptr; |
| 900 |
gchar* content = nullptr; |
| 901 |
|
| 902 |
g_file_get_contents(path.c_str(), /* path */ |
| 903 |
&content, /* data */ |
| 904 |
nullptr, /* size */ |
| 905 |
&error); /* error */ |
| 906 |
|
| 907 |
if (error != nullptr) |
| 908 |
{ |
| 909 |
auto serror = unique_glib(error); |
| 910 |
error = nullptr; |
| 911 |
throw std::runtime_error("Unable to access OOM value for '" + std::string(appId_) + "' primary PID '" + |
| 912 |
std::to_string(pid) + "' because: " + serror->message); |
| 913 |
} |
| 914 |
|
| 915 |
auto score = static_cast<oom::Score>(std::atoi(content)); |
| 916 |
g_free(content); |
| 917 |
return score; |
| 918 |
} |
| 919 |
|
| 920 |
/** Sends a signal to a PID with a warning if we can't send it. |
| 921 |
We could throw an exception, but we can't handle it usefully anyway |
| 922 |
|
| 923 |
\param pid PID to send the signal to |
| 924 |
\param signal signal to send |
| 925 |
*/ |
| 926 |
void Base::signalToPid(pid_t pid, int signal) |
| 927 |
{ |
| 928 |
if (-1 == kill(pid, signal)) |
| 929 |
{ |
| 930 |
/* While that didn't work, we still want to try as many as we can */ |
| 931 |
g_warning("Unable to send signal %d to pid %d", signal, pid); |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
/** Get the path to the PID's OOM adjust path, with allowing for an |
| 936 |
override for testing using the environment variable |
| 937 |
LOMIRI_APP_LAUNCH_OOM_PROC_PATH |
| 938 |
|
| 939 |
\param pid PID to build path for |
| 940 |
*/ |
| 941 |
std::string Base::pidToOomPath(pid_t pid) |
| 942 |
{ |
| 943 |
static std::string procpath; |
| 944 |
if (G_UNLIKELY(procpath.empty())) |
| 945 |
{ |
| 946 |
/* Set by the test suite, probably not anyone else */ |
| 947 |
auto envvar = g_getenv("LOMIRI_APP_LAUNCH_OOM_PROC_PATH"); |
| 948 |
if (G_LIKELY(envvar == nullptr)) |
| 949 |
procpath = "/proc"; |
| 950 |
else |
| 951 |
procpath = envvar; |
| 952 |
} |
| 953 |
|
| 954 |
auto gpath = |
| 955 |
unique_gchar(g_build_filename(procpath.c_str(), std::to_string(pid).c_str(), "oom_score_adj", nullptr)); |
| 956 |
return std::string(gpath.get()); |
| 957 |
} |
| 958 |
|
| 959 |
/** Writes an OOM value to proc, assuming we have a string |
| 960 |
in the outer loop |
| 961 |
|
| 962 |
\param pid PID to change the OOM value of |
| 963 |
\param oomvalue OOM value to set |
| 964 |
*/ |
| 965 |
void Base::oomValueToPid(pid_t pid, const oom::Score oomvalue) |
| 966 |
{ |
| 967 |
auto oomstr = std::to_string(static_cast<std::int32_t>(oomvalue)); |
| 968 |
auto path = pidToOomPath(pid); |
| 969 |
ResourcePtr<FILE*, void (*)(FILE*)> adj(fopen(path.c_str(), "w"), [](FILE* fp) { |
| 970 |
if (fp != nullptr) |
| 971 |
{ |
| 972 |
fclose(fp); |
| 973 |
} |
| 974 |
}); |
| 975 |
int openerr = errno; |
| 976 |
|
| 977 |
if (adj.get() == nullptr) |
| 978 |
{ |
| 979 |
switch (openerr) |
| 980 |
{ |
| 981 |
case ENOENT: |
| 982 |
/* ENOENT happens a fair amount because of races, so it's not |
| 983 |
worth printing a warning about */ |
| 984 |
return; |
| 985 |
case EACCES: |
| 986 |
{ |
| 987 |
/* We can get this error when trying to set the OOM value on |
| 988 |
Oxide renderers because they're started by the sandbox and |
| 989 |
don't have their adjustment value available for us to write. |
| 990 |
We have a helper to deal with this, but it's kinda expensive |
| 991 |
so we only use it when we have to. */ |
| 992 |
oomValueToPidHelper(pid, oomvalue); |
| 993 |
return; |
| 994 |
} |
| 995 |
default: |
| 996 |
g_warning("Unable to set OOM value for '%d' to '%s': %s", int(pid), oomstr.c_str(), |
| 997 |
std::strerror(openerr)); |
| 998 |
return; |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
size_t writesize = fwrite(oomstr.c_str(), 1, oomstr.size(), adj.get()); |
| 1003 |
int writeerr = errno; |
| 1004 |
adj.dealloc(); |
| 1005 |
|
| 1006 |
if (writesize == oomstr.size()) |
| 1007 |
return; |
| 1008 |
|
| 1009 |
if (writeerr != 0) |
| 1010 |
g_warning("Unable to set OOM value for '%d' to '%s': %s", int(pid), oomstr.c_str(), strerror(writeerr)); |
| 1011 |
else |
| 1012 |
/* No error, but yet, wrong size. Not sure, what could cause this. */ |
| 1013 |
g_debug("Unable to set OOM value for '%d' to '%s': Wrote %d bytes", int(pid), oomstr.c_str(), int(writesize)); |
| 1014 |
} |
| 1015 |
|
| 1016 |
/** Use a setuid root helper for setting the oom value of |
| 1017 |
Chromium instances |
| 1018 |
|
| 1019 |
\param pid PID to change the OOM value of |
| 1020 |
\param oomvalue OOM value to set |
| 1021 |
*/ |
| 1022 |
void Base::oomValueToPidHelper(pid_t pid, const oom::Score oomvalue) |
| 1023 |
{ |
| 1024 |
GError* error = nullptr; |
| 1025 |
std::string oomstr = std::to_string(static_cast<std::int32_t>(oomvalue)); |
| 1026 |
std::string pidstr = std::to_string(pid); |
| 1027 |
std::array<const char*, 4> args = {OOM_HELPER, pidstr.c_str(), oomstr.c_str(), nullptr}; |
| 1028 |
|
| 1029 |
g_debug("Executing OOM Helper (pid: %d, score: %d): %s", int(pid), int(oomvalue), |
| 1030 |
std::accumulate(args.begin(), args.end(), std::string{}, [](const std::string& instr, |
| 1031 |
const char* output) -> std::string { |
| 1032 |
if (instr.empty()) |
| 1033 |
{ |
| 1034 |
return output; |
| 1035 |
} |
| 1036 |
else if (output != nullptr) |
| 1037 |
{ |
| 1038 |
return instr + " " + std::string(output); |
| 1039 |
} |
| 1040 |
else |
| 1041 |
{ |
| 1042 |
return instr; |
| 1043 |
} |
| 1044 |
}).c_str()); |
| 1045 |
|
| 1046 |
g_spawn_async(nullptr, /* working dir */ |
| 1047 |
(char**)(args.data()), /* args */ |
| 1048 |
nullptr, /* env */ |
| 1049 |
G_SPAWN_DEFAULT, /* flags */ |
| 1050 |
nullptr, /* child setup */ |
| 1051 |
nullptr, /* child setup userdata*/ |
| 1052 |
nullptr, /* pid */ |
| 1053 |
&error); /* error */ |
| 1054 |
|
| 1055 |
if (error != nullptr) |
| 1056 |
{ |
| 1057 |
g_warning("Unable to launch OOM helper '" OOM_HELPER "' on PID '%d': %s", pid, error->message); |
| 1058 |
g_error_free(error); |
| 1059 |
return; |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
/** Reformat a C++ vector of URLs into a C GStrv of strings |
| 1064 |
|
| 1065 |
\param urls Vector of URLs to make into C strings |
| 1066 |
*/ |
| 1067 |
GCharVUPtr Base::urlsToStrv(const std::vector<Application::URL>& urls) |
| 1068 |
{ |
| 1069 |
if (urls.empty()) |
| 1070 |
{ |
| 1071 |
return GCharVUPtr(nullptr, &g_strfreev); |
| 1072 |
} |
| 1073 |
|
| 1074 |
auto array = g_array_new(TRUE, FALSE, sizeof(gchar*)); |
| 1075 |
|
| 1076 |
for (auto url : urls) |
| 1077 |
{ |
| 1078 |
auto str = g_strdup(url.value().c_str()); |
| 1079 |
g_debug("Converting URL: %s", str); |
| 1080 |
g_array_append_val(array, str); |
| 1081 |
} |
| 1082 |
|
| 1083 |
return unique_gcharv((gchar**)g_array_free(array, FALSE)); |
| 1084 |
} |
| 1085 |
|
| 1086 |
} // namespace instance |
| 1087 |
|
| 1088 |
} // namespace jobs |
| 1089 |
} // namespace app_launch |
| 1090 |
} // namespace lomiri |