Please - explain the C++ code / function
-
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.
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 yourcommand
directly via QProcess, rather than launching an xterm? I am assuming 2 things here: 1) you need to capture the output ofcommand
, at least in part, and 2) QProcess can do that for you, similar to usingpipe()
, rather than usingsystem()
to run a shell command from a program."A little song, a little dance, a little seltzer down your pants" Chuckles the clown
-
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 yourcommand
directly via QProcess, rather than launching an xterm? I am assuming 2 things here: 1) you need to capture the output ofcommand
, at least in part, and 2) QProcess can do that for you, similar to usingpipe()
, rather than usingsystem()
to run a shell command from a program."A little song, a little dance, a little seltzer down your pants" Chuckles the clown
-
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);
-
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);
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/foobarallow 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/fooand 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
-
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);
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
andQProcess
, and what you are doing looks correct. But why it fails is impossible to guess. -
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/foobarallow 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/fooand 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
-
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);
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[^]
-
: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
-
: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
-
Fairly common behaviour from someone who is on their (at least) fourth incarnation here.
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