xref: /freebsd/contrib/sendmail/libsm/refill.c (revision ae83180158c4c937f170e31eff311b18c0286a93)
1 /*
2  * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
3  *      All rights reserved.
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Chris Torek.
9  *
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.
13  */
14 
15 #include <sm/gen.h>
16 SM_RCSID("@(#)$Id: refill.c,v 1.49 2001/09/11 04:04:49 gshapiro Exp $")
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <setjmp.h>
21 #include <signal.h>
22 #include <sys/time.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <sm/io.h>
26 #include <sm/conf.h>
27 #include <sm/assert.h>
28 #include "local.h"
29 
30 static int sm_lflush __P((SM_FILE_T *, int *));
31 
32 /*
33 **  SM_IO_RD_TIMEOUT -- measured timeout for reads
34 **
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).
46 **
47 **	Parameters
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()
53 **
54 **	Returns:
55 **		nothing, flow through code
56 */
57 
58 #define SM_IO_RD_TIMEOUT(fp, fd, to, timeout, sel_ret)			\
59 {									\
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;				\
62 	errno = 0;							\
63 	if (timeout == SM_TIME_IMMEDIATE)				\
64 	{								\
65 		errno = EAGAIN;						\
66 		return SM_IO_EOF;					\
67 	}								\
68 	FD_ZERO(&sm_io_to_mask);					\
69 	FD_SET((fd), &sm_io_to_mask);					\
70 	FD_ZERO(&sm_io_x_mask);						\
71 	FD_SET((fd), &sm_io_x_mask);					\
72 	if (gettimeofday(&sm_io_to_before, NULL) < 0)			\
73 		return SM_IO_EOF;					\
74 	(sel_ret) = select((fd) + 1, &sm_io_to_mask, NULL,		\
75 			   &sm_io_x_mask, (to));			\
76 	if ((sel_ret) < 0)						\
77 	{								\
78 		/* something went wrong, errno set */			\
79 		fp->f_r = 0;						\
80 		fp->f_flags |= SMERR;					\
81 		return SM_IO_EOF;					\
82 	}								\
83 	else if ((sel_ret) == 0)					\
84 	{								\
85 		/* timeout */						\
86 		errno = EAGAIN;						\
87 		return SM_IO_EOF;					\
88 	}								\
89 	/* calulate wall-clock time used */				\
90 	if (gettimeofday(&sm_io_to_after, NULL) < 0)			\
91 		return SM_IO_EOF;					\
92 	timersub(&sm_io_to_before, &sm_io_to_after, &sm_io_to_diff);	\
93 	timersub((to), &sm_io_to_diff, (to));				\
94 }
95 
96 /*
97 **  SM_LFLUSH -- flush a file if it is line buffered and writable
98 **
99 **	Parameters:
100 **		fp -- file pointer to flush
101 **		timeout -- original timeout value (in milliseconds)
102 **
103 **	Returns:
104 **		Failure: returns SM_IO_EOF and sets errno
105 **		Success: returns 0
106 */
107 
108 static int
109 sm_lflush(fp, timeout)
110 	SM_FILE_T *fp;
111 	int *timeout;
112 {
113 
114 	if ((fp->f_flags & (SMLBF|SMWR)) == (SMLBF|SMWR))
115 		return sm_flush(fp, timeout);
116 	return 0;
117 }
118 
119 /*
120 **  SM_REFILL -- refill a buffer
121 **
122 **	Parameters:
123 **		fp -- file pointer for buffer refill
124 **		timeout -- time to complete filling the buffer in milliseconds
125 **
126 **	Returns:
127 **		Success: returns 0
128 **		Failure: returns SM_IO_EOF
129 */
130 
131 int
132 sm_refill(fp, timeout)
133 	register SM_FILE_T *fp;
134 	int timeout;
135 {
136 	int ret, r;
137 	struct timeval to;
138 	int fd;
139 
140 	if (timeout == SM_TIME_DEFAULT)
141 		timeout = fp->f_timeout;
142 	if (timeout == SM_TIME_IMMEDIATE)
143 	{
144 		/*
145 		**  Filling the buffer will take time and we are wanted to
146 		**  return immediately. And we're not EOF or ERR really.
147 		**  So... the failure is we couldn't do it in time.
148 		*/
149 
150 		errno = EAGAIN;
151 		fp->f_r = 0; /* just to be sure */
152 		return 0;
153 	}
154 
155 	/* make sure stdio is set up */
156 	if (!Sm_IO_DidInit)
157 		sm_init();
158 
159 	fp->f_r = 0;		/* largely a convenience for callers */
160 
161 	if (fp->f_flags & SMFEOF)
162 		return SM_IO_EOF;
163 
164 	SM_CONVERT_TIME(fp, fd, timeout, &to);
165 
166 	/* if not already reading, have to be reading and writing */
167 	if ((fp->f_flags & SMRD) == 0)
168 	{
169 		if ((fp->f_flags & SMRW) == 0)
170 		{
171 			errno = EBADF;
172 			fp->f_flags |= SMERR;
173 			return SM_IO_EOF;
174 		}
175 
176 		/* switch to reading */
177 		if (fp->f_flags & SMWR)
178 		{
179 			if (sm_flush(fp, &timeout))
180 				return SM_IO_EOF;
181 			fp->f_flags &= ~SMWR;
182 			fp->f_w = 0;
183 			fp->f_lbfsize = 0;
184 		}
185 		fp->f_flags |= SMRD;
186 	}
187 	else
188 	{
189 		/*
190 		**  We were reading.  If there is an ungetc buffer,
191 		**  we must have been reading from that.  Drop it,
192 		**  restoring the previous buffer (if any).  If there
193 		**  is anything in that buffer, return.
194 		*/
195 
196 		if (HASUB(fp))
197 		{
198 			FREEUB(fp);
199 			if ((fp->f_r = fp->f_ur) != 0)
200 			{
201 				fp->f_p = fp->f_up;
202 
203 				/* revert blocking state */
204 				return 0;
205 			}
206 		}
207 	}
208 
209 	if (fp->f_bf.smb_base == NULL)
210 		sm_makebuf(fp);
211 
212 	/*
213 	**  Before reading from a line buffered or unbuffered file,
214 	**  flush all line buffered output files, per the ANSI C standard.
215 	*/
216 
217 	if (fp->f_flags & (SMLBF|SMNBF))
218 		(void) sm_fwalk(sm_lflush, &timeout);
219 
220 	/*
221 	**  If this file is linked to another, and we are going to hang
222 	**  on the read, flush the linked file before continuing.
223 	*/
224 
225 	if (fp->f_flushfp != NULL &&
226 	    (*fp->f_getinfo)(fp, SM_IO_IS_READABLE, NULL) <= 0)
227 		sm_flush(fp->f_flushfp, &timeout);
228 
229 	fp->f_p = fp->f_bf.smb_base;
230 
231 	/*
232 	**  The do-while loop stops trying to read when something is read
233 	**  or it appears that the timeout has expired before finding
234 	**  something available to be read (via select()).
235 	*/
236 
237 	ret = 0;
238 	do
239 	{
240 		errno = 0; /* needed to ensure EOF correctly found */
241 		r = (*fp->f_read)(fp, (char *)fp->f_p, fp->f_bf.smb_size);
242 		if (r <= 0)
243 		{
244 			if (r == 0 && errno == 0)
245 				break; /* EOF found */
246 			if (IS_IO_ERROR(fd, r, timeout))
247 				goto err; /* errno set */
248 
249 			/* read would block */
250 			SM_IO_RD_TIMEOUT(fp, fd, &to, timeout, ret);
251 		}
252 	} while (r <= 0 && ret > 0);
253 
254 err:
255 	if (r <= 0)
256 	{
257 		if (r == 0)
258 			fp->f_flags |= SMFEOF;
259 		else
260 			fp->f_flags |= SMERR;
261 		fp->f_r = 0;
262 		return SM_IO_EOF;
263 	}
264 	fp->f_r = r;
265 	return 0;
266 }
267 
268 /*
269 **  SM_RGET -- refills buffer and returns first character
270 **
271 **  Handle sm_getc() when the buffer ran out:
272 **  Refill, then return the first character in the newly-filled buffer.
273 **
274 **	Parameters:
275 **		fp -- file pointer to work on
276 **		timeout -- time to complete refill
277 **
278 **	Returns:
279 **		Success: first character in refilled buffer as an int
280 **		Failure: SM_IO_EOF
281 */
282 
283 int
284 sm_rget(fp, timeout)
285 	register SM_FILE_T *fp;
286 	int timeout;
287 {
288 	if (sm_refill(fp, timeout) == 0)
289 	{
290 		fp->f_r--;
291 		return *fp->f_p++;
292 	}
293 	return SM_IO_EOF;
294 }
295