Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get output stream from QProcess



  • Hello, this is my code:

        QProcess process;
        process.start("sudo apt update");
        process.waitForFinished(-1);
    
        QString stdout = process.readAllStandardOutput();
        QString stderr = process.readAllStandardError();
    

    I have 3 questions:

    • How can I get realtime stream of process output? Let's say that process will wait for user input,t hen it will not finish, but I would like to get output from that running process.
    • Can I send key to that process? For example process is waiting for Y or N press, so can I send "Y" key to it?
    • How can I run that process as sudo? Forexample when my app hit that code where is sudo command then user will have to write password to some system popup.

  • Lifetime Qt Champion

    @t0msk said in Get output stream from QProcess:

    How can I get realtime stream of process output?

    First: do not wait for process to finish.
    Connect a slot to https://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput
    In that slot call https://doc.qt.io/qt-5/qprocess.html#readAllStandardOutput

    "Can I send key to that process?" - yes:

    process.write("Y");
    

    See https://doc.qt.io/qt-5/qprocess.html for an example:

    QProcess gzip;
    gzip.start("gzip", QStringList() << "-c");
    if (!gzip.waitForStarted())
        return false;
    
    gzip.write("Qt rocks!");
    gzip.closeWriteChannel();
    
    if (!gzip.waitForFinished())
        return false;
    
    QByteArray result = gzip.readAll();
    


  • @t0msk said in Get output stream from QProcess:

    How can I run that process as sudo? Forexample when my app hit that code where is sudo command then user will have to write password to some system popup.

    After @jsulm's answers to the first two questions, it leaves this one.

    You can put sudo in front of the command. This gets asked many times, and I'm sorry but I've answered it so many times I'm not prepared to type in all the stuff again. Yes, you will have problem if your sudo requires password. Basically you need to man sudo for your target Linux(es). They have various options. there is/may be a -A for "ask password interactively", which would be nice, but it often does not work on distros. Or, there is/might be -S allowing you to pass the password (which you would prompt for in advance in Qt app) via stdin.

    Otherwise, I haven't tried this, but you might consider running some sort of xterm -e <command> so that the user gets a terminal where sudo can prompt if necessary. (Having said that, I see that Ubuntu 19 at least seems to have removed xterm, so I don't know what you could rely on).

    There is also polkit (Google for it) which can be set up to permit certain programs having sudo permission without password. Personally I thought it a lot of hassle for the administrator to have to set up, but you could read up on it and see if it suits you.



  • @jsulm thanks :)

    @JonB I was looking for polkit, and it seems to be over-complicated, I thought that it will be only some lines of code.

    Can be workaround that app will require to be started as sudo? Then if my application runs with sudo privileges then it means that I don't have to use sudo command and everything should work, right?



  • @t0msk
    polkit requires the administrator to set it up to allow a program through. It's not great for "one-off"s.

    Yes, to be clear, if the user is prepared to invoke your whole program via sudo then it has those privileges throughout. So if it spawns a QProcess of apt update that will run as you intend.

    This is not usually the solution people are looking for when they ask about a sudo QProcess, as they do not expect/intend the user to run the whole Qt program sudo. But if that's is going to be OK with your users, it will solve your issue, Be especially careful about what your code does, as everything it does will be sudo now! This could be an issue if you create files, and also you should check the desktop windowing system being used in case it does not like UI programs running under sudo.



  • @JonB said in Get output stream from QProcess:

    polkit requires the administrator to set it up to allow a program through. It's not great for "one-off"s

    So you mean that if user downloads my app then he have to set up polkit first and then he will be able to run my app and use "sudo functions"?



  • @t0msk
    I have never used polkit. But my understanding is first the system administrator would have to set up for polkit on the system. Then you supply, I believe, an XML file or something for your program to do the sudo command, and the system administrator then has to "incorporate" that into his polkit setup to authorize it. That would make sense conceptually to me. I stand it be corrected! As I said, I suggest you Google.



  • @JonB said in Get output stream from QProcess:

    @t0msk
    I have never used polkit. But my understanding is first the system administrator would have to set up for polkit on the system. Then you supply, I believe, an XML file or something for your program to do the sudo command, and the system administrator then has to "incorporate" that into his polkit setup to authorize it. That would make sense conceptually to me. I stand it be corrected! As I said, I suggest you Google.

    That approach is very bad for me, I just want that user downloads my app and runs it, and if needed he will be prompted for password.



  • @t0msk
    Which is why I originally wrote

    Personally I thought it a lot of hassle for the administrator to have to set up, but you could read up on it and see if it suits you.

    and

    polkit requires the administrator to set it up to allow a program through. It's not great for "one-off"s.

    :)

    I see this question asked frequently in this forum. The obvious question to me is: why does the apt-get need to be run sudo? If it does, it's best done by the admin user! Why does your app need to do it for the user?



  • @JonB it is only example, Im working on frontend app for some console only program, and that console program requires sudo.



  • @t0msk
    Then going back to what I suggested originally, have you checked out sudo -S ?



  • @JonB sudo -A doesn't work for my by default and -S what does it mean? I read manpage that it will read password from stdin, but Im working on GUI app, how can I show popup "Enter password" and then pass it to that process?



  • @t0msk
    So your Qt GUI will get the password from the user once. Then you will invoke sudo -S .... You will send the password to it via QProcess::write(). That will be connected to sub-process's stdin. Similar to the way that you can read from sub-process's stout/stderr in the code we have been discussing. Look back up at @jsulm's post. Have a read through the QProcess doc page.



  • @t0msk I ran into this on a PHP web app I wrote once where I needed to control some system services. My VERY dangerous approach was to modify the /etc/sudoers file to allow a certain user to run certain programs without a sudo password. Again this was VERY dangerous but I assumed the risk.

    I added a line in my sudoers file like this...

    <username>  ALL=(ALL:ALL) NOPASSWD:<comma separated list of programs that I wanted to run without a password>
    
    

    Again, this is a VERY dangerous approach but I assumed the risks by running the program as a user that had no login shell. The chances of exploitation was very small.

    As far as the need to communicating with the process I'm sure you come up with some sort of non blocking or interactive mode for QProcess. As @jsulm said read up and maybe try communicating via the channels of QProcess.

    Chris~



  • @Chrisw01
    One can run with NOPASSWD, and indeed I do on my development machine where I am the only user. If the application is only to be run on his own machine this might be acceptable. However, for the OP to make his end user sites change over to no root password, admittedly only for apt-get, seems inappropriate. It also requires setup from the system administrator. I would not dream of accepting software with this! Again, I would ask why his application requires a sudo apt-get rather than just an apt-get?

    As I have noted above, you can run sudo with a password from a UI by using the -S option. If you are going to write code which uses channel communication with QProcess anyway you are already halfway there to what is needed.



  • @JonB Agreed, which is why all the warnings of it being dangerous. And only control of that was given to a specific user, not the whole system. It fit my needs...

    @t0msk I think that @JonB has hit the nail on the head. Run sudo with the -S parameter. And use channels to read and write to and from stdin/stdout/stderr as required.

    From the sudo man page:

         -S, --stdin
            Write the prompt to the standard error and read the password from the standard input instead of using the terminal device.  The password must be followed by a newline character.
    

    Oh and one more thing to keep in mind, not all distros of Linux install sudo by default. Debian server for instance requires you to use the su program or manually install sudo. Some distro's only allow sudo to the admin or users in the sudoers group but not to regular users. So some adjustments may be required.



  • @JonB I need to use sudo with apt you can look here:

    tomsk@tomsk-Ntb:~$ apt update
    Reading package lists... Done
    E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
    E: Unable to lock directory /var/lib/apt/lists/
    W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
    W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)
    

    So if I understand correctly, if user will want to do something which requires sudo, then I will show QDialog with lineEdit where he enters password and then I will call QProcess with sudo -S and after that I use QProcess::write() where I insert his password?

    I just downloaded application which requires administration rights from Linux repository and if I launch it, it looks like this:

    PolicyKit 1

    As you can see it uses PolicyKit 1 and I didn't have to set up polkit on my system as you said (that xml what you mentioned), sorry but I am confused.

    IT'S working now I used pkexec apt update.



  • @t0msk

    • You only need to use sudo because you are going apt update, which does a system-wide update of stuff. Why does your app need that? That's my point.

    • Yes to what you wrote about sudo -S.

    • Like I said I have never used polkit so it's up to you to read up. You say "and I didn't have to set up polkit on my system", but isn't the point that you are being prompted for a password here where the point of using polkit is to allow stuff through without requiring a password? So it seems to me it is not currently set up to allow whatever you are trying through without a password, and an admin would need to configure it to make it allow no password here?



  • @JonB said in Get output stream from QProcess:

    @t0msk

    • You only need to use sudo because you are going apt update, which does a system-wide update of stuff. Why does your app need that? That's my point.

    • Yes to what you wrote about sudo -S.

    • Like I said I have never used polkit so it's up to you to read up. You say "and I didn't have to set up polkit on my system", but isn't the point that you are being prompted for a password here where the point of using polkit is to allow stuff through without requiring a password? So it seems to me it is not currently set up to allow whatever you are trying through without a password, and an admin would need to configure it to make it allow no password here?

    As I said many times apt update is only example, and if you use pkexec it will show system popup like on screenshot in my last post, then you have to enter password and after that command will be executed.


  • Lifetime Qt Champion

    @t0msk said in Get output stream from QProcess:

    As I said many times apt update is only example

    @JonB understands that "apt update" is just an example, he actually wanted to know why your app needs to be executed as root...



  • @t0msk

    As I said many times apt update is only example, and if you use pkexec it will show system popup like on screenshot ...

    @jsulm has leapt to my defence :) And as I have said many times, my question is not about apt update but about why you need to run it or anything else sudo from your program? This equally applies if you go pkexec (though the whole point of that is it's part of polkit, and the point of polkit is that you are supposed to configure it so that it does not "show system popup like on screenshot").


Log in to reply