LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Popen bash command eval check for command that read from stdin? (https://www.linuxquestions.org/questions/linux-general-1/popen-bash-command-eval-check-for-command-that-read-from-stdin-4175602893/)

MrUmunhum 03-30-2017 11:32 PM

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.

rigor 04-01-2017 12:15 AM

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.

MrUmunhum 04-01-2017 02:30 PM

Quote:

Originally Posted by rigor (Post 5691148)
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'?

rigor 04-01-2017 08:50 PM

Quote:

Originally Posted by MrUmunhum (Post 5691360)
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 (Post 5691360)
]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 (Post 5691360)
]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.

MrUmunhum 04-05-2017 05:24 PM

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.


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