10Sstevel@tonic-gate /*
2*1658Sjbeck * Copyright (c) 2000-2001, 2005-2006 Sendmail, Inc. and its suppliers.
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate * Copyright (c) 1990, 1993
50Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
60Sstevel@tonic-gate *
70Sstevel@tonic-gate * This code is derived from software contributed to Berkeley by
80Sstevel@tonic-gate * Chris Torek.
90Sstevel@tonic-gate *
100Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set
110Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of
120Sstevel@tonic-gate * the sendmail distribution.
130Sstevel@tonic-gate */
140Sstevel@tonic-gate
150Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
160Sstevel@tonic-gate
170Sstevel@tonic-gate #include <sm/gen.h>
18*1658Sjbeck SM_RCSID("@(#)$Id: refill.c,v 1.53 2006/02/28 18:48:25 ca Exp $")
190Sstevel@tonic-gate #include <stdlib.h>
200Sstevel@tonic-gate #include <unistd.h>
210Sstevel@tonic-gate #include <errno.h>
220Sstevel@tonic-gate #include <setjmp.h>
230Sstevel@tonic-gate #include <signal.h>
24616Sjbeck #include <sm/time.h>
250Sstevel@tonic-gate #include <fcntl.h>
260Sstevel@tonic-gate #include <string.h>
270Sstevel@tonic-gate #include <sm/io.h>
280Sstevel@tonic-gate #include <sm/conf.h>
290Sstevel@tonic-gate #include <sm/assert.h>
300Sstevel@tonic-gate #include "local.h"
310Sstevel@tonic-gate
320Sstevel@tonic-gate static int sm_lflush __P((SM_FILE_T *, int *));
330Sstevel@tonic-gate
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate ** SM_IO_RD_TIMEOUT -- measured timeout for reads
360Sstevel@tonic-gate **
370Sstevel@tonic-gate ** This #define uses a select() to wait for the 'fd' to become readable.
380Sstevel@tonic-gate ** The select() can be active for up to 'To' time. The select() may not
390Sstevel@tonic-gate ** use all of the the 'To' time. Hence, the amount of "wall-clock" time is
400Sstevel@tonic-gate ** measured to decide how much to subtract from 'To' to update it. On some
410Sstevel@tonic-gate ** BSD-based/like systems the timeout for a select() is updated for the
420Sstevel@tonic-gate ** amount of time used. On many/most systems this does not happen. Therefore
430Sstevel@tonic-gate ** the updating of 'To' must be done ourselves; a copy of 'To' is passed
440Sstevel@tonic-gate ** since a BSD-like system will have updated it and we don't want to
450Sstevel@tonic-gate ** double the time used!
460Sstevel@tonic-gate ** Note: if a valid 'fd' doesn't exist yet, don't use this (e.g. the
470Sstevel@tonic-gate ** sendmail buffered file type in sendmail/bf.c; see use below).
480Sstevel@tonic-gate **
490Sstevel@tonic-gate ** Parameters
500Sstevel@tonic-gate ** fp -- the file pointer for the active file
510Sstevel@tonic-gate ** fd -- raw file descriptor (from 'fp') to use for select()
520Sstevel@tonic-gate ** to -- struct timeval of the timeout
530Sstevel@tonic-gate ** timeout -- the original timeout value
540Sstevel@tonic-gate ** sel_ret -- the return value from the select()
550Sstevel@tonic-gate **
560Sstevel@tonic-gate ** Returns:
570Sstevel@tonic-gate ** nothing, flow through code
580Sstevel@tonic-gate */
590Sstevel@tonic-gate
600Sstevel@tonic-gate #define SM_IO_RD_TIMEOUT(fp, fd, to, timeout, sel_ret) \
610Sstevel@tonic-gate { \
620Sstevel@tonic-gate struct timeval sm_io_to_before, sm_io_to_after, sm_io_to_diff; \
630Sstevel@tonic-gate fd_set sm_io_to_mask, sm_io_x_mask; \
640Sstevel@tonic-gate errno = 0; \
650Sstevel@tonic-gate if (timeout == SM_TIME_IMMEDIATE) \
660Sstevel@tonic-gate { \
670Sstevel@tonic-gate errno = EAGAIN; \
680Sstevel@tonic-gate return SM_IO_EOF; \
690Sstevel@tonic-gate } \
700Sstevel@tonic-gate if (FD_SETSIZE > 0 && (fd) >= FD_SETSIZE) \
710Sstevel@tonic-gate { \
720Sstevel@tonic-gate errno = EINVAL; \
730Sstevel@tonic-gate return SM_IO_EOF; \
740Sstevel@tonic-gate } \
750Sstevel@tonic-gate FD_ZERO(&sm_io_to_mask); \
760Sstevel@tonic-gate FD_SET((fd), &sm_io_to_mask); \
770Sstevel@tonic-gate FD_ZERO(&sm_io_x_mask); \
780Sstevel@tonic-gate FD_SET((fd), &sm_io_x_mask); \
790Sstevel@tonic-gate if (gettimeofday(&sm_io_to_before, NULL) < 0) \
800Sstevel@tonic-gate return SM_IO_EOF; \
81*1658Sjbeck do \
82*1658Sjbeck { \
83*1658Sjbeck (sel_ret) = select((fd) + 1, &sm_io_to_mask, NULL, \
84*1658Sjbeck &sm_io_x_mask, (to)); \
85*1658Sjbeck } while ((sel_ret) < 0 && errno == EINTR); \
860Sstevel@tonic-gate if ((sel_ret) < 0) \
870Sstevel@tonic-gate { \
880Sstevel@tonic-gate /* something went wrong, errno set */ \
890Sstevel@tonic-gate fp->f_r = 0; \
900Sstevel@tonic-gate fp->f_flags |= SMERR; \
910Sstevel@tonic-gate return SM_IO_EOF; \
920Sstevel@tonic-gate } \
930Sstevel@tonic-gate else if ((sel_ret) == 0) \
940Sstevel@tonic-gate { \
950Sstevel@tonic-gate /* timeout */ \
960Sstevel@tonic-gate errno = EAGAIN; \
970Sstevel@tonic-gate return SM_IO_EOF; \
980Sstevel@tonic-gate } \
990Sstevel@tonic-gate /* calulate wall-clock time used */ \
1000Sstevel@tonic-gate if (gettimeofday(&sm_io_to_after, NULL) < 0) \
1010Sstevel@tonic-gate return SM_IO_EOF; \
102*1658Sjbeck timersub(&sm_io_to_after, &sm_io_to_before, &sm_io_to_diff); \
1030Sstevel@tonic-gate timersub((to), &sm_io_to_diff, (to)); \
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate ** SM_LFLUSH -- flush a file if it is line buffered and writable
1080Sstevel@tonic-gate **
1090Sstevel@tonic-gate ** Parameters:
1100Sstevel@tonic-gate ** fp -- file pointer to flush
1110Sstevel@tonic-gate ** timeout -- original timeout value (in milliseconds)
1120Sstevel@tonic-gate **
1130Sstevel@tonic-gate ** Returns:
1140Sstevel@tonic-gate ** Failure: returns SM_IO_EOF and sets errno
1150Sstevel@tonic-gate ** Success: returns 0
1160Sstevel@tonic-gate */
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate static int
sm_lflush(fp,timeout)1190Sstevel@tonic-gate sm_lflush(fp, timeout)
1200Sstevel@tonic-gate SM_FILE_T *fp;
1210Sstevel@tonic-gate int *timeout;
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate if ((fp->f_flags & (SMLBF|SMWR)) == (SMLBF|SMWR))
1250Sstevel@tonic-gate return sm_flush(fp, timeout);
1260Sstevel@tonic-gate return 0;
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate ** SM_REFILL -- refill a buffer
1310Sstevel@tonic-gate **
1320Sstevel@tonic-gate ** Parameters:
1330Sstevel@tonic-gate ** fp -- file pointer for buffer refill
1340Sstevel@tonic-gate ** timeout -- time to complete filling the buffer in milliseconds
1350Sstevel@tonic-gate **
1360Sstevel@tonic-gate ** Returns:
1370Sstevel@tonic-gate ** Success: returns 0
1380Sstevel@tonic-gate ** Failure: returns SM_IO_EOF
1390Sstevel@tonic-gate */
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate int
sm_refill(fp,timeout)1420Sstevel@tonic-gate sm_refill(fp, timeout)
1430Sstevel@tonic-gate register SM_FILE_T *fp;
1440Sstevel@tonic-gate int timeout;
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate int ret, r;
1470Sstevel@tonic-gate struct timeval to;
1480Sstevel@tonic-gate int fd;
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate if (timeout == SM_TIME_DEFAULT)
1510Sstevel@tonic-gate timeout = fp->f_timeout;
1520Sstevel@tonic-gate if (timeout == SM_TIME_IMMEDIATE)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate ** Filling the buffer will take time and we are wanted to
1560Sstevel@tonic-gate ** return immediately. And we're not EOF or ERR really.
1570Sstevel@tonic-gate ** So... the failure is we couldn't do it in time.
1580Sstevel@tonic-gate */
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate errno = EAGAIN;
1610Sstevel@tonic-gate fp->f_r = 0; /* just to be sure */
1620Sstevel@tonic-gate return 0;
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /* make sure stdio is set up */
1660Sstevel@tonic-gate if (!Sm_IO_DidInit)
1670Sstevel@tonic-gate sm_init();
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate fp->f_r = 0; /* largely a convenience for callers */
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if (fp->f_flags & SMFEOF)
1720Sstevel@tonic-gate return SM_IO_EOF;
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate SM_CONVERT_TIME(fp, fd, timeout, &to);
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /* if not already reading, have to be reading and writing */
1770Sstevel@tonic-gate if ((fp->f_flags & SMRD) == 0)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate if ((fp->f_flags & SMRW) == 0)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate errno = EBADF;
1820Sstevel@tonic-gate fp->f_flags |= SMERR;
1830Sstevel@tonic-gate return SM_IO_EOF;
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate /* switch to reading */
1870Sstevel@tonic-gate if (fp->f_flags & SMWR)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate if (sm_flush(fp, &timeout))
1900Sstevel@tonic-gate return SM_IO_EOF;
1910Sstevel@tonic-gate fp->f_flags &= ~SMWR;
1920Sstevel@tonic-gate fp->f_w = 0;
1930Sstevel@tonic-gate fp->f_lbfsize = 0;
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate fp->f_flags |= SMRD;
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate else
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate ** We were reading. If there is an ungetc buffer,
2010Sstevel@tonic-gate ** we must have been reading from that. Drop it,
2020Sstevel@tonic-gate ** restoring the previous buffer (if any). If there
2030Sstevel@tonic-gate ** is anything in that buffer, return.
2040Sstevel@tonic-gate */
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate if (HASUB(fp))
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate FREEUB(fp);
2090Sstevel@tonic-gate if ((fp->f_r = fp->f_ur) != 0)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate fp->f_p = fp->f_up;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate /* revert blocking state */
2140Sstevel@tonic-gate return 0;
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate if (fp->f_bf.smb_base == NULL)
2200Sstevel@tonic-gate sm_makebuf(fp);
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate ** Before reading from a line buffered or unbuffered file,
2240Sstevel@tonic-gate ** flush all line buffered output files, per the ANSI C standard.
2250Sstevel@tonic-gate */
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate if (fp->f_flags & (SMLBF|SMNBF))
2280Sstevel@tonic-gate (void) sm_fwalk(sm_lflush, &timeout);
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate ** If this file is linked to another, and we are going to hang
2320Sstevel@tonic-gate ** on the read, flush the linked file before continuing.
2330Sstevel@tonic-gate */
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate if (fp->f_flushfp != NULL &&
2360Sstevel@tonic-gate (*fp->f_getinfo)(fp, SM_IO_IS_READABLE, NULL) <= 0)
2370Sstevel@tonic-gate sm_flush(fp->f_flushfp, &timeout);
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate fp->f_p = fp->f_bf.smb_base;
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate ** The do-while loop stops trying to read when something is read
2430Sstevel@tonic-gate ** or it appears that the timeout has expired before finding
2440Sstevel@tonic-gate ** something available to be read (via select()).
2450Sstevel@tonic-gate */
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate ret = 0;
2480Sstevel@tonic-gate do
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate errno = 0; /* needed to ensure EOF correctly found */
2510Sstevel@tonic-gate r = (*fp->f_read)(fp, (char *)fp->f_p, fp->f_bf.smb_size);
2520Sstevel@tonic-gate if (r <= 0)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate if (r == 0 && errno == 0)
2550Sstevel@tonic-gate break; /* EOF found */
2560Sstevel@tonic-gate if (IS_IO_ERROR(fd, r, timeout))
2570Sstevel@tonic-gate goto err; /* errno set */
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate /* read would block */
2600Sstevel@tonic-gate SM_IO_RD_TIMEOUT(fp, fd, &to, timeout, ret);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate } while (r <= 0 && ret > 0);
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate err:
2650Sstevel@tonic-gate if (r <= 0)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate if (r == 0)
2680Sstevel@tonic-gate fp->f_flags |= SMFEOF;
2690Sstevel@tonic-gate else
2700Sstevel@tonic-gate fp->f_flags |= SMERR;
2710Sstevel@tonic-gate fp->f_r = 0;
2720Sstevel@tonic-gate return SM_IO_EOF;
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate fp->f_r = r;
2750Sstevel@tonic-gate return 0;
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate ** SM_RGET -- refills buffer and returns first character
2800Sstevel@tonic-gate **
2810Sstevel@tonic-gate ** Handle sm_getc() when the buffer ran out:
2820Sstevel@tonic-gate ** Refill, then return the first character in the newly-filled buffer.
2830Sstevel@tonic-gate **
2840Sstevel@tonic-gate ** Parameters:
2850Sstevel@tonic-gate ** fp -- file pointer to work on
2860Sstevel@tonic-gate ** timeout -- time to complete refill
2870Sstevel@tonic-gate **
2880Sstevel@tonic-gate ** Returns:
2890Sstevel@tonic-gate ** Success: first character in refilled buffer as an int
2900Sstevel@tonic-gate ** Failure: SM_IO_EOF
2910Sstevel@tonic-gate */
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate int
sm_rget(fp,timeout)2940Sstevel@tonic-gate sm_rget(fp, timeout)
2950Sstevel@tonic-gate register SM_FILE_T *fp;
2960Sstevel@tonic-gate int timeout;
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate if (sm_refill(fp, timeout) == 0)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate fp->f_r--;
3010Sstevel@tonic-gate return *fp->f_p++;
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate return SM_IO_EOF;
3040Sstevel@tonic-gate }
305