Home > Blockchain >  Trying to perform setcap from Qt program
Trying to perform setcap from Qt program

Time:05-05

I'm trying to perform setcap from Qt program this way:

QProcess process;
QString command = "cat";
QStringList args;
args << _fileName;

process.start(command, args);
process.waitForFinished();

QString StdOut   = process.readAllStandardOutput();
QString StdError = process.readAllStandardError();
QString err      = process.errorString();



QProcess process_2;
command = "setcap";
args.clear();
args << "cap_kill=ep" << _fileName;

process_2.start(command, args);
process_2.waitForFinished();

StdOut   = process_2.readAllStandardOutput();
StdError = process_2.readAllStandardError();
err      = process_2.errorString();

As _fileName I use value from QFileSystemModel, in my case it looks like "/home/ekaterina/example".

The first part (which was written just to test path correctness) works fine and puts file content into StdOut. I expect the second part to return something like "operation is not permitted" as QtCreator is run its projects not as root. But I get "execvp: No such file or directory" in err string. How is that?

When I'm trying to run project executable with "cap_setfcap" capability I get exactly the same result.

CodePudding user response:

The best thing to do, as @G.M. suggests, is to provide the full path to the binary. You can find setcap's location on your system with:

$ sudo which setcap

On Debian and Fedora, that returns /usr/sbin/setcap. On your system it might also be /sbin/setcap. Then embed that string in your program explicitly:

command = "/sbin/setcap";
  • Related