fix: linux cap test with libcap-progs at /usr/sbin (gh#764)

Signed-off-by: xtex <xtexchooser@duck.com>
This commit is contained in:
xtex
2023-10-14 21:58:25 +08:00
parent 991ac66634
commit 0175f5d456
2 changed files with 19 additions and 2 deletions

View File

@@ -1,12 +1,14 @@
#include "LinuxCap.h" #include "LinuxCap.h"
#include <QDebug>
#include <QProcess> #include <QProcess>
#include <QStandardPaths>
#define EXIT_CODE(p) (p.exitStatus() == QProcess::NormalExit ? p.exitCode() : -1) #define EXIT_CODE(p) (p.exitStatus() == QProcess::NormalExit ? p.exitCode() : -1)
QString Linux_GetCapString(const QString &path) { QString Linux_GetCapString(const QString &path) {
QProcess p; QProcess p;
p.setProgram("getcap"); p.setProgram(Linux_FindCapProgsExec("getcap"));
p.setArguments({path}); p.setArguments({path});
p.start(); p.start();
p.waitForFinished(500); p.waitForFinished(500);
@@ -16,7 +18,7 @@ QString Linux_GetCapString(const QString &path) {
int Linux_Pkexec_SetCapString(const QString &path, const QString &cap) { int Linux_Pkexec_SetCapString(const QString &path, const QString &cap) {
QProcess p; QProcess p;
p.setProgram("pkexec"); p.setProgram("pkexec");
p.setArguments({"setcap", cap, path}); p.setArguments({Linux_FindCapProgsExec("setcap"), cap, path});
p.start(); p.start();
p.waitForFinished(-1); p.waitForFinished(-1);
return EXIT_CODE(p); return EXIT_CODE(p);
@@ -31,3 +33,16 @@ bool Linux_HavePkexec() {
p.waitForFinished(500); p.waitForFinished(500);
return EXIT_CODE(p) == 0; return EXIT_CODE(p) == 0;
} }
QString Linux_FindCapProgsExec(const QString &name) {
QString exec = QStandardPaths::findExecutable(name);
if (exec.isEmpty())
exec = QStandardPaths::findExecutable(name, {"/usr/sbin", "/sbin"});
if (exec.isEmpty())
qDebug() << "Executable" << name << "could not be resolved";
else
qDebug() << "Found exec" << name << "at" << exec;
return exec.isEmpty() ? name : exec;
}

View File

@@ -7,3 +7,5 @@ QString Linux_GetCapString(const QString &path);
int Linux_Pkexec_SetCapString(const QString &path, const QString &cap); int Linux_Pkexec_SetCapString(const QString &path, const QString &cap);
bool Linux_HavePkexec(); bool Linux_HavePkexec();
QString Linux_FindCapProgsExec(const QString &name);