Using /bin/sh "partially " works
-
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.
-
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.
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
-
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.
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 fromhci info
, thesystem()
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 usepopen()
. 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
-
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.
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.