Cleanup when the server process exits
This commit is contained in:
committed by
Patrick Nicolas
parent
7fb2cca038
commit
0b99099527
@@ -53,6 +53,7 @@ add_executable(wivrn-server
|
||||
target_instance_wivrn.cpp
|
||||
hostname.cpp
|
||||
avahi_publisher.cpp
|
||||
wivrn_ipc.cpp
|
||||
|
||||
driver/audio_setup.cpp
|
||||
driver/configuration.cpp
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "util/u_logging.h"
|
||||
|
||||
#include "../wivrn_ipc.h"
|
||||
#include "wivrn_sockets.h"
|
||||
#include <fcntl.h>
|
||||
#include <netinet/tcp.h>
|
||||
@@ -85,6 +86,8 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
void unload_module(uintptr_t id);
|
||||
|
||||
void unload_module(pa_context * ctx, uint32_t id)
|
||||
{
|
||||
std::promise<void> p;
|
||||
@@ -100,6 +103,8 @@ void unload_module(pa_context * ctx, uint32_t id)
|
||||
auto op = pa_context_unload_module(ctx, id, cb, cb);
|
||||
pa_operation_unref(op);
|
||||
p.get_future().get();
|
||||
|
||||
remove_cleanup_function(unload_module, id);
|
||||
}
|
||||
|
||||
std::optional<module_entry> get_sink(pa_context * ctx, const char * name)
|
||||
@@ -179,6 +184,9 @@ module_entry ensure_sink(pa_context * ctx, const char * name, const std::string
|
||||
if (not sink)
|
||||
std::runtime_error("failed to create audio sink " + std::string(name));
|
||||
sink->socket = fifo;
|
||||
|
||||
add_cleanup_function(unload_module, sink->module);
|
||||
|
||||
return *sink;
|
||||
}
|
||||
|
||||
@@ -444,3 +452,9 @@ std::shared_ptr<audio_publish_handle> audio_publish_handle::create(
|
||||
{
|
||||
return pulse_publish_handle::create(source_name, source_description, sink_name, sink_description, listen_port, info);
|
||||
}
|
||||
|
||||
void unload_module(uintptr_t id)
|
||||
{
|
||||
pa_connection cnx("WiVRn");
|
||||
unload_module(cnx, id);
|
||||
}
|
||||
|
||||
+76
-15
@@ -10,13 +10,20 @@
|
||||
|
||||
#include "util/u_trace_marker.h"
|
||||
|
||||
#include "wivrn_ipc.h"
|
||||
#include "wivrn_packets.h"
|
||||
#include "wivrn_sockets.h"
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <sys/signalfd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include <sys/pidfd.h>
|
||||
}
|
||||
|
||||
#include "avahi_publisher.h"
|
||||
#include "hostname.h"
|
||||
|
||||
@@ -51,32 +58,52 @@ using namespace xrt::drivers::wivrn;
|
||||
|
||||
std::unique_ptr<TCP> tcp;
|
||||
|
||||
void avahi_callback(AvahiWatch * w, int fd, AvahiWatchEvent event, void * userdata)
|
||||
void avahi_set_bool_callback(AvahiWatch * w, int fd, AvahiWatchEvent event, void * userdata)
|
||||
{
|
||||
bool * client_connected = (bool *)userdata;
|
||||
*client_connected = true;
|
||||
bool * flag = (bool *)userdata;
|
||||
*flag = true;
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
u_trace_marker_init();
|
||||
|
||||
while (true)
|
||||
sigset_t sigint_mask;
|
||||
sigemptyset(&sigint_mask);
|
||||
sigaddset(&sigint_mask, SIGINT);
|
||||
sigaddset(&sigint_mask, SIGTERM);
|
||||
|
||||
sigprocmask(SIG_BLOCK, &sigint_mask, nullptr);
|
||||
int sigint_fd = signalfd(-1, &sigint_mask, 0);
|
||||
|
||||
bool quit = false;
|
||||
while (!quit)
|
||||
{
|
||||
{
|
||||
avahi_publisher publisher(hostname().c_str(), "_wivrn._tcp", control_port);
|
||||
|
||||
TCPListener listener(control_port);
|
||||
bool client_connected = false;
|
||||
bool sigint_received = false;
|
||||
|
||||
AvahiWatch * watch = publisher.watch_new(listener.get_fd(), AVAHI_WATCH_IN, &avahi_callback, &client_connected);
|
||||
AvahiWatch * watch_listener = publisher.watch_new(listener.get_fd(), AVAHI_WATCH_IN, &avahi_set_bool_callback, &client_connected);
|
||||
AvahiWatch * watch_sigint = publisher.watch_new(sigint_fd, AVAHI_WATCH_IN, &avahi_set_bool_callback, &sigint_received);
|
||||
|
||||
while (publisher.iterate() && !client_connected)
|
||||
while (publisher.iterate() && !client_connected && !sigint_received)
|
||||
;
|
||||
|
||||
publisher.watch_free(watch);
|
||||
publisher.watch_free(watch_listener);
|
||||
publisher.watch_free(watch_sigint);
|
||||
|
||||
tcp = std::make_unique<TCP>(listener.accept().first);
|
||||
if (client_connected)
|
||||
{
|
||||
tcp = std::make_unique<TCP>(listener.accept().first);
|
||||
|
||||
init_cleanup_functions();
|
||||
}
|
||||
|
||||
if (sigint_received)
|
||||
break;
|
||||
}
|
||||
|
||||
pid_t child = fork();
|
||||
@@ -88,20 +115,54 @@ int main(int argc, char * argv[])
|
||||
}
|
||||
if (child == 0)
|
||||
{
|
||||
sigprocmask(SIG_UNBLOCK, &sigint_mask, nullptr);
|
||||
close(sigint_fd);
|
||||
|
||||
return ipc_server_main(argc, argv);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Server started, PID " << child << std::endl;
|
||||
|
||||
int wstatus = 0;
|
||||
waitpid(child, &wstatus, 0);
|
||||
tcp.reset();
|
||||
|
||||
std::cerr << "Server exited, exit status " << WEXITSTATUS(wstatus) << std::endl;
|
||||
if (WIFSIGNALED(wstatus))
|
||||
std::cerr << "Received signal " << sigabbrev_np(WTERMSIG(wstatus)) << " ("
|
||||
<< strsignal(WTERMSIG(wstatus)) << ")"
|
||||
<< (WCOREDUMP(wstatus) ? ", core dumped" : "") << std::endl;
|
||||
int child_fd = pidfd_open(child, 0);
|
||||
|
||||
pollfd fds[2]{};
|
||||
fds[0].fd = child_fd;
|
||||
fds[0].events = POLLIN;
|
||||
fds[1].fd = sigint_fd;
|
||||
fds[1].events = POLLIN;
|
||||
|
||||
while (true)
|
||||
{
|
||||
poll(fds, std::size(fds), -1);
|
||||
|
||||
if (fds[1].revents & POLLIN)
|
||||
{
|
||||
// SIGINT/SIGTERM received
|
||||
quit = true;
|
||||
}
|
||||
|
||||
if (fds[0].revents & POLLIN)
|
||||
{
|
||||
// Child exited
|
||||
int wstatus = 0;
|
||||
waitpid(child, &wstatus, 0);
|
||||
|
||||
std::cerr << "Server exited, exit status " << WEXITSTATUS(wstatus) << std::endl;
|
||||
if (WIFSIGNALED(wstatus))
|
||||
std::cerr << "Received signal " << sigabbrev_np(WTERMSIG(wstatus)) << " ("
|
||||
<< strsignal(WTERMSIG(wstatus)) << ")"
|
||||
<< (WCOREDUMP(wstatus) ? ", core dumped" : "") << std::endl;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
close(child_fd);
|
||||
|
||||
run_cleanup_functions();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "wivrn_ipc.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
struct cleanup_function
|
||||
{
|
||||
void (*callback)(uintptr_t);
|
||||
uintptr_t userdata;
|
||||
};
|
||||
|
||||
std::array<cleanup_function, 1024> * cleanup_functions; // In shared memory
|
||||
|
||||
void init_cleanup_functions()
|
||||
{
|
||||
if (cleanup_functions)
|
||||
munmap(cleanup_functions, sizeof(*cleanup_functions));
|
||||
cleanup_functions = (decltype(cleanup_functions))mmap(nullptr, sizeof(*cleanup_functions), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
|
||||
|
||||
std::fill(cleanup_functions->begin(), cleanup_functions->end(), cleanup_function{});
|
||||
}
|
||||
|
||||
void add_cleanup_function(void (*callback)(uintptr_t), uintptr_t userdata)
|
||||
{
|
||||
for(auto& i: *cleanup_functions)
|
||||
{
|
||||
if (i.callback == nullptr)
|
||||
{
|
||||
i = {callback, userdata};
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void remove_cleanup_function(void (*callback)(uintptr_t), uintptr_t userdata)
|
||||
{
|
||||
for(auto& i: *cleanup_functions)
|
||||
{
|
||||
if (i.callback == callback && i.userdata == userdata)
|
||||
{
|
||||
i = {};
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void run_cleanup_functions()
|
||||
{
|
||||
// Don't fork if cleanup_functions is empty
|
||||
if (std::ranges::all_of(*cleanup_functions, [](auto& i){ return i.callback == nullptr; }))
|
||||
return;
|
||||
|
||||
// Fork because pulseaudio doesn't like being initialized in the parent and the child
|
||||
// of a fork, and ipc_server_main() is called in a child process
|
||||
pid_t child = fork();
|
||||
|
||||
if (child < 0)
|
||||
{
|
||||
perror("fork");
|
||||
}
|
||||
else if (child == 0)
|
||||
{
|
||||
for(auto& i: *cleanup_functions)
|
||||
{
|
||||
if (i.callback)
|
||||
{
|
||||
i.callback(i.userdata);
|
||||
}
|
||||
i = {};
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
else if (child > 0)
|
||||
{
|
||||
int wstatus = 0;
|
||||
waitpid(child, &wstatus, 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <wivrn_sockets.h>
|
||||
|
||||
extern std::unique_ptr<xrt::drivers::wivrn::TCP> tcp;
|
||||
|
||||
void init_cleanup_functions();
|
||||
void add_cleanup_function(void (*callback)(uintptr_t), uintptr_t userdata);
|
||||
void remove_cleanup_function(void (*callback)(uintptr_t), uintptr_t userdata);
|
||||
void run_cleanup_functions();
|
||||
Reference in New Issue
Block a user