diff --git a/server/main.cpp b/server/main.cpp index 3e52a746..b5ca313b 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -416,8 +416,6 @@ gboolean headset_connected_success(void *) if (enc_state == wivrn_connection::encryption_state::pairing) set_encryption_state(wivrn_connection::encryption_state::enabled); - init_cleanup_functions(); - std::cerr << "Client connected" << std::endl; expose_known_keys_on_dbus(); diff --git a/server/wivrn_ipc.cpp b/server/wivrn_ipc.cpp index 9dc9b135..f773790e 100644 --- a/server/wivrn_ipc.cpp +++ b/server/wivrn_ipc.cpp @@ -20,87 +20,9 @@ #include "wivrn_ipc.h" #include "driver/wivrn_connection.h" -#include -#include -#include - std::unique_ptr connection; -struct cleanup_function -{ - void (*callback)(uintptr_t); - uintptr_t userdata; -}; - -std::array * cleanup_functions; // In shared memory - std::optional receive_from_main() { return wivrn_ipc_socket_monado->receive(); } - -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 the audio backend 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); - } -} diff --git a/server/wivrn_ipc.h b/server/wivrn_ipc.h index b92c2b84..6d173700 100644 --- a/server/wivrn_ipc.h +++ b/server/wivrn_ipc.h @@ -82,8 +82,3 @@ void send_to_main(T packet) { wivrn_ipc_socket_monado->send(std::move(packet)); } - -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();