2 * Copyright (c) 2000-2001, 2005-2006 Proofpoint, 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.54 2013-11-22 20:51:43 ca Exp $")
27 #include <sm/assert.h>
31 static int sm_lflush __P((SM_FILE_T *, int *));
34 ** SM_IO_RD_TIMEOUT -- measured timeout for reads
36 ** This #define uses a select() to wait for the 'fd' to become readable.
37 ** The select() can be active for up to 'To' time. The select() may not
38 ** use all of the the 'To' time. Hence, the amount of "wall-clock" time is
39 ** measured to decide how much to subtract from 'To' to update it. On some
40 ** BSD-based/like systems the timeout for a select() is updated for the
41 ** amount of time used. On many/most systems this does not happen. Therefore
42 ** the updating of 'To' must be done ourselves; a copy of 'To' is passed
43 ** since a BSD-like system will have updated it and we don't want to
44 ** double the time used!
45 ** Note: if a valid 'fd' doesn't exist yet, don't use this (e.g. the
46 ** sendmail buffered file type in sendmail/bf.c; see use below).
49 ** fp -- the file pointer for the active file
50 ** fd -- raw file descriptor (from 'fp') to use for select()
51 ** to -- struct timeval of the timeout
52 ** timeout -- the original timeout value
53 ** sel_ret -- the return value from the select()
56 ** nothing, flow through code
59 #define SM_IO_RD_TIMEOUT(fp, fd, to, timeout, sel_ret) \
61 struct timeval sm_io_to_before, sm_io_to_after, sm_io_to_diff; \
62 fd_set sm_io_to_mask, sm_io_x_mask; \
64 if (timeout == SM_TIME_IMMEDIATE) \
69 if (!SM_FD_OK_SELECT(fd)) \
74 FD_ZERO(&sm_io_to_mask); \
75 FD_SET((fd), &sm_io_to_mask); \
76 FD_ZERO(&sm_io_x_mask); \
77 FD_SET((fd), &sm_io_x_mask); \
78 if (gettimeofday(&sm_io_to_before, NULL) < 0) \
82 (sel_ret) = select((fd) + 1, &sm_io_to_mask, NULL, \
83 &sm_io_x_mask, (to)); \
84 } while ((sel_ret) < 0 && errno == EINTR); \
87 /* something went wrong, errno set */ \
89 fp->f_flags |= SMERR; \
92 else if ((sel_ret) == 0) \
98 /* calulate wall-clock time used */ \
99 if (gettimeofday(&sm_io_to_after, NULL) < 0) \
101 timersub(&sm_io_to_after, &sm_io_to_before, &sm_io_to_diff); \
102 timersub((to), &sm_io_to_diff, (to)); \
106 ** SM_LFLUSH -- flush a file if it is line buffered and writable
109 ** fp -- file pointer to flush
110 ** timeout -- original timeout value (in milliseconds)
113 ** Failure: returns SM_IO_EOF and sets errno
114 ** Success: returns 0
118 sm_lflush(fp, timeout)
123 if ((fp->f_flags & (SMLBF|SMWR)) == (SMLBF|SMWR))
124 return sm_flush(fp, timeout);
129 ** SM_REFILL -- refill a buffer
132 ** fp -- file pointer for buffer refill
133 ** timeout -- time to complete filling the buffer in milliseconds
136 ** Success: returns 0
137 ** Failure: returns SM_IO_EOF
141 sm_refill(fp, timeout)
142 register SM_FILE_T *fp;
149 if (timeout == SM_TIME_DEFAULT)
150 timeout = fp->f_timeout;
151 if (timeout == SM_TIME_IMMEDIATE)
154 ** Filling the buffer will take time and we are wanted to
155 ** return immediately. And we're not EOF or ERR really.
156 ** So... the failure is we couldn't do it in time.
160 fp->f_r = 0; /* just to be sure */
164 /* make sure stdio is set up */
168 fp->f_r = 0; /* largely a convenience for callers */
170 if (fp->f_flags & SMFEOF)
173 SM_CONVERT_TIME(fp, fd, timeout, &to);
175 /* if not already reading, have to be reading and writing */
176 if ((fp->f_flags & SMRD) == 0)
178 if ((fp->f_flags & SMRW) == 0)
181 fp->f_flags |= SMERR;
185 /* switch to reading */
186 if (fp->f_flags & SMWR)
188 if (sm_flush(fp, &timeout))
190 fp->f_flags &= ~SMWR;
199 ** We were reading. If there is an ungetc buffer,
200 ** we must have been reading from that. Drop it,
201 ** restoring the previous buffer (if any). If there
202 ** is anything in that buffer, return.
208 if ((fp->f_r = fp->f_ur) != 0)
212 /* revert blocking state */
218 if (fp->f_bf.smb_base == NULL)
222 ** Before reading from a line buffered or unbuffered file,
223 ** flush all line buffered output files, per the ANSI C standard.
226 if (fp->f_flags & (SMLBF|SMNBF))
227 (void) sm_fwalk(sm_lflush, &timeout);
230 ** If this file is linked to another, and we are going to hang
231 ** on the read, flush the linked file before continuing.
234 if (fp->f_flushfp != NULL &&
235 (*fp->f_getinfo)(fp, SM_IO_IS_READABLE, NULL) <= 0)
236 sm_flush(fp->f_flushfp, &timeout);
238 fp->f_p = fp->f_bf.smb_base;
241 ** The do-while loop stops trying to read when something is read
242 ** or it appears that the timeout has expired before finding
243 ** something available to be read (via select()).
249 errno = 0; /* needed to ensure EOF correctly found */
250 r = (*fp->f_read)(fp, (char *)fp->f_p, fp->f_bf.smb_size);
253 if (r == 0 && errno == 0)
254 break; /* EOF found */
255 if (IS_IO_ERROR(fd, r, timeout))
256 goto err; /* errno set */
258 /* read would block */
259 SM_IO_RD_TIMEOUT(fp, fd, &to, timeout, ret);
261 } while (r <= 0 && ret > 0);
267 fp->f_flags |= SMFEOF;
269 fp->f_flags |= SMERR;
278 ** SM_RGET -- refills buffer and returns first character
280 ** Handle sm_getc() when the buffer ran out:
281 ** Refill, then return the first character in the newly-filled buffer.
284 ** fp -- file pointer to work on
285 ** timeout -- time to complete refill
288 ** Success: first character in refilled buffer as an int
289 ** Failure: SM_IO_EOF
294 register SM_FILE_T *fp;
297 if (sm_refill(fp, timeout) == 0)