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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/*
* 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: fopen.c,v 1.55 2001/08/27 18:38:17 gshapiro Exp $")
#include <errno.h>
#include <setjmp.h>
#include <sys/time.h>
#include <sm/heap.h>
#include <sm/signal.h>
#include <sm/assert.h>
#include <sm/io.h>
#include <sm/clock.h>
#include "local.h"
extern int sm_io_fclose __P((SM_FILE_T *));
static jmp_buf OpenTimeOut, ReopenTimeOut;
/*
** OPENALRM -- handler when timeout activated for sm_io_open()
**
** Returns flow of control to where setjmp(OpenTimeOut) was set.
**
** Parameters:
** sig -- unused
**
** Returns:
** does not return
**
** Side Effects:
** returns flow of control to setjmp(OpenTimeOut).
**
** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
** DOING.
*/
/* ARGSUSED0 */
static void
openalrm(sig)
int sig;
{
longjmp(OpenTimeOut, 1);
}
/*
** REOPENALRM -- handler when timeout activated for sm_io_reopen()
**
** Returns flow of control to where setjmp(ReopenTimeOut) was set.
**
** Parameters:
** sig -- unused
**
** Returns:
** does not return
**
** Side Effects:
** returns flow of control to setjmp(ReopenTimeOut).
**
** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
** DOING.
*/
/* ARGSUSED0 */
static void
reopenalrm(sig)
int sig;
{
longjmp(ReopenTimeOut, 1);
}
/*
** SM_IO_OPEN -- open a file of a specific type
**
** Parameters:
** type -- type of file to open
** timeout -- time to complete the open
** info -- info describing what is to be opened (type dependant)
** flags -- user selected flags
** rpool -- pointer to rpool to be used for this open
**
** Returns:
** Raises exception on heap exhaustion.
** Aborts if type is invalid.
** Returns NULL and sets errno
** - when the type specific open fails
** - when open vector errors
** - when flags not set or invalid
** Success returns a file pointer to the opened file type.
*/
SM_FILE_T *
sm_io_open(type, timeout, info, flags, rpool)
const SM_FILE_T *type;
int SM_NONVOLATILE timeout; /* this is not the file type timeout */
const void *info;
int flags;
const void *rpool;
{
register SM_FILE_T *fp;
int ioflags;
SM_EVENT *evt = NULL;
ioflags = sm_flags(flags);
if (ioflags == 0)
{
errno = EINVAL;
return NULL; /* must give some indication/intent */
}
if (timeout == SM_TIME_DEFAULT)
timeout = SM_TIME_FOREVER;
if (timeout == SM_TIME_IMMEDIATE)
{
errno = EAGAIN;
return NULL;
}
fp = sm_fp(type, ioflags, NULL);
/* Okay, this is where we set the timeout. */
if (timeout != SM_TIME_FOREVER)
{
if (setjmp(OpenTimeOut) != 0)
{
errno = EAGAIN;
return NULL;
}
evt = sm_seteventm(timeout, openalrm, 0);
}
if ((*fp->f_open)(fp, info, flags, rpool) < 0)
{
fp->f_flags = 0; /* release */
fp->sm_magic = NULL; /* release */
return NULL;
}
/* We're back. So undo our timeout and handler */
if (evt != NULL)
sm_clrevent(evt);
#if SM_RPOOL
if (rpool != NULL)
sm_rpool_attach_x(rpool, sm_io_fclose, fp);
#endif /* SM_RPOOL */
return fp;
}
/*
** SM_IO_DUP -- duplicate a file pointer
**
** Parameters:
** fp -- file pointer to duplicate
**
** Returns:
** Success - the duplicated file pointer
** Failure - NULL (was an invalid file pointer or too many open)
**
** Increments the duplicate counter (dup_cnt) for the open file pointer.
** The counter counts the number of duplicates. When the duplicate
** counter is 0 (zero) then the file pointer is the only one left
** (no duplicates, it is the only one).
*/
SM_FILE_T *
sm_io_dup(fp)
SM_FILE_T *fp;
{
SM_REQUIRE_ISA(fp, SmFileMagic);
if (fp->sm_magic != SmFileMagic)
{
errno = EBADF;
return NULL;
}
if (fp->f_dup_cnt >= INT_MAX - 1)
{
/* Can't let f_dup_cnt wrap! */
errno = EMFILE;
return NULL;
}
fp->f_dup_cnt++;
return fp;
}
/*
** SM_IO_REOPEN -- open a new file using the old file pointer
**
** Parameters:
** type -- file type to be opened
** timeout -- time to complete the reopen
** info -- infomation about what is to be "re-opened" (type dep.)
** flags -- user flags to map to internal flags
** rpool -- rpool file to be associated with
** fp -- the file pointer to reuse
**
** Returns:
** Raises an exception on heap exhaustion.
** Aborts if type is invalid.
** Failure: returns NULL
** Success: returns "reopened" file pointer
*/
SM_FILE_T *
sm_io_reopen(type, timeout, info, flags, rpool, fp)
const SM_FILE_T *type;
int SM_NONVOLATILE timeout;
const void *info;
int flags;
const void *rpool;
SM_FILE_T *fp;
{
int ioflags, ret;
SM_FILE_T *fp2;
SM_EVENT *evt = NULL;
if ((ioflags = sm_flags(flags)) == 0)
{
(void) sm_io_close(fp, timeout);
return NULL;
}
if (!Sm_IO_DidInit)
sm_init();
if (timeout == SM_TIME_DEFAULT)
timeout = SM_TIME_FOREVER;
if (timeout == SM_TIME_IMMEDIATE)
{
/*
** Filling the buffer will take time and we are wanted to
** return immediately. So...
*/
errno = EAGAIN;
return NULL;
}
/* Okay, this is where we set the timeout. */
if (timeout != SM_TIME_FOREVER)
{
if (setjmp(ReopenTimeOut) != 0)
{
errno = EAGAIN;
return NULL;
}
evt = sm_seteventm(timeout, reopenalrm, 0);
}
/*
** There are actually programs that depend on being able to "reopen"
** descriptors that weren't originally open. Keep this from breaking.
** Remember whether the stream was open to begin with, and which file
** descriptor (if any) was associated with it. If it was attached to
** a descriptor, defer closing it; reopen("/dev/stdin", "r", stdin)
** should work. This is unnecessary if it was not a Unix file.
*/
if (fp != NULL)
{
if (fp->sm_magic != SmFileMagic)
fp->f_flags = SMFEOF; /* hold on to it */
else
{
/* flush the stream; ANSI doesn't require this. */
(void) sm_io_flush(fp, SM_TIME_FOREVER);
(void) sm_io_close(fp, SM_TIME_FOREVER);
}
}
fp2 = sm_fp(type, ioflags, fp);
ret = (*fp2->f_open)(fp2, info, flags, rpool);
/* We're back. So undo our timeout and handler */
if (evt != NULL)
sm_clrevent(evt);
if (ret < 0)
{
fp2->f_flags = 0; /* release */
fp2->sm_magic = NULL; /* release */
return NULL;
}
/*
** We're not preserving this logic (below) for sm_io because it is now
** abstracted at least one "layer" away. By closing and reopening
** the 1st fd used should be the just released one (when Unix
** behavior followed). Old comment::
** If reopening something that was open before on a real file, try
** to maintain the descriptor. Various C library routines (perror)
** assume stderr is always fd STDERR_FILENO, even if being reopen'd.
*/
#if SM_RPOOL
if (rpool != NULL)
sm_rpool_attach_x(rpool, sm_io_close, fp2);
#endif /* SM_RPOOL */
return fp2;
}
/*
** SM_IO_AUTOFLUSH -- link another file to this for auto-flushing
**
** When a read occurs on fp, fp2 will be flushed iff there is no
** data waiting on fp.
**
** Parameters:
** fp -- the file opened for reading.
** fp2 -- the file opened for writing.
**
** Returns:
** The old flush file pointer.
*/
SM_FILE_T *
sm_io_autoflush(fp, fp2)
SM_FILE_T *fp;
SM_FILE_T *fp2;
{
SM_FILE_T *savefp;
SM_REQUIRE_ISA(fp, SmFileMagic);
if (fp2 != NULL)
SM_REQUIRE_ISA(fp2, SmFileMagic);
savefp = fp->f_flushfp;
fp->f_flushfp = fp2;
return savefp;
}
/*
** SM_IO_AUTOMODE -- link another file to this for auto-moding
**
** When the mode (blocking or non-blocking) changes for fp1 then
** update fp2's mode at the same time. This is to be used when
** a system dup() has generated a second file descriptor for
** another sm_io_open() by file descriptor. The modes have been
** linked in the system and this formalizes it for sm_io internally.
**
** Parameters:
** fp1 -- the first file
** fp2 -- the second file
**
** Returns:
** nothing
*/
void
sm_io_automode(fp1, fp2)
SM_FILE_T *fp1;
SM_FILE_T *fp2;
{
SM_REQUIRE_ISA(fp1, SmFileMagic);
SM_REQUIRE_ISA(fp2, SmFileMagic);
fp1->f_modefp = fp2;
fp2->f_modefp = fp1;
}
|