xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/non_blocking.c (revision 41fbaed053f8fbfdf9d2a4ee0a7386a3c83f8505)
1*41fbaed0Stron /*	$NetBSD: non_blocking.c,v 1.1.1.1 2009/06/23 10:09:00 tron Exp $	*/
2*41fbaed0Stron 
3*41fbaed0Stron /*++
4*41fbaed0Stron /* NAME
5*41fbaed0Stron /*	non_blocking 3
6*41fbaed0Stron /* SUMMARY
7*41fbaed0Stron /*	set/clear non-blocking flag
8*41fbaed0Stron /* SYNOPSIS
9*41fbaed0Stron /*	#include <iostuff.h>
10*41fbaed0Stron /*
11*41fbaed0Stron /*	int	non_blocking(int fd, int on)
12*41fbaed0Stron /* DESCRIPTION
13*41fbaed0Stron /*	the \fInon_blocking\fR() function manipulates the non-blocking
14*41fbaed0Stron /*	flag for the specified open file, and returns the old setting.
15*41fbaed0Stron /*
16*41fbaed0Stron /*	Arguments:
17*41fbaed0Stron /* .IP fd
18*41fbaed0Stron /*	A file descriptor.
19*41fbaed0Stron /* .IP on
20*41fbaed0Stron /*	For non-blocking I/O, specify a non-zero value (or use the
21*41fbaed0Stron /*	NON_BLOCKING constant); for blocking I/O, specify zero
22*41fbaed0Stron /*	(or use the BLOCKING constant).
23*41fbaed0Stron /*
24*41fbaed0Stron /*	The result is non-zero when the non-blocking flag was enabled.
25*41fbaed0Stron /* DIAGNOSTICS
26*41fbaed0Stron /*	All errors are fatal.
27*41fbaed0Stron /* LICENSE
28*41fbaed0Stron /* .ad
29*41fbaed0Stron /* .fi
30*41fbaed0Stron /*	The Secure Mailer license must be distributed with this software.
31*41fbaed0Stron /* AUTHOR(S)
32*41fbaed0Stron /*	Wietse Venema
33*41fbaed0Stron /*	IBM T.J. Watson Research
34*41fbaed0Stron /*	P.O. Box 704
35*41fbaed0Stron /*	Yorktown Heights, NY 10598, USA
36*41fbaed0Stron /*--*/
37*41fbaed0Stron 
38*41fbaed0Stron /* System interfaces. */
39*41fbaed0Stron 
40*41fbaed0Stron #include "sys_defs.h"
41*41fbaed0Stron #include <fcntl.h>
42*41fbaed0Stron 
43*41fbaed0Stron /* Utility library. */
44*41fbaed0Stron 
45*41fbaed0Stron #include "msg.h"
46*41fbaed0Stron #include "iostuff.h"
47*41fbaed0Stron 
48*41fbaed0Stron /* Backwards compatibility */
49*41fbaed0Stron #ifndef O_NONBLOCK
50*41fbaed0Stron #define PATTERN	FNDELAY
51*41fbaed0Stron #else
52*41fbaed0Stron #define PATTERN	O_NONBLOCK
53*41fbaed0Stron #endif
54*41fbaed0Stron 
55*41fbaed0Stron /* non_blocking - set/clear non-blocking flag */
56*41fbaed0Stron 
non_blocking(fd,on)57*41fbaed0Stron int     non_blocking(fd, on)
58*41fbaed0Stron int     fd;
59*41fbaed0Stron int     on;
60*41fbaed0Stron {
61*41fbaed0Stron     int     flags;
62*41fbaed0Stron 
63*41fbaed0Stron     if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
64*41fbaed0Stron 	msg_fatal("fcntl: get flags: %m");
65*41fbaed0Stron     if (fcntl(fd, F_SETFL, on ? flags | PATTERN : flags & ~PATTERN) < 0)
66*41fbaed0Stron 	msg_fatal("fcntl: set non-blocking flag %s: %m", on ? "on" : "off");
67*41fbaed0Stron     return ((flags & PATTERN) != 0);
68*41fbaed0Stron }
69