How to execute complex linux commands in Qt?

I want to restart my computer by running a command in linux using QProcess . I have a hard password of my root in my application.

When I run the following in the terminal, it works fine:

 echo myPass | sudo -S shutdown -r now 

When I put the command in a shell script and QProcess it through QProcess , it is also successful:

 QProcess process; process.startDetached("/bin/sh", QStringList()<< "myScript.sh"); 

But I can not start it by going directly to QProcess :

 process.startDetached("echo myPass | sudo -S shutdown -r now "); 

It just prints myPass | sudo -S shutdown -r now myPass | sudo -S shutdown -r now

How can you run such relatively complex commands directly using QProcess . (Do not put shell script).

+7
c ++ linux shell qt qprocess
source share
3 answers

The main methods that exist for this purpose are installed in QProcess :

void QProcess :: setProcessChannelMode (mode ProcessChannelMode)

and

void QProcess :: setStandardOutputProcess (QProcess * destination)

Therefore, the following code fragment will be the equivalent of command1 | command2 command1 | command2 , not limited to one translator:

 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 can refuse the sudo -S part, because you can run this small program with root privileges, as well as configure the rights. You can even set setuid or setcap for a shutdown program.

What we usually do when creating commercial Linux systems is to have a minimal application that can get setuid or setcap for the activity that it is trying to execute, and then we call it explicitly using system(3) or QProcess in Linux Mostly,

I would write this small application to avoid full root access to the entire application, so to limit access rights against malicious use:

 sudo chmod u+s /path/to/my/application 
+9
source share

You can configure sudo to avoid asking for a password. For example, being a member of the sudo group and has the line

  %sudo ALL=NOPASSWD: ALL 

in your /etc/sudoers file. Of course, without asking for a password, it reduces the security of your system.

To answer your question about Qt, remember that bash (1) , like all Posix shells, hence /bin/sh , accept the -c argument with a string (in fact, system (3) expands /bin/sh -c ). Therefore just follow

  process.startDetached("/bin/sh", QStringList()<< "-c" << "echo myPass | sudo -S shutdown -r now"); 

As AntiClimacus replied , setting a root password in an executable is a bad idea.

+2
source share

You must put your command in a shell script and execute sh or bash using QProcess with your shell script as an argument, because your command contains | which should be interpreted by sh or bash .

However, this is just my opinion, but: I do not think that this is a good solution for doing what you are doing, that is, specify your root password in the executable file.

+1
source share

All Articles