2 * Copyright (c) 2000-2001 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.
16 SM_RCSID("@(#)$Id: refill.c,v 1.49.2.1 2002/09/09 21:38:08 gshapiro Exp $")
27 #include <sm/assert.h>
30 static int sm_lflush
__P((SM_FILE_T
*, int *));
33 ** SM_IO_RD_TIMEOUT -- measured timeout for reads
35 ** This #define uses a select() to wait for the 'fd' to become readable.
36 ** The select() can be active for up to 'To' time. The select() may not
37 ** use all of the the 'To' time. Hence, the amount of "wall-clock" time is
38 ** measured to decide how much to subtract from 'To' to update it. On some
39 ** BSD-based/like systems the timeout for a select() is updated for the
40 ** amount of time used. On many/most systems this does not happen. Therefore
41 ** the updating of 'To' must be done ourselves; a copy of 'To' is passed
42 ** since a BSD-like system will have updated it and we don't want to
43 ** double the time used!
44 ** Note: if a valid 'fd' doesn't exist yet, don't use this (e.g. the
45 ** sendmail buffered file type in sendmail/bf.c; see use below).
48 ** fp -- the file pointer for the active file
49 ** fd -- raw file descriptor (from 'fp') to use for select()
50 ** to -- struct timeval of the timeout
51 ** timeout -- the original timeout value
52 ** sel_ret -- the return value from the select()
55 ** nothing, flow through code
58 #define SM_IO_RD_TIMEOUT(fp, fd, to, timeout, sel_ret) \
60 struct timeval sm_io_to_before, sm_io_to_after, sm_io_to_diff; \
61 fd_set sm_io_to_mask, sm_io_x_mask; \
63 if (timeout == SM_TIME_IMMEDIATE) \
68 if (FD_SETSIZE > 0 && (fd) >= FD_SETSIZE) \
73 FD_ZERO(&sm_io_to_mask); \
74 FD_SET((fd), &sm_io_to_mask); \
75 FD_ZERO(&sm_io_x_mask); \
76 FD_SET((fd), &sm_io_x_mask); \
77 if (gettimeofday(&sm_io_to_before, NULL) < 0) \
79 (sel_ret) = select((fd) + 1, &sm_io_to_mask, NULL, \
80 &sm_io_x_mask, (to)); \
83 /* something went wrong, errno set */ \
85 fp->f_flags |= SMERR; \
88 else if ((sel_ret) == 0) \
94 /* calulate wall-clock time used */ \
95 if (gettimeofday(&sm_io_to_after, NULL) < 0) \
97 timersub(&sm_io_to_before, &sm_io_to_after, &sm_io_to_diff); \
98 timersub((to), &sm_io_to_diff, (to)); \
102 ** SM_LFLUSH -- flush a file if it is line buffered and writable
105 ** fp -- file pointer to flush
106 ** timeout -- original timeout value (in milliseconds)
109 ** Failure: returns SM_IO_EOF and sets errno
110 ** Success: returns 0
114 sm_lflush(fp
, timeout
)
119 if ((fp
->f_flags
& (SMLBF
|SMWR
)) == (SMLBF
|SMWR
))
120 return sm_flush(fp
, timeout
);
125 ** SM_REFILL -- refill a buffer
128 ** fp -- file pointer for buffer refill
129 ** timeout -- time to complete filling the buffer in milliseconds
132 ** Success: returns 0
133 ** Failure: returns SM_IO_EOF
137 sm_refill(fp
, timeout
)
138 register SM_FILE_T
*fp
;
145 if (timeout
== SM_TIME_DEFAULT
)
146 timeout
= fp
->f_timeout
;
147 if (timeout
== SM_TIME_IMMEDIATE
)
150 ** Filling the buffer will take time and we are wanted to
151 ** return immediately. And we're not EOF or ERR really.
152 ** So... the failure is we couldn't do it in time.
156 fp
->f_r
= 0; /* just to be sure */
160 /* make sure stdio is set up */
164 fp
->f_r
= 0; /* largely a convenience for callers */
166 if (fp
->f_flags
& SMFEOF
)
169 SM_CONVERT_TIME(fp
, fd
, timeout
, &to
);
171 /* if not already reading, have to be reading and writing */
172 if ((fp
->f_flags
& SMRD
) == 0)
174 if ((fp
->f_flags
& SMRW
) == 0)
177 fp
->f_flags
|= SMERR
;
181 /* switch to reading */
182 if (fp
->f_flags
& SMWR
)
184 if (sm_flush(fp
, &timeout
))
186 fp
->f_flags
&= ~SMWR
;
195 ** We were reading. If there is an ungetc buffer,
196 ** we must have been reading from that. Drop it,
197 ** restoring the previous buffer (if any). If there
198 ** is anything in that buffer, return.
204 if ((fp
->f_r
= fp
->f_ur
) != 0)
208 /* revert blocking state */
214 if (fp
->f_bf
.smb_base
== NULL
)
218 ** Before reading from a line buffered or unbuffered file,
219 ** flush all line buffered output files, per the ANSI C standard.
222 if (fp
->f_flags
& (SMLBF
|SMNBF
))
223 (void) sm_fwalk(sm_lflush
, &timeout
);
226 ** If this file is linked to another, and we are going to hang
227 ** on the read, flush the linked file before continuing.
230 if (fp
->f_flushfp
!= NULL
&&
231 (*fp
->f_getinfo
)(fp
, SM_IO_IS_READABLE
, NULL
) <= 0)
232 sm_flush(fp
->f_flushfp
, &timeout
);
234 fp
->f_p
= fp
->f_bf
.smb_base
;
237 ** The do-while loop stops trying to read when something is read
238 ** or it appears that the timeout has expired before finding
239 ** something available to be read (via select()).
245 errno
= 0; /* needed to ensure EOF correctly found */
246 r
= (*fp
->f_read
)(fp
, (char *)fp
->f_p
, fp
->f_bf
.smb_size
);
249 if (r
== 0 && errno
== 0)
250 break; /* EOF found */
251 if (IS_IO_ERROR(fd
, r
, timeout
))
252 goto err
; /* errno set */
254 /* read would block */
255 SM_IO_RD_TIMEOUT(fp
, fd
, &to
, timeout
, ret
);
257 } while (r
<= 0 && ret
> 0);
263 fp
->f_flags
|= SMFEOF
;
265 fp
->f_flags
|= SMERR
;
274 ** SM_RGET -- refills buffer and returns first character
276 ** Handle sm_getc() when the buffer ran out:
277 ** Refill, then return the first character in the newly-filled buffer.
280 ** fp -- file pointer to work on
281 ** timeout -- time to complete refill
284 ** Success: first character in refilled buffer as an int
285 ** Failure: SM_IO_EOF
290 register SM_FILE_T
*fp
;
293 if (sm_refill(fp
, timeout
) == 0)