From owner-freebsd-net Wed Mar 19 2: 0:29 2003 Delivered-To: freebsd-net@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 78C1737B401 for ; Wed, 19 Mar 2003 02:00:26 -0800 (PST) Received: from gandalf.online.bg (gandalf.online.bg [217.75.128.9]) by mx1.FreeBSD.org (Postfix) with SMTP id 6168543F85 for ; Wed, 19 Mar 2003 02:00:24 -0800 (PST) (envelope-from roam@ringlet.net) Received: (qmail 11848 invoked from network); 19 Mar 2003 09:55:48 -0000 Received: from office.sbnd.net (HELO straylight.ringlet.net) (217.75.140.130) by gandalf.online.bg with SMTP; 19 Mar 2003 09:55:47 -0000 Received: (qmail 29767 invoked by uid 1000); 19 Mar 2003 09:58:42 -0000 Date: Wed, 19 Mar 2003 11:58:42 +0200 From: Peter Pentchev To: Luigi Rizzo Cc: net@FreeBSD.ORG, Tristan Goode Subject: Re: write(2) SIGPIPE on a closed socket? Message-ID: <20030319095842.GC27330@straylight.oblivion.bg> References: <20030319093002.GT468@straylight.oblivion.bg> <20030319013748.A84035@xorpc.icir.org> <20030319094506.GB27330@straylight.oblivion.bg> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="H1spWtNR+x+ondvy" Content-Disposition: inline In-Reply-To: <20030319094506.GB27330@straylight.oblivion.bg> User-Agent: Mutt/1.5.3i Sender: owner-freebsd-net@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org --H1spWtNR+x+ondvy Content-Type: text/plain; charset=windows-1251 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Mar 19, 2003 at 11:45:06AM +0200, Peter Pentchev wrote: > On Wed, Mar 19, 2003 at 01:37:48AM -0800, Luigi Rizzo wrote: > > On Wed, Mar 19, 2003 at 11:30:02AM +0200, Peter Pentchev wrote: > > ... > > > dnscache) getting a SIGPIPE when attempting to write to an incoming > > > connection's socket. Presumably, the client closed the connection in > > ... > > > The question: if the client closed the socket, shouldn't a write(2) > > > return -1 with errno =3D=3D EPIPE before sending a SIGPIPE? Does any= one > >=20 > > well, what would "before" mean ? the system sends a signal when > > the error is detected, not after an arbitrary amount of time > > to give the user a chance of handling the return from the syscall. >=20 > Well, the documented behavior of write(2) - and the one I have seen on > many cases when writing to a socket or a FIFO - is that the first time a > write is attempted after the fd is no longer available for writing, > write(2) returns -1 and sets errno to EPIPE. This is the way read(2) > behaves, too; this is the way programs are written to accommodate - > dnscache does a check for write(2) returning -1, and closes the > connection if this condition is detected. >=20 > IMHO, this is way more logical - the system call should first try to > return an error, so the application can detect a problem *immediately*, > not asynchronously via signal handlers setting flags and such. I - and > many others, apparently - have come to depend on the fact that the first > read(2) or write(2) operation on a closed socket will return -1, and > only if I am foolish enough to attempt a second one will the system send > me a signal (isn't this the whole purpose of SIGPIPE - forcibly > terminate foolish applications which do not honor errors signalled by > return code?). >=20 > > Sounds like the correct approach is to set the handler for > > SIGPIPE to sig_ign >=20 > This would work, but is somewhat besides the point IMHO. >=20 > > Maybe one should wonder why this is not just the default given > > that you can get this signal not because your program > > did something wrong, but because the other end did. >=20 > See above; it is much easier for the program to understand that > something is wrong if the system call will return -1, *as documented* in > the write(2) manual page. Actually, I wonder if I have answered my own question. dnscache seems to use poll(2), and it would be poll(2)'s task to notify the program of any exceptional (error) conditions. I wonder if it is possible that dnscache does not handle POLLERR properly... Let me check. G'luck, Peter --=20 Peter Pentchev roam@ringlet.net roam@sbnd.net roam@FreeBSD.org PGP key: http://people.FreeBSD.org/~roam/roam.key.asc Key fingerprint FDBA FD79 C26F 3C51 C95E DF9E ED18 B68D 1619 4553 Do you think anybody has ever had *precisely this thought* before? --H1spWtNR+x+ondvy Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (FreeBSD) iD8DBQE+eD9S7Ri2jRYZRVMRAhmsAJ9buEeyiRm0N4kwld9tRu8TK4Q2+wCeN1Wn SQGu/NixCbHwrbNbIsjd7+c= =M9hd -----END PGP SIGNATURE----- --H1spWtNR+x+ondvy-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-net" in the body of the message