0

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.

Ekaterina
  • 5
  • 2
  • Is `setcap` on your path? Can you run the desired command in a terminal? – G.M. Apr 28 '22 at 08:58
  • @G.M., yes, everything works fine from terminal (with sudo of course) – Ekaterina Apr 28 '22 at 09:02
  • And you're running your application via `sudo` (or as `root`)? – G.M. Apr 28 '22 at 09:08
  • @G.M., I've tried both options - set "chmod u+s" and "sudo setcap cap_setfcap executable", nothing helps – Ekaterina Apr 28 '22 at 09:12
  • Take care. Under `sudo` `$PATH` might differ from what a regular user has. Compare `env | grep -E '^PATH'` with `sudo env | grep -E '^PATH'`. It is quite likely the `sudo` version looks in a directory a regular user doesn't. ` – Andrew G Morgan Apr 29 '22 at 04:52
  • @AndrewGMorgan, yes, they are different. Is there any way I can fix this? – Ekaterina Apr 29 '22 at 07:06
  • Have you tried just providing the full path to `setcap`? Usually `/usr/sbin/setcap` or just `/sbin/setcap`. – G.M. Apr 29 '22 at 07:44
  • @G.M., yes, that's it, thanks! Could you write your reply as an answer to close the question? – Ekaterina Apr 30 '22 at 11:25

1 Answers1

0

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";
Tinkerer
  • 865
  • 7
  • 9