问题描述
我想使用 c> QProcess :
和
因此,以下代码段等于 command1 | command2 ,而不限制一个解释器或另一个:
QProcess process1
QProcess process2;
process1.setStandardOutputProcess(& process2);
process1.start(echo myPass);
process2.start(sudo -S shutdown -r now);
process2.setProcessChannelMode(QProcess :: ForwardedChannels);
//等待它启动
if(!process1.waitForStarted())
return 0;
bool retval = false;
QByteArray buffer;
//公平:你只需要在这里等待一个关闭,
//但我仍然会留下一个通用的解决方案
while((retval = process2 .waitForFinished()));
buffer.append(process2.readAll());
if(!retval){
qDebug()<< Process 2 error:<< process2.errorString();
return 1;
}
您可以删除 sudo -S 部分,因为你可以作为root运行这个小程序,以及设置权限。您甚至可以为关闭程序设置setuid或setcap。
在构建商业Linux系统时,我们通常会做的最小的应用程序可以获得setuid或setcap活动,然后我们在Linux上用 system(3)或 QProcess 显式调用它。基本上,
我会编写这个小应用程序,以避免给整个应用程序的完全根访问,所以限制访问权限对恶意使用,如下:
sudo chmod u + s / path / to / my / application
I want to restart the computer by running a command in linux using QProcess. I have hard-coded my root password in my application.
When i run the following in a terminal it works perfect:
echo myPass | sudo -S shutdown -r now
When i put the command in a shell script and call it via QProcess it is also successful :
QProcess process; process.startDetached("/bin/sh", QStringList()<< "myScript.sh");
But i can not run it by directly passing to QProcess:
process.startDetached("echo myPass | sudo -S shutdown -r now ");
It will just print myPass | sudo -S shutdown -r now
How is it possible to run such relatively complex commands directly using QProcess. (Not putting in a shell script).
The key methods that exist for this purpose established in QProcess:
and
Therefore, the following code snippet would be the equivalence of command1 | command2 without limiting yourself to one interpreter or another:
QProcess process1 QProcess process2; process1.setStandardOutputProcess(&process2); process1.start("echo myPass"); process2.start("sudo -S shutdown -r now"); process2.setProcessChannelMode(QProcess::ForwardedChannels); // Wait for it to start if(!process1.waitForStarted()) return 0; bool retval = false; QByteArray buffer; // To be fair: you only need to wait here for a bit with shutdown, // but I will still leave the rest here for a generic solution while ((retval = process2.waitForFinished())); buffer.append(process2.readAll()); if (!retval) { qDebug() << "Process 2 error:" << process2.errorString(); return 1; }
You could drop the sudo -S part because you could run this small program as root, as well as setting up the rights. You could even set setuid or setcap for the shutdown program.
What we usually do when building commercial Linux systems is to have a minimal application that can get setuid or setcap for the activity it is trying to do, and then we call that explicitly with system(3) or QProcess on Linux. Basically,
I would write that small application to avoid giving full root access to the whole application, so to restrict the access right against malicious use as follows:
sudo chmod u+s /path/to/my/application
这篇关于如何在Qt中执行复杂的linux命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!