/* * WiVRn VR streaming * Copyright (C) 2025 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "start_application.h" #include "utils/flatpak.h" #include #include #include #include #include #include namespace wivrn { children_manager::~children_manager() {} forked_children::forked_children(std::function state_changed_cb) : state_changed_cb(state_changed_cb) {} void forked_children::start_application(const std::vector & args) { if (args.empty()) return; pid_t application_pid = fork(); if (application_pid < 0) throw std::system_error(errno, std::system_category(), "fork"); if (application_pid > 0) { guint app_watch = g_child_watch_add( application_pid, [](pid_t pid, int status, void * self_) { auto self = (forked_children *)self_; bool empty = self->children.empty(); auto node = self->children.extract(pid); if (not node) { std::cerr << "Failed to update child application information" << std::endl; } display_child_status(status, "Application"); const auto & watches = node.mapped(); g_source_remove(watches.child); if (watches.kill) g_source_remove(watches.kill); if (self->state_changed_cb and self->children.empty() and not empty) self->state_changed_cb(); }, this); children.emplace(application_pid, watches{.child = app_watch}); return; } // Start a new process group so that all processes started by the // application can be signaled setpgrp(); std::vector tmp; std::vector argv; argv.reserve(args.size() + 3); if (wivrn::flatpak_key(wivrn::flatpak::section::session_bus_policy, "org.freedesktop.Flatpak") == "talk") { tmp.push_back("flatpak-spawn"); tmp.push_back("--host"); } tmp.insert(tmp.end(), args.begin(), args.end()); for (auto & arg: tmp) argv.push_back(arg.data()); argv.push_back(nullptr); std::string executable = argv.front(); std::cerr << "Launching " << executable << std::endl; std::cerr << "With args:" << std::endl; for (auto & i: argv) { if (i) std::cerr << " " << std::quoted(i) << std::endl; } execvp(executable.c_str(), argv.data()); perror("Failed to start application"); exit(EXIT_FAILURE); } forked_children::~forked_children() { for (auto & [pid, watches]: children) { g_source_remove(watches.child); if (watches.kill) g_source_remove(watches.kill); } } bool forked_children::running() const { return not children.empty(); } void forked_children::stop() { for (auto & [pid, watches]: children) { kill(-pid, SIGTERM); intptr_t pid_ptr = pid; // Send SIGKILL after 1s if it is still running static_assert(sizeof(pid_t) < sizeof(intptr_t)); watches.kill = g_timeout_add( 1000, [](void * pid_) { auto pid = (intptr_t)pid_; assert(pid > 0); kill(-pid, SIGKILL); return G_SOURCE_REMOVE; }, (void *)pid_ptr); } } void display_child_status(int wstatus, const std::string & name) { std::cerr << name << " exited, exit status " << WEXITSTATUS(wstatus); if (WIFSIGNALED(wstatus)) { std::cerr << ", received signal " << sigabbrev_np(WTERMSIG(wstatus)) << " (" << strsignal(WTERMSIG(wstatus)) << ")" << (WCOREDUMP(wstatus) ? ", core dumped" : "") << std::endl; } else { std::cerr << std::endl; } } } // namespace wivrn