read DBUS env variables for --systemd mode
This commit is contained in:
+11
-2
@@ -246,7 +246,7 @@ void start_app()
|
|||||||
|
|
||||||
assert(app_watch == 0);
|
assert(app_watch == 0);
|
||||||
assert(app_kill_watch == 0);
|
assert(app_kill_watch == 0);
|
||||||
if (app_pid)
|
if (app_pid > 0)
|
||||||
{
|
{
|
||||||
app_watch = g_child_watch_add(app_pid, [](pid_t, int status, void *) {
|
app_watch = g_child_watch_add(app_pid, [](pid_t, int status, void *) {
|
||||||
display_child_status(status, "Application");
|
display_child_status(status, "Application");
|
||||||
@@ -262,6 +262,8 @@ void start_app()
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
app_watch = 0;
|
app_watch = 0;
|
||||||
|
if (app_pid < 0)
|
||||||
|
throw std::system_error(-app_pid, std::system_category());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -478,7 +480,14 @@ gboolean headset_connected_success(void *)
|
|||||||
expose_known_keys_on_dbus();
|
expose_known_keys_on_dbus();
|
||||||
|
|
||||||
start_server(configuration::read_user_configuration());
|
start_server(configuration::read_user_configuration());
|
||||||
start_app();
|
try
|
||||||
|
{
|
||||||
|
start_app();
|
||||||
|
}
|
||||||
|
catch (std::exception & e)
|
||||||
|
{
|
||||||
|
std::cerr << "Failed to start application: " << e.what();
|
||||||
|
}
|
||||||
|
|
||||||
delay_next_try = default_delay_next_try;
|
delay_next_try = default_delay_next_try;
|
||||||
|
|
||||||
|
|||||||
@@ -23,9 +23,56 @@
|
|||||||
#include <systemd/sd-bus.h>
|
#include <systemd/sd-bus.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
static int get_service_pid(const std::string & service_name, pid_t & pid)
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
struct deleter
|
||||||
|
{
|
||||||
|
void operator()(sd_bus * p)
|
||||||
|
{
|
||||||
|
sd_bus_unref(p);
|
||||||
|
}
|
||||||
|
void operator()(sd_bus_message * p)
|
||||||
|
{
|
||||||
|
sd_bus_message_unref(p);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using sd_bus_ptr = std::unique_ptr<sd_bus, deleter>;
|
||||||
|
|
||||||
|
sd_bus_ptr get_user_bus()
|
||||||
{
|
{
|
||||||
sd_bus * bus = nullptr;
|
sd_bus * bus = nullptr;
|
||||||
|
int ret;
|
||||||
|
// reimplement sd_bus_open_user to honor the env variable
|
||||||
|
if (const char * bus_address = getenv("DBUS_SESSION_BUS_ADDRESS"))
|
||||||
|
{
|
||||||
|
if (ret = sd_bus_new(&bus); ret < 0)
|
||||||
|
throw std::system_error(-ret, std::system_category(), "failed to create dbus object");
|
||||||
|
sd_bus_ptr res(bus);
|
||||||
|
|
||||||
|
if (ret = sd_bus_set_address(bus, bus_address); ret < 0)
|
||||||
|
throw std::system_error(-ret, std::system_category(), std::string("failed to connect to dbus at address ") + bus_address);
|
||||||
|
|
||||||
|
if (ret = sd_bus_set_bus_client(bus, 1); ret < 0)
|
||||||
|
throw std::system_error(-ret, std::system_category(), std::string("failed to configure dbus at address ") + bus_address);
|
||||||
|
if (ret = sd_bus_set_trusted(bus, 1); ret < 0)
|
||||||
|
throw std::system_error(-ret, std::system_category(), std::string("failed to trust dbus at address ") + bus_address);
|
||||||
|
|
||||||
|
if (ret = sd_bus_start(bus); ret < 0)
|
||||||
|
throw std::system_error(-ret, std::system_category(), std::string("failed to start dbus connection ") + bus_address);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
if (ret = sd_bus_open_user(&bus); ret < 0)
|
||||||
|
throw std::system_error(-ret, std::system_category(), "failed to connect to dbus");
|
||||||
|
|
||||||
|
return sd_bus_ptr(bus);
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
static int get_service_pid(sd_bus * bus, const std::string & service_name, pid_t & pid)
|
||||||
|
{
|
||||||
sd_bus_error error = SD_BUS_ERROR_NULL;
|
sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||||
sd_bus_message * msg = nullptr;
|
sd_bus_message * msg = nullptr;
|
||||||
const char * destination = "org.freedesktop.systemd1";
|
const char * destination = "org.freedesktop.systemd1";
|
||||||
@@ -35,14 +82,6 @@ static int get_service_pid(const std::string & service_name, pid_t & pid)
|
|||||||
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
// Connect to the session bus
|
|
||||||
ret = sd_bus_open_user(&bus);
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
std::cerr << "Failed to connect to session bus: " << strerror(-ret) << std::endl;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call the method
|
// Call the method
|
||||||
ret = sd_bus_call_method(bus, destination, path, interface, member, &error, &msg, "s", service_name.c_str());
|
ret = sd_bus_call_method(bus, destination, path, interface, member, &error, &msg, "s", service_name.c_str());
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
@@ -71,13 +110,11 @@ static int get_service_pid(const std::string & service_name, pid_t & pid)
|
|||||||
finish:
|
finish:
|
||||||
sd_bus_error_free(&error);
|
sd_bus_error_free(&error);
|
||||||
sd_bus_message_unref(msg);
|
sd_bus_message_unref(msg);
|
||||||
sd_bus_unref(bus);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int start_service(const std::string & service_name)
|
int start_service(sd_bus * bus, const std::string & service_name)
|
||||||
{
|
{
|
||||||
sd_bus * bus = nullptr;
|
|
||||||
sd_bus_error error = SD_BUS_ERROR_NULL;
|
sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||||
sd_bus_message * msg = nullptr;
|
sd_bus_message * msg = nullptr;
|
||||||
const char * destination = "org.freedesktop.systemd1";
|
const char * destination = "org.freedesktop.systemd1";
|
||||||
@@ -86,18 +123,8 @@ int start_service(const std::string & service_name)
|
|||||||
const char * member = "StartUnit";
|
const char * member = "StartUnit";
|
||||||
const char * mode = "replace";
|
const char * mode = "replace";
|
||||||
|
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
// Connect to the session bus
|
|
||||||
ret = sd_bus_open_user(&bus);
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
std::cerr << "Failed to connect to session bus: " << strerror(-ret) << std::endl;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call the method to start the service
|
// Call the method to start the service
|
||||||
ret = sd_bus_call_method(bus, destination, path, interface, member, &error, &msg, "ss", service_name.c_str(), mode);
|
int ret = sd_bus_call_method(bus, destination, path, interface, member, &error, &msg, "ss", service_name.c_str(), mode);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
std::cerr << "Failed to start service: " << error.message << std::endl;
|
std::cerr << "Failed to start service: " << error.message << std::endl;
|
||||||
@@ -107,13 +134,11 @@ int start_service(const std::string & service_name)
|
|||||||
finish:
|
finish:
|
||||||
sd_bus_error_free(&error);
|
sd_bus_error_free(&error);
|
||||||
sd_bus_message_unref(msg);
|
sd_bus_message_unref(msg);
|
||||||
sd_bus_unref(bus);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_service_active(const std::string & service_name)
|
bool is_service_active(sd_bus * bus, const std::string & service_name)
|
||||||
{
|
{
|
||||||
sd_bus * bus = nullptr;
|
|
||||||
sd_bus_error error = SD_BUS_ERROR_NULL;
|
sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||||
sd_bus_message * msg = nullptr;
|
sd_bus_message * msg = nullptr;
|
||||||
const char * destination = "org.freedesktop.systemd1";
|
const char * destination = "org.freedesktop.systemd1";
|
||||||
@@ -122,19 +147,9 @@ bool is_service_active(const std::string & service_name)
|
|||||||
const char * member = "GetUnit";
|
const char * member = "GetUnit";
|
||||||
|
|
||||||
bool is_active = false;
|
bool is_active = false;
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
// Connect to the session bus
|
|
||||||
ret = sd_bus_open_user(&bus);
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
std::cerr << "Failed to connect to session bus: " << strerror(-ret) << std::endl;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char * object_path;
|
const char * object_path;
|
||||||
// Call the method
|
// Call the method
|
||||||
ret = sd_bus_call_method(bus, destination, path, interface, member, &error, &msg, "s", service_name.c_str());
|
int ret = sd_bus_call_method(bus, destination, path, interface, member, &error, &msg, "s", service_name.c_str());
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
std::cerr << "Failed to issue method call: " << error.message << std::endl;
|
std::cerr << "Failed to issue method call: " << error.message << std::endl;
|
||||||
@@ -163,7 +178,6 @@ bool is_service_active(const std::string & service_name)
|
|||||||
finish:
|
finish:
|
||||||
sd_bus_error_free(&error);
|
sd_bus_error_free(&error);
|
||||||
sd_bus_message_unref(msg);
|
sd_bus_message_unref(msg);
|
||||||
sd_bus_unref(bus);
|
|
||||||
return is_active;
|
return is_active;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,11 +185,12 @@ pid_t wivrn::start_unit_file()
|
|||||||
{
|
{
|
||||||
std::string service_name = "wivrn-application.service";
|
std::string service_name = "wivrn-application.service";
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
int ret = get_service_pid(service_name, pid);
|
auto bus = get_user_bus();
|
||||||
|
int ret = get_service_pid(bus.get(), service_name, pid);
|
||||||
|
|
||||||
if (ret < 0 || pid == 0)
|
if (ret < 0 || pid == 0)
|
||||||
{
|
{
|
||||||
ret = start_service(service_name);
|
ret = start_service(bus.get(), service_name);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
std::cerr << "Failed to start service " << service_name << std::endl;
|
std::cerr << "Failed to start service " << service_name << std::endl;
|
||||||
@@ -183,13 +198,13 @@ pid_t wivrn::start_unit_file()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Wait until the service is active
|
// Wait until the service is active
|
||||||
while (!is_service_active(service_name))
|
while (!is_service_active(bus.get(), service_name))
|
||||||
{
|
{
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to get the PID again
|
// Try to get the PID again
|
||||||
ret = get_service_pid(service_name, pid);
|
ret = get_service_pid(bus.get(), service_name, pid);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
std::cerr << "Failed to get PID for service " << service_name << std::endl;
|
std::cerr << "Failed to get PID for service " << service_name << std::endl;
|
||||||
|
|||||||
Reference in New Issue
Block a user