source: web/old/remctl-2.14/util/fdflag.c @ f6f3e91

web
Last change on this file since f6f3e91 was f6f3e91, checked in by Jessica B. Hamrick <jhamrick@…>, 15 years ago

Preserve directory hierarchy (not sure what happened to it)

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Set or clear file descriptor flags.
3 *
4 * Simple functions (wrappers around fcntl) to set or clear file descriptor
5 * flags like close-on-exec or nonblocking I/O.
6 *
7 * Copyright 2008 Board of Trustees, Leland Stanford Jr. University
8 * Copyright (c) 2004, 2005, 2006
9 *     by Internet Systems Consortium, Inc. ("ISC")
10 * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
11 *     2002, 2003 by The Internet Software Consortium and Rich Salz
12 *
13 * See LICENSE for licensing terms.
14 */
15
16#include <config.h>
17#include <portable/system.h>
18
19#include <errno.h>
20#include <fcntl.h>
21#ifndef O_NONBLOCK
22# include <sys/ioctl.h>
23# if HAVE_SYS_FILIO_H
24#  include <sys/filio.h>
25# endif
26#endif
27
28#include <util/util.h>
29
30
31/*
32 * Set a file to close-on-exec (or clear that setting if the flag is false),
33 * returning true on success and false on failure.
34 *
35 * One is supposed to retrieve the flags, add FD_CLOEXEC, and then set them,
36 * although I've never seen a system with any flags other than close-on-exec.
37 * Do it right anyway; it's not that expensive.
38*/
39bool
40fdflag_close_exec(int fd, bool flag)
41{
42    int oflag, mode;
43
44    oflag = fcntl(fd, F_GETFD, 0);
45    if (oflag < 0)
46        return false;
47    mode = flag ? (oflag | FD_CLOEXEC) : (oflag & ~FD_CLOEXEC);
48    return (fcntl(fd, F_SETFD, mode) == 0);
49}
50
51
52/*
53 * Set a file descriptor to nonblocking (or clear the nonblocking flag if flag
54 * is false), returning true on success and false on failure.
55 *
56 * Always use O_NONBLOCK; O_NDELAY is not the same thing historically.  The
57 * semantics of O_NDELAY are that if the read would block, it returns 0
58 * instead.  This is indistinguishable from an end of file condition.  POSIX
59 * added O_NONBLOCK, which requires read to return -1 and set errno to EAGAIN,
60 * which is what we want.
61 *
62 * FNDELAY (4.3BSD) originally did the correct thing, although it has a
63 * different incompatibility (affecting all users of a socket rather than just
64 * a file descriptor and returning EWOULDBLOCK instead of EAGAIN) that we
65 * probably don't care about.  Using it is probably safe, but BSD should also
66 * have the ioctl, and at least on Solaris FNDELAY does the same thing as
67 * O_NDELAY, not O_NONBLOCK.  So if we don't have O_NONBLOCK, fall back to the
68 * ioctl instead.
69 *
70 * Reference:  Stevens, Advanced Unix Programming, pg. 364.
71 *
72 * Note that O_NONBLOCK is known not to work on earlier versions of ULTRIX,
73 * SunOS, and AIX, possibly not setting the socket nonblocking at all, despite
74 * the fact that they do define it.  It works in later SunOS and, current AIX,
75 * however, and a 1999-10-25 survey of current operating systems failed to
76 * turn up any that didn't handle it correctly (as required by POSIX), while
77 * HP-UX 11.00 did use the broken return-zero semantics of O_NDELAY (most
78 * other operating systems surveyed treated O_NDELAY as synonymous with
79 * O_NONBLOCK).  Accordingly, we currently unconditionally use O_NONBLOCK.  If
80 * this causes too many problems, an autoconf test may be required.
81 */
82#ifdef O_NONBLOCK
83bool
84fdflag_nonblocking(int fd, bool flag)
85{
86    int mode;
87
88    mode = fcntl(fd, F_GETFL, 0);
89    if (mode < 0)
90        return false;
91    mode = (flag ? (mode | O_NONBLOCK) : (mode & ~O_NONBLOCK));
92    return (fcntl(fd, F_SETFL, mode) == 0);
93}
94#else /* !O_NONBLOCK */
95int
96nonblocking(int fd, bool flag)
97{
98    int state;
99
100    state = flag ? 1 : 0;
101    return ioctl(fd, FIONBIO, &state);
102}
103#endif /* !O_NONBLOCK */
Note: See TracBrowser for help on using the repository browser.