diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 6da090d6..4dbe5a9e 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -62,6 +62,7 @@ add_executable(wivrn-server avahi_publisher.cpp hostname.cpp main.cpp + start_application.cpp target_instance_wivrn.cpp wivrn_ipc.cpp @@ -161,6 +162,10 @@ configure_file(dist/wivrn.service.in wivrn.service) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/wivrn.service DESTINATION lib/systemd/user/) +configure_file(dist/wivrn-application.service.in wivrn-application.service) +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/wivrn-application.service + DESTINATION lib/systemd/user/) + install(FILES dist/firewalld-wivrn.xml RENAME wivrn.xml DESTINATION lib/firewalld/services) diff --git a/server/dist/wivrn-application.service.in b/server/dist/wivrn-application.service.in new file mode 100644 index 00000000..d783dad4 --- /dev/null +++ b/server/dist/wivrn-application.service.in @@ -0,0 +1,9 @@ +[Unit] +Description=OpenXR streaming server + +[Service] +Type=simple +ExecStart=@CMAKE_INSTALL_PREFIX@/bin/wivrn-server --application +Restart=on-failure +RestartSec=0 +PrivateTmp=true diff --git a/server/dist/wivrn.service.in b/server/dist/wivrn.service.in index 41973933..3fff3a38 100644 --- a/server/dist/wivrn.service.in +++ b/server/dist/wivrn.service.in @@ -4,3 +4,18 @@ Description=OpenXR streaming server [Service] Type=simple ExecStart=@CMAKE_INSTALL_PREFIX@/bin/wivrn-server +CapabilityBoundingSet=CAP_SYS_NICE +AmbientCapabilities=CAP_SYS_NICE +LockPersonality=true +NoNewPrivileges=true +PrivateTmp=true +ProtectClock=true +ProtectControlGroups=true +ProtectKernelLogs=true +ProtectKernelModules=true +ProtectKernelTunables=true +ProtectProc=invisible +ProtectSystem=strict +RemoveIPC=true +RestrictNamespaces=true +RestrictSUIDSGID=true diff --git a/server/main.cpp b/server/main.cpp index ec7ae91d..fa1379a8 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -3,9 +3,6 @@ /*! * @file * @brief Main file for WiVRn Monado service. - * @author - * @author - * @ingroup ipc */ #include "util/u_trace_marker.h" @@ -14,6 +11,7 @@ #include "active_runtime.h" #include "driver/configuration.h" #include "pidfd.h" +#include "start_application.h" #include "version.h" #include "wivrn_config.h" #include "wivrn_ipc.h" @@ -106,42 +104,6 @@ int create_listen_socket() } #endif -pid_t start_application() -{ - auto config = configuration::read_user_configuration(); - - if (!config.application.empty()) - { - 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(); - - std::vector argv; - argv.reserve(config.application.size() + 1); - for (auto & arg: config.application) - argv.push_back(arg.data()); - argv.push_back(nullptr); - - execvp(config.application.front().c_str(), argv.data()); - - perror("Cannot start application"); - exit(EXIT_FAILURE); - } - - return application_pid; - } - - return 0; -} - void waitpid_verbose(pid_t pid, const std::string & name) { int wstatus = 0; @@ -214,7 +176,7 @@ int inner_main(int argc, char * argv[]) active_runtime runtime_setter; - pid_t client_pid = start_application(); + pid_t client_pid = fork_application(); pid_t server_pid = fork(); @@ -376,11 +338,27 @@ int inner_main(int argc, char * argv[]) int main(int argc, char * argv[]) { - if (argc > 1 and (strcmp(argv[1], "-f") != 0 or argc < 3)) + if (argc > 1) { - std::cerr << "Usage: " << argv[0] << " [-f file]" << std::endl - << "\t -f file: configuration file to use" << std::endl; - return EXIT_FAILURE; + if ((strcmp(argv[1], "--application") == 0)) + { + pid_t pid = exec_application(configuration::read_user_configuration()); + if (pid < 0) + { + return EXIT_FAILURE; + } + return EXIT_SUCCESS; + } + else if ((strcmp(argv[1], "-f") != 0 or argc < 3)) + { + std::cerr << "Usage: " << argv[0] << " [-f file]" << std::endl + << "\t -f file: configuration file to use" << std::endl; + if (strcmp(argv[1], "-h") != 0 and strcmp(argv[1], "--help") != 0) + { + return EXIT_FAILURE; + } + return EXIT_SUCCESS; + } } try diff --git a/server/start_application.cpp b/server/start_application.cpp new file mode 100644 index 00000000..7af515c4 --- /dev/null +++ b/server/start_application.cpp @@ -0,0 +1,247 @@ +// Copyright 2024, Gavin John et al. +// SPDX-License-Identifier: BSL-1.0 +/*! + * @file + * @brief Main file for WiVRn Monado service. + * @author Gavin John + */ + +#include "start_application.h" + +#include "driver/configuration.h" +#include "wivrn_config.h" + +#include +#include +#include + +#ifdef WIVRN_USE_SYSTEMD +#include +#include +#include +#include +#include +#endif + +int exec_application(configuration config) +{ + if (config.application.empty()) + return 0; + + std::vector argv; + argv.reserve(config.application.size() + 1); + for (auto & arg: config.application) + argv.push_back(arg.data()); + argv.push_back(nullptr); + + execvp(config.application.front().c_str(), argv.data()); + + perror("Cannot start application"); + exit(EXIT_FAILURE); +} + +#ifdef WIVRN_USE_SYSTEMD +int get_service_pid(const std::string & service_name, pid_t & pid) +{ + sd_bus * bus = nullptr; + sd_bus_error error = SD_BUS_ERROR_NULL; + sd_bus_message * msg = nullptr; + const char * destination = "org.freedesktop.systemd1"; + const char * path = "/org/freedesktop/systemd1"; + const char * interface = "org.freedesktop.systemd1.Manager"; + const char * member = "GetUnit"; + + int ret = 0; + + // Connect to the system bus + ret = sd_bus_open_user(&bus); + if (ret < 0) + { + std::cerr << "Failed to connect to system bus: " << strerror(-ret) << std::endl; + return ret; + } + + // Call the method + ret = sd_bus_call_method(bus, destination, path, interface, member, &error, &msg, "s", service_name.c_str()); + if (ret < 0) + { + std::cerr << "Failed to issue method call: " << error.message << std::endl; + goto finish; + } + + // Parse the response message + const char * object_path; + ret = sd_bus_message_read(msg, "o", &object_path); + if (ret < 0) + { + std::cerr << "Failed to parse response message: " << strerror(-ret) << std::endl; + goto finish; + } + + // Get the service's main PID + ret = sd_bus_get_property_trivial(bus, destination, object_path, "org.freedesktop.systemd1.Service", "MainPID", &error, 'u', &pid); + if (ret < 0) + { + std::cerr << "Failed to get PID: " << error.message << std::endl; + goto finish; + } + +finish: + sd_bus_error_free(&error); + sd_bus_message_unref(msg); + sd_bus_unref(bus); + return ret; +} + +int start_service(const std::string & service_name) +{ + sd_bus * bus = nullptr; + sd_bus_error error = SD_BUS_ERROR_NULL; + sd_bus_message * msg = nullptr; + const char * destination = "org.freedesktop.systemd1"; + const char * path = "/org/freedesktop/systemd1"; + const char * interface = "org.freedesktop.systemd1.Manager"; + const char * member = "StartUnit"; + const char * mode = "replace"; + + int ret = 0; + + // Connect to the system bus + ret = sd_bus_open_user(&bus); + if (ret < 0) + { + std::cerr << "Failed to connect to system bus: " << strerror(-ret) << std::endl; + return ret; + } + + // 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); + if (ret < 0) + { + std::cerr << "Failed to start service: " << error.message << std::endl; + goto finish; + } + +finish: + sd_bus_error_free(&error); + sd_bus_message_unref(msg); + sd_bus_unref(bus); + return ret; +} + +bool is_service_active(const std::string & service_name) +{ + sd_bus * bus = nullptr; + sd_bus_error error = SD_BUS_ERROR_NULL; + sd_bus_message * msg = nullptr; + const char * destination = "org.freedesktop.systemd1"; + const char * path = "/org/freedesktop/systemd1"; + const char * interface = "org.freedesktop.systemd1.Manager"; + const char * member = "GetUnit"; + + bool is_active = false; + int ret = 0; + + // Connect to the system bus + ret = sd_bus_open_user(&bus); + if (ret < 0) + { + std::cerr << "Failed to connect to system bus: " << strerror(-ret) << std::endl; + return false; + } + + const char * object_path; + // Call the method + ret = sd_bus_call_method(bus, destination, path, interface, member, &error, &msg, "s", service_name.c_str()); + if (ret < 0) + { + std::cerr << "Failed to issue method call: " << error.message << std::endl; + goto finish; + } + + // Parse the response message + ret = sd_bus_message_read(msg, "o", &object_path); + if (ret < 0) + { + std::cerr << "Failed to parse response message: " << strerror(-ret) << std::endl; + goto finish; + } + + // Check the service's ActiveState property + char * active_state; + ret = sd_bus_get_property_string(bus, destination, object_path, "org.freedesktop.systemd1.Unit", "ActiveState", &error, &active_state); + if (ret < 0) + { + std::cerr << "Failed to get ActiveState: " << error.message << std::endl; + goto finish; + } + + is_active = (std::string_view(active_state) == "active"); + +finish: + sd_bus_error_free(&error); + sd_bus_message_unref(msg); + sd_bus_unref(bus); + return is_active; +} + +pid_t fork_application() +{ + std::string service_name = "wivrn-application.service"; + pid_t pid; + int ret = get_service_pid(service_name, pid); + + if (ret < 0 || pid == 0) + { + ret = start_service(service_name); + if (ret < 0) + { + std::cerr << "Failed to start service " << service_name << std::endl; + return ret; + } + + // Wait until the service is active + while (!is_service_active(service_name)) + { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + // Try to get the PID again + ret = get_service_pid(service_name, pid); + if (ret < 0) + { + std::cerr << "Failed to get PID for service " << service_name << std::endl; + return ret; + } + } + + return pid; +} + +#else + +pid_t fork_application() +{ + auto config = configuration::read_user_configuration(); + + if (!config.application.empty()) + { + 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(config); + } + } + + return 0; +} +#endif diff --git a/server/start_application.h b/server/start_application.h new file mode 100644 index 00000000..21859e8a --- /dev/null +++ b/server/start_application.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +struct configuration; + +pid_t fork_application(); +int exec_application(configuration);