LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-01-2004, 09:40 AM   #1
prell
Member
 
Registered: Jul 2004
Posts: 73

Rep: Reputation: 15
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 );

        /* .. */
    }

    /* .. */
 
Old 09-02-2004, 06:38 AM   #2
mhearn
Guru
 
Registered: Nov 2002
Location: Durham, England
Distribution: Fedora Core 4
Posts: 1,565

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


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
redirecting stdin in bash script artur Programming 3 12-09-2011 06:07 AM
redirecting /dev/ttyS0 -> stdin goestin Linux - Software 2 10-26-2011 11:34 AM
stdout stdin Furlinastis Linux - Newbie 3 08-11-2005 11:00 PM
redirect stdin/stdout of running process xtravar Linux - Software 3 03-06-2004 07:54 PM
stdin -> port -> stdout acid_kewpie Linux - Networking 5 04-12-2002 06:01 AM


All times are GMT -5. The time now is 11:20 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration