Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
is it possible to write to the cmd Line from a C/C++ program (i´m afraid no)?
In case yes, how?
Or may be it is possible to create a bash file by a C/C++ program and run it it from there?
The reason i´m asking is, that i want to write to the "export" file in sys/class/gpio from an App. But i can´t because the App is not on root level and i don´t want to be forced to install "stuff" - fu__ing bad for distribution...
So if i could write to the cmd line, i could tell the system to do a chmod operation on the files i need.
You can use popen/pclose to interact with a shell.
you'd be using BASH script code to open and close files not C or C++ so no.
Quote:
The popen() function shall execute the command specified by the string command. It shall create a pipe between the calling program and the executed command, and shall return a pointer to a stream that can be used to either read from or write to the pipe.
...
The pclose() function shall close a stream that was opened by popen(), wait for the command to terminate, and return the termination status of the process that was running the command language interpreter. However, if a call caused the termination status to be unavailable to pclose(), then pclose() shall return -1 with errno set to [ECHILD] to report this situation. This can happen if the application calls one of the following functions:
them two calls are replaced with this
Code:
|
its called the "Pipe" that is used to send information between two programs.
sends all of the information to grep that picks out what I tell it to look for returning just that information and not everything that is running by the use of the Pipe. It does the same function as the two system calls you gave.
If you want to use c++ stream syntax instead you should look into the pstreams project on sourceforge - does the same thing but with streams instead of popen..
This sounds usefull. As i wrote, i want to write to the "export" file in sys/class/gpio from an App. But the export file needs root priviliges.
So if i open a process with popen and do a "su" with it the process should become root. (?)
The complete background:
I have a Banana-Pi with Android 4.2. I can write Apps for it with Qt Creator which can build .apk files out of the box which makes distributing quite simple. But the moment i need to preinstall something to perfrom GPIO access or type something to a terminal on the device all the convinient feeling from distributing .apk file is useless.
So i need a way to make my Qt App to write to this "export" file.
To be honest, i dont see a reason to provide GPIO drivers and block them for non root accesses. That makes it kind of useless if i cant use the drivers from a program...
This sounds usefull. As i wrote, i want to write to the "export" file in sys/class/gpio from an App. But the export file needs root priviliges.
So if i open a process with popen and do a "su" with it the process should become root. (?)
The complete background:
I have a Banana-Pi with Android 4.2. I can write Apps for it with Qt Creator which can build .apk files out of the box which makes distributing quite simple. But the moment i need to preinstall something to perfrom GPIO access or type something to a terminal on the device all the convinient feeling from distributing .apk file is useless.
So i need a way to make my Qt App to write to this "export" file.
To be honest, i dont see a reason to provide GPIO drivers and block them for non root accesses. That makes it kind of useless if i cant use the drivers from a program...
Two things:
1) Usually systems like that have a dedicated group for access to gpios and the like, and the default user is already a member of the group, so you shouldn't have to worry about permissions.
2) Usually systems like that have passwordless sudo access for the default user, so you could just stick "sudo" in front of the command to run it. Does the Banana-Pi not do this?
I have tried to put sudo or su in front of the commands, but that doesnt work in my distribution.
What i want do is performing
su [hit ENTER]
echo 101 > /sys/class/gpio/export [hit ENTER]
from C code in order to get the gpio101 folder exported.
I tried that with the "yes" command, but that is not installed on the distribution, i cant accept to be forced to do installations.
It does NOT work, if i open the pipe with a command and try to use "fprintf" on the filepointer with further commands like
fp = popen(su, "r");
if(fp == NULL) close();
fprintf(fp, "s", "echo 101 > /sys/class/gpio/export"); There IS a percentage-sign in front of the s, its just not showing here...
I have also been searching on how to [hit ENTER] in scribt files - didnt help
I have tried
fp = popen("su\n echo 101 > /sys/class/gpio/export\n", "r");
which didnt work
I know, that a SINGLE command is performed when doing for example
fp = popen("echo 101 > /sys/class/gpio/export", "r");
I verified that, by performing
su
cd /sys/class/gpio
chmod 777 export
in a shell before starting the app.
But i must get around using the shell
Note: using popen/system is NOT a secure function. The interaction between environment variables/functions, search lists, file globbing opens all kinds of security issues.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.