Postfix3.3.1
write_buf.c
[詳解]
1 /*++
2 /* NAME
3 /* write_buf 3
4 /* SUMMARY
5 /* write buffer or bust
6 /* SYNOPSIS
7 /* #include <iostuff.h>
8 /*
9 /* ssize_t write_buf(fd, buf, len, timeout)
10 /* int fd;
11 /* const char *buf;
12 /* ssize_t len;
13 /* int timeout;
14 /* DESCRIPTION
15 /* write_buf() writes a buffer to the named stream in as many
16 /* fragments as needed, and returns the number of bytes written,
17 /* which is always the number requested or an error indication.
18 /*
19 /* Arguments:
20 /* .IP fd
21 /* File descriptor in the range 0..FD_SETSIZE.
22 /* .IP buf
23 /* Address of data to be written.
24 /* .IP len
25 /* Amount of data to be written.
26 /* .IP timeout
27 /* Bounds the time in seconds to wait until \fIfd\fD becomes writable.
28 /* A value <= 0 means do not wait; this is useful only when \fIfd\fR
29 /* uses blocking I/O.
30 /* DIAGNOSTICS
31 /* write_buf() returns -1 in case of trouble. The global \fIerrno\fR
32 /* variable reflects the nature of the problem.
33 /* LICENSE
34 /* .ad
35 /* .fi
36 /* The Secure Mailer license must be distributed with this software.
37 /* AUTHOR(S)
38 /* Wietse Venema
39 /* IBM T.J. Watson Research
40 /* P.O. Box 704
41 /* Yorktown Heights, NY 10598, USA
42 /*--*/
43 
44 /* System library. */
45 
46 #include <sys_defs.h>
47 #include <unistd.h>
48 #include <errno.h>
49 #include <time.h>
50 
51 /* Utility library. */
52 
53 #include <msg.h>
54 #include <iostuff.h>
55 
56 /* write_buf - write buffer or bust */
57 
58 ssize_t write_buf(int fd, const char *buf, ssize_t len, int timeout)
59 {
60  const char *start = buf;
61  ssize_t count;
62  time_t expire;
63  int time_left = timeout;
64 
65  if (time_left > 0)
66  expire = time((time_t *) 0) + time_left;
67 
68  while (len > 0) {
69  if (time_left > 0 && write_wait(fd, time_left) < 0)
70  return (-1);
71  if ((count = write(fd, buf, len)) < 0) {
72  if ((errno == EAGAIN && time_left > 0) || errno == EINTR)
73  /* void */ ;
74  else
75  return (-1);
76  } else {
77  buf += count;
78  len -= count;
79  }
80  if (len > 0 && time_left > 0) {
81  time_left = expire - time((time_t *) 0);
82  if (time_left <= 0) {
83  errno = ETIMEDOUT;
84  return (-1);
85  }
86  }
87  }
88  return (buf - start);
89 }
ssize_t write_buf(int fd, const char *buf, ssize_t len, int timeout)
Definition: write_buf.c:58
#define write_wait(fd, timeout)
Definition: iostuff.h:40
unsigned timeout
Definition: watchdog.c:107