Inherit file descriptors from parent process?

Discussion in 'Perl Misc' started by James Thornton, Sep 26, 2003.

  1. How do you inherit the stdin and stdout file descriptors from a parent
    process? Specifically, the following function "run_filters" forks and
    calls several perl scripts via execvp.

    One of the perl scripts needs to execute a virus scanner on the file
    pointed to by the parent file descriptor, modifying the file as
    needed. The virus scanner will be invoked via a system call where
    $file is the file name associated with the file parent file
    descriptor:

    system $virus_binary $file;

    // C program that forks and executes several
    // Perl scripts via execvp
    int run_filters(command* first, int fdin)
    {
    command* c;

    for(c = first; c; c = c->next) {
    pid_t pid;
    int status;
    int fdout;

    fdout = mktmpfile();
    if(fdout == -1)
    return -QQ_WRITE_ERROR;
    pid = fork();
    if(pid == -1)
    return -QQ_OOM;
    if(pid == 0) {
    if(close(0) == -1 ||
    dup2(fdin, 0) != 0 ||
    close(1) == -1 ||
    dup2(fdout, 1) != 1)
    exit(QQ_WRITE_ERROR);
    execvp(c->argv[0], c->argv);
    exit(QQ_INTERNAL);
    }
    if(waitpid(pid, &status, WUNTRACED) == -1)
    return -QQ_INTERNAL;
    if(!WIFEXITED(status))
    return -QQ_INTERNAL;
    if(WEXITSTATUS(status))
    return -WEXITSTATUS(status);
    close(fdin);
    if(lseek(fdout, 0, SEEK_SET) != 0)
    return -QQ_WRITE_ERROR;
    fdin = fdout;
    }
    return fdin;
    }
    James Thornton, Sep 26, 2003
    #1
    1. Advertising

  2. James Thornton () wrote:
    : How do you inherit the stdin and stdout file descriptors from a parent
    : process? Specifically, the following function "run_filters" forks and
    : calls several perl scripts via execvp.

    Normally you inherit them buy doing nothing. The command that is spawned
    will read from the same stdin and write to the same stdout. (If several
    programs are all ready the same stdin at the same time then they each get
    a turn at reading.)

    For example, this runs perl, then spawns out to the cat command which
    echoes its stdin to stdout, and then when it return to perl then perl
    prints to stdout.

    C:> perl -e "print 'this is perl'; system('cat'); print 'back to perl'"

    The output I get (includes some things I typed)

    this is perl

    hello
    hello
    back to perl

    (My example uses system(), but all system does is fork and exec, so its
    really no different.)
    Malcolm Dew-Jones, Oct 2, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Dieterich

    inherit without calling parent class constructor?

    Christian Dieterich, Jan 26, 2005, in forum: Python
    Replies:
    3
    Views:
    402
    Jeff Shannon
    Jan 26, 2005
  2. Christian Dieterich
    Replies:
    1
    Views:
    382
    Steven Bethard
    Jan 26, 2005
  3. Christian Dieterich
    Replies:
    1
    Views:
    363
    Steven Bethard
    Jan 26, 2005
  4. Roger Davis
    Replies:
    12
    Views:
    676
    Chris Torek
    Oct 16, 2010
  5. Darko Bazulj

    disable/enable "Inherit from parent the permissions ...

    Darko Bazulj, Mar 10, 2005, in forum: ASP .Net Security
    Replies:
    3
    Views:
    144
    [MSFT]
    Mar 16, 2005
Loading...

Share This Page