Skip to content

Commit

Permalink
fix: linux cap test with libcap-progs at /usr/sbin (gh#764)
Browse files Browse the repository at this point in the history
Signed-off-by: xtex <[email protected]>
  • Loading branch information
xtexx committed Oct 14, 2023
1 parent 991ac66 commit 0175f5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 17 additions & 2 deletions sys/linux/LinuxCap.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "LinuxCap.h"

#include <QDebug>
#include <QProcess>
#include <QStandardPaths>

#define EXIT_CODE(p) (p.exitStatus() == QProcess::NormalExit ? p.exitCode() : -1)

QString Linux_GetCapString(const QString &path) {
QProcess p;
p.setProgram("getcap");
p.setProgram(Linux_FindCapProgsExec("getcap"));
p.setArguments({path});
p.start();
p.waitForFinished(500);
Expand All @@ -16,7 +18,7 @@ QString Linux_GetCapString(const QString &path) {
int Linux_Pkexec_SetCapString(const QString &path, const QString &cap) {
QProcess p;
p.setProgram("pkexec");
p.setArguments({"setcap", cap, path});
p.setArguments({Linux_FindCapProgsExec("setcap"), cap, path});
p.start();
p.waitForFinished(-1);
return EXIT_CODE(p);
Expand All @@ -31,3 +33,16 @@ bool Linux_HavePkexec() {
p.waitForFinished(500);
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;
}
2 changes: 2 additions & 0 deletions sys/linux/LinuxCap.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ QString Linux_GetCapString(const QString &path);
int Linux_Pkexec_SetCapString(const QString &path, const QString &cap);

bool Linux_HavePkexec();

QString Linux_FindCapProgsExec(const QString &name);

0 comments on commit 0175f5d

Please sign in to comment.