aboutsummaryrefslogtreecommitdiff
path: root/contrib/sendmail/libsm/stdio.c
blob: 16277c09a8ecb9e48180f32b9b10721c47d3f91e (plain) (blame)
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
 * Copyright (c) 2000-2005 Proofpoint, 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("@(#)$Id: stdio.c,v 1.72 2013-11-22 20:51:43 ca Exp $")
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>	/* FreeBSD: FD_ZERO needs <string.h> */
#include <sys/stat.h>
#include <sm/time.h>
#include <sm/heap.h>
#include <sm/assert.h>
#include <sm/varargs.h>
#include <sm/io.h>
#include <sm/setjmp.h>
#include <sm/conf.h>
#include <sm/fdset.h>
#include "local.h"

static int	sm_stdsetmode __P((SM_FILE_T *, const int *));
static int	sm_stdgetmode __P((SM_FILE_T *, int *));

/*
**  Overall:
**  Small standard I/O/seek/close functions.
**  These maintain the `known seek offset' for seek optimization.
*/

/*
**  SM_STDOPEN -- open a file with stdio behavior
**
**  Not associated with the system's stdio in libc.
**
**	Parameters:
**		fp -- file pointer to be associated with the open
**		info -- pathname of the file to be opened
**		flags -- indicates type of access methods
**		rpool -- ignored
**
**	Returns:
**		Failure: -1 and set errno
**		Success: 0 or greater (fd of file from open(2)).
**
*/

