LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-31-2016, 02:38 PM   #1
Jasson
LQ Newbie
 
Registered: Jan 2016
Posts: 15

Rep: Reputation: Disabled
Non-closing version of execl ???


Hello there,

QUESTION:
is there a non-closing version of execl() ?
I want to write to cmd line...

UNDERSTANDING PROBLEM
Here
https://www.cs.cf.ac.uk/Dave/C/OLDC/..._18_4_1_1.html
i found, that "execl stands for execute and leave"
My problem of understanding is the use of fork-execl() procedure in order to write to cmd line from C program:
If the brand new forked process gets closed when execl() is done, and i can only put one command to the pipe, where is the sense of this? For example i canīt even parse to a folder by "cd /sys..." and THEN do a "ls -l" because after first command the process is allready closed.

WHAT I WOULD NEED
Code:
int fd[2];

 if(pipe(fd) < 0) return NULL;
 switch(fork())
  {

   case  -1 : return(NULL);
   case   0 : close(wr(fd[WRITE],fd[READ]));
              close(0,1);
              dup(wr(fd[READ],fd[WRITE]));
              close(wr(fd[READ],fd[WRITE]));
              //execl("/bin/sh", "sh", "-c", commando, NULL);
              "exe"("/bin/sh", "sh", "-c", "su", NULL);
              "exe"("/bin/sh", "sh", "-c", "echo 101 > /sys/class/gpio", NULL);

   default  : close(wr(fd[READ],fd[WRITE]));
              return(FILE *)(wr(fd[WRITE],fd[READ]));
  }
 
Old 01-31-2016, 03:20 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 3,876
Blog Entries: 1

Rep: Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998
I am not sure I understand what you are trying to do, but from this and your other posts it appears that you are trying to open an interactive shell with root privs from your non-priv'd C++ application.

With regard to the execl() example in this thread, the problem may be that you are opening a non-interactive shell (i.e. not connected to a terminal) and expecting it to behave as an interactive shell (connected to a terminal).

From man bash:

Code:
       An  interactive  shell is one started without non-option arguments and
       without the -c option whose standard input and error are both connected to terminals
       (as determined by isatty(3)), or one  started  with the  -i  option.
So your execl function opens a non-interactive shell, passing its args to that shell, then closes because the non-interactive shell is finished at that point - it is not interactive.

Last edited by astrogeek; 01-31-2016 at 03:23 PM.
 
Old 01-31-2016, 03:43 PM   #3
Jasson
LQ Newbie
 
Registered: Jan 2016
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
interactive shell with root privs from your non-priv'd C++ application
Exactly - im new to linux so, im not able yet to put my needs to clear words...
Actually i see, that this might be impossible by intention, since it would brake system saftyness, but on the other hand, if my code knows the password odr if the system grants paswordless sudo, i see no reason to block this aproach.

My idea was to
- fork a process
- overlay the child process with a terminal (actually didnt know terminal and shell arent the same)
- start a pipe to the shell
- make the process root by putting "su" to the pipe
- write further instructions
 
Old 01-31-2016, 03:56 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 3,876
Blog Entries: 1

Rep: Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998
Yea, I think that you are bumping into the boundary between Unix design principles and your as yet incomplete knowledge.

Other than a critical reading of the man pages, which is always a good thing, I would suggest that you get the book Advanced Programming In The Unix Environment by W. Richard Stephens (current edition also covers things specific to Linux and the BSDs). That is the most complete, readable and useful single source that I know of for getting your knowledge of the system quickly up to par.

It is not always a simple matter of whether a thing can be done, it is also important whether and how it should be done.

Good luck!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
execl problem jodie Linux - Newbie 1 04-27-2008 03:36 PM
execl and sprintf not working together mohammednv Linux - Kernel 2 01-27-2008 05:04 PM
execl question kev000 Programming 3 05-09-2007 09:17 AM
Help using popen and execl cdog Linux - Software 3 02-28-2007 07:44 PM
How to get the execl() output? estratos Programming 4 06-07-2006 06:18 PM


All times are GMT -5. The time now is 12:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration