Refactor the main loop to use glib
This commit is contained in:
@@ -25,7 +25,7 @@ jobs:
|
||||
|
||||
- name: Install dependencies
|
||||
run: >
|
||||
sudo apt-get update && sudo apt-get install libvulkan-dev libcjson-dev libx264-dev libavcodec-dev libavutil-dev libswscale-dev libavfilter-dev libbsd-dev libavahi-client-dev libeigen3-dev glslang-tools libudev-dev libwayland-dev libx11-xcb-dev libxrandr-dev libxcb-randr0-dev libgl-dev libglx-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libsystemd-dev libva-dev nlohmann-json3-dev libpulse-dev xz-utils libpipewire-0.3-dev libcli11-dev
|
||||
sudo apt-get update && sudo apt-get install libvulkan-dev libcjson-dev libx264-dev libavcodec-dev libavutil-dev libswscale-dev libavfilter-dev libbsd-dev libavahi-client-dev libavahi-glib-dev libeigen3-dev glslang-tools libudev-dev libwayland-dev libx11-xcb-dev libxrandr-dev libxcb-randr0-dev libgl-dev libglx-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libsystemd-dev libva-dev nlohmann-json3-dev libpulse-dev xz-utils libpipewire-0.3-dev libcli11-dev
|
||||
- name: Prepare Vulkan SDK
|
||||
uses: humbletim/setup-vulkan-sdk@286ed76c72de45dbf50c515779cf4ca2f6aa3944
|
||||
with:
|
||||
|
||||
+1
-1
@@ -139,7 +139,7 @@ if (WIVRN_BUILD_SERVER)
|
||||
pkg_check_modules(libpulse REQUIRED IMPORTED_TARGET libpulse)
|
||||
endif()
|
||||
|
||||
pkg_check_modules(AVAHI REQUIRED IMPORTED_TARGET avahi-client)
|
||||
pkg_check_modules(AVAHI REQUIRED IMPORTED_TARGET avahi-client avahi-glib)
|
||||
find_package(Eigen3 REQUIRED)
|
||||
find_package(nlohmann_json REQUIRED)
|
||||
find_package(CLI11 REQUIRED)
|
||||
|
||||
@@ -19,53 +19,47 @@
|
||||
|
||||
#include "accept_connection.h"
|
||||
|
||||
#include "avahi_publisher.h"
|
||||
#include "driver/configuration.h"
|
||||
#include "hostname.h"
|
||||
#include "version.h"
|
||||
#include "wivrn_packets.h"
|
||||
#include "wivrn_sockets.h"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <sys/poll.h>
|
||||
|
||||
static void avahi_set_bool_callback(AvahiWatch * w, int fd, AvahiWatchEvent event, void * userdata)
|
||||
extern int control_pipe_fd;
|
||||
|
||||
static void report_status(int status)
|
||||
{
|
||||
bool * flag = (bool *)userdata;
|
||||
*flag = true;
|
||||
uint8_t status_byte = status;
|
||||
write(control_pipe_fd, &status_byte, sizeof(status_byte));
|
||||
}
|
||||
|
||||
std::unique_ptr<xrt::drivers::wivrn::TCP> accept_connection(int watch_fd, std::function<bool()> quit)
|
||||
{
|
||||
char protocol_string[17];
|
||||
sprintf(protocol_string, "%016lx", xrt::drivers::wivrn::protocol_version);
|
||||
|
||||
std::map<std::string, std::string> TXT = {
|
||||
{"protocol", protocol_string},
|
||||
{"version", xrt::drivers::wivrn::git_version},
|
||||
{"cookie", server_cookie()},
|
||||
};
|
||||
|
||||
avahi_publisher publisher(hostname().c_str(), "_wivrn._tcp", xrt::drivers::wivrn::default_port, TXT);
|
||||
report_status(0);
|
||||
|
||||
xrt::drivers::wivrn::TCPListener listener(xrt::drivers::wivrn::default_port);
|
||||
bool client_connected = false;
|
||||
bool fd_triggered = false;
|
||||
|
||||
AvahiWatch * watch_listener = publisher.watch_new(listener.get_fd(), AVAHI_WATCH_IN, &avahi_set_bool_callback, &client_connected);
|
||||
AvahiWatch * watch_user = publisher.watch_new(watch_fd, AVAHI_WATCH_IN, &avahi_set_bool_callback, &fd_triggered);
|
||||
pollfd fds[2]{
|
||||
{.fd = watch_fd, .events = POLLIN},
|
||||
{.fd = listener.get_fd(), .events = POLLIN},
|
||||
};
|
||||
|
||||
while (not(client_connected or fd_triggered or (quit and quit())))
|
||||
while (not(quit and quit()))
|
||||
{
|
||||
if (not publisher.iterate(quit ? 100 : -1))
|
||||
break;
|
||||
if (poll(fds, std::size(fds), 100) < 0)
|
||||
{
|
||||
perror("poll");
|
||||
return {};
|
||||
}
|
||||
|
||||
if (fds[0].revents & POLLIN)
|
||||
return {};
|
||||
|
||||
if (fds[1].revents & POLLIN)
|
||||
{
|
||||
report_status(1);
|
||||
return std::make_unique<xrt::drivers::wivrn::TCP>(listener.accept().first);
|
||||
}
|
||||
}
|
||||
|
||||
publisher.watch_free(watch_listener);
|
||||
publisher.watch_free(watch_user);
|
||||
|
||||
if (client_connected)
|
||||
return std::make_unique<xrt::drivers::wivrn::TCP>(listener.accept().first);
|
||||
|
||||
return nullptr;
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -131,19 +131,17 @@ void avahi_publisher::client_callback(AvahiClient * s,
|
||||
}
|
||||
}
|
||||
|
||||
avahi_publisher::avahi_publisher(const char * name, std::string type, int port, const std::map<std::string, std::string> & txt) :
|
||||
name(avahi_strdup(name)), type(std::move(type)), port(port)
|
||||
avahi_publisher::avahi_publisher(const AvahiPoll * poll_api, const std::string & name, std::string type, int port, const std::map<std::string, std::string> & txt) :
|
||||
poll_api(poll_api), name(avahi_strdup(name.c_str())), type(std::move(type)), port(port)
|
||||
{
|
||||
for (const auto & [key, value]: txt)
|
||||
{
|
||||
this->txt.push_back(key + "=" + value);
|
||||
}
|
||||
|
||||
avahi_poll = avahi_simple_poll_new();
|
||||
|
||||
int error;
|
||||
avahi_client = avahi_client_new(
|
||||
avahi_simple_poll_get(avahi_poll),
|
||||
poll_api,
|
||||
(AvahiClientFlags)0,
|
||||
&client_callback,
|
||||
this,
|
||||
@@ -157,31 +155,19 @@ avahi_publisher::~avahi_publisher()
|
||||
{
|
||||
avahi_free(name);
|
||||
avahi_client_free(avahi_client);
|
||||
avahi_simple_poll_free(avahi_poll);
|
||||
}
|
||||
|
||||
/*
|
||||
AvahiWatch * avahi_publisher::watch_new(int fd,
|
||||
AvahiWatchEvent event,
|
||||
void (*callback)(AvahiWatch * w, int fd, AvahiWatchEvent event, void * userdata),
|
||||
void * userdata)
|
||||
{
|
||||
AvahiWatch * watch = avahi_simple_poll_get(avahi_poll)
|
||||
->watch_new(avahi_simple_poll_get(avahi_poll), fd, event, callback, userdata);
|
||||
AvahiWatch * watch = poll_api->watch_new(poll_api, fd, event, callback, userdata);
|
||||
|
||||
return watch;
|
||||
return watch;
|
||||
}
|
||||
|
||||
void avahi_publisher::watch_free(AvahiWatch * watch)
|
||||
{
|
||||
avahi_simple_poll_get(avahi_poll)->watch_free(watch);
|
||||
}
|
||||
|
||||
bool avahi_publisher::iterate(int sleep_time)
|
||||
{
|
||||
int r = avahi_simple_poll_iterate(avahi_poll, sleep_time);
|
||||
|
||||
if (r < 0 && errno != EINTR)
|
||||
throw std::system_error(errno, std::system_category(), "avahi_simple_poll_iterate: ");
|
||||
|
||||
return r == 0;
|
||||
}
|
||||
poll_api->watch_free(watch);
|
||||
}*/
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <avahi-client/publish.h>
|
||||
#include <avahi-common/simple-watch.h>
|
||||
#include <avahi-common/watch.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
@@ -15,7 +14,7 @@ class avahi_publisher
|
||||
int port;
|
||||
std::vector<std::string> txt;
|
||||
|
||||
AvahiSimplePoll * avahi_poll{};
|
||||
const AvahiPoll * poll_api{};
|
||||
AvahiClient * avahi_client{};
|
||||
|
||||
void alt_name();
|
||||
@@ -32,16 +31,8 @@ class avahi_publisher
|
||||
void * userdata /**< The user data that was passed to avahi_client_new() */);
|
||||
|
||||
public:
|
||||
avahi_publisher(const char * name, std::string type, int port, const std::map<std::string, std::string> & txt = {});
|
||||
|
||||
avahi_publisher(const AvahiPoll * poll_api, const std::string & name, std::string type, int port, const std::map<std::string, std::string> & txt = {});
|
||||
avahi_publisher(const avahi_publisher &) = delete;
|
||||
avahi_publisher & operator=(const avahi_publisher &) = delete;
|
||||
~avahi_publisher();
|
||||
|
||||
AvahiWatch * watch_new(int fd,
|
||||
AvahiWatchEvent event,
|
||||
void (*callback)(AvahiWatch * w, int fd, AvahiWatchEvent event, void * userdata),
|
||||
void * userdata);
|
||||
|
||||
void watch_free(AvahiWatch * watch);
|
||||
|
||||
bool iterate(int sleep_time = -1);
|
||||
};
|
||||
|
||||
+348
-218
@@ -7,10 +7,10 @@
|
||||
|
||||
#include "util/u_trace_marker.h"
|
||||
|
||||
#include "accept_connection.h"
|
||||
#include "active_runtime.h"
|
||||
#include "avahi_publisher.h"
|
||||
#include "driver/configuration.h"
|
||||
#include "pidfd.h"
|
||||
#include "hostname.h"
|
||||
#include "start_application.h"
|
||||
#include "version.h"
|
||||
#include "wivrn_config.h"
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "wivrn_sockets.h"
|
||||
|
||||
#include <CLI/CLI.hpp>
|
||||
#include <avahi-glib/glib-watch.h>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
@@ -27,6 +28,10 @@
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <gio/gio.h>
|
||||
#include <glib-unix.h>
|
||||
#include <glib.h>
|
||||
|
||||
#include <shared/ipc_protocol.h>
|
||||
#include <util/u_file.h>
|
||||
|
||||
@@ -42,6 +47,8 @@ extern "C"
|
||||
using namespace xrt::drivers::wivrn;
|
||||
|
||||
std::unique_ptr<TCP> tcp;
|
||||
std::unique_ptr<TCPListener> listener;
|
||||
int control_pipe_fd;
|
||||
|
||||
#ifdef WIVRN_USE_SYSTEMD
|
||||
std::string socket_path()
|
||||
@@ -106,11 +113,8 @@ int create_listen_socket()
|
||||
}
|
||||
#endif
|
||||
|
||||
void waitpid_verbose(pid_t pid, const std::string & name)
|
||||
void display_child_status(int wstatus, const std::string & name)
|
||||
{
|
||||
int wstatus = 0;
|
||||
waitpid(pid, &wstatus, 0);
|
||||
|
||||
std::cerr << name << " exited, exit status " << WEXITSTATUS(wstatus);
|
||||
if (WIFSIGNALED(wstatus))
|
||||
{
|
||||
@@ -156,22 +160,306 @@ static std::string steam_command()
|
||||
if (app_path.starts_with("/usr") or app_path.starts_with("/var"))
|
||||
pressure_vessel_filesystems_rw += ":" + app_path;
|
||||
}
|
||||
return "PRESSURE_VESSEL_FILESYSTEMS_RW=" + pressure_vessel_filesystems_rw + " %command%";
|
||||
}
|
||||
|
||||
int inner_main(int argc, char * argv[], bool use_systemd, bool show_instructions)
|
||||
{
|
||||
std::cerr << "WiVRn " << xrt::drivers::wivrn::git_version << " starting" << std::endl;
|
||||
|
||||
if (show_instructions)
|
||||
{
|
||||
std::string command = steam_command();
|
||||
std::string command = "PRESSURE_VESSEL_FILESYSTEMS_RW=" + pressure_vessel_filesystems_rw + " %command%";
|
||||
|
||||
if (auto p = active_runtime::manifest_path().string(); p.starts_with("/usr"))
|
||||
command = "XR_RUNTIME_JSON=/run/host" + p + " " + command;
|
||||
|
||||
std::cerr << "For Steam games, set command to " << command << std::endl;
|
||||
return command;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
int stdin_pipe_fds[2];
|
||||
int control_pipe_fds[2];
|
||||
|
||||
GMainLoop * main_loop;
|
||||
const AvahiPoll * poll_api;
|
||||
bool use_systemd;
|
||||
|
||||
guint server_watch;
|
||||
guint server_kill_watch;
|
||||
pid_t server_pid;
|
||||
|
||||
guint app_watch;
|
||||
guint app_kill_watch;
|
||||
pid_t app_pid;
|
||||
|
||||
bool quitting_main_loop;
|
||||
|
||||
guint listener_watch;
|
||||
|
||||
/* TODO: Document FSM
|
||||
*/
|
||||
std::optional<active_runtime> runtime_setter;
|
||||
std::optional<avahi_publisher> publisher;
|
||||
|
||||
gboolean headset_connected(gint fd, GIOCondition condition, gpointer user_data);
|
||||
void stop_listening();
|
||||
|
||||
void start_publishing();
|
||||
void stop_publishing();
|
||||
|
||||
void update_fsm();
|
||||
|
||||
void start_app()
|
||||
{
|
||||
#ifdef WIVRN_USE_SYSTEMD
|
||||
app_pid = use_systemd ? start_unit_file() : fork_application();
|
||||
#else
|
||||
app_pid = fork_application();
|
||||
#endif
|
||||
|
||||
assert(app_watch == 0);
|
||||
assert(app_kill_watch == 0);
|
||||
if (app_pid)
|
||||
{
|
||||
app_watch = g_child_watch_add(app_pid, [](pid_t, int status, void *) {
|
||||
display_child_status(status, "Application");
|
||||
g_source_remove(app_watch);
|
||||
if (app_kill_watch)
|
||||
g_source_remove(app_kill_watch);
|
||||
|
||||
app_watch = 0;
|
||||
app_kill_watch = 0;
|
||||
|
||||
update_fsm(); }, nullptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
app_watch = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void start_server()
|
||||
{
|
||||
stop_listening();
|
||||
stop_publishing();
|
||||
server_pid = fork();
|
||||
|
||||
if (server_pid < 0)
|
||||
{
|
||||
perror("fork");
|
||||
g_main_loop_quit(main_loop);
|
||||
}
|
||||
else if (server_pid == 0)
|
||||
{
|
||||
// Redirect stdin
|
||||
dup2(stdin_pipe_fds[0], 0);
|
||||
close(stdin_pipe_fds[0]);
|
||||
close(stdin_pipe_fds[1]);
|
||||
|
||||
setenv("LISTEN_PID", std::to_string(getpid()).c_str(), true);
|
||||
|
||||
// In most cases there is no server-side reprojection and
|
||||
// there is no need for oversampling.
|
||||
setenv("XRT_COMPOSITOR_SCALE_PERCENTAGE", "100", false);
|
||||
|
||||
// Enable mipmaps for distortion
|
||||
setenv("XRT_DISTORTION_MIP_LEVELS", "0", false);
|
||||
|
||||
// FIXME: synchronization fails on gfx pipeline
|
||||
setenv("XRT_COMPOSITOR_COMPUTE", "1", true);
|
||||
|
||||
setenv("AMD_DEBUG", "lowlatencyenc", false);
|
||||
|
||||
try
|
||||
{
|
||||
exit(ipc_server_main(0, 0 /*argc, argv*/));
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::cerr << e.what() << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
__builtin_unreachable();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Server started, PID " << server_pid << std::endl;
|
||||
|
||||
assert(server_watch == 0);
|
||||
assert(server_kill_watch == 0);
|
||||
server_watch = g_child_watch_add(server_pid, [](pid_t, int status, void *) {
|
||||
display_child_status(status, "Server");
|
||||
g_source_remove(server_watch);
|
||||
if (server_kill_watch)
|
||||
g_source_remove(server_kill_watch);
|
||||
|
||||
server_watch = 0;
|
||||
server_kill_watch = 0;
|
||||
|
||||
update_fsm(); }, nullptr);
|
||||
}
|
||||
|
||||
runtime_setter.emplace();
|
||||
}
|
||||
|
||||
void kill_app()
|
||||
{
|
||||
kill(-app_pid, SIGTERM);
|
||||
|
||||
// Send SIGKILL after 1s if it is still running
|
||||
app_kill_watch = g_timeout_add(1000, [](void *) {
|
||||
assert(app_pid > 0);
|
||||
kill(-app_pid, SIGKILL);
|
||||
return G_SOURCE_REMOVE; }, 0);
|
||||
}
|
||||
|
||||
void kill_server()
|
||||
{
|
||||
// FIXME: server doesn't listen on stdin when used in socket activation mode
|
||||
// Write to the server's stdin to make it quit
|
||||
char buffer[] = "\n";
|
||||
(void)write(stdin_pipe_fds[1], &buffer, strlen(buffer));
|
||||
|
||||
// Send SIGTERM after 1s if it is still running
|
||||
server_kill_watch = g_timeout_add(1000, [](void *) {
|
||||
assert(server_pid > 0);
|
||||
kill(-server_pid, SIGTERM);
|
||||
return G_SOURCE_REMOVE; }, 0);
|
||||
}
|
||||
|
||||
void start_listening()
|
||||
{
|
||||
if (listener)
|
||||
return;
|
||||
|
||||
assert(listener_watch == 0);
|
||||
|
||||
listener = std::make_unique<TCPListener>(xrt::drivers::wivrn::default_port);
|
||||
auto source_listener = g_unix_fd_source_new(listener->get_fd(), GIOCondition::G_IO_IN);
|
||||
g_source_set_callback(source_listener, G_SOURCE_FUNC(&headset_connected), nullptr, nullptr);
|
||||
listener_watch = g_source_attach(source_listener, nullptr);
|
||||
g_source_unref(source_listener);
|
||||
}
|
||||
|
||||
void stop_listening()
|
||||
{
|
||||
if (!listener)
|
||||
return;
|
||||
|
||||
assert(listener_watch != 0);
|
||||
|
||||
g_source_remove(listener_watch);
|
||||
listener_watch = 0;
|
||||
listener.reset();
|
||||
}
|
||||
|
||||
void start_publishing()
|
||||
{
|
||||
if (publisher)
|
||||
return;
|
||||
|
||||
char protocol_string[17];
|
||||
sprintf(protocol_string, "%016lx", xrt::drivers::wivrn::protocol_version);
|
||||
std::map<std::string, std::string> TXT = {
|
||||
{"protocol", protocol_string},
|
||||
{"version", xrt::drivers::wivrn::git_version},
|
||||
{"cookie", server_cookie()},
|
||||
};
|
||||
publisher.emplace(poll_api, hostname(), "_wivrn._tcp", xrt::drivers::wivrn::default_port, TXT);
|
||||
}
|
||||
|
||||
void stop_publishing()
|
||||
{
|
||||
publisher.reset();
|
||||
}
|
||||
|
||||
void update_fsm()
|
||||
{
|
||||
bool server_running = server_watch != 0;
|
||||
bool app_running = app_watch != 0;
|
||||
|
||||
if (quitting_main_loop)
|
||||
{
|
||||
if (server_running)
|
||||
kill_server();
|
||||
|
||||
if (app_running)
|
||||
kill_app();
|
||||
|
||||
if (not server_running and not app_running)
|
||||
g_main_loop_quit(main_loop);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (not server_running and app_running)
|
||||
kill_app();
|
||||
|
||||
if (not server_running)
|
||||
{
|
||||
runtime_setter.reset();
|
||||
|
||||
start_listening();
|
||||
start_publishing();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int sigint(void * sig_nr)
|
||||
{
|
||||
std::cout << strsignal((uintptr_t)sig_nr) << std::endl;
|
||||
|
||||
quitting_main_loop = true;
|
||||
|
||||
update_fsm();
|
||||
|
||||
return G_SOURCE_CONTINUE;
|
||||
}
|
||||
|
||||
gboolean headset_connected(gint fd, GIOCondition condition, gpointer user_data)
|
||||
{
|
||||
assert(server_watch == 0);
|
||||
assert(app_watch == 0);
|
||||
|
||||
assert(listener);
|
||||
|
||||
tcp = std::make_unique<xrt::drivers::wivrn::TCP>(listener->accept().first);
|
||||
init_cleanup_functions();
|
||||
|
||||
std::cout << "Client connected" << std::endl;
|
||||
|
||||
start_server();
|
||||
start_app();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
gboolean control_received(gint fd, GIOCondition condition, gpointer user_data)
|
||||
{
|
||||
uint8_t status;
|
||||
if (read(fd, &status, sizeof(status)) == sizeof(status))
|
||||
{
|
||||
switch (status)
|
||||
{
|
||||
case 0:
|
||||
start_publishing();
|
||||
break;
|
||||
case 1:
|
||||
stop_publishing();
|
||||
break;
|
||||
default:
|
||||
std::cerr << "Unexpected status received: " << (int)status << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
perror("read control pipe");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int inner_main(int argc, char * argv[], bool show_instructions)
|
||||
{
|
||||
std::cerr << "WiVRn " << xrt::drivers::wivrn::git_version << " starting" << std::endl;
|
||||
std::cerr << "For Steam games, set command to " << steam_command() << std::endl;
|
||||
|
||||
#ifdef WIVRN_USE_SYSTEMD
|
||||
create_listen_socket();
|
||||
@@ -181,220 +469,63 @@ int inner_main(int argc, char * argv[], bool use_systemd, bool show_instructions
|
||||
|
||||
u_trace_marker_init();
|
||||
|
||||
sigset_t sigint_mask;
|
||||
sigemptyset(&sigint_mask);
|
||||
sigaddset(&sigint_mask, SIGINT);
|
||||
sigaddset(&sigint_mask, SIGTERM);
|
||||
// Initialize main loop
|
||||
main_loop = g_main_loop_new(nullptr, false);
|
||||
auto main_context = g_main_loop_get_context(main_loop);
|
||||
|
||||
sigprocmask(SIG_BLOCK, &sigint_mask, nullptr);
|
||||
int sigint_fd = signalfd(-1, &sigint_mask, 0);
|
||||
// avahi glib integration
|
||||
AvahiGLibPoll * glib_poll = avahi_glib_poll_new(main_context, G_PRIORITY_DEFAULT);
|
||||
poll_api = avahi_glib_poll_get(glib_poll);
|
||||
|
||||
// Create a pipe to quit monado properly
|
||||
int pipe_fds[2];
|
||||
if (pipe(pipe_fds) < 0)
|
||||
if (pipe(stdin_pipe_fds) < 0)
|
||||
{
|
||||
perror("pipe");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
fcntl(pipe_fds[0], F_SETFD, FD_CLOEXEC);
|
||||
fcntl(pipe_fds[1], F_SETFD, FD_CLOEXEC);
|
||||
fcntl(stdin_pipe_fds[0], F_SETFD, FD_CLOEXEC);
|
||||
fcntl(stdin_pipe_fds[1], F_SETFD, FD_CLOEXEC);
|
||||
|
||||
char protocol_string[17];
|
||||
sprintf(protocol_string, "%016lx", xrt::drivers::wivrn::protocol_version);
|
||||
|
||||
std::map<std::string, std::string> TXT;
|
||||
TXT["protocol"] = protocol_string;
|
||||
TXT["version"] = xrt::drivers::wivrn::git_version;
|
||||
TXT["cookie"] = server_cookie();
|
||||
|
||||
bool quit = false;
|
||||
while (!quit)
|
||||
// Create a pipe to report monado status to the main loop
|
||||
if (pipe(control_pipe_fds) < 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
tcp = accept_connection(sigint_fd);
|
||||
if (tcp)
|
||||
init_cleanup_functions();
|
||||
else
|
||||
break;
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::cerr << e.what() << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
perror("pipe");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
fcntl(control_pipe_fds[0], F_SETFD, FD_CLOEXEC);
|
||||
fcntl(control_pipe_fds[1], F_SETFD, FD_CLOEXEC);
|
||||
control_pipe_fd = control_pipe_fds[1];
|
||||
|
||||
active_runtime runtime_setter;
|
||||
auto control_listener = g_unix_fd_source_new(control_pipe_fds[0], GIOCondition::G_IO_IN);
|
||||
g_source_set_callback(control_listener, G_SOURCE_FUNC(&control_received), nullptr, nullptr);
|
||||
g_source_attach(control_listener, nullptr);
|
||||
g_source_unref(control_listener);
|
||||
|
||||
// Initialize avahi publisher
|
||||
start_publishing();
|
||||
|
||||
// Initialize listener
|
||||
start_listening();
|
||||
|
||||
// Catch SIGINT & SIGTERM
|
||||
g_unix_signal_add(SIGINT, &sigint, (void *)SIGINT);
|
||||
g_unix_signal_add(SIGTERM, &sigint, (void *)SIGTERM);
|
||||
|
||||
// Main loop
|
||||
g_main_loop_run(main_loop);
|
||||
|
||||
// Cleanup
|
||||
runtime_setter.reset();
|
||||
stop_publishing();
|
||||
stop_listening();
|
||||
avahi_glib_poll_free(glib_poll);
|
||||
g_main_loop_unref(main_loop);
|
||||
|
||||
#ifdef WIVRN_USE_SYSTEMD
|
||||
pid_t client_pid = use_systemd ? start_unit_file() : fork_application();
|
||||
#else
|
||||
pid_t client_pid = fork_application();
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(socket_path(), ec);
|
||||
#endif
|
||||
|
||||
pid_t server_pid = fork();
|
||||
|
||||
if (server_pid < 0)
|
||||
{
|
||||
perror("fork");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (server_pid == 0)
|
||||
{
|
||||
// Unmask SIGTERM, keep SIGINT masked
|
||||
sigset_t sigint_mask;
|
||||
sigemptyset(&sigint_mask);
|
||||
sigaddset(&sigint_mask, SIGTERM);
|
||||
sigprocmask(SIG_UNBLOCK, &sigint_mask, nullptr);
|
||||
close(sigint_fd);
|
||||
|
||||
// Redirect stdin
|
||||
dup2(pipe_fds[0], 0);
|
||||
close(pipe_fds[0]);
|
||||
close(pipe_fds[1]);
|
||||
|
||||
setenv("LISTEN_PID", std::to_string(getpid()).c_str(), true);
|
||||
|
||||
// In most cases there is no server-side reprojection and
|
||||
// there is no need for oversampling.
|
||||
setenv("XRT_COMPOSITOR_SCALE_PERCENTAGE", "100", false);
|
||||
|
||||
// Enable mipmaps for distortion
|
||||
setenv("XRT_DISTORTION_MIP_LEVELS", "0", false);
|
||||
|
||||
// FIXME: synchronization fails on gfx pipeline
|
||||
setenv("XRT_COMPOSITOR_COMPUTE", "1", true);
|
||||
|
||||
setenv("AMD_DEBUG", "lowlatencyenc", false);
|
||||
|
||||
try
|
||||
{
|
||||
return ipc_server_main(argc, argv);
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::cerr << e.what() << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Server started, PID " << server_pid << std::endl;
|
||||
|
||||
tcp.reset();
|
||||
|
||||
int server_fd = pidfd_open(server_pid, 0);
|
||||
int client_fd = client_pid > 0 ? pidfd_open(client_pid, 0) : -1;
|
||||
|
||||
pollfd fds[3]{};
|
||||
fds[0].fd = sigint_fd;
|
||||
fds[0].events = POLLIN;
|
||||
fds[1].fd = server_fd;
|
||||
fds[1].events = POLLIN;
|
||||
fds[2].fd = client_fd;
|
||||
fds[2].events = POLLIN;
|
||||
|
||||
bool server_running = true;
|
||||
bool client_running = client_pid > 0;
|
||||
|
||||
while (!quit && server_running)
|
||||
{
|
||||
poll(fds, std::size(fds), -1);
|
||||
|
||||
if (fds[0].revents & POLLIN)
|
||||
{
|
||||
// SIGINT/SIGTERM received
|
||||
quit = true;
|
||||
}
|
||||
|
||||
if (fds[1].revents & POLLIN)
|
||||
{
|
||||
// Server exited
|
||||
waitpid_verbose(server_pid, "Server");
|
||||
|
||||
server_running = false;
|
||||
fds[1].fd = -1;
|
||||
}
|
||||
|
||||
if (fds[2].revents & POLLIN)
|
||||
{
|
||||
// Client application exited
|
||||
waitpid_verbose(client_pid, "Client");
|
||||
|
||||
client_running = false;
|
||||
fds[2].fd = -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Quit the server and the client
|
||||
|
||||
if (client_running)
|
||||
kill(-client_pid, SIGTERM);
|
||||
|
||||
if (server_running)
|
||||
{
|
||||
// FIXME: server doesn't listen on stdin when used in socket activation mode
|
||||
// Write to the server's stdin to make it quit
|
||||
char buffer[] = "\n";
|
||||
(void)write(pipe_fds[1], &buffer, strlen(buffer));
|
||||
}
|
||||
|
||||
// Wait until both the server and the client exit
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
while (server_running or client_running)
|
||||
{
|
||||
poll(fds, std::size(fds), 100);
|
||||
|
||||
if (fds[1].revents & POLLIN)
|
||||
{
|
||||
// Server exited
|
||||
waitpid_verbose(server_pid, "Server");
|
||||
|
||||
server_running = false;
|
||||
fds[1].fd = -1;
|
||||
}
|
||||
|
||||
if (fds[2].revents & POLLIN)
|
||||
{
|
||||
// Client application exited
|
||||
waitpid_verbose(server_pid, "Client");
|
||||
|
||||
client_running = false;
|
||||
fds[2].fd = -1;
|
||||
}
|
||||
|
||||
if (std::chrono::steady_clock::now() - now > std::chrono::seconds(1))
|
||||
{
|
||||
// Send SIGTERM if the server takes more than 1s to quit
|
||||
if (server_running)
|
||||
{
|
||||
pidfd_send_signal(server_fd, SIGTERM, nullptr, 0);
|
||||
}
|
||||
|
||||
// Send SIGKILL if the client takes more than 1s to quit
|
||||
if (client_running)
|
||||
{
|
||||
kill(-client_pid, SIGKILL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
close(server_fd);
|
||||
if (client_fd > 0)
|
||||
close(client_fd);
|
||||
|
||||
run_cleanup_functions();
|
||||
}
|
||||
}
|
||||
#ifdef WIVRN_USE_SYSTEMD
|
||||
try
|
||||
{
|
||||
std::filesystem::remove(socket_path());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -403,7 +534,6 @@ int main(int argc, char * argv[])
|
||||
CLI::App app;
|
||||
|
||||
std::string config_file;
|
||||
bool use_systemd = false;
|
||||
app.add_option("-f", config_file, "configuration file")->option_text("FILE")->check(CLI::ExistingFile);
|
||||
auto no_instructions = app.add_flag("--no-instructions")->group("");
|
||||
#ifdef WIVRN_USE_SYSTEMD
|
||||
@@ -423,7 +553,7 @@ int main(int argc, char * argv[])
|
||||
#endif
|
||||
try
|
||||
{
|
||||
return inner_main(argc, argv, use_systemd, not *no_instructions);
|
||||
return inner_main(argc, argv, not *no_instructions);
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user