|
Salvatore Terress wrote: Apparently there are at least two ways to accomplish that.
All languages, C++ or anything else, for an external app has the following communication paths.
- Start Up - Command line parameters.
- Running. Exit code. Which is an int value and nothing else.
- Running. Stdio: stderr, stdin, stdout.
Using the above I have implemented the following in the past
- One computer connecting to multiple hardware devices.
- Error output collected.
- Logging collected.
- Commands controlling each device.
- Binary data sent to and from each device.
An application might provide another API. But that has nothing to do with running it from the first application. And for most standard 'OS Command line' commands it will not apply. As a specific example you can start and stop IIS using command line commands. But you are definitely not going to be accessing web sites from it using exactly the same commands (if at all using a OS command.)
Using a command line app and controlling it requires that one understand exactly what the app does. And that has nothing to do with the controller code from the calling app.
Using a OS shell is going to make the above MUCH harder. That is because the shell must then redirect all output from any commands it runs back along the stdio path (in and out.)
Getting the shell to do that has nothing to do with the calling application. One will need to figure that out by looking for command line parameter to the shell itself. Although I suspect that that it is possible to do this, at the very least I know it is difficult.
|
|
|
|
|
Salvatore Terress wrote: I need to have "sudo" access...
Such access has NOTHING to do with
Linux security. Yes, it has everything to do with Linux security, as clearly explained in the man page: https://linux.die.net/man/8/sudo[^]. And although I have not tried this myself, I strongly suspect that you cannot use it in the way you are trying to do. If you want elevated/admin permissions then you most likely need to call sudo before you start the original application.
And this question has nothing to do with C/C++; please use the correct forums for your questions.
|
|
|
|
|
In today's O/S security systems, there is usually a wall between normal user processes and admin-level processes. You cannot access the streams of an admin process from a normal user process. Think about it for just 5 seconds and you'll see why this is a security risk and is now not allowed.
|
|
|
|
|
I'm sure've been over this before:
$ id
uid=1002(k5054) gid=1002(k5054) groups=1002(k5054),27(sudo),100(users)
# create a new group for bluetooth users, call it btusers
$ sudo groupadd btusers
# add user k5054 to the btusers group
$ sudo usermod -a -G btusers k5054
# edit /etc/sudoers.d/btusers to allow any user in btusers group
# to run hcitool without a password:
$ sudo visudo /etc/sudoers.d/btusers
add line %btusers ALL=(ALL) NOPASSWD:/usr/bin/hcitool
# log out and log back in again ...
$ sudo ps
[sudo] password for k5054: (CTL-D)
sudo: no password was provided
sudo: a password is required
# We're now part of the btusers group, so we don't need a password for hcitool
# id
uid=1002(k5054) gid=1002(k5054) groups=1002(k5054),27(sudo),100(users),1003(btusers)
# sudo hcitool without needing a password
$ sudo hcitool dev
Devices: If you need to be able to run other commands you can append them as a comma separated items to the sudoers file e.g.
%btusers ALL=(ALL) NOPASSWD:/usr/bin/hcitool,/usr/bin/hcidump
With knowledge comes great power. Use it wisely.
But this should really be int the linux programming forum, or maybe System Admin.
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
Salvatore Terress wrote: what am I missing ? It makes no difference how you run that command string, it will never work. If that command succeeds then the generated shell that runs the sudo command will be elevated to (possibly root) status. It will then terminate and return to your application which runs at non-elevated level. If you want root access in your application then you have two very simple choices:
1. Do what k5054 advised and set up your account to run at higher level.
2. Call sudo in your shell before launching your application.
|
|
|
|
|
k5054 wrote: I'm sure've been over this before:
To me it is the same as the question that was posted a while ago. One of my posts in this thread has a link to my post in that previous post.
|
|
|
|
|
As we lkeep telling you, you cannot call sudo from within the program. Whether you use QProcess or system makes no difference, as neither will do what you want. You must call sudo at the actual terminal before you run your application.
|
|
|
|
|
Inappropriate post - voluntary removed
modified 11-Feb-24 20:19pm.
|
|
|
|
|
Not knowing what type of object process is, it's impossible to say, with any accuracy.
However, since it looks like it might be a QProcess, so once again, this isn't a C++ question, but a QT question.
Maybe if you took the time to look at the documentation when you run into an issue, you might be able to resolve your questions in a timely manner ... QProcess Class | Qt Core 6.6.1
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
Me, I'm wondering a few things:
1. What makes you believe this is a C++ question? The answer obviously depends on what QProcess object decided to do in the waitForStarted function.
2. Why don't you Google before asking here? I tried "waitforstarted qt" and the first hit points me to QProcess Class | Qt Core 6.6.1[^] and I read there:
Quote: If msecs is -1, this function will not time out.
3. Why you keep asking in a slightly annoying tone? Makes you feel better?
Mircea
|
|
|
|
|
Thank you for answering my question.
|
|
|
|
|
Mircea Neacsu wrote: Why you keep asking in a slightly annoying tone? Makes you feel better? He has always* behaved in this manner, even once complaining to Chris when he was asked to put his code between <pre> tags. He was thrown off the site last year for abuse, but has managed to more or less behave up to now. Quite why he continues with this code, given his level of understanding, remains a mystery.
*has had different accounts, or changed the name more than once.
|
|
|
|
|
Reading the QT documentation could save you some pain.
Just saying.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
And save us some as well. ![Laugh | :laugh:](https://www.codeproject.com/script/Forums/Images/smiley_laugh.gif)
|
|
|
|
|
Amen!
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
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
|
|
|
|
|
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 <cstdio>
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
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
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.
|
|
|
|
|
Hi, I'm working on implementing a concurrent queue (implemented as single linked list) and I've encountered a problem. The structure of my linked list node looks like this:
struct queue {
queue *head, *tail;
pthread_mutex_t head_mut, tail_mut;
}
struct node {
_Atomic(node*) next;
int item;
};
I am currently implementing pop and push operations, both of them have their own separate mutex - tail_mut and head_mut respectively.
The list always includes a dummy node at the beginning.
I'm encountering an issue when there's only one element in the list (the dummy node plus this one element). I can't figure out how to handle the case where one thread is trying to push an element and another is trying to pop an element from the list.
The problem arises when we perform a pop operation first and change the head to head->next. In this scenario, another thread can still push elements to the list by adding them to the end of the list (tail). However, the list has only one element (the one that is being popped = tail), so i must find a solution to somehow block this operation or to signal 'pushing' thread that it should push to the head not to tail.
Acquiring the tail mutex would obviously solve the problem, but I'm looking for a potentially more efficient solution (so I would not block push operations by acquiring the tail mutex).
Is there a more efficient way to handle this situation? Any suggestions would be greatly appreciated.
|
|
|
|
|
cod3Ninj4 wrote: I can't figure out how to handle the case
Good thing because there is no way to handle that case.
You can only do it with one of the following.
1. One and only one lock.
2. Two dummy nodes, one at each end.
cod3Ninj4 wrote: Is there a more efficient way to handle this situation?
Probably but that answer depends on how it is used and not on how it is implemented.
|
|
|
|
|
I am taking Harvard's CS50 class. We are learning C.
They use their own library where you include cs50.h.
There is a function called get_string that returns a string.
I think it has a memory leak.
I think this is the source code:
https://github.com/cs50/libcs50/blob/main/src/cs50.c
They are saying stuff like:
string name = get_string("What is your name?");
Then, they never call free on the memory in main.
Anyone know how the memory gets freed or if it is a leak?
Thanks.
|
|
|
|
|
No.
From the very code you linked to:
static void teardown(void)
{
if (strings != NULL)
{
for (size_t i = 0; i < allocations; i++)
{
free(strings[i]);
}
free(strings);
}
}
The library keeps track of all the strings it allocates and frees them when your app exits.
|
|
|
|
|
After Dave's on-point answer, this is just a general comment with the hope that your question becomes a teachable moment.
Way back when, as I was learning programming, I came up with a saying: "the silly is on the other side of the screen". It obviously can be interpreted both ways, but the idea is to look where the balance of probability indicates that the error should be. In my case, I learned my first assembly language trying to prove that the compiler was wrong - of course it wasn't. In your case, the chance that a course from a prestigious institution contains a trivial error are rather small compared with the chance that you are missing something.
Try to find what you are missing and those "Aha!" moments may benefit you more than the rest of the assignment. And, when in doubt, try to remember: the silly is on the other side of the screen.
Mircea
|
|
|
|
|
This is their pseudocode for bubble sort:
-------------------------------------------------------
Repeat n-1 times
For i from 0 to nā2
If numbers[i] and numbers[i+1] out of order
Swap them
If no swaps
Quit
-------------------------------------------------------
Then, David Malan supposedly analyzed the running time
by multiplying n-1 by n-2.
|
|
|
|