[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: LuaSocket - how can a tcp client detect a closed connection?
- From: "Mark Edgar" <medgar123@...>
- Date: Tue, 6 Mar 2007 15:23:46 -0700
On 3/5/07, gary ng <email@example.com> wrote:
However, given the generic and widespread usage of
fork()/exec(), even 2 file descriptors may not be
enough. As they are in general passed to childs which
unless explicitly closed in the forked child, would
have the same effect as it is now. So a shutdown() is
still needed which effectively means, close this
pipe/socket regardless of how many fd are dup().
Neglecting to explicitly (or implicity via FD_CLOEXEC) close unused
descriptors is a bug in the program, not a flaw in the design of
shutdown() is not legal for use with pipes. It is specific to sockets.
In the hypothetical design of dual-fd sockets (let's call these
twockets), a close() on the writing twocket is equivalent to
shutdown(sock, SHUT_RD) on the socket, and a close() on the reading
twocket is equivalent to shutdown(sock, SHUT_WR) on the socket.
I was in this situation trying to create a loopback
pipe in lua like pipethrough("gzip -dc"). Initially, I
juse use pipe(2) create the pipe pair then dup
stdin/stdout and fork()/exec(). However, even I close
the write end of the parent, I still don't get the
result because it was passed to the fork() child. I
have to explicitly close them before the exec().
Right. You need to ensure that each unused end is closed before
calling exec, or use FD_CLOEXEC instead to accomplish this.
BTW, the "ex" API can do this for you in "pure" Lua. See the popen2()
example at http://lua-users.org/wiki/ExtensionProposal