From 6baeceb74a3baa3b284eebcf56293d5ab643987b Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Fri, 11 Oct 2024 10:18:32 +0200 Subject: [PATCH] Check if the server has CAP_SYS_NICE, allow user to grant it --- CMakeLists.txt | 6 + dashboard/CMakeLists.txt | 3 + dashboard/gui_config.h.in | 2 +- dashboard/main_window.cpp | 171 +++++++++++++++++++++++++-- dashboard/main_window.h | 5 +- dashboard/main_window.ui | 62 +++++++++- flatpak/io.github.wivrn.wivrn.yml.in | 1 + 7 files changed, 234 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a1483abe..6048ce17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,8 @@ option(WIVRN_FEATURE_RENDERDOC "Support renderdoc" OFF) option(WIVRN_FEATURE_SOLARXR "Enable SolarXR driver" OFF) option(WIVRN_FEATURE_STEAMVR_LIGHTHOUSE "Enable SteamVR Lighthouse driver" OFF) +option(WIVRN_CHECK_CAPSYSNICE "Check if CAP_SYS_NICE is available at startup" ON) + option(WIVRN_OPENXR_INSTALL_ABSOLUTE_RUNTIME_PATH OFF) option(ENABLE_COLOURED_OUTPUT "Always produce ANSI-coloured output (GNU/Clang only)." ON) @@ -149,6 +151,10 @@ endif() if (WIVRN_BUILD_DASHBOARD) find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets DBus Network LinguistTools) find_package(nlohmann_json REQUIRED) + if (WIVRN_CHECK_CAPSYSNICE) + pkg_check_modules(libcap REQUIRED IMPORTED_TARGET libcap) + endif() + set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) diff --git a/dashboard/CMakeLists.txt b/dashboard/CMakeLists.txt index a7a0c4dd..081816a2 100644 --- a/dashboard/CMakeLists.txt +++ b/dashboard/CMakeLists.txt @@ -55,6 +55,9 @@ target_link_libraries(wivrn-dashboard PRIVATE Qt6::Network nlohmann_json ) +if (WIVRN_CHECK_CAPSYSNICE) + target_link_libraries(wivrn-dashboard PRIVATE PkgConfig::libcap) +endif() file(GLOB TRANSLATION_FILES ${CMAKE_CURRENT_SOURCE_DIR}/wivrn_*.ts) qt_add_translations(wivrn-dashboard TS_FILES ${TRANSLATION_FILES}) diff --git a/dashboard/gui_config.h.in b/dashboard/gui_config.h.in index 8d323438..33459e9e 100644 --- a/dashboard/gui_config.h.in +++ b/dashboard/gui_config.h.in @@ -19,6 +19,6 @@ #pragma once -#define WIVRN_CLIENT_URL "@WIVRN_CLIENT_URL@" +#cmakedefine01 WIVRN_CHECK_CAPSYSNICE #define WORKAROUND_QTBUG_90005 // See https://bugreports.qt.io/browse/QTBUG-90005 diff --git a/dashboard/main_window.cpp b/dashboard/main_window.cpp index c3288ed7..a9283019 100644 --- a/dashboard/main_window.cpp +++ b/dashboard/main_window.cpp @@ -35,6 +35,10 @@ #include "wivrn_server.h" #include "wizard.h" +#if WIVRN_CHECK_CAPSYSNICE +#include +#endif + Q_LOGGING_CATEGORY(wivrn_log_category, "wivrn") enum class server_state @@ -43,6 +47,33 @@ enum class server_state started = 1 }; +static QString server_path() +{ + return QCoreApplication::applicationDirPath() + "/wivrn-server"; +} + +#if WIVRN_CHECK_CAPSYSNICE +static bool server_has_cap_sys_nice() +{ + auto caps = cap_get_file(server_path().toStdString().c_str()); + + if (not caps) + return false; + + char * cap_text = cap_to_text(caps, nullptr); + qDebug() << "Server capabilities:" << cap_text; + cap_free(cap_text); + + cap_flag_value_t value{}; + if (cap_get_flag(caps, CAP_SYS_NICE, CAP_EFFECTIVE, &value) < 0) + return false; + + cap_free(caps); + + return value == CAP_SET; +} +#endif + main_window::main_window() { ui = new Ui::MainWindow; @@ -89,9 +120,8 @@ main_window::main_window() connect(server_interface, &wivrn_server::serverRunningChanged, this, &main_window::on_server_running_changed); connect(server_interface, &wivrn_server::headsetConnectedChanged, this, &main_window::on_headset_connected_changed); - if (server_interface->isServerRunning()) - on_server_dbus_registered(); - else + on_server_running_changed(server_interface->isServerRunning()); + if (not server_interface->isServerRunning()) start_server(); connect(server_interface, &wivrn_server::recommendedEyeSizeChanged, this, &main_window::on_recommended_eye_size_changed); @@ -127,6 +157,17 @@ main_window::main_window() connect(&adb_service, &adb::android_devices_changed, this, &main_window::on_android_device_list_changed); on_android_device_list_changed(adb_service.devices()); +#if WIVRN_CHECK_CAPSYSNICE + QIcon icon = QIcon::fromTheme(QIcon::ThemeIcon::DialogInformation); + ui->banner_capsysnice_icon->setPixmap(icon.pixmap(ui->banner_capsysnice_dismiss->height())); + connect(ui->banner_capsysnice_text, &QLabel::linkActivated, this, &main_window::on_banner_capsysnice); + + if (server_has_cap_sys_nice()) + ui->banner_capsysnice->hide(); +#else + ui->banner_capsysnice->hide(); +#endif + retranslate(); } @@ -284,11 +325,108 @@ void main_window::on_android_device_list_changed(const std::vector } } +void main_window::on_banner_capsysnice(const QString & link) +{ +#if WIVRN_CHECK_CAPSYSNICE + if (link == "setcap" and not setcap_process) + { + setcap_process = new QProcess(this); + setcap_process->setProgram("pkexec"); + setcap_process->setArguments({"setcap", "CAP_SYS_NICE=+ep", server_path()}); + setcap_process->setProcessChannelMode(QProcess::MergedChannels); + setcap_process->start(); + + connect(setcap_process, &QProcess::finished, this, [this](int exit_code, QProcess::ExitStatus exit_status) { + // Exit codes: + // 0: setcap successful + // 1: setcap failed + // 126: pkexec: not authorized or authentication error + // 127: pkexec: dismissed by user + qDebug() << "pkexec setcap exited with code" << exit_code << "and status" << exit_status; + + qDebug() << "--------"; + for (auto line: setcap_process->readAllStandardOutput().split('\n')) + { + QDebug dbg = qDebug(); + dbg.noquote(); + dbg << " " << QString::fromStdString(line.toStdString()); + } + qDebug() << "--------"; + + if (exit_status == QProcess::NormalExit) + { + if (exit_code == 0) + { + if (not server_has_cap_sys_nice()) + { + qDebug() << "pkexec setcap returned successfully but the server does not have the CAP_SYS_NICE capability"; + } + else + { + ui->banner_capsysnice->hide(); + + // Don't restart if it wasn't already started + if (server_interface) + { + QMessageBox msgbox; + msgbox.setIcon(QMessageBox::Information); + msgbox.setText(tr("You have to restart the WiVRn server to use the CAP_SYS_NICE capability.\nDo you want to restart it now?")); + + if (server_interface->isHeadsetConnected()) + { + msgbox.setText(msgbox.text() + "\n\n" + tr("This will disconnect the currently connected headset.")); + } + + auto restart_button = msgbox.addButton(tr("Restart WiVRn"), QMessageBox::YesRole); + auto close_button = msgbox.addButton(QMessageBox::Close); + +#ifdef WORKAROUND_QTBUG_90005 + setEnabled(false); +#endif + msgbox.exec(); +#ifdef WORKAROUND_QTBUG_90005 + setEnabled(true); +#endif + + if (msgbox.clickedButton() == restart_button) + { + server_process_restart = true; + stop_server(); + } + } + } + } + } + else + { + QMessageBox msgbox; + msgbox.setIcon(QMessageBox::Critical); + msgbox.setText(tr("Cannot start setcap: %1").arg(exit_status)); + + auto close_button = msgbox.addButton(QMessageBox::Close); + +#ifdef WORKAROUND_QTBUG_90005 + setEnabled(false); +#endif + msgbox.exec(); +#ifdef WORKAROUND_QTBUG_90005 + setEnabled(true); +#endif + } + + setcap_process->deleteLater(); + setcap_process = nullptr; + }); + } +#endif +} + void main_window::on_server_running_changed(bool running) { if (running) { - on_server_dbus_registered(); + qDebug() << "Server started"; + server_process_timeout->stop(); ui->stacked_widget_server->setCurrentIndex((int)server_state::started); ui->group_client->setEnabled(true); @@ -298,6 +436,7 @@ void main_window::on_server_running_changed(bool running) } else { + qDebug() << "Server stopped"; ui->stacked_widget_server->setCurrentIndex((int)server_state::stopped); ui->group_client->setEnabled(false); @@ -309,10 +448,12 @@ void main_window::on_headset_connected_changed(bool connected) { if (connected) { + qDebug() << "Headset connected"; ui->label_client_status->setText(tr("Connected")); } else { + qDebug() << "Headset disconnected"; ui->label_client_status->setText(tr("Not connected")); } @@ -408,13 +549,10 @@ void main_window::on_steam_command_changed(QString value) ui->label_steam_command->setText(value); } -void main_window::on_server_dbus_registered() -{ - server_process_timeout->stop(); -} - void main_window::on_server_finished(int exit_code, QProcess::ExitStatus status) { + qDebug() << "Server exited with code" << exit_code << ", status" << status; + disconnect(server_process, &QProcess::finished, this, &main_window::on_server_finished); disconnect(server_process, &QProcess::errorOccurred, this, &main_window::on_server_error_occurred); server_process->deleteLater(); @@ -451,6 +589,12 @@ void main_window::on_server_finished(int exit_code, QProcess::ExitStatus status) setEnabled(true); #endif } + + if (server_process_restart) + { + server_process_restart = false; + start_server(); + } } void main_window::on_server_error_occurred(QProcess::ProcessError error) @@ -581,6 +725,7 @@ void main_window::on_action_usb(const std::string & serial) return; // TODO: in another thread + // TODO: check if adb works bool ok = false; for (auto & app: dev.installed_apps()) { @@ -614,6 +759,8 @@ void main_window::on_action_usb(const std::string & serial) void main_window::start_server() { + qDebug() << "Starting server"; + // TODO activate by dbus? server_process = new QProcess; connect(server_process, &QProcess::finished, this, &main_window::on_server_finished); @@ -621,7 +768,7 @@ void main_window::start_server() server_process->setProcessChannelMode(QProcess::ForwardedChannels); - server_process->start(QCoreApplication::applicationDirPath() + "/wivrn-server"); + server_process->start(server_path()); server_process_timeout->start(); ui->button_start->setEnabled(false); @@ -629,6 +776,8 @@ void main_window::start_server() void main_window::stop_server() { + qDebug() << "Stopping server"; + if (server_interface) server_interface->quit() /*.waitForFinished()*/; // server_process.terminate(); // TODO timer and kill @@ -637,6 +786,8 @@ void main_window::stop_server() void main_window::disconnect_client() { + qDebug() << "Disconnecting client"; + if (server_interface) server_interface->disconnect_headset(); } diff --git a/dashboard/main_window.h b/dashboard/main_window.h index 1a9cf63e..3272c013 100644 --- a/dashboard/main_window.h +++ b/dashboard/main_window.h @@ -62,12 +62,15 @@ class main_window : public QMainWindow QProcess * server_process = nullptr; QTimer * server_process_timeout = nullptr; + bool server_process_restart = false; QMenu * usb_device_menu; std::map> usb_actions; adb adb_service; + QProcess * setcap_process = nullptr; + public: main_window(); ~main_window(); @@ -78,12 +81,12 @@ public: void setVisible(bool visible) override; private: - void on_server_dbus_registered(); void on_server_finished(int exit_code, QProcess::ExitStatus status); void on_server_error_occurred(QProcess::ProcessError error); void on_server_start_timeout(); void on_button_details_toggled(bool); + void on_banner_capsysnice(const QString & link); void on_server_running_changed(bool running); void on_headset_connected_changed(bool connected); diff --git a/dashboard/main_window.ui b/dashboard/main_window.ui index 6f0b5873..5bef1108 100644 --- a/dashboard/main_window.ui +++ b/dashboard/main_window.ui @@ -7,7 +7,7 @@ 0 0 898 - 796 + 757 @@ -18,7 +18,45 @@ :/images/wivrn.svg:/images/wivrn.svg - + + + + + #banner_capsysnice +{ +background-color: rgb(255, 170, 127); +border: 1px solid rgb(255, 127, 127); +border-radius: 5px; +} + + + QFrame::Shape::Box + + + + + + + + + + + + + <html><head/><body><p>The server does not have CAP_SYS_NICE capabilities. Click <a href="setcap"><span style=" text-decoration: underline; color:#2980b9;">here</span></a> to grant it.</p></body></html> + + + + + + + + + + + + + @@ -514,7 +552,23 @@ - - + + + banner_capsysnice_dismiss + clicked() + banner_capsysnice + hide() + + + 837 + 31 + + + 799 + 54 + + + + diff --git a/flatpak/io.github.wivrn.wivrn.yml.in b/flatpak/io.github.wivrn.wivrn.yml.in index 5218f338..13f2a83b 100644 --- a/flatpak/io.github.wivrn.wivrn.yml.in +++ b/flatpak/io.github.wivrn.wivrn.yml.in @@ -156,6 +156,7 @@ modules: - -DWIVRN_USE_NVENC=ON - -DWIVRN_USE_PULSEAUDIO=OFF - -DWIVRN_WERROR=ON + - -DWIVRN_CHECK_CAPSYSNICE=OFF # Capabilities don't work inside flatpak - -DFETCHCONTENT_FULLY_DISCONNECTED=ON - -DFETCHCONTENT_BASE_DIR=deps - -DGIT_DESC=WIVRN_GIT_DESC