Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess: run app via sudo
QtWS25 Last Chance

QProcess: run app via sudo

Scheduled Pinned Locked Moved Solved General and Desktop
qprocesssudo
9 Posts 3 Posters 3.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    debian
    wrote on 6 Apr 2022, 11:57 last edited by
    #1

    Hello!
    I should execute several command which require privileged access from my app which run as non-privileged.
    For this I was add command to /etc/sudoerc and allow run it without password, command work if I run it at shell

    ~ $ sudo ping -c 3 ya.ru
    PING ya.ru (87.250.250.242): 56 data bytes
    64 bytes from 87.250.250.242: seq=0 ttl=249 time=9.118 ms
    64 bytes from 87.250.250.242: seq=1 ttl=249 time=8.910 ms
    64 bytes from 87.250.250.242: seq=2 ttl=249 time=8.949 ms
    
    --- ya.ru ping statistics ---
    3 packets transmitted, 3 packets received, 0% packet loss
    round-trip min/avg/max = 8.910/8.992/9.118 ms
    

    Now I add QProcess to my app

    QProcess pingProcess;
    pingProcess.start("/usr/bin/sudo",{"/bin/ping -c 5 ya.ru"});
    

    Unfortunately this is not work, after run app I see Password: at console output, at app finish console output contain follow lines

    ...............
    QProcess: Destroyed while process ("/usr/bin/sudo") is still running.
    error ping "sudo: unable to mkdir /run: Read-only file system\nsudo: a password is required\n"
    ................
    

    and sudo log contain line command not allowed, look like what sudo can't found or can't read /etc/sudoerc.
    How should I correct use QProcess for run externals app via sudo ?
    Thank you.

    K J 2 Replies Last reply 6 Apr 2022, 12:03
    0
    • D debian
      6 Apr 2022, 11:57

      Hello!
      I should execute several command which require privileged access from my app which run as non-privileged.
      For this I was add command to /etc/sudoerc and allow run it without password, command work if I run it at shell

      ~ $ sudo ping -c 3 ya.ru
      PING ya.ru (87.250.250.242): 56 data bytes
      64 bytes from 87.250.250.242: seq=0 ttl=249 time=9.118 ms
      64 bytes from 87.250.250.242: seq=1 ttl=249 time=8.910 ms
      64 bytes from 87.250.250.242: seq=2 ttl=249 time=8.949 ms
      
      --- ya.ru ping statistics ---
      3 packets transmitted, 3 packets received, 0% packet loss
      round-trip min/avg/max = 8.910/8.992/9.118 ms
      

      Now I add QProcess to my app

      QProcess pingProcess;
      pingProcess.start("/usr/bin/sudo",{"/bin/ping -c 5 ya.ru"});
      

      Unfortunately this is not work, after run app I see Password: at console output, at app finish console output contain follow lines

      ...............
      QProcess: Destroyed while process ("/usr/bin/sudo") is still running.
      error ping "sudo: unable to mkdir /run: Read-only file system\nsudo: a password is required\n"
      ................
      

      and sudo log contain line command not allowed, look like what sudo can't found or can't read /etc/sudoerc.
      How should I correct use QProcess for run externals app via sudo ?
      Thank you.

      K Offline
      K Offline
      KroMignon
      wrote on 6 Apr 2022, 12:03 last edited by KroMignon 4 Jun 2022, 12:06
      #2

      @debian said in QProcess: run app via sudo:

      QProcess pingProcess;
      pingProcess.start("/usr/bin/sudo",{"/bin/ping -c 5 ya.ru"});
      

      This is the wrong way to do, each parameter should be an entry in the QStringList.
      This should work:

      QProcess pingProcess;
      pingProcess.start("/usr/bin/sudo", QStringList() << "/bin/ping" << "-c" << "5" << "ya.ru")
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      D 1 Reply Last reply 6 Apr 2022, 12:32
      5
      • K KroMignon
        6 Apr 2022, 12:03

        @debian said in QProcess: run app via sudo:

        QProcess pingProcess;
        pingProcess.start("/usr/bin/sudo",{"/bin/ping -c 5 ya.ru"});
        

        This is the wrong way to do, each parameter should be an entry in the QStringList.
        This should work:

        QProcess pingProcess;
        pingProcess.start("/usr/bin/sudo", QStringList() << "/bin/ping" << "-c" << "5" << "ya.ru")
        
        D Offline
        D Offline
        debian
        wrote on 6 Apr 2022, 12:32 last edited by
        #3

        @KroMignon , thank you!
        Can you provide more details?

        K 2 Replies Last reply 6 Apr 2022, 12:38
        0
        • D debian
          6 Apr 2022, 12:32

          @KroMignon , thank you!
          Can you provide more details?

          K Offline
          K Offline
          KroMignon
          wrote on 6 Apr 2022, 12:38 last edited by
          #4

          @debian said in QProcess: run app via sudo:

          Can you provide more details?

          More than what is documentation?
          https://doc.qt.io/qt-5/qprocess.html#details

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          0
          • D debian
            6 Apr 2022, 12:32

            @KroMignon , thank you!
            Can you provide more details?

            K Offline
            K Offline
            KroMignon
            wrote on 6 Apr 2022, 12:46 last edited by KroMignon 4 Jul 2022, 06:21
            #5

            @debian said in QProcess: run app via sudo:

            Can you provide more details?

            One more think to note, as QProcess instance is on stack, you should use QProcess::execute() to wait until process is done:

            QProcess::execute("/usr/bin/sudo", QStringList() << "/bin/ping" << "-c" << "5" << "ya.ru")
            

            or do it yourself with waitForFinished(), so you could add a timeout:

            QProcess pingProcess;
            pingProcess.start("/usr/bin/sudo", QStringList() << "/bin/ping" << "-c" << "5" << "ya.ru")
            if(!pingProcess.waitForStarted(1000))
               qDebug() << "Could NOT start sudo!!";
            else if(!pingProcess.waitForFinished(20000))
               qDebug() << "Ping is not ending!!!";
            else
              qDebug() << "Done.";
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            1
            • D debian
              6 Apr 2022, 11:57

              Hello!
              I should execute several command which require privileged access from my app which run as non-privileged.
              For this I was add command to /etc/sudoerc and allow run it without password, command work if I run it at shell

              ~ $ sudo ping -c 3 ya.ru
              PING ya.ru (87.250.250.242): 56 data bytes
              64 bytes from 87.250.250.242: seq=0 ttl=249 time=9.118 ms
              64 bytes from 87.250.250.242: seq=1 ttl=249 time=8.910 ms
              64 bytes from 87.250.250.242: seq=2 ttl=249 time=8.949 ms
              
              --- ya.ru ping statistics ---
              3 packets transmitted, 3 packets received, 0% packet loss
              round-trip min/avg/max = 8.910/8.992/9.118 ms
              

              Now I add QProcess to my app

              QProcess pingProcess;
              pingProcess.start("/usr/bin/sudo",{"/bin/ping -c 5 ya.ru"});
              

              Unfortunately this is not work, after run app I see Password: at console output, at app finish console output contain follow lines

              ...............
              QProcess: Destroyed while process ("/usr/bin/sudo") is still running.
              error ping "sudo: unable to mkdir /run: Read-only file system\nsudo: a password is required\n"
              ................
              

              and sudo log contain line command not allowed, look like what sudo can't found or can't read /etc/sudoerc.
              How should I correct use QProcess for run externals app via sudo ?
              Thank you.

              J Offline
              J Offline
              JonB
              wrote on 6 Apr 2022, 12:54 last edited by
              #6

              @debian
              For my own interest, why do you need sudo/"privileged access" to run ping -c 5 ya.ru?

              D 1 Reply Last reply 7 Apr 2022, 06:09
              0
              • J JonB
                6 Apr 2022, 12:54

                @debian
                For my own interest, why do you need sudo/"privileged access" to run ping -c 5 ya.ru?

                D Offline
                D Offline
                debian
                wrote on 7 Apr 2022, 06:09 last edited by
                #7

                @JonB , this is embedded system, I'm use busybox, so ping is not separate app and I can't use linux capabilities, this is simplink to busybox.

                J 1 Reply Last reply 7 Apr 2022, 08:09
                0
                • D debian
                  7 Apr 2022, 06:09

                  @JonB , this is embedded system, I'm use busybox, so ping is not separate app and I can't use linux capabilities, this is simplink to busybox.

                  J Offline
                  J Offline
                  JonB
                  wrote on 7 Apr 2022, 08:09 last edited by
                  #8

                  @debian OK, thanks for replying, embedded systems/"busybox"/"simplink" are quite beyond me! Under plain Linux you would not need sudo for ping.

                  D 1 Reply Last reply 7 Apr 2022, 11:09
                  0
                  • J JonB
                    7 Apr 2022, 08:09

                    @debian OK, thanks for replying, embedded systems/"busybox"/"simplink" are quite beyond me! Under plain Linux you would not need sudo for ping.

                    D Offline
                    D Offline
                    debian
                    wrote on 7 Apr 2022, 11:09 last edited by
                    #9
                    $ /usr/sbin/getcap /bin/ping
                    /bin/ping cap_net_raw=ep
                    

                    This work because /bin/ping have capabilites
                    https://man7.org/linux/man-pages/man7/capabilities.7.html

                    CAP_NET_RAW

                    • Use RAW and PACKET sockets;
                    • bind to any address for transparent proxying.
                    1 Reply Last reply
                    0

                    3/9

                    6 Apr 2022, 12:32

                    6 unread
                    • Login

                    • Login or register to search.
                    3 out of 9
                    • First post
                      3/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved