pipe() dup2() fork() execl() select()

Discussion in 'C Programming' started by PC, Aug 9, 2003.

  1. PC

    PC Guest

    Is it possible to create pipe()s (stdin/stdout) for a child process
    and forking a shell with execl() and controlling the shell's
    stdin/stdout from the parent with select()?
    Heres a little snippet of code. it doesnt actually work.. but it is
    how I envisioned it::

    if((n=accept(s,(struct sockaddr *)&remote,&sz))==-1)
    {printf("accept\n"); exit(0);}
    close(0); close(1); close(2);
    pipe(&i[0]);
    dup2(0,i[0]);
    dup2(1,i[1]);
    dup2(n,i[0]);
    dup2(n,i[1]);
    if(!fork()) {
    execl("/bin/sh","sh",0); close(n); exit(0); }
    FD_ZERO(&rd); FD_ZERO(&wr);
    FD_SET(0,&rd); FD_SET(1,&wr);
    select(i[1]+1,&rd,&wr,0,0);
    while(1) {
    if(FD_ISSET(0,&rd)) { read(0,wb,1); }
    if(FD_ISSET(1,&wr)) { read(1,wb,1); }
    }}


    I want the client's interaction (connecting via telnet) to work via
    select. This is because, later I will add blowfish encryption routines
    within FD_ISSET() Which will be encrypting/decrypting stdin/stdout of
    the child process.

    Am I headed in the right direction by using the functions I have?
    Thanks in advance
    PC
    PC, Aug 9, 2003
    #1
    1. Advertising

  2. PC

    Artie Gold Guest

    Artie Gold wrote:
    > PC wrote:
    >
    >> Is it possible to create pipe()s (stdin/stdout) for a child process
    >> and forking a shell with execl() and controlling the shell's
    >> stdin/stdout from the parent with select()?

    >
    >
    > Hold it right there!
    >
    > None of the functions to which you are referring are part of standard C,
    > making your question off topic here (news:comp.unix.programmer would be
    > an appropriate place).
    >
    > A piece of advice -- when posting code to usenet it is a good idea to:
    >
    > include the smallest possible compilable
    > snippet that exhibits the problem[1]
    >
    > indent appropriately
    >
    > specify what you think should be happening
    > and what is happening instead (annotations
    > within the code isn't a bad idea either)
    >
    > All this is to enhance the probability of someone who knows the answer
    > actually spending the time to help.
    >
    > One more piece of advice -- please read the FAQ, at:
    >
    > http://www.eskimo.com/~scs/C-faq/top.html
    >
    > before posting here again. It's really for your benefit.
    >
    >> Heres a little snippet of code. it doesnt actually work.. but it is
    >> how I envisioned it::
    >>
    >> if((n=accept(s,(struct sockaddr *)&remote,&sz))==-1)
    >> {printf("accept\n"); exit(0);}
    >> close(0); close(1); close(2);
    >> pipe(&i[0]);
    >> dup2(0,i[0]);
    >> dup2(1,i[1]);
    >> dup2(n,i[0]);
    >> dup2(n,i[1]);

    >
    >
    > These last four lines can't be correct. Notice that you're immediately
    > overwriting the values in `i'.
    >
    >> if(!fork()) {
    >> execl("/bin/sh","sh",0); close(n); exit(0); } FD_ZERO(&rd); FD_ZERO(&wr);
    >> FD_SET(0,&rd); FD_SET(1,&wr);
    >> select(i[1]+1,&rd,&wr,0,0); while(1) {
    >> if(FD_ISSET(0,&rd)) { read(0,wb,1); }
    >> if(FD_ISSET(1,&wr)) { read(1,wb,1); }
    >> }}
    >>
    >>
    >> I want the client's interaction (connecting via telnet) to work via
    >> select. This is because, later I will add blowfish encryption routines
    >> within FD_ISSET() Which will be encrypting/decrypting stdin/stdout of
    >> the child process.
    >>
    >> Am I headed in the right direction by using the functions I have?

    >
    >
    > <OT>
    > Erm...
    > You're headed in the right direction, but you have fundamental
    > misunderstandings about how some things work. Reread whatever reference
    > you're using, think about it, and wander over to c.u.p if you run into
    > trouble.
    > </OT>
    >
    > HTH,
    > --ag


    I forgot my footnote:
    [1] Of course that's not always possible, particularly if you're trying
    to track down a syntax error. But even then...

    >
    >




    --
    Artie Gold -- Austin, Texas
    Artie Gold, Aug 9, 2003
    #2
    1. Advertising

  3. PC

    Jens Schicke Guest

    PC <> wrote:
    > if((n=accept(s,(struct sockaddr *)&remote,&sz))==-1)
    > {printf("accept\n"); exit(0);}
    > close(0); close(1); close(2);
    > pipe(&i[0]);
    > dup2(0,i[0]);
    > dup2(1,i[1]);
    > dup2(n,i[0]);
    > dup2(n,i[1]);
    > if(!fork()) {
    > execl("/bin/sh","sh",0); close(n); exit(0); }
    > FD_ZERO(&rd); FD_ZERO(&wr);
    > FD_SET(0,&rd); FD_SET(1,&wr);
    > select(i[1]+1,&rd,&wr,0,0);
    > while(1) {
    > if(FD_ISSET(0,&rd)) { read(0,wb,1); }
    > if(FD_ISSET(1,&wr)) { read(1,wb,1); }
    > }}


    As already stated that is OT in comp.lang.c...

    <OT>
    You made a lot of error as I see it...

    1. pipe() will create a pair of filedescriptors which _together_ form
    _one_ pipe - you have to use pipe() two times for pipes are by
    definition only useable in one direction.

    2. dup2(n, i[0]) closes n and reopens it as i[0], but you still need the
    socket filedescriptor, so remove the dup2(n... lines

    3. select should be called inside a loop in order to react each time new
    data becomes available.

    4. in the select loop, if data becomes available copy it from the pipe
    to the socket or the other way around.
    </OT>
    Jens Schicke, Aug 9, 2003
    #3
  4. PC

    PC Guest

    > <OT>
    > You made a lot of error as I see it...
    >
    > 1. pipe() will create a pair of filedescriptors which _together_ form
    > _one_ pipe - you have to use pipe() two times for pipes are by
    > definition only useable in one direction.
    >
    > 2. dup2(n, i[0]) closes n and reopens it as i[0], but you still need the
    > socket filedescriptor, so remove the dup2(n... lines
    >
    > 3. select should be called inside a loop in order to react each time new
    > data becomes available.
    >
    > 4. in the select loop, if data becomes available copy it from the pipe
    > to the socket or the other way around.
    > </OT>

    Thanks for clearing things up
    PC, Aug 9, 2003
    #4
    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. Help with open, dup and dup2

    , Mar 16, 2006, in forum: C Programming
    Replies:
    2
    Views:
    368
    Ian Collins
    Mar 16, 2006
  2. DyslexicAnaboko

    problems redirecting an open file to stdin using dup2()

    DyslexicAnaboko, Dec 5, 2006, in forum: C Programming
    Replies:
    4
    Views:
    777
    DyslexicAnaboko
    Dec 5, 2006
  3. David Thompson

    Re: dup2, fclose and rename ...

    David Thompson, Sep 1, 2008, in forum: C Programming
    Replies:
    0
    Views:
    426
    David Thompson
    Sep 1, 2008
  4. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    563
    Eric Snow
    Jan 8, 2009
  5. Hongliang Wang

    dup2

    Hongliang Wang, Apr 22, 2014, in forum: C++
    Replies:
    14
    Views:
    130
    Hongliang Wang
    Apr 30, 2014
Loading...

Share This Page