LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   redirecting stdin and stdout with pipes: convincing apps they're for a terminal? (https://www.linuxquestions.org/questions/programming-9/redirecting-stdin-and-stdout-with-pipes-convincing-apps-theyre-for-a-terminal-225254/)

prell 09-01-2004 09:40 AM

redirecting stdin and stdout with pipes: convincing apps they're for a terminal?
 
I'm writing an application which will monitor standard input (for timeout). I'm currently attempting to do this by creating a pipe with pipe(2), and using that as the stdin and stdout for the child process (i.e. the process to be monitored).

(Before I continue: I'm running RedHat 8.0 on kernel 2.4.20-27.8.)

However, running /bin/sh with this method causes sh to wait, and running /bin/vi causes vi to complain that input isn't from a terminal. How can I make my pipe act exactly as stdin and stdout would, no matter what the actual implementation of stdin and stdout was?

Also: If I use the method detailed below, do I have to do anything special for child threads and processes of the monitored application? I'm assuming not, since it's my understanding that child processes inherit the stdin and stdout.

Here's some code:
Code:

void parent( pid_t* child ) {
}

void child( ) {
    char** args = NULL;

    args = realloc( args, sizeof( char* ) * 2 );

    *args = realloc( *args, sizeof( char ) * 8 );

    strcpy( *args, "/bin/vi\0" );

    *( args + 1 ) = NULL;

    execve( "/bin/vi\0", args, NULL );
}

int main( int argc, char** argv, char** env ) {
    int* in = NULL;

    in = realloc( in, sizeof( int ) * 2 );

    int pipe_in_create_result;

    pipe_in_create_result = pipe( in );

    /* .. */

    pid_t fork_result;

    fork_result = fork( );

    /* .. */

    if ( fork_result == 0 ) {
        close( 0 );  /* Close standard input */

        dup( *in );  /* Since 0 closed, dup will use 0 (stdin) */

        /* close( *in ); */  /* Close for reading */
        close( *( in + 1 ) );  /* Close for writing */

        child( );
    } else {
        close( 1 );  /* Close standard output */

        dup( *( in + 1 ) );  /* Since 1 closed, dup will use 1 (stdout) */

        close( *in );

        parent( &fork_result );

        /* .. */
    }

    /* .. */


mhearn 09-02-2004 06:38 AM

You need to use a pseudo-tty, look in the glibc manual for how to do that.


All times are GMT -5. The time now is 02:29 PM.