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