| 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 "jobs-posix.h" |
| 21 |
#include "application-impl-base.h" |
| 22 |
#include "registry-impl.h" |
| 23 |
#include "second-exec-core.h" |
| 24 |
#include "string-util.h" |
| 25 |
#include "utils.h" |
| 26 |
#include "lomiri-app-launch-trace.h" |
| 27 |
|
| 28 |
#include <gio/gio.h> |
| 29 |
#include <sys/types.h> |
| 30 |
#include <unistd.h> |
| 31 |
|
| 32 |
#include <algorithm> |
| 33 |
#include <numeric> |
| 34 |
#include <regex> |
| 35 |
#include <lomiri/util/GlibMemory.h> |
| 36 |
|
| 37 |
using namespace lomiri::util; |
| 38 |
|
| 39 |
namespace lomiri |
| 40 |
{ |
| 41 |
namespace app_launch |
| 42 |
{ |
| 43 |
namespace jobs |
| 44 |
{ |
| 45 |
namespace instance |
| 46 |
{ |
| 47 |
|
| 48 |
class POSIX : public instance::Base |
| 49 |
{ |
| 50 |
friend class manager::POSIX; |
| 51 |
|
| 52 |
public: |
| 53 |
explicit POSIX(const AppID& appId, |
| 54 |
const std::string& job, |
| 55 |
const std::string& instance, |
| 56 |
const std::vector<Application::URL>& urls, |
| 57 |
const std::shared_ptr<Registry::Impl>& registry); |
| 58 |
virtual ~POSIX() |
| 59 |
{ |
| 60 |
g_debug("Destroying a POSIX for '%s' instance '%s'", std::string(appId_).c_str(), instance_.c_str()); |
| 61 |
} |
| 62 |
|
| 63 |
/* Query lifecycle */ |
| 64 |
pid_t primaryPid() override; |
| 65 |
std::vector<pid_t> pids() override; |
| 66 |
|
| 67 |
/* Manage lifecycle */ |
| 68 |
void stop() override; |
| 69 |
|
| 70 |
}; // class POSIX |
| 71 |
|
| 72 |
POSIX::POSIX(const AppID& appId, |
| 73 |
const std::string& job, |
| 74 |
const std::string& instance, |
| 75 |
const std::vector<Application::URL>& urls, |
| 76 |
const std::shared_ptr<Registry::Impl>& registry) |
| 77 |
: Base(appId, job, instance, urls, registry) |
| 78 |
{ |
| 79 |
g_debug("Creating a new POSIX for '%s' instance '%s'", std::string(appId).c_str(), instance.c_str()); |
| 80 |
} |
| 81 |
|
| 82 |
pid_t POSIX::primaryPid() |
| 83 |
{ |
| 84 |
auto manager = std::dynamic_pointer_cast<manager::POSIX>(registry_->jobs()); |
| 85 |
return manager->unitPrimaryPid(appId_, job_, instance_); |
| 86 |
} |
| 87 |
|
| 88 |
std::vector<pid_t> POSIX::pids() |
| 89 |
{ |
| 90 |
auto manager = std::dynamic_pointer_cast<manager::POSIX>(registry_->jobs()); |
| 91 |
return manager->unitPids(appId_, job_, instance_); |
| 92 |
} |
| 93 |
|
| 94 |
void POSIX::stop() |
| 95 |
{ |
| 96 |
auto manager = std::dynamic_pointer_cast<manager::POSIX>(registry_->jobs()); |
| 97 |
manager->stopUnit(appId_, job_, instance_); |
| 98 |
} |
| 99 |
|
| 100 |
} // namespace instance |
| 101 |
|
| 102 |
namespace manager |
| 103 |
{ |
| 104 |
|
| 105 |
// static const char* SYSTEMD_DBUS_ADDRESS{"org.freedesktop.systemd1"}; |
| 106 |
// static const char* SYSTEMD_DBUS_IFACE_MANAGER{"org.freedesktop.systemd1.Manager"}; |
| 107 |
// static const char* SYSTEMD_DBUS_PATH_MANAGER{"/org/freedesktop/systemd1"}; |
| 108 |
// static const char * SYSTEMD_DBUS_IFACE_UNIT{"org.freedesktop.systemd1.Unit"}; |
| 109 |
// static const char* SYSTEMD_DBUS_IFACE_SERVICE{"org.freedesktop.systemd1.Service"}; |
| 110 |
|
| 111 |
POSIX::POSIX(const std::shared_ptr<Registry::Impl>& registry) |
| 112 |
: Base(registry) |
| 113 |
{ |
| 114 |
// TODO STUB |
| 115 |
g_debug("POSIX constructor stub called..."); |
| 116 |
} |
| 117 |
|
| 118 |
POSIX::~POSIX() |
| 119 |
{ |
| 120 |
} |
| 121 |
|
| 122 |
void POSIX::copyEnv(const std::string& envname, std::list<std::pair<std::string, std::string>>& env) |
| 123 |
{ |
| 124 |
if (!findEnv(envname, env).empty()) |
| 125 |
{ |
| 126 |
g_debug("Already a value set for '%s' ignoring", envname.c_str()); |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
auto cvalue = getenv(envname.c_str()); |
| 131 |
g_debug("Copying Environment: %s", envname.c_str()); |
| 132 |
if (cvalue != nullptr) |
| 133 |
{ |
| 134 |
std::string value{cvalue}; |
| 135 |
env.emplace_back(std::make_pair(envname, value)); |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
g_debug("Unable to copy environment '%s'", envname.c_str()); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
void POSIX::copyEnvByPrefix(const std::string& prefix, std::list<std::pair<std::string, std::string>>& env) |
| 144 |
{ |
| 145 |
for (unsigned int i = 0; environ[i] != nullptr; i++) |
| 146 |
{ |
| 147 |
if (g_str_has_prefix(environ[i], prefix.c_str())) |
| 148 |
{ |
| 149 |
std::string envname = environ[i]; |
| 150 |
envname.erase(envname.find('=')); |
| 151 |
copyEnv(envname, env); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
std::shared_ptr<Application::Instance> POSIX::launch( |
| 157 |
const AppID& appId, |
| 158 |
const std::string& job, |
| 159 |
const std::string& instance, |
| 160 |
const std::vector<Application::URL>& urls, |
| 161 |
launchMode mode, |
| 162 |
std::function<std::list<std::pair<std::string, std::string>>(void)>& getenv) |
| 163 |
{ |
| 164 |
if (appId.empty()) |
| 165 |
return {}; |
| 166 |
|
| 167 |
auto appJobs = getAllApplicationJobs(); |
| 168 |
bool isApplication = std::find(appJobs.begin(), appJobs.end(), job) != appJobs.end(); |
| 169 |
|
| 170 |
auto reg = getReg(); |
| 171 |
return reg->thread.executeOnThread<std::shared_ptr<instance::POSIX>>([&]() -> std::shared_ptr<instance::POSIX> { |
| 172 |
auto manager = std::dynamic_pointer_cast<manager::POSIX>(reg->jobs()); |
| 173 |
std::string appIdStr{appId}; |
| 174 |
g_debug("Initializing params for an new instance::POSIX for: %s", appIdStr.c_str()); |
| 175 |
|
| 176 |
tracepoint(lomiri_app_launch, liblal_start, appIdStr.c_str()); |
| 177 |
|
| 178 |
int timeout = 1; |
| 179 |
if (lomiri::app_launch::Registry::Impl::isWatchingAppStarting()) |
| 180 |
{ |
| 181 |
timeout = 0; |
| 182 |
} |
| 183 |
|
| 184 |
handshake_t* handshake{nullptr}; |
| 185 |
|
| 186 |
if (isApplication) |
| 187 |
{ |
| 188 |
handshake = starting_handshake_start(appIdStr.c_str(), instance.c_str(), timeout); |
| 189 |
if (handshake == nullptr) |
| 190 |
{ |
| 191 |
g_warning("Unable to setup starting handshake"); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
/* Build up our environment */ |
| 196 |
auto env = getenv(); |
| 197 |
|
| 198 |
env.emplace_back(std::make_pair("APP_ID", appIdStr)); /* Application ID */ |
| 199 |
env.emplace_back(std::make_pair("APP_LAUNCHER_PID", std::to_string(getpid()))); /* Who we are, for bugs */ |
| 200 |
|
| 201 |
copyEnv("DISPLAY", env); |
| 202 |
|
| 203 |
for (const auto& prefix : {"DBUS_", "MIR_", "LOMIRI_APP_LAUNCH_"}) |
| 204 |
{ |
| 205 |
copyEnvByPrefix(prefix, env); |
| 206 |
} |
| 207 |
|
| 208 |
/* If we're in deb mode and launching legacy apps, they're gonna need |
| 209 |
* more context, they really have no other way to get it. */ |
| 210 |
if (g_getenv("SNAP") == nullptr && appId.package.value().empty()) |
| 211 |
{ |
| 212 |
copyEnvByPrefix("QT_", env); |
| 213 |
copyEnvByPrefix("XDG_", env); |
| 214 |
|
| 215 |
/* If we're in Lomiri we don't want to pass it's platform, we want |
| 216 |
* an application platform. */ |
| 217 |
removeEnv("QT_QPA_PLATFORM", env); |
| 218 |
} |
| 219 |
|
| 220 |
/* Mir socket if we don't have one in our env */ |
| 221 |
if (findEnv("MIR_SOCKET", env).empty()) |
| 222 |
{ |
| 223 |
env.emplace_back(std::make_pair("MIR_SOCKET", g_get_user_runtime_dir() + std::string{"/mir_socket"})); |
| 224 |
} |
| 225 |
|
| 226 |
if (!urls.empty()) |
| 227 |
{ |
| 228 |
auto accumfunc = [](const std::string& prev, Application::URL thisurl) -> std::string { |
| 229 |
gchar* gescaped = g_shell_quote(thisurl.value().c_str()); |
| 230 |
std::string escaped; |
| 231 |
if (gescaped != nullptr) |
| 232 |
{ |
| 233 |
escaped = gescaped; |
| 234 |
g_free(gescaped); |
| 235 |
} |
| 236 |
else |
| 237 |
{ |
| 238 |
g_warning("Unable to escape URL: %s", thisurl.value().c_str()); |
| 239 |
return prev; |
| 240 |
} |
| 241 |
|
| 242 |
if (prev.empty()) |
| 243 |
{ |
| 244 |
return escaped; |
| 245 |
} |
| 246 |
else |
| 247 |
{ |
| 248 |
return prev + " " + escaped; |
| 249 |
} |
| 250 |
}; |
| 251 |
auto urlstring = std::accumulate(urls.begin(), urls.end(), std::string{}, accumfunc); |
| 252 |
env.emplace_back(std::make_pair("APP_URIS", urlstring)); |
| 253 |
} |
| 254 |
|
| 255 |
if (mode == launchMode::TEST) |
| 256 |
{ |
| 257 |
env.emplace_back(std::make_pair("QT_LOAD_TESTABILITY", "1")); |
| 258 |
} |
| 259 |
|
| 260 |
/* ExecStart */ |
| 261 |
auto commands = parseExec(env); |
| 262 |
if (commands.empty()) |
| 263 |
{ |
| 264 |
// TODO: Handle??? |
| 265 |
g_debug("Commands stuff is empty!!"); |
| 266 |
return nullptr; |
| 267 |
} |
| 268 |
|
| 269 |
/* Working Directory */ |
| 270 |
if (!findEnv("APP_DIR", env).empty()) |
| 271 |
{ |
| 272 |
// g_variant_builder_open(&builder, G_VARIANT_TYPE_TUPLE); |
| 273 |
// g_variant_builder_add_value(&builder, g_variant_new_string("WorkingDirectory")); |
| 274 |
// g_variant_builder_open(&builder, G_VARIANT_TYPE_VARIANT); |
| 275 |
// g_variant_builder_add_value(&builder, g_variant_new_string(findEnv("APP_DIR", env).c_str())); |
| 276 |
} |
| 277 |
|
| 278 |
/* Clean up env before shipping it */ |
| 279 |
for (const auto& rmenv : |
| 280 |
{"APP_DIR", "APP_URIS", "APP_EXEC", "APP_EXEC_POLICY", "APP_LAUNCHER_PID", |
| 281 |
"INSTANCE_ID", "MIR_SERVER_PLATFORM_PATH", "MIR_SERVER_PROMPT_FILE", "MIR_SERVER_HOST_SOCKET", |
| 282 |
"LOMIRI_APP_LAUNCH_OOM_HELPER", "LOMIRI_APP_LAUNCH_LEGACY_ROOT"}) |
| 283 |
{ |
| 284 |
removeEnv(rmenv, env); |
| 285 |
} |
| 286 |
|
| 287 |
g_debug("Environment length: %d", envSize(env)); |
| 288 |
|
| 289 |
/* Environment */ |
| 290 |
for (const auto& envvar : env) |
| 291 |
{ |
| 292 |
if (!envvar.first.empty() && !envvar.second.empty()) |
| 293 |
{ |
| 294 |
// g_variant_builder_add_value(&builder, g_variant_new_take_string(g_strdup_printf( |
| 295 |
// "%s=%s", envvar.first.c_str(), envvar.second.c_str()))); |
| 296 |
// g_debug("Setting environment: %s=%s", envvar.first.c_str(), envvar.second.c_str()); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
auto retval = std::make_shared<instance::POSIX>(appId, job, instance, urls, reg); |
| 301 |
|
| 302 |
tracepoint(lomiri_app_launch, handshake_wait, appIdStr.c_str()); |
| 303 |
starting_handshake_wait(handshake); |
| 304 |
tracepoint(lomiri_app_launch, handshake_complete, appIdStr.c_str()); |
| 305 |
|
| 306 |
// From https://stackoverflow.com/a/7026414 |
| 307 |
g_debug("PREPARING ARGV..."); |
| 308 |
std::vector<char*> argv; |
| 309 |
for(auto loop = commands.begin(); loop != commands.end(); ++loop) |
| 310 |
{ |
| 311 |
argv.push_back(&(*loop)[0]); |
| 312 |
g_debug("%s", argv.back()); |
| 313 |
} |
| 314 |
argv.push_back(NULL); |
| 315 |
|
| 316 |
g_debug("PREPARING ENVP..."); |
| 317 |
std::vector<std::string> envp_str; |
| 318 |
std::vector<char*> envp; |
| 319 |
for(auto loop = env.begin(); loop != env.end(); ++loop) |
| 320 |
{ |
| 321 |
std::string envStr; |
| 322 |
envStr += (*loop).first; |
| 323 |
envStr += "="; |
| 324 |
envStr += (*loop).second; |
| 325 |
envp_str.push_back(envStr); |
| 326 |
envp.push_back(&envp_str.back()[0]); |
| 327 |
} |
| 328 |
envp.push_back(NULL); |
| 329 |
|
| 330 |
std::cout << "printing members of envp:" << std::endl; |
| 331 |
for(char* n : envp) { |
| 332 |
std::cout << n << std::endl; |
| 333 |
} |
| 334 |
|
| 335 |
pid_t child_pid = fork(); |
| 336 |
if(child_pid == 0) { |
| 337 |
/* Call the job start function */ |
| 338 |
g_debug("argv[0]: %s", argv[0]); |
| 339 |
execvpe(argv[0], &argv[0], &envp[0]); |
| 340 |
|
| 341 |
g_debug("execvpe failed from child."); |
| 342 |
g_debug("errno: %d", errno); |
| 343 |
g_debug("errmsg: %s", strerror(errno)); |
| 344 |
} |
| 345 |
g_debug("child_pid: %d", child_pid); |
| 346 |
launchedpids.emplace_back(child_pid); |
| 347 |
|
| 348 |
tracepoint(lomiri_app_launch, liblal_start_message_sent, appIdStr.c_str()); |
| 349 |
|
| 350 |
return retval; |
| 351 |
}); |
| 352 |
} |
| 353 |
|
| 354 |
std::shared_ptr<Application::Instance> POSIX::existing(const AppID& appId, |
| 355 |
const std::string& job, |
| 356 |
const std::string& instance, |
| 357 |
const std::vector<Application::URL>& urls) |
| 358 |
{ |
| 359 |
return std::make_shared<instance::POSIX>(appId, job, instance, urls, getReg()); |
| 360 |
} |
| 361 |
|
| 362 |
std::vector<std::shared_ptr<instance::Base>> POSIX::instances(const AppID& appID, const std::string& job) |
| 363 |
{ |
| 364 |
// TODO: STUB |
| 365 |
g_debug("instances stub called..."); |
| 366 |
auto reg = getReg(); |
| 367 |
|
| 368 |
std::vector<std::shared_ptr<instance::Base>> instances; |
| 369 |
std::vector<Application::URL> urls; |
| 370 |
std::string instance = "somestring"; |
| 371 |
|
| 372 |
instances.emplace_back(std::make_shared<instance::POSIX>(appID, job, instance, urls, reg)); |
| 373 |
g_debug("Found %d instances for AppID '%s'", int(instances.size()), std::string(appID).c_str()); |
| 374 |
|
| 375 |
return instances; |
| 376 |
} |
| 377 |
|
| 378 |
std::list<std::string> POSIX::runningAppIds(const std::list<std::string>& allJobs) |
| 379 |
{ |
| 380 |
// TODO: STUB |
| 381 |
g_debug("runningAppIds stub called..."); |
| 382 |
std::set<std::string> appids; |
| 383 |
return {appids.begin(), appids.end()}; |
| 384 |
} |
| 385 |
|
| 386 |
pid_t POSIX::unitPrimaryPid(const AppID& appId, const std::string& job, const std::string& instance) |
| 387 |
{ |
| 388 |
// TODO: STUB |
| 389 |
g_debug("unitPrimaryPid stub called..."); |
| 390 |
return 0; |
| 391 |
} |
| 392 |
|
| 393 |
std::vector<pid_t> POSIX::unitPids(const AppID& appId, const std::string& job, const std::string& instance) |
| 394 |
{ |
| 395 |
// TODO: STUB |
| 396 |
g_debug("unitPids stub called..."); |
| 397 |
return launchedpids; |
| 398 |
} |
| 399 |
|
| 400 |
void POSIX::stopUnit(const AppID& appId, const std::string& job, const std::string& instance) |
| 401 |
{ |
| 402 |
// TODO: STUB |
| 403 |
g_debug("stopUnit stub called..."); |
| 404 |
} |
| 405 |
|
| 406 |
core::Signal<const std::string&, const std::string&, const std::string&>& POSIX::jobStarted() |
| 407 |
{ |
| 408 |
/* Ensure we're connecting to the signals */ |
| 409 |
return sig_jobStarted; |
| 410 |
} |
| 411 |
|
| 412 |
core::Signal<const std::string&, const std::string&, const std::string&>& POSIX::jobStopped() |
| 413 |
{ |
| 414 |
/* Ensure we're connecting to the signals */ |
| 415 |
return sig_jobStopped; |
| 416 |
} |
| 417 |
|
| 418 |
core::Signal<const std::string&, const std::string&, const std::string&, Registry::FailureType>& POSIX::jobFailed() |
| 419 |
{ |
| 420 |
// TODO: STUB |
| 421 |
g_debug("jobFailed stub called..."); |
| 422 |
return sig_jobFailed; |
| 423 |
} |
| 424 |
|
| 425 |
std::string POSIX::findEnv(const std::string& value, std::list<std::pair<std::string, std::string>>& env) |
| 426 |
{ |
| 427 |
std::string retval; |
| 428 |
auto entry = std::find_if(env.begin(), env.end(), |
| 429 |
[&value](std::pair<std::string, std::string>& entry) { return entry.first == value; }); |
| 430 |
|
| 431 |
if (entry != env.end()) |
| 432 |
{ |
| 433 |
retval = entry->second; |
| 434 |
} |
| 435 |
|
| 436 |
return retval; |
| 437 |
} |
| 438 |
|
| 439 |
void POSIX::removeEnv(const std::string& value, std::list<std::pair<std::string, std::string>>& env) |
| 440 |
{ |
| 441 |
auto entry = std::find_if(env.begin(), env.end(), |
| 442 |
[&value](std::pair<std::string, std::string>& entry) { return entry.first == value; }); |
| 443 |
|
| 444 |
if (entry != env.end()) |
| 445 |
{ |
| 446 |
env.erase(entry); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
int POSIX::envSize(std::list<std::pair<std::string, std::string>>& env) |
| 451 |
{ |
| 452 |
int len = std::string{"Environment="}.length(); |
| 453 |
|
| 454 |
for (const auto& entry : env) |
| 455 |
{ |
| 456 |
len += 3; /* two quotes, one space */ |
| 457 |
len += entry.first.length(); |
| 458 |
len += entry.second.length(); |
| 459 |
} |
| 460 |
|
| 461 |
len -= 1; /* We account for a space each time but the first doesn't have */ |
| 462 |
|
| 463 |
return len; |
| 464 |
} |
| 465 |
|
| 466 |
std::vector<std::string> POSIX::parseExec(std::list<std::pair<std::string, std::string>>& env) |
| 467 |
{ |
| 468 |
auto exec = findEnv("APP_EXEC", env); |
| 469 |
if (exec.empty()) |
| 470 |
{ |
| 471 |
g_warning("Application exec line is empty?!?!?"); |
| 472 |
return {}; |
| 473 |
} |
| 474 |
auto uris = findEnv("APP_URIS", env); |
| 475 |
|
| 476 |
g_debug("Exec line: %s", exec.c_str()); |
| 477 |
g_debug("App URLS: %s", uris.c_str()); |
| 478 |
|
| 479 |
auto execarray = desktop_exec_parse(exec.c_str(), uris.c_str()); |
| 480 |
|
| 481 |
std::vector<std::string> retval; |
| 482 |
for (unsigned int i = 0; i < execarray->len; i++) |
| 483 |
{ |
| 484 |
auto cstr = g_array_index(execarray, gchar*, i); |
| 485 |
if (cstr != nullptr) |
| 486 |
{ |
| 487 |
retval.emplace_back(cstr); |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
/* This seems to work better than array_free(), I can't figure out why */ |
| 492 |
auto strv = (gchar**)g_array_free(execarray, FALSE); |
| 493 |
g_strfreev(strv); |
| 494 |
|
| 495 |
if (retval.empty()) |
| 496 |
{ |
| 497 |
g_warning("After parsing 'APP_EXEC=%s' we ended up with no tokens", exec.c_str()); |
| 498 |
} |
| 499 |
|
| 500 |
/* See if we're doing apparmor by hand */ |
| 501 |
auto appexecpolicy = findEnv("APP_EXEC_POLICY", env); |
| 502 |
if (!appexecpolicy.empty() && appexecpolicy != "unconfined") |
| 503 |
{ |
| 504 |
retval.emplace(retval.begin(), "--"); |
| 505 |
retval.emplace(retval.begin(), appexecpolicy); |
| 506 |
retval.emplace(retval.begin(), "-p"); |
| 507 |
retval.emplace(retval.begin(), AA_EXEC_PATH); |
| 508 |
} |
| 509 |
|
| 510 |
return retval; |
| 511 |
} |
| 512 |
|
| 513 |
} // namespace manager |
| 514 |
} // namespace jobs |
| 515 |
} // namespace app_launch |
| 516 |
} // namespace lomiri |