2 * Copyright (c) 2000-2001, 2005-2006 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * By using this file, you agree to the terms and conditions set
11 * forth in the LICENSE file which can be found at the top level of
12 * the sendmail distribution.
15 #pragma ident "%Z%%M% %I% %E% SMI"
18 SM_RCSID("@(#)$Id: refill.c,v 1.53 2006/02/28 18:48:25 ca Exp $")
29 #include <sm/assert.h>
32 static int sm_lflush
__P((SM_FILE_T
*, int *));
35 ** SM_IO_RD_TIMEOUT -- measured timeout for reads
37 ** This #define uses a select() to wait for the 'fd' to become readable.
38 ** The select() can be active for up to 'To' time. The select() may not
39 ** use all of the the 'To' time. Hence, the amount of "wall-clock" time is
40 ** measured to decide how much to subtract from 'To' to update it. On some
41 ** BSD-based/like systems the timeout for a select() is updated for the
42 ** amount of time used. On many/most systems this does not happen. Therefore
43 ** the updating of 'To' must be done ourselves; a copy of 'To' is passed
44 ** since a BSD-like system will have updated it and we don't want to
45 ** double the time used!
46 ** Note: if a valid 'fd' doesn't exist yet, don't use this (e.g. the
47 ** sendmail buffered file type in sendmail/bf.c; see use below).
50 ** fp -- the file pointer for the active file
51 ** fd -- raw file descriptor (from 'fp') to use for select()
52 ** to -- struct timeval of the timeout
53 ** timeout -- the original timeout value
54 ** sel_ret -- the return value from the select()
57 ** nothing, flow through code
60 #define SM_IO_RD_TIMEOUT(fp, fd, to, timeout, sel_ret) \
62 struct timeval sm_io_to_before, sm_io_to_after, sm_io_to_diff; \
63 fd_set sm_io_to_mask, sm_io_x_mask; \
65 if (timeout == SM_TIME_IMMEDIATE) \
70 if (FD_SETSIZE > 0 && (fd) >= FD_SETSIZE) \
75 FD_ZERO(&sm_io_to_mask); \
76 FD_SET((fd), &sm_io_to_mask); \
77 FD_ZERO(&sm_io_x_mask); \
78 FD_SET((fd), &sm_io_x_mask); \
79 if (gettimeofday(&sm_io_to_before, NULL) < 0) \
83 (sel_ret) = select((fd) + 1, &sm_io_to_mask, NULL, \
84 &sm_io_x_mask, (to)); \
85 } while ((sel_ret) < 0 && errno == EINTR); \
88 /* something went wrong, errno set */ \
90 fp->f_flags |= SMERR; \
93 else if ((sel_ret) == 0) \
99 /* calulate wall-clock time used */ \
100 if (gettimeofday(&sm_io_to_after, NULL) < 0) \
102 timersub(&sm_io_to_after, &sm_io_to_before, &sm_io_to_diff); \
103 timersub((to), &sm_io_to_diff, (to)); \
107 ** SM_LFLUSH -- flush a file if it is line buffered and writable
110 ** fp -- file pointer to flush
111 ** timeout -- original timeout value (in milliseconds)
114 ** Failure: returns SM_IO_EOF and sets errno
115 ** Success: returns 0
119 sm_lflush(fp
, timeout
)
124 if ((fp
->f_flags
& (SMLBF
|SMWR
)) == (SMLBF
|SMWR
))
125 return sm_flush(fp
, timeout
);
130 ** SM_REFILL -- refill a buffer
133 ** fp -- file pointer for buffer refill
134 ** timeout -- time to complete filling the buffer in milliseconds
137 ** Success: returns 0
138 ** Failure: returns SM_IO_EOF
142 sm_refill(fp
, timeout
)
143 register SM_FILE_T
*fp
;
150 if (timeout
== SM_TIME_DEFAULT
)
151 timeout
= fp
->f_timeout
;
152 if (timeout
== SM_TIME_IMMEDIATE
)
155 ** Filling the buffer will take time and we are wanted to
156 ** return immediately. And we're not EOF or ERR really.
157 ** So... the failure is we couldn't do it in time.
161 fp
->f_r
= 0; /* just to be sure */
165 /* make sure stdio is set up */
169 fp
->f_r
= 0; /* largely a convenience for callers */
171 if (fp
->f_flags
& SMFEOF
)
174 SM_CONVERT_TIME(fp
, fd
, timeout
, &to
);
176 /* if not already reading, have to be reading and writing */
177 if ((fp
->f_flags
& SMRD
) == 0)
179 if ((fp
->f_flags
& SMRW
) == 0)
182 fp
->f_flags
|= SMERR
;
186 /* switch to reading */
187 if (fp
->f_flags
& SMWR
)
189 if (sm_flush(fp
, &timeout
))
191 fp
->f_flags
&= ~SMWR
;
200 ** We were reading. If there is an ungetc buffer,
201 ** we must have been reading from that. Drop it,
202 ** restoring the previous buffer (if any). If there
203 ** is anything in that buffer, return.
209 if ((fp
->f_r
= fp
->f_ur
) != 0)
213 /* revert blocking state */
219 if (fp
->f_bf
.smb_base
== NULL
)
223 ** Before reading from a line buffered or unbuffered file,
224 ** flush all line buffered output files, per the ANSI C standard.
227 if (fp
->f_flags
& (SMLBF
|SMNBF
))
228 (void) sm_fwalk(sm_lflush
, &timeout
);
231 ** If this file is linked to another, and we are going to hang
232 ** on the read, flush the linked file before continuing.
235 if (fp
->f_flushfp
!= NULL
&&
236 (*fp
->f_getinfo
)(fp
, SM_IO_IS_READABLE
, NULL
) <= 0)
237 sm_flush(fp
->f_flushfp
, &timeout
);
239 fp
->f_p
= fp
->f_bf
.smb_base
;
242 ** The do-while loop stops trying to read when something is read
243 ** or it appears that the timeout has expired before finding
244 ** something available to be read (via select()).
250 errno
= 0; /* needed to ensure EOF correctly found */
251 r
= (*fp
->f_read
)(fp
, (char *)fp
->f_p
, fp
->f_bf
.smb_size
);
254 if (r
== 0 && errno
== 0)
255 break; /* EOF found */
256 if (IS_IO_ERROR(fd
, r
, timeout
))
257 goto err
; /* errno set */
259 /* read would block */
260 SM_IO_RD_TIMEOUT(fp
, fd
, &to
, timeout
, ret
);
262 } while (r
<= 0 && ret
> 0);
268 fp
->f_flags
|= SMFEOF
;
270 fp
->f_flags
|= SMERR
;
279 ** SM_RGET -- refills buffer and returns first character
281 ** Handle sm_getc() when the buffer ran out:
282 ** Refill, then return the first character in the newly-filled buffer.
285 ** fp -- file pointer to work on
286 ** timeout -- time to complete refill
289 ** Success: first character in refilled buffer as an int
290 ** Failure: SM_IO_EOF
295 register SM_FILE_T
*fp
;
298 if (sm_refill(fp
, timeout
) == 0)