Give application to launch instead of reading conf

This commit is contained in:
Patrick Nicolas
2025-07-17 15:28:41 +02:00
parent 286268d2c6
commit 2f5496c06e
3 changed files with 53 additions and 64 deletions
+5 -4
View File
@@ -236,10 +236,10 @@ void stop_publishing();
void update_fsm(); void update_fsm();
void start_app() void start_app(const std::vector<std::string> & args)
{ {
#if WIVRN_USE_SYSTEMD #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 #else
app_pid = fork_application(); app_pid = fork_application();
#endif #endif
@@ -479,10 +479,11 @@ gboolean headset_connected_success(void *)
expose_known_keys_on_dbus(); expose_known_keys_on_dbus();
start_server(configuration()); configuration c;
start_server(c);
try try
{ {
start_app(); start_app(c.application);
} }
catch (std::exception & e) catch (std::exception & e)
{ {
+44 -55
View File
@@ -147,8 +147,9 @@ finish:
return ret; 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<std::string> & args)
{ {
assert(not args.empty());
raii_sd_bus_error error; raii_sd_bus_error error;
sd_bus_message * msg = nullptr; sd_bus_message * msg = nullptr;
const char * destination = "org.freedesktop.systemd1"; 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 v(msg, 'v', "a(sasb)");
container a1(msg, 'a', "(sasb)"); container a1(msg, 'a', "(sasb)");
container r1(msg, 'r', "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"); throw std::system_error(-ret, std::system_category(), "sd_bus_message_append failed");
{ {
container a(msg, 'a', "s"); 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) 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"); throw std::system_error(-ret, std::system_category(), "sd_bus_message_append failed");
@@ -254,11 +255,9 @@ finish:
return is_active; return is_active;
} }
pid_t wivrn::start_unit_file() pid_t wivrn::start_unit_file(const std::vector<std::string> & args)
{ {
configuration config; if (args.empty())
if (config.application.empty())
return 0; return 0;
std::string service_name = std::format("wivrn-application-{}.service", std::chrono::steady_clock::now().time_since_epoch().count()); 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) 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; std::cout << "started service: " << obj << std::endl;
// Wait until the service is active // Wait until the service is active
@@ -288,60 +287,29 @@ pid_t wivrn::start_unit_file()
#endif #endif
pid_t wivrn::fork_application() static int exec_application(std::vector<std::string> args)
{ {
configuration config; if (args.empty())
if (config.application.empty())
return 0; return 0;
pid_t application_pid = fork(); std::vector<std::string> tmp;
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<std::string> 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<char *> argv; std::vector<char *> argv;
argv.reserve(args.size()); argv.reserve(args.size() + 3);
for (auto & i: args) if (wivrn::flatpak_key(wivrn::flatpak::section::session_bus_policy, "org.freedesktop.Flatpak") == "talk")
argv.push_back(i.data()); {
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); argv.push_back(nullptr);
std::string executable = argv.front();
std::cerr << "Launching " << executable << std::endl; std::cerr << "Launching " << executable << std::endl;
std::cerr << "With args:" << std::endl; std::cerr << "With args:" << std::endl;
for (auto & i: argv) for (auto & i: argv)
@@ -355,3 +323,24 @@ int wivrn::exec_application(configuration config)
perror("Cannot start application"); perror("Cannot start application");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
pid_t wivrn::fork_application(const std::vector<std::string> & 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;
}
+4 -5
View File
@@ -2,17 +2,16 @@
#include "wivrn_config.h" #include "wivrn_config.h"
#include <string>
#include <sys/types.h> #include <sys/types.h>
#include <vector>
namespace wivrn namespace wivrn
{ {
struct configuration;
pid_t fork_application(); pid_t fork_application(const std::vector<std::string> & args);
#if WIVRN_USE_SYSTEMD #if WIVRN_USE_SYSTEMD
pid_t start_unit_file(); pid_t start_unit_file(const std::vector<std::string> & args);
#endif #endif
int exec_application(configuration);
} // namespace wivrn } // namespace wivrn