Check if the server has CAP_SYS_NICE, allow user to grant it

This commit is contained in:
Guillaume Meunier
2024-10-11 17:54:52 +02:00
parent 18b8f549ad
commit 6baeceb74a
7 changed files with 234 additions and 16 deletions
+6
View File
@@ -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)
+3
View File
@@ -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})
+1 -1
View File
@@ -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
+161 -10
View File
@@ -35,6 +35,10 @@
#include "wivrn_server.h"
#include "wizard.h"
#if WIVRN_CHECK_CAPSYSNICE
#include <sys/capability.h>
#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<adb::device>
}
}
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();
}
+4 -1
View File
@@ -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<std::string, std::unique_ptr<QAction>> 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);
+58 -4
View File
@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>898</width>
<height>796</height>
<height>757</height>
</rect>
</property>
<property name="windowTitle">
@@ -18,7 +18,45 @@
<normaloff>:/images/wivrn.svg</normaloff>:/images/wivrn.svg</iconset>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="0,0,1,0">
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="0,0,0,1,0">
<item>
<widget class="QFrame" name="banner_capsysnice">
<property name="styleSheet">
<string notr="true">#banner_capsysnice
{
background-color: rgb(255, 170, 127);
border: 1px solid rgb(255, 127, 127);
border-radius: 5px;
}</string>
</property>
<property name="frameShape">
<enum>QFrame::Shape::Box</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_9" stretch="0,1,0">
<item>
<widget class="QLabel" name="banner_capsysnice_icon">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="banner_capsysnice_text">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;The server does not have CAP_SYS_NICE capabilities. Click &lt;a href=&quot;setcap&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#2980b9;&quot;&gt;here&lt;/span&gt;&lt;/a&gt; to grant it.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="banner_capsysnice_dismiss">
<property name="icon">
<iconset theme="QIcon::ThemeIcon::WindowClose"/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="group_server">
<property name="title">
@@ -514,7 +552,23 @@
</widget>
<resources>
<include location="../wivrn-dashboard.qrc"/>
<include location="../wivrn-dashboard.qrc"/>
</resources>
<connections/>
<connections>
<connection>
<sender>banner_capsysnice_dismiss</sender>
<signal>clicked()</signal>
<receiver>banner_capsysnice</receiver>
<slot>hide()</slot>
<hints>
<hint type="sourcelabel">
<x>837</x>
<y>31</y>
</hint>
<hint type="destinationlabel">
<x>799</x>
<y>54</y>
</hint>
</hints>
</connection>
</connections>
</ui>
+1
View File
@@ -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