access to fd created AFTER a child splits off with execlp - Unix

This is a discussion on access to fd created AFTER a child splits off with execlp - Unix ; All the examples for passing file descriptors to a child process which I have seen create the file descriptors before splitting off the child process, something like this: if (!socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) { child=fork(); if (child > 0) { ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: access to fd created AFTER a child splits off with execlp

  1. access to fd created AFTER a child splits off with execlp

    All the examples for passing file descriptors to a child process
    which I have seen create the file descriptors before splitting
    off the child process, something like this:

    if (!socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) {
    child=fork();
    if (child > 0) {
    if(usesocket==1){
    /* read from socket */
    close(sv[1]); /* close unused side of the socket */
    input=sv[0]; /* input will be from socket */
    }
    else if(usesocket==2){
    /* write to socket */
    close(sv[0]); /* close unused side of the socket */
    output=sv[1]; /* output will be to socket */
    }
    } else if (child == 0) {
    if(usesocket==1){
    /* child writes output to stdout*/
    close(sv[0]); /* close stdin*/
    dup2(sv[1], 1); /* duplicate stdout*/
    }
    else if(usesocket==2){
    /* child reads from stdin and sends output to disk*/
    close(sv[1]); /* close stdout*/
    dup2(sv[0], 0); /* duplicate stdin*/
    }
    execlp("sh", "sh", "-c", cmdstring,NULL);
    }
    }

    My question is, if the parent program opens more sockets after creating
    the child, can it pass those new ones to the child as well? For
    instance if the child did:

    A -> process -> B

    when it's finished it would then be free to do:

    C -> process -> D

    Where A,B,C,D are file descriptors, and process is code which reads from
    the input, does something, and writes to the output. Assuming there's
    some sort of communication channel (it doesn't matter which type)
    between the parent and the child they might have a little conversation
    like this:

    parent: (opens a socket to communicate with the child
    using "tell " syntax, below)
    (execlp, creates child)
    child: tell parent: ready
    parent: (opens a socket which creates fd A)
    (opens a socket which creates fd B)
    tell child: read A
    tell child: write B
    tell child: go
    child: tell parent: running
    (reads from A, processes, writes to B, until done)
    tell parent: ready
    parent: (closes A and B)
    (opens a socket which creates fd C)
    (opens a socket which creates fd D)
    tell child: read C
    tell child: write D
    tell child: go


    and so forth.

    If new file descriptors are created in the parent will the child be able
    to see them? Is it sufficient to just pass an integer value for the
    child to use with read() and write()? That is, will it share that same
    "file descriptor space" or does the child have a copy of the one in
    existence at the time the parent created the child?

    I know the parent could kill the child, or the child could exit, and a
    second execlp could create a second child which would pick up the new fd
    values. But even on unix/linux that process creation has to be
    "heavier" than passing file descriptor information to an existing
    child process.

    Thanks,

    David Mathog


  2. Re: access to fd created AFTER a child splits off with execlp

    David Mathog writes:

    > My question is, if the parent program opens more sockets after
    > creating the child, can it pass those new ones to the child as well?


    Not portably. See APUE, 15.3: Passing File Descriptors.

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.

  3. Re: access to fd created AFTER a child splits off with execlp

    Paul Pluzhnikov wrote:
    > David Mathog writes:
    >
    >> My question is, if the parent program opens more sockets after
    >> creating the child, can it pass those new ones to the child as well?

    >
    > Not portably. See APUE, 15.3: Passing File Descriptors.
    >


    Now that I think about it, only one half of each socket pair is needed
    in the child - it either reads from a socket, or writes to it, but not
    both on any one socket.

    So is this possible?


    parent: (create socket pairs A->B, C->D)
    (execlp to create a child, which picks up the FD for B,C)
    tell child: go
    child: (runs, reading from B, writing to C)
    ready
    parent: (break connection between A-B, C-D. Eliminate sockets A and D)
    (create sockets E and F)
    (hook E->B, C->F - note that FD has not changed for B and C,
    and child already has those values)
    tell child: go
    child: (runs, reading from B, writing to C)
    ready

    Thanks,

    David Mathog



  4. Re: access to fd created AFTER a child splits off with execlp

    David Mathog writes:

    > Now that I think about it, only one half of each socket pair is needed
    > in the child - it either reads from a socket, or writes to it, but not
    > both on any one socket.
    >
    > So is this possible?
    >

    ....
    > parent: (break connection between A-B, C-D. Eliminate sockets A and D)
    > (create sockets E and F)
    > (hook E->B, C->F - note that FD has not changed for B and C,


    I am not aware of any UNIX that allows to "break and reconnect to
    different endpoints" a socketpair connection.

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.

+ Reply to Thread