From owner-freebsd-hackers@FreeBSD.ORG Fri Aug 8 14:33:59 2008 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C7E60106564A for ; Fri, 8 Aug 2008 14:33:59 +0000 (UTC) (envelope-from unixmania@gmail.com) Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.174]) by mx1.freebsd.org (Postfix) with ESMTP id 5759A8FC32 for ; Fri, 8 Aug 2008 14:33:58 +0000 (UTC) (envelope-from unixmania@gmail.com) Received: by ug-out-1314.google.com with SMTP id q2so40869uge.37 for ; Fri, 08 Aug 2008 07:33:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=3LbnMryAYb0UsCIFCHfVvnvI9/B5Jqb7VCQ8RRv+ARM=; b=pFE2ntbCdZXylmymOs6n7J7cBxLD2naVG3JekQ0pug87oyEMEilJE7kEvKNNb20A24 Ymikg0n9KtJgz33pkhoCcNPpe0BZJWV98d/5MhPfq4kRgeczuKPoRAluTbuxpbBlwktV z6GrFCI6Rb9aqJ/gChGbPBivpgG0nh8CNvumI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=TCucdjx05z64hQN4KHZoh13JcD+0bAGJdlG8VgnyxUwvxbBvXGBCYFaMkM+6oDM4e8 NtMceaB+FUgAkIh2xcXkUZSJOd7C9ohLONvt4j15LGPLmyibjdcAWIKMhnbH/TxKP925 QpQPN5eoGTT9xBFWNTlwTnPCCicea6dhDT4xg= Received: by 10.103.131.18 with SMTP id i18mr3185344mun.116.1218206037760; Fri, 08 Aug 2008 07:33:57 -0700 (PDT) Received: by 10.103.169.13 with HTTP; Fri, 8 Aug 2008 07:33:57 -0700 (PDT) Message-ID: Date: Fri, 8 Aug 2008 11:33:57 -0300 From: "Carlos A. M. dos Santos" To: "Pieter de Goeje" In-Reply-To: <200808081009.56521.pieter@degoeje.nl> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <489B9D4D.4010009@telenix.org> <200808081009.56521.pieter@degoeje.nl> Cc: freebsd-hackers@freebsd.org, freebsd-standards@freebsd.org Subject: Re: read with timeout ?? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 Aug 2008 14:33:59 -0000 On Fri, Aug 8, 2008 at 5:09 AM, Pieter de Goeje wrote: > On Friday 08 August 2008, Carlos A. M. dos Santos wrote: >> On Thu, Aug 7, 2008 at 10:14 PM, Nate Eldredge > wrote: >> > On Thu, 7 Aug 2008, Chuck Robey wrote: >> >> -----BEGIN PGP SIGNED MESSAGE----- >> >> Hash: SHA1 >> >> >> >> I have my head lost in a code problem. I just hit a point where I need >> >> to do a >> >> read from an fd, but I need to associate it with a timeout, on the order >> >> of 1 >> >> second, something like that. I had the feeling that there's a function >> >> in FreeBSD's libc that makes that simple, but I forget the function >> >> name. If anyone can remember something like what I'm talking about, I >> >> sure would appreciate a function name. I can figure out how it works, >> >> if I could only >> >> dredge up that name. >> > >> > man 2 select >> >> If the fd is a socket then you can also use setsockopt(2) to set >> SO_RCVTIMEO and check for EWOULDBLOCK (same as EAGAIN) upon read(2) or >> recv(2) errors. The net effect is the same of using select but the >> syntax is simpler, IMO. > > I think poll(2) is also simpler than select for this purpose. BTW, the setsockopt(2) manual page stands that a send or receive timeout returns with the error EWOULDBLOCK but read(2) recv(2) send(2) and write(2) only list EAGAIN in their ERRORS section. This is harmless on BSD and Linux because EAGAIN and EWOULDBLOCK are the same, but may sound confusing for people porting from/to System V. On HP-UX, for instance, recv(2) lists both EAGAIN and EWOULDBLOCK. The setsockopt man page should be improved in order to explain standards conformance and porting issues. I volunteer to do it but the changes must be reviewed by a native English speaker. -- If you think things can't get worse it's probably only because you lack sufficient imagination.