Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 27 Feb 2008 08:25:10 -0500
From:      James Bailie <jimmy@mammothcheese.ca>
To:        Mark Linn <mark.linn@gmail.com>, freebsd-hackers@freebsd.org
Subject:   Re: non-blocking io, EINTR
Message-ID:  <47C564B6.10906@mammothcheese.ca>
In-Reply-To: <84fb42ef0802270107y4ddb8fd2scd83fe086414869f@mail.gmail.com>
References:  <84fb42ef0802270107y4ddb8fd2scd83fe086414869f@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Mark Linn wrote:

 > I am setting the O_NONBLOCK flag on a socket file descriptor using fcntl,
 >
 > will a read() on the socket return EINTR when the process get a signal?

By default, read() will restart itself automatically, regardless
of whether the socket is blocking or not, as long as there is
data to be read in the socket receive buffer.  You can change
this behavior by calling sigaction().  For example, the code
below will make SIGTERM interrupt system calls.  They will return
an error code, usually -1, with the global errno set to EINTR.
If the socket is non-blocking and the socket receive buffer is
empty, then read() will also return an error, but with errno set
to EWOULDBLOCK.

#include <signal.h>

struct sigaction sigact;

sigact.sa_handler = sigterm_handler;
sigemptyset( &sigact.sa_mask );
sigact.sa_flags = 0;

if ( sigaction( SIGTERM, &sigact, NULL ) < 0 )
{
    perror( "sigaction()" );
    exit( 1 );
}

-- 
James Bailie <jimmy@mammothcheese.ca>
http://www.mammothcheese.ca



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?47C564B6.10906>