diff --git a/server/main.cpp b/server/main.cpp index d317eee0..4e04048a 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -236,10 +236,10 @@ void stop_publishing(); void update_fsm(); -void start_app() +void start_app(const std::vector & args) { #if WIVRN_USE_SYSTEMD - app_pid = use_systemd ? start_unit_file() : fork_application(); + app_pid = use_systemd ? start_unit_file(args) : fork_application(args); #else app_pid = fork_application(); #endif @@ -479,10 +479,11 @@ gboolean headset_connected_success(void *) expose_known_keys_on_dbus(); - start_server(configuration()); + configuration c; + start_server(c); try { - start_app(); + start_app(c.application); } catch (std::exception & e) { diff --git a/server/start_application.cpp b/server/start_application.cpp index cda905fc..1932a042 100644 --- a/server/start_application.cpp +++ b/server/start_application.cpp @@ -147,8 +147,9 @@ finish: return ret; } -std::string start_service(sd_bus * bus, const std::string & service_name, const wivrn::configuration & config) +std::string start_service(sd_bus * bus, const std::string & service_name, const std::vector & args) { + assert(not args.empty()); raii_sd_bus_error error; sd_bus_message * msg = nullptr; const char * destination = "org.freedesktop.systemd1"; @@ -178,12 +179,12 @@ std::string start_service(sd_bus * bus, const std::string & service_name, const container v(msg, 'v', "a(sasb)"); container a1(msg, 'a', "(sasb)"); container r1(msg, 'r', "sasb"); - if (ret = sd_bus_message_append(msg, "s", config.application[0].c_str()); ret < 0) + if (ret = sd_bus_message_append(msg, "s", args[0].c_str()); ret < 0) throw std::system_error(-ret, std::system_category(), "sd_bus_message_append failed"); { container a(msg, 'a', "s"); - for (const auto & arg: config.application) + for (const auto & arg: args) { if (ret = sd_bus_message_append(msg, "s", arg.c_str()); ret < 0) throw std::system_error(-ret, std::system_category(), "sd_bus_message_append failed"); @@ -254,11 +255,9 @@ finish: return is_active; } -pid_t wivrn::start_unit_file() +pid_t wivrn::start_unit_file(const std::vector & args) { - configuration config; - - if (config.application.empty()) + if (args.empty()) return 0; std::string service_name = std::format("wivrn-application-{}.service", std::chrono::steady_clock::now().time_since_epoch().count()); @@ -268,7 +267,7 @@ pid_t wivrn::start_unit_file() if (ret < 0 || pid == 0) { - auto obj = start_service(bus.get(), service_name, config); + auto obj = start_service(bus.get(), service_name, args); std::cout << "started service: " << obj << std::endl; // Wait until the service is active @@ -288,60 +287,29 @@ pid_t wivrn::start_unit_file() #endif -pid_t wivrn::fork_application() +static int exec_application(std::vector args) { - configuration config; - - if (config.application.empty()) + if (args.empty()) return 0; - pid_t application_pid = fork(); - if (application_pid < 0) - { - throw std::system_error(errno, std::system_category(), "fork"); - } - - if (application_pid == 0) - { - // Start a new process group so that all processes started by the - // application can be signaled - setpgrp(); - - return exec_application(config); - } - - return application_pid; -} - -int wivrn::exec_application(configuration config) -{ - if (config.application.empty()) - return 0; - - std::string executable; - std::vector args; - - if (flatpak_key(flatpak::section::session_bus_policy, "org.freedesktop.Flatpak") == "talk") - { - executable = "flatpak-spawn"; - args.push_back("flatpak-spawn"); - args.push_back("--host"); - } - else - { - executable = config.application.front(); - } - - for (auto & arg: config.application) - args.push_back(arg.data()); - + std::vector tmp; std::vector argv; - argv.reserve(args.size()); + argv.reserve(args.size() + 3); - for (auto & i: args) - argv.push_back(i.data()); + if (wivrn::flatpak_key(wivrn::flatpak::section::session_bus_policy, "org.freedesktop.Flatpak") == "talk") + { + tmp.push_back("flatpak-spawn"); + tmp.push_back("--host"); + for (auto & arg: tmp) + argv.push_back(arg.data()); + } + + for (auto & arg: args) + 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) @@ -355,3 +323,24 @@ int wivrn::exec_application(configuration config) perror("Cannot start application"); exit(EXIT_FAILURE); } + +pid_t wivrn::fork_application(const std::vector & args) +{ + if (args.empty()) + return 0; + + pid_t application_pid = fork(); + if (application_pid < 0) + throw std::system_error(errno, std::system_category(), "fork"); + + if (application_pid == 0) + { + // Start a new process group so that all processes started by the + // application can be signaled + setpgrp(); + + return exec_application(args); + } + + return application_pid; +} diff --git a/server/start_application.h b/server/start_application.h index fc09db30..378d42aa 100644 --- a/server/start_application.h +++ b/server/start_application.h @@ -2,17 +2,16 @@ #include "wivrn_config.h" +#include #include +#include namespace wivrn { -struct configuration; -pid_t fork_application(); +pid_t fork_application(const std::vector & args); #if WIVRN_USE_SYSTEMD -pid_t start_unit_file(); +pid_t start_unit_file(const std::vector & args); #endif - -int exec_application(configuration); } // namespace wivrn