/* ARGSUSED3 */
int
sm_stdopen(fp, info, flags, rpool)
	SM_FILE_T *fp;
	const void *info;
	int flags;
	const void *rpool;
{
	char *path = (char *) info;
	int oflags;

	switch (SM_IO_MODE(flags))
	{
	  case SM_IO_RDWR:
		oflags = O_RDWR;
		break;
	  case SM_IO_RDWRTR:
		oflags = O_RDWR | O_CREAT | O_TRUNC;
		break;
	  case SM_IO_RDONLY:
		oflags = O_RDONLY;
		break;
	  case SM_IO_WRONLY:
		oflags = O_WRONLY | O_CREAT | O_TRUNC;
		break;
	  case SM_IO_APPEND:
		oflags = O_APPEND | O_WRONLY | O_CREAT;
		break;
	  case SM_IO_APPENDRW:
		oflags = O_APPEND | O_RDWR | O_CREAT;
		break;
	  default:
		errno = EINVAL;
		return -1;
	}
#ifdef O_BINARY
	if (SM_IS_BINARY(flags))
		oflags |= O_BINARY;
#endif /* O_BINARY */
	fp->f_file = open(path, oflags,
			  S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
	if (fp->f_file < 0)
		return -1; /* errno set by open() */

	if (oflags & O_APPEND)
		(void) (*fp->f_seek)((void *)fp, (off_t)0, SEEK_END);

	return fp->f_file;
}

/*
**  SM_STDREAD -- read from the file
**
**	Parameters:
**		fp -- file pointer to read from
**		buf -- location to place read data
**		n -- number of bytes to read
**
**	Returns:
**		Failure: -1 and sets errno
**		Success: number of bytes read
**
**	Side Effects:
**		Updates internal offset into file.
*/

ssize_t
sm_stdread(fp, buf, n)
	SM_FILE_T *fp;
	char *buf;
	size_t n;
{
	register int ret;

	ret = read(fp->f_file, buf, n);

	/* if the read succeeded, update the current offset */
	if (ret > 0)
		fp->f_lseekoff += ret;
	return ret;
}

/*
**  SM_STDWRITE -- write to the file
**
**	Parameters:
**		fp -- file pointer ro write to
**		buf -- location of data to be written
**		n - number of bytes to write
**
**	Returns:
**		Failure: -1 and sets errno
**		Success: number of bytes written
*/

ssize_t
sm_stdwrite(fp, buf, n)
	SM_FILE_T *fp;
	char const *buf;
	size_t n;
{
	return write(fp->f_file, buf, n);
}

/*
**  SM_STDSEEK -- set the file offset position
**
**	Parmeters:
**		fp -- file pointer to position
**		offset -- how far to position from "base" (set by 'whence')
**		whence -- indicates where the "base" of the 'offset' to start
**
**	Results:
**		Failure: -1 and sets errno
**		Success: the current offset
**
**	Side Effects:
**		Updates the internal value of the offset.
*/

off_t
sm_stdseek(fp, offset, whence)
	SM_FILE_T *fp;
	off_t offset;
	int whence;
{
	register off_t ret;

	ret = lseek(fp->f_file, (off_t) offset, whence);
	if (ret != (off_t) -1)
		fp->f_lseekoff = ret;
	return ret;
}

/*
**  SM_STDCLOSE -- close the file
**
**	Parameters:
**		fp -- the file pointer to close
**
**	Returns:
**		Success: 0 (zero)
**		Failure: -1 and sets errno
*/

int
sm_stdclose(fp)
	SM_FILE_T *fp;
{
	return close(fp->f_file);
}

/*
**  SM_STDSETMODE -- set the access mode for the file
**
**  Called by sm_stdsetinfo().
**
**	Parameters:
**		fp -- file pointer
**		mode -- new mode to set the file access to
**
**	Results:
**		Success: 0 (zero);
**		Failure: -1 and sets errno
*/

static int
sm_stdsetmode(fp, mode)
	SM_FILE_T *fp;
	const int *mode;
{
	int flags = 0;

	switch (SM_IO_MODE(*mode))
	{
	  case SM_IO_RDWR:
		flags |= SMRW;
		break;
	  case SM_IO_RDONLY:
		flags |= SMRD;
		break;
	  case SM_IO_WRONLY:
		flags |= SMWR;
		break;
	  case SM_IO_APPEND:
	  default:
		errno = EINVAL;
		return -1;
	}
	fp->f_flags = fp->f_flags & ~SMMODEMASK;
	fp->f_flags |= flags;
	return 0;
}

/*
**  SM_STDGETMODE -- for getinfo determine open mode
**
**  Called by sm_stdgetinfo().
**
**	Parameters:
**		fp -- the file mode being determined
**		mode -- internal mode to map to external value
**
**	Results:
**		Failure: -1 and sets errno
**		Success: external mode value
*/

static int
sm_stdgetmode(fp, mode)
	SM_FILE_T *fp;
	int *mode;
{
	switch (fp->f_flags & SMMODEMASK)
	{
	  case SMRW:
		*mode = SM_IO_RDWR;
		break;
	  case SMRD:
		*mode = SM_IO_RDONLY;
		break;
	  case SMWR:
		*mode = SM_IO_WRONLY;
		break;
	  default:
		errno = EINVAL;
		return -1;
	}
	return 0;
}

/*
**  SM_STDSETINFO -- set/modify information for a file
**
**	Parameters:
**		fp -- file to set info for
**		what -- type of info to set
**		valp -- location of data used for setting
**
**	Returns:
**		Failure: -1 and sets errno
**		Success: >=0
*/

int
sm_stdsetinfo(fp, what, valp)
	SM_FILE_T *fp;
	int what;
	void *valp;
{
	switch (what)
	{
	  case SM_IO_WHAT_MODE:
		return sm_stdsetmode(fp, (const int *)valp);

	  default:
		errno = EINVAL;
		return -1;
	}
}

/*
**  SM_STDGETINFO -- get information about the open file
**
**	Parameters:
**		fp -- file to get info for
**		what -- type of info to get
**		valp -- location to place found info
**
**	Returns:
**		Success: may or may not place info in 'valp' depending
**			on 'what' value, and returns values >=0. Return
**			value may be the obtained info
**		Failure: -1 and sets errno
*/

int
sm_stdgetinfo(fp, what, valp)
	SM_FILE_T *fp;
	int what;
	void *valp;
{
	switch (what)
	{
	  case SM_IO_WHAT_MODE:
		return sm_stdgetmode(fp, (int *)valp);

	  case SM_IO_WHAT_FD:
		return fp->f_file;

	  case SM_IO_WHAT_SIZE:
	  {
		  struct stat st;

		  if (fstat(fp->f_file, &st) == 0)
			  return st.st_size;
		  else
			  return -1;
	  }

	  case SM_IO_IS_READABLE:
	  {
		  fd_set readfds;
		  struct timeval timeout;

		  if (SM_FD_SETSIZE > 0 && fp->f_file >= SM_FD_SETSIZE)
		  {
			  errno = EINVAL;
			  return -1;
		  }
		  FD_ZERO(&readfds);
		  SM_FD_SET(fp->f_file, &readfds);
		  timeout.tv_sec = 0;
		  timeout.tv_usec = 0;
		  if (select(fp->f_file + 1, FDSET_CAST &readfds,
			     NULL, NULL, &timeout) > 0 &&
		      SM_FD_ISSET(fp->f_file, &readfds))
			  return 1;
		  return 0;
	  }

	  default:
		errno = EINVAL;
		return -1;
	}
}

/*
**  SM_STDFDOPEN -- open file by primitive 'fd' rather than pathname
**
**	I/O function to handle fdopen() stdio equivalence. The rest of
**	the functions are the same as the sm_stdopen() above.
**
**	Parameters:
**		fp -- the file pointer to be associated with the open
**		name -- the primitive file descriptor for association
**		flags -- indicates type of access methods
**		rpool -- ignored
**
**	Results:
**		Success: primitive file descriptor value
**		Failure: -1 and sets errno
*/

/* ARGSUSED3 */
int
sm_stdfdopen(fp, info, flags, rpool)
	SM_FILE_T *fp;
	const void *info;
	int flags;
	const void *rpool;
{
	int oflags, tmp, fdflags, fd = *((int *) info);

	switch (SM_IO_MODE(flags))
	{
	  case SM_IO_RDWR:
		oflags = O_RDWR | O_CREAT;
		break;
	  case SM_IO_RDONLY:
		oflags = O_RDONLY;
		break;
	  case SM_IO_WRONLY:
		oflags = O_WRONLY | O_CREAT | O_TRUNC;
		break;
	  case SM_IO_APPEND:
		oflags = O_APPEND | O_WRONLY | O_CREAT;
		break;
	  case SM_IO_APPENDRW:
		oflags = O_APPEND | O_RDWR | O_CREAT;
		break;
	  default:
		errno = EINVAL;
		return -1;
	}
#ifdef O_BINARY
	if (SM_IS_BINARY(flags))
		oflags |= O_BINARY;
#endif /* O_BINARY */

	/* Make sure the mode the user wants is a subset of the actual mode. */
	if ((fdflags = fcntl(fd, F_GETFL, 0)) < 0)
		return -1;
	tmp = fdflags & O_ACCMODE;
	if (tmp != O_RDWR && (tmp != (oflags & O_ACCMODE)))
	{
		errno = EINVAL;
		return -1;
	}
	fp->f_file = fd;
	if (oflags & O_APPEND)
		(void) (*fp->f_seek)(fp, (off_t)0, SEEK_END);
	return fp->f_file;
}

/*
**  SM_IO_FOPEN -- open a file
**
**	Same interface and semantics as the open() system call,
**	except that it returns SM_FILE_T* instead of a file descriptor.
**
**	Parameters:
**		pathname -- path of file to open
**		flags -- flags controlling the open
**		...  -- option "mode" for opening the file
**
**	Returns:
**		Raises an exception on heap exhaustion.
**		Returns NULL and sets errno if open() fails.
**		Returns an SM_FILE_T pointer on success.
*/

SM_FILE_T *
#if SM_VA_STD
sm_io_fopen(char *pathname, int flags, ...)
#else /* SM_VA_STD */
sm_io_fopen(pathname, flags, va_alist)
	char *pathname;
	int flags;
	va_dcl
#endif /* SM_VA_STD */
{
	MODE_T mode;
	SM_FILE_T *fp;
	int ioflags;

	if (flags & O_CREAT)
	{
		SM_VA_LOCAL_DECL

		SM_VA_START(ap, flags);
		mode = (MODE_T) SM_VA_ARG(ap, int);
		SM_VA_END(ap);
	}
	else
		mode = 0;

	switch (flags & O_ACCMODE)
	{
	  case O_RDONLY:
		ioflags = SMRD;
		break;
	  case O_WRONLY:
		ioflags = SMWR;
		break;
	  case O_RDWR:
		ioflags = SMRW;
		break;
	  default:
		sm_abort("sm_io_fopen: bad flags 0%o", flags);
	}

	fp = sm_fp(SmFtStdio, ioflags, NULL);
	fp->f_file = open(pathname, flags, mode);
	if (fp->f_file == -1)
	{
		fp->f_flags = 0;
		fp->sm_magic = NULL;
		return NULL;
	}
	return fp;
}