By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,079 Members | 915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

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

P: n/a
PC
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
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

Nov 13 '05 #2

P: n/a
PC <pc@syclon.com> 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>
Nov 13 '05 #3

P: n/a
PC
> <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
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.