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. Please - explain the C++ code / function

Please - explain the C++ code / function

Scheduled Pinned Locked Moved C / C++ / MFC
c++
18 Posts 5 Posters 47 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 Lost User

    Could you help me with one more question? I have managed to build correct "command string " and received expected response.

    QP->start("sh", QStringList() << "-c" << " xterm -maximized  -hold -fa 'Monospace' -fs 24 -e  lsusb " ); 
    

    I need to replace the -e lsusb with -e command where the command is QString passed to the function. I did try few approaches , likes

    QP->start("sh", QStringList() << "-c" << " xterm -maximized  -hold -fa 'Monospace' -fs 24 -e  << command " ); 
    

    or

    QP->start("sh", QStringList() << "-c" << " xterm -maximized  -hold -fa 'Monospace' -fs 24 -e  << "-c" << command " ); 
    

    I think the "problem" is that -e option must be the last option passed to

    xterm

    and I do not know if xterm can return meaningful error. I have been running my entire test code to see if the xterm actually executes. All I am getting is QProcess starts and then terminates without actually running xterm. Thank you for your help.

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

    xterm will only return whether xterm itself failed or not. So for example xterm -e /bin/false returns true (0), since xterm executed successfully. I'm suspicious about your approach, here though. Can you not run your command directly via QProcess, rather than launching an xterm? I am assuming 2 things here: 1) you need to capture the output of command, at least in part, and 2) QProcess can do that for you, similar to using pipe(), rather than using system() to run a shell command from a program.

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

    J 1 Reply Last reply
    0
    • K k5054

      xterm will only return whether xterm itself failed or not. So for example xterm -e /bin/false returns true (0), since xterm executed successfully. I'm suspicious about your approach, here though. Can you not run your command directly via QProcess, rather than launching an xterm? I am assuming 2 things here: 1) you need to capture the output of command, at least in part, and 2) QProcess can do that for you, similar to using pipe(), rather than using system() to run a shell command from a program.

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

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

      k5054 wrote:

      I'm suspicious about your approach

      I agree with that.

      L 1 Reply Last reply
      0
      • J jschell

        k5054 wrote:

        I'm suspicious about your approach

        I agree with that.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #11

        OK, reason for using xterm - I am trying to run "sudo command..." and have not figured out how to enter password...into my code. I have actually try both "sh" and "xterm" and my primary issue (as of this post ) is to get the parameters to passed correctly to QProcess. I am well aware that there is always " many ways to skin a cat " but at this point I am still stuck at resolving the passing of parameters. For now I am looking for explanations why I cannot pass multiple options to QProcess using the "<<" operator. ( Maybe I need to go back to QT forum to resolve this ...) Here is my latest code

        QStringList params;
        params << "-maximized"; //  work fine 
        params  << "-e" << "lsusb"; // QProcess starts but terminates 
        // 
        QP->start("xterm", params);
        
        K L J 3 Replies Last reply
        0
        • L Lost User

          OK, reason for using xterm - I am trying to run "sudo command..." and have not figured out how to enter password...into my code. I have actually try both "sh" and "xterm" and my primary issue (as of this post ) is to get the parameters to passed correctly to QProcess. I am well aware that there is always " many ways to skin a cat " but at this point I am still stuck at resolving the passing of parameters. For now I am looking for explanations why I cannot pass multiple options to QProcess using the "<<" operator. ( Maybe I need to go back to QT forum to resolve this ...) Here is my latest code

          QStringList params;
          params << "-maximized"; //  work fine 
          params  << "-e" << "lsusb"; // QProcess starts but terminates 
          // 
          QP->start("xterm", params);
          
          K Offline
          K Offline
          k5054
          wrote on last edited by
          #12

          You can tell sudo to allow certain users the ability to run a given command without having to prompt for a password. For example you might add a file foo to the /etc/sudoers.d directory

          # allow user "joe" to use command /usr/sbin/foobar
          joe ALL(ALL) NOPASSWD:/usr/sbin/foobar

          allow users in group foo to use command /usr/sbin/frobnicate

          %foo ALL(ALL) NOPASSWD:/usr/sbin/frobnicate

          Google for sudoers to see what other options you have for the sudoers file. But you should be aware that any time a user process "needs" sudo access, its something of a smell. That is it should be considered a signal that there may be something fundamentally wrong with the approach to solving the problem. If your app definitely, absolutely, no-question-about-it needs to elevate its permissions to perform some tasks, I would be inclined to write a program that only does that one thing, and use setuid/setgid with appropriate setuid/setgid permissions e.g.

          $ ls /usr/local/bin/foo
          ---s--x--x 1 root root 12345 Jan 1 08:00 /usr/local/bin/foo

          and the code for foo would do something like

          std::string build_cmd_str(int argc, char *argv)
          {
          std::string cmd{"/usr/local/bin/foo"};
          /* build up rest of command string here */
          if(bad_things_happened) {
          cmd.erase();
          }
          return cmd;
          }

          main(int argc, char *argv[])
          {

          // create a command string
          std::string cmd{build_cmd_str(argc, argv)};

          if(cmd.length() == 0) {
          // send diagnostic to cerr, maybe?
          return -1;
          } else {
          setuid(0); // set actual uid to root
          FILE *p = pipe(cmd.c_str(), "r");
          // process output
          // send to stdout as needed
          pclose(p);
          }

          return 0;
          }

          This can be as simple or complex as you need, but should only execute one system command. Ideally, if you only need to run something as perhaps the database administrator, the helper program would have setuid as the dba, not root.

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

          J 1 Reply Last reply
          0
          • L Lost User

            OK, reason for using xterm - I am trying to run "sudo command..." and have not figured out how to enter password...into my code. I have actually try both "sh" and "xterm" and my primary issue (as of this post ) is to get the parameters to passed correctly to QProcess. I am well aware that there is always " many ways to skin a cat " but at this point I am still stuck at resolving the passing of parameters. For now I am looking for explanations why I cannot pass multiple options to QProcess using the "<<" operator. ( Maybe I need to go back to QT forum to resolve this ...) Here is my latest code

            QStringList params;
            params << "-maximized"; //  work fine 
            params  << "-e" << "lsusb"; // QProcess starts but terminates 
            // 
            QP->start("xterm", params);
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #13

            Salvatore Terress wrote:

            Maybe I need to go back to QT forum to resolve this

            Definitely yes, as none of this has anything to do with C++. I have looked at the documentationm for QStringList and QProcess, and what you are doing looks correct. But why it fails is impossible to guess.

            1 Reply Last reply
            0
            • K k5054

              You can tell sudo to allow certain users the ability to run a given command without having to prompt for a password. For example you might add a file foo to the /etc/sudoers.d directory

              # allow user "joe" to use command /usr/sbin/foobar
              joe ALL(ALL) NOPASSWD:/usr/sbin/foobar

              allow users in group foo to use command /usr/sbin/frobnicate

              %foo ALL(ALL) NOPASSWD:/usr/sbin/frobnicate

              Google for sudoers to see what other options you have for the sudoers file. But you should be aware that any time a user process "needs" sudo access, its something of a smell. That is it should be considered a signal that there may be something fundamentally wrong with the approach to solving the problem. If your app definitely, absolutely, no-question-about-it needs to elevate its permissions to perform some tasks, I would be inclined to write a program that only does that one thing, and use setuid/setgid with appropriate setuid/setgid permissions e.g.

              $ ls /usr/local/bin/foo
              ---s--x--x 1 root root 12345 Jan 1 08:00 /usr/local/bin/foo

              and the code for foo would do something like

              std::string build_cmd_str(int argc, char *argv)
              {
              std::string cmd{"/usr/local/bin/foo"};
              /* build up rest of command string here */
              if(bad_things_happened) {
              cmd.erase();
              }
              return cmd;
              }

              main(int argc, char *argv[])
              {

              // create a command string
              std::string cmd{build_cmd_str(argc, argv)};

              if(cmd.length() == 0) {
              // send diagnostic to cerr, maybe?
              return -1;
              } else {
              setuid(0); // set actual uid to root
              FILE *p = pipe(cmd.c_str(), "r");
              // process output
              // send to stdout as needed
              pclose(p);
              }

              return 0;
              }

              This can be as simple or complex as you need, but should only execute one system command. Ideally, if you only need to run something as perhaps the database administrator, the helper program would have setuid as the dba, not root.

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

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

              k5054 wrote:

              I would be inclined to write a program that only does that one thing, and use setuid/setgid with appropriate setuid/setgid permissions e.g.

              That sounds like a really good solution to me.

              1 Reply Last reply
              0
              • L Lost User

                OK, reason for using xterm - I am trying to run "sudo command..." and have not figured out how to enter password...into my code. I have actually try both "sh" and "xterm" and my primary issue (as of this post ) is to get the parameters to passed correctly to QProcess. I am well aware that there is always " many ways to skin a cat " but at this point I am still stuck at resolving the passing of parameters. For now I am looking for explanations why I cannot pass multiple options to QProcess using the "<<" operator. ( Maybe I need to go back to QT forum to resolve this ...) Here is my latest code

                QStringList params;
                params << "-maximized"; //  work fine 
                params  << "-e" << "lsusb"; // QProcess starts but terminates 
                // 
                QP->start("xterm", params);
                
                J Offline
                J Offline
                jschell
                wrote on last edited by
                #15

                Salvatore Terress wrote:

                I am still stuck at resolving the passing of parameters.

                Following is not specific to sudo. When you execute an OS command in any application the following applies 1. The command to run the executable 2. What happens AFTER the executable starts running. The first represents the command (binary exe) and the command line options that the command accepts. What happens with the second depends on the command. But a normal command (non-UI) will be using STDIO (stdin, stdout, stderr.) The second can NOT be controlled with 'parameters'. Only the first can. The only way you can interact with the second in an application is by accessing the STDIO of the application as it runs. The second becomes more complicated if the application has a GUI. It can also be more complicated in specific situations depending on exactly what the application does. (There is a way to bypass STDIO.) For sudo and the first and the way you are executing it with "QP->start" you can only do what the command line options for the command allow. I suspect sudo probably varies by the specific OS. But following is one example. sudo(8) - Linux manual page[^] There is no way to change that. Either it is allowed by that or it isn't. Additionally "start" might have a specific meaning on Windows which can also impact what happens. That doesn't mean is actually applies in this case. start | Microsoft Learn[^]

                1 Reply Last reply
                0
                • L Lost User

                  Voluntarily removed - incorrect forum.

                  J Offline
                  J Offline
                  jeron1
                  wrote on last edited by
                  #16

                  :thumbsdown: For the removal. :thumbsdown:

                  "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                  L 1 Reply Last reply
                  0
                  • J jeron1

                    :thumbsdown: For the removal. :thumbsdown:

                    "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #17

                    Fairly common behaviour from someone who is on their (at least) fourth incarnation here.

                    J 1 Reply Last reply
                    0
                    • L Lost User

                      Fairly common behaviour from someone who is on their (at least) fourth incarnation here.

                      J Offline
                      J Offline
                      jeron1
                      wrote on last edited by
                      #18

                      It takes all kinds. :sigh:

                      "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                      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