Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Using /bin/sh "partially " works

Using /bin/sh "partially " works

Scheduled Pinned Locked Moved C / C++ / MFC
linuxhelp
4 Posts 4 Posters 11 Views 1 Watching
  • 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    OK, another inherited code and I am not sure how it works... I am using this to process "system commands" - Linux. It works as expected with command = "hcitool dev" now I need to use "sudo hcitool info (address) " and it fails it also fails with just "sudo" The failing code is this :

                  if(QP->waitForReadyRead() | QP->waitForFinished())
                    {
    

    #ifdef TASK_PROCESS_COMMAND
    text = "SUCCESS waitForReadyRead ";
    text += " real time ";
    text += " elapsed time ";
    text += QString::number(QET->elapsed());
    text += " mS ";
    qDebug() << text;
    #endif
    }
    else
    {
    text = "FAILED waitForReadyRead ";
    text += " real time ";
    text += " elapsed time ";
    text += QString::number(QET->elapsed());
    text += " mS ";
    qDebug() << text;
    }

    Here is my passed to /bin/sh code

    QP->start("/bin/sh", QStringList() << "-c" << command);

    The way I interpret the error there is no response to "sudo" and I do not understand why.

    Mircea NeacsuM K J 3 Replies Last reply
    0
    • L Lost User

      OK, another inherited code and I am not sure how it works... I am using this to process "system commands" - Linux. It works as expected with command = "hcitool dev" now I need to use "sudo hcitool info (address) " and it fails it also fails with just "sudo" The failing code is this :

                    if(QP->waitForReadyRead() | QP->waitForFinished())
                      {
      

      #ifdef TASK_PROCESS_COMMAND
      text = "SUCCESS waitForReadyRead ";
      text += " real time ";
      text += " elapsed time ";
      text += QString::number(QET->elapsed());
      text += " mS ";
      qDebug() << text;
      #endif
      }
      else
      {
      text = "FAILED waitForReadyRead ";
      text += " real time ";
      text += " elapsed time ";
      text += QString::number(QET->elapsed());
      text += " mS ";
      qDebug() << text;
      }

      Here is my passed to /bin/sh code

      QP->start("/bin/sh", QStringList() << "-c" << command);

      The way I interpret the error there is no response to "sudo" and I do not understand why.

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      This is clearly a nix question, not a C/C++ one, but since you asked: Try executing from a terminal a command like:

      sudo ls

      and see what happens. You will probably be prompted to enter a sudo password. Obviously that cannot happen if you issue the command from a shell without an input file. The whole purpose of sudo command is to allow only certain users (sudo-ers) to change important settings. The way it verifies that, is by asking for password. Password is cached for a few minutes. You can find many more details in the sudo man page[^].

      Mircea

      1 Reply Last reply
      0
      • L Lost User

        OK, another inherited code and I am not sure how it works... I am using this to process "system commands" - Linux. It works as expected with command = "hcitool dev" now I need to use "sudo hcitool info (address) " and it fails it also fails with just "sudo" The failing code is this :

                      if(QP->waitForReadyRead() | QP->waitForFinished())
                        {
        

        #ifdef TASK_PROCESS_COMMAND
        text = "SUCCESS waitForReadyRead ";
        text += " real time ";
        text += " elapsed time ";
        text += QString::number(QET->elapsed());
        text += " mS ";
        qDebug() << text;
        #endif
        }
        else
        {
        text = "FAILED waitForReadyRead ";
        text += " real time ";
        text += " elapsed time ";
        text += QString::number(QET->elapsed());
        text += " mS ";
        qDebug() << text;
        }

        Here is my passed to /bin/sh code

        QP->start("/bin/sh", QStringList() << "-c" << command);

        The way I interpret the error there is no response to "sudo" and I do not understand why.

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #3

        We keep telling you, QT is not within the scope of this forum. If you're going to continue posting questions, at least rephrase them as non QT C++ As such, you could rewrite your sudo problem as follows

        #include

        int main()
        {
        system("sudo ps");
        }

        Now, either you'll have sudo set up to allow you to run commands without a password, in which case you should get a process list back, or you will be prompted for a password. My guess is that sudo is trying to prompt for a password, but whatever QT is doing, you never see the password prompt. Or maybe if you look at an attached terminal or wherever stdout gets sent to, you'll find the password prompt there. I'd suggest you compile this very simple program and try it from a command line. If it prompts for a password, then you know what the issue is. If it works, then you probably need to ask on a QT forum, as it would seem the issue is something that QT is doing. For all we know QT intercepts sudo requests and handles them differently. Note that the parameter to the system call is just "sudo ps", you don't need to specify the shell to use, sudo will use some default shell, probably the shell named in /etc/passwd as the user's shell. But there may be a way to override that. I'm just feeling too lazy to dig into the sudo docs :) Addionally, assuming you need the output from hci info, the system() call is not a good choice. To capture the output, you'd need to redirect stdout to a file, then read that file back in to examine the output. Much better would be to use popen(). Or since you're using QT, then maybe QProcess would be a better fit. There's a quick example of how to do this here : [https://stackoverflow.com/a/19413789\](https://stackoverflow.com/a/19413789)

        "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

        1 Reply Last reply
        0
        • L Lost User

          OK, another inherited code and I am not sure how it works... I am using this to process "system commands" - Linux. It works as expected with command = "hcitool dev" now I need to use "sudo hcitool info (address) " and it fails it also fails with just "sudo" The failing code is this :

                        if(QP->waitForReadyRead() | QP->waitForFinished())
                          {
          

          #ifdef TASK_PROCESS_COMMAND
          text = "SUCCESS waitForReadyRead ";
          text += " real time ";
          text += " elapsed time ";
          text += QString::number(QET->elapsed());
          text += " mS ";
          qDebug() << text;
          #endif
          }
          else
          {
          text = "FAILED waitForReadyRead ";
          text += " real time ";
          text += " elapsed time ";
          text += QString::number(QET->elapsed());
          text += " mS ";
          qDebug() << text;
          }

          Here is my passed to /bin/sh code

          QP->start("/bin/sh", QStringList() << "-c" << command);

          The way I interpret the error there is no response to "sudo" and I do not understand why.

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          Most if not all of what I said the last time you posted about sudo still applies. Re: Please - explain the C++ code / function - C / C++ / MFC Discussion Boards[^] Before following up with this or anything in the future at a minimum you need to understand what the command does, including the exact output when you run it yourself from the command line. You also need to understand everything the QT QProcess class does. QProcess Class | Qt Core 6.6.1[^] You must specifically understand how to code to all of the following - exitCode() - readAllStandardError() - readAllStandardOutput() - There must be a way to control stdin dynamically but I could not see it. But you can use setStandardInputFile() for basic understanding. I suggest you start with the command 'ls' and do NOT start with 'sudo' so you can understand how to use the above methods. You should test the following - A directory that does not exist - A directory that does exist. Keep in mind that although you are working with the QT library coding to run a process in ANY programming language is going to require that you understand the dynamics of how that works. It might help to read up on what the following means. - Exit value - stdio: stderr, stdout, stdin.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups