LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-30-2017, 11:32 PM   #1
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Rep: Reputation: 40
Popen bash command eval check for command that read from stdin?


Hi group,
I'm writing a C program that calls popen to eval a command string.
One problem is if the command wants to read from stdin, it hangs!
For example 'popen( "eval cat", "r" )' will hang.

Is there a way I can filter such programs?

Thanks for your time.
 
Old 04-01-2017, 12:15 AM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Well, umm, Mr. Umunhum :-)

From only what you've provided, I'm not sure what you're overall goal or approach is, beyond not wanting to hang will trying to read from a FILE * you've popen'd.

But the following doesn't hang for me.

poeb.c:
Code:
# include <stdio.h>
# include <unistd.h>
# include <stdlib.h>

# define BUFSIZE    512


/*  poeb.c  */

int main(  int argc ,  char *argv[]  )
{
    FILE        *myFile ;
    char        aLine[  BUFSIZE  ] ;
    

    myFile = popen(  "bash -c 'eval cat ./data.txt  >  out_data.txt'" , "r" ) ;
    
    if (  myFile  ==  ( (FILE *) NULL )  )
    {
        perror(  "poeb"  ) ;
        
        exit( EXIT_FAILURE )  ;
    }
    else
    {
        while(  fgets(  (char * __restrict__)  &aLine ,  BUFSIZE ,  myFile  )  !=  NULL  )
        {
            printf(  aLine  ) ;
        }
        
        printf(  "DID NOT HANG!\n"  ) ;
        
        exit( EXIT_SUCCESS )  ;
    }
}
data.txt:
Code:
Congratulations!  You have been successful!!
data_out.txt:
Code:
Congratulations!  You have been successful!!
PLEASE NOTE:

running the cat command with no file specified to cat will cause the cat command to read from "stdin"/the-tty which will never end unless stdin is terminated in some way, or the process is killed. I generally try a command manually before I run it from a program.

I hope this helps.

Last edited by rigor; 04-01-2017 at 12:16 AM.
 
Old 04-01-2017, 02:30 PM   #3
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by rigor View Post
Well, umm, Mr. Umunhum :-)
Umunhum means 'Home of the hummingbird', it's where I live.

I'm writing a user interface and have little control over what the user does.

Quote:
I hope this helps.
Thanks for your post. Your info was correct but I did not state my problem the right way. I am thinking about using a 'select' or a 'timeout'?
 
Old 04-01-2017, 08:50 PM   #4
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Quote:
Originally Posted by MrUmunhum View Post
Umunhum means 'Home of the hummingbird', it's where I live
I'm aware of it, and saw it as your location on your post; please pardon my lame attempt at humor.


Quote:
Originally Posted by MrUmunhum View Post
]I'm writing a user interface and have little control over what the user does.
AFAIK, I've heard plenty of people suggest that User input should be validated before being used.

But if somehow you really can't, then perhaps some sort of "Sandbox" can be used to prevent problems.

Especially if they have unrestricted access to a shell where they could run commands that could start other commands, and even start multiple commands.

Quote:
Originally Posted by MrUmunhum View Post
]I am thinking about using a 'select' or a 'timeout'?
How well any sort of timeout, whether that built into select or a separate timeout, might work could depend on the range of different types of things a User is allowed to do, the collection of different machines on which they could run a command, and the amount of other work being done on the various machines.

If on a computer which is more or less just sitting around not doing much, a User wants to run a simple command, perhaps to do a quick calculation:

Code:
dc -e '135 29 * p'
a "short" timeout might be appropriate. But if a User could also run a program to translate the text of an entire encyclopedia from one language to another, on a machine which is already heavily loaded with other work, a very large timeout might be needed.
 
Old 04-05-2017, 05:24 PM   #5
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
Well I have concluded there is no way to use Popen for this. I have rewritten my program so many times with varying levels of success. The latest version is at:http://http://mt-umunhum-wireless.ne...curses/Popen.c. But is has one last bug, after timeing out the command, it tries to pclose the sub-command and hangs on a waitpid(). Requiring yet another 'Enter'.

My solution is to use Pipe() instead of Popen().

Thanks for your time and posts.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Security issues with BASH's eval command. towheedm Programming 12 06-08-2012 10:43 AM
[SOLVED] Allow TAB completion with BASH read command? Optimistic Programming 3 08-19-2011 10:05 PM
[SOLVED] BASH eval + command fabtk Programming 5 03-09-2011 02:46 PM
[SOLVED] redirecting STDOUT of previous command to STDIN of next command urban.yoga.journeys Linux - General 9 11-22-2009 03:16 AM
Trouble with bash read command Bebo Programming 5 05-21-2004 08:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:15 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
Open Source Consulting | Domain Registration