Postfix3.3.1
peekfd.c
[詳解]
1 /*++
2 /* NAME
3 /* peekfd 3
4 /* SUMMARY
5 /* determine amount of data ready to read
6 /* SYNOPSIS
7 /* #include <iostuff.h>
8 /*
9 /* ssize_t peekfd(fd)
10 /* int fd;
11 /* DESCRIPTION
12 /* peekfd() attempts to find out how many bytes are available to
13 /* be read from the named file descriptor. The result value is
14 /* the number of available bytes.
15 /* DIAGNOSTICS
16 /* peekfd() returns -1 in case of trouble. The global \fIerrno\fR
17 /* variable reflects the nature of the problem.
18 /* BUGS
19 /* On some systems, non-blocking read() may fail even after a
20 /* positive return from peekfd(). The smtp-sink program works
21 /* around this by using the readable() function instead.
22 /* LICENSE
23 /* .ad
24 /* .fi
25 /* The Secure Mailer license must be distributed with this software.
26 /* AUTHOR(S)
27 /* Wietse Venema
28 /* IBM T.J. Watson Research
29 /* P.O. Box 704
30 /* Yorktown Heights, NY 10598, USA
31 /*
32 /* Wietse Venema
33 /* Google, Inc.
34 /* 111 8th Avenue
35 /* New York, NY 10011, USA
36 /*--*/
37 
38 /* System library. */
39 
40 #include <sys_defs.h>
41 #include <sys/ioctl.h>
42 #ifdef FIONREAD_IN_SYS_FILIO_H
43 #include <sys/filio.h>
44 #endif
45 #ifdef FIONREAD_IN_TERMIOS_H
46 #include <termios.h>
47 #endif
48 #include <unistd.h>
49 
50 #ifndef SHUT_RDWR
51 #define SHUT_RDWR 2
52 #endif
53 
54 /* Utility library. */
55 
56 #include "iostuff.h"
57 
58 /* peekfd - return amount of data ready to read */
59 
60 ssize_t peekfd(int fd)
61 {
62 
63  /*
64  * Anticipate a series of system-dependent code fragments.
65  */
66 #ifdef FIONREAD
67  int count;
68 
69 #ifdef SUNOS5
70 
71  /*
72  * With Solaris10, write_wait() hangs in poll() until timeout, when
73  * invoked after peekfd() has received an ECONNRESET error indication.
74  * This happens when a client sends QUIT and closes the connection
75  * immediately.
76  */
77  if (ioctl(fd, FIONREAD, (char *) &count) < 0) {
78  (void) shutdown(fd, SHUT_RDWR);
79  return (-1);
80  } else {
81  return (count);
82  }
83 #else /* SUNOS5 */
84  return (ioctl(fd, FIONREAD, (char *) &count) < 0 ? -1 : count);
85 #endif /* SUNOS5 */
86 #else
87 #error "don't know how to look ahead"
88 #endif
89 }
#define SHUT_RDWR
Definition: peekfd.c:51
ssize_t peekfd(int fd)
Definition: peekfd.c:60