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
|
/*
* Copyright (c) 2000-2002, 2006 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: findfp.c,v 1.68 2013-11-22 20:51:42 ca Exp $")
#include <stdlib.h>
#include <unistd.h>
#include <sys/param.h>
#include <errno.h>
#include <string.h>
#include <syslog.h>
#include <sm/io.h>
#include <sm/assert.h>
#include <sm/heap.h>
#include <sm/string.h>
#include <sm/conf.h>
#include "local.h"
#include "glue.h"
bool Sm_IO_DidInit; /* IO system has been initialized? */
const char SmFileMagic[] = "sm_file";
/* An open type to map to fopen()-like behavior */
SM_FILE_T SmFtStdio_def =
{SmFileMagic, 0, 0, 0, (SMRW|SMFBF), -1, {0, 0}, 0, 0, 0,
sm_stdclose, sm_stdread, sm_stdseek, sm_stdwrite,
sm_stdopen, sm_stdsetinfo, sm_stdgetinfo, SM_TIME_FOREVER,
SM_TIME_BLOCK, "stdio" };
/* An open type to map to fdopen()-like behavior */
SM_FILE_T SmFtStdiofd_def =
{SmFileMagic, 0, 0, 0, (SMRW|SMFBF), -1, {0, 0}, 0, 0, 0,
sm_stdclose, sm_stdread, sm_stdseek, sm_stdwrite,
sm_stdfdopen, sm_stdsetinfo, sm_stdgetinfo, SM_TIME_FOREVER,
SM_TIME_BLOCK, "stdiofd" };
/* A string file type */
SM_FILE_T SmFtString_def =
{SmFileMagic, 0, 0, 0, (SMRW|SMNBF), -1, {0, 0}, 0, 0, 0,
sm_strclose, sm_strread, sm_strseek, sm_strwrite,
sm_stropen, sm_strsetinfo, sm_strgetinfo, SM_TIME_FOREVER,
SM_TIME_BLOCK, "string" };
#if 0
/* A file type for syslog communications */
SM_FILE_T SmFtSyslog_def =
{SmFileMagic, 0, 0, 0, (SMRW|SMNBF), -1, {0, 0}, 0, 0, 0,
sm_syslogclose, sm_syslogread, sm_syslogseek, sm_syslogwrite,
sm_syslogopen, sm_syslogsetinfo, sm_sysloggetinfo, SM_TIME_FOREVER,
SM_TIME_BLOCK, "syslog" };
#endif /* 0 */
#define NDYNAMIC 10 /* add ten more whenever necessary */
#define smio(flags, file, name) \
{SmFileMagic, 0, 0, 0, flags, file, {0}, 0, SmIoF+file, 0, \
sm_stdclose, sm_stdread, sm_stdseek, sm_stdwrite, \
sm_stdopen, sm_stdsetinfo, sm_stdgetinfo, SM_TIME_FOREVER, \
SM_TIME_BLOCK, name}
/* sm_magic p r w flags file bf lbfsize cookie ival */
#define smstd(flags, file, name) \
{SmFileMagic, 0, 0, 0, flags, -1, {0}, 0, 0, file, \
sm_stdioclose, sm_stdioread, sm_stdioseek, sm_stdiowrite, \
sm_stdioopen, sm_stdiosetinfo, sm_stdiogetinfo, SM_TIME_FOREVER,\
SM_TIME_BLOCK, name}
/* A file type for interfacing to stdio FILE* streams. */
SM_FILE_T SmFtRealStdio_def = smstd(SMRW|SMNBF, -1, "realstdio");
/* the usual - (stdin + stdout + stderr) */
static SM_FILE_T usual[SM_IO_OPEN_MAX - 3];
static struct sm_glue smuglue = { 0, SM_IO_OPEN_MAX - 3, usual };
/* List of builtin automagically already open file pointers */
SM_FILE_T SmIoF[6] =
{
smio(SMRD|SMLBF, SMIOIN_FILENO, "smioin"), /* smioin */
smio(SMWR|SMLBF, SMIOOUT_FILENO, "smioout"), /* smioout */
smio(SMWR|SMNBF, SMIOERR_FILENO, "smioerr"), /* smioerr */
smstd(SMRD|SMNBF, SMIOIN_FILENO, "smiostdin"), /* smiostdin */
smstd(SMWR|SMNBF, SMIOOUT_FILENO, "smiostdout"),/* smiostdout */
smstd(SMWR|SMNBF, SMIOERR_FILENO, "smiostderr") /* smiostderr */
};
/* Structure containing list of currently open file pointers */
struct sm_glue smglue = { &smuglue, 3, SmIoF };
/*
** SM_MOREGLUE -- adds more space for open file pointers
**
** Parameters:
** n -- number of new spaces for file pointers
**
** Returns:
** Raises an exception if no more memory.
** Otherwise, returns a pointer to new spaces.
*/
static struct sm_glue *sm_moreglue_x __P((int));
static SM_FILE_T empty;
static struct sm_glue *
sm_moreglue_x(n)
register int n;
{
register struct sm_glue *g;
register SM_FILE_T *p;
g = (struct sm_glue *) sm_pmalloc_x(sizeof(*g) + SM_ALIGN_BITS +
n * sizeof(SM_FILE_T));
p = (SM_FILE_T *) SM_ALIGN(g + 1);
g->gl_next = NULL;
g->gl_niobs = n;
g->gl_iobs = p;
while (--n >= 0)
*p++ = empty;
return g;
}
/*
** SM_FP -- allocate and initialize an SM_FILE structure
**
** Parameters:
** t -- file type requested to be opened.
** flags -- control flags for file type behavior
** oldfp -- file pointer to reuse if available (optional)
**
** Returns:
** Raises exception on memory exhaustion.
** Aborts if type is invalid.
** Otherwise, returns file pointer for requested file type.
*/
SM_FILE_T *
sm_fp(t, flags, oldfp)
const SM_FILE_T *t;
const int flags;
SM_FILE_T *oldfp;
{
register SM_FILE_T *fp;
register int n;
register struct sm_glue *g;
SM_REQUIRE(t->f_open && t->f_close && (t->f_read || t->f_write));
if (!Sm_IO_DidInit)
sm_init();
if (oldfp != NULL)
{
fp = oldfp;
goto found; /* for opening reusing an 'fp' */
}
for (g = &smglue;; g = g->gl_next)
{
for (fp = g->gl_iobs, n = g->gl_niobs; --n >= 0; fp++)
if (fp->sm_magic == NULL)
goto found;
if (g->gl_next == NULL)
g->gl_next = sm_moreglue_x(NDYNAMIC);
}
found:
fp->sm_magic = SmFileMagic; /* 'fp' now valid and in-use */
fp->f_p = NULL; /* no current pointer */
fp->f_w = 0; /* nothing to write */
fp->f_r = 0; /* nothing to read */
fp->f_flags = flags;
fp->f_file = -1; /* no file */
fp->f_bf.smb_base = NULL; /* no buffer */
fp->f_bf.smb_size = 0; /* no buffer size with no buffer */
fp->f_lbfsize = 0; /* not line buffered */
fp->f_flushfp = NULL; /* no associated flush file */
fp->f_cookie = fp; /* default: *open* overrides cookie setting */
fp->f_close = t->f_close; /* assign close function */
fp->f_read = t->f_read; /* assign read function */
fp->f_seek = t->f_seek; /* assign seek function */
fp->f_write = t->f_write; /* assign write function */
fp->f_open = t->f_open; /* assign open function */
fp->f_setinfo = t->f_setinfo; /* assign setinfo function */
fp->f_getinfo = t->f_getinfo; /* assign getinfo function */
fp->f_type = t->f_type; /* file type */
fp->f_ub.smb_base = NULL; /* no ungetc buffer */
fp->f_ub.smb_size = 0; /* no size for no ungetc buffer */
if (fp->f_timeout == SM_TIME_DEFAULT)
fp->f_timeout = SM_TIME_FOREVER;
else
fp->f_timeout = t->f_timeout; /* traditional behavior */
fp->f_timeoutstate = SM_TIME_BLOCK; /* by default */
return fp;
}
/*
** SM_CLEANUP -- cleanup function when exit called.
**
** This function is registered via atexit().
**
** Parameters:
** none
**
** Returns:
** nothing.
**
** Side Effects:
** flushes open files before they are forced closed
*/
void
sm_cleanup()
{
int timeout = SM_TIME_DEFAULT;
(void) sm_fwalk(sm_flush, &timeout); /* `cheating' */
}
/*
** SM_INIT -- called whenever sm_io's internal variables must be set up.
**
** Parameters:
** none
**
** Returns:
** none
**
** Side Effects:
** Registers sm_cleanup() using atexit().
*/
void
sm_init()
{
if (Sm_IO_DidInit) /* paranoia */
return;
/* more paranoia: initialize pointers in a static variable */
empty.f_type = NULL;
empty.sm_magic = NULL;
/* make sure we clean up on exit */
atexit(sm_cleanup); /* conservative */
Sm_IO_DidInit = true;
}
/*
** SM_IO_SETINFO -- change info for an open file type (fp)
**
** The generic SM_IO_WHAT_VECTORS is auto supplied for all file types.
** If the request is to set info other than SM_IO_WHAT_VECTORS then
** the request is passed on to the file type's specific setinfo vector.
** WARNING: this is working on an active/open file type.
**
** Parameters:
** fp -- file to make the setting on
** what -- type of information to set
** valp -- structure to obtain info from
**
** Returns:
** 0 on success
** -1 on error and sets errno:
** - when what != SM_IO_WHAT_VECTORS and setinfo vector
** not set
** - when vectored setinfo returns -1
*/
int
sm_io_setinfo(fp, what, valp)
SM_FILE_T *fp;
int what;
void *valp;
{
SM_FILE_T *v = (SM_FILE_T *) valp;
SM_REQUIRE_ISA(fp, SmFileMagic);
switch (what)
{
case SM_IO_WHAT_VECTORS:
/*
** This is the "generic" available for all.
** This allows the function vectors to be replaced
** while the file type is active.
*/
fp->f_close = v->f_close;
fp->f_read = v->f_read;
fp->f_seek = v->f_seek;
fp->f_write = v->f_write;
fp->f_open = v->f_open;
fp->f_setinfo = v->f_setinfo;
fp->f_getinfo = v->f_getinfo;
sm_free(fp->f_type);
fp->f_type = sm_strdup_x(v->f_type);
return 0;
case SM_IO_WHAT_TIMEOUT:
fp->f_timeout = *((int *)valp);
return 0;
}
/* Otherwise the vector will check it out */
if (fp->f_setinfo == NULL)
{
errno = EINVAL;
return -1;
}
else
return (*fp->f_setinfo)(fp, what, valp);
}
/*
** SM_IO_GETINFO -- get information for an active file type (fp)
**
** This function supplies for all file types the answers for the
** three requests SM_IO_WHAT_VECTORS, and SM_IO_WHAT_ISTYPE.
** Other requests are handled by the getinfo
** vector if available for the open file type.
** SM_IO_WHAT_VECTORS returns information for the file pointer vectors.
** SM_IO_WHAT_ISTYPE returns >0 if the passed in type matches the
** file pointer's type.
** SM_IO_IS_READABLE returns 1 if there is data available for reading,
** 0 otherwise.
**
** Parameters:
** fp -- file pointer for active file type
** what -- type of information request
** valp -- structure to place obtained info into
**
** Returns:
** -1 on error and sets errno:
** - when valp==NULL and request expects otherwise
** - when request is not SM_IO_WHAT_VECTORS
** and not SM_IO_WHAT_ISTYPE
** and getinfo vector is NULL
** - when getinfo type vector returns -1
** >=0 on success
*/
int
sm_io_getinfo(fp, what, valp)
SM_FILE_T *fp;
int what;
void *valp;
{
SM_FILE_T *v = (SM_FILE_T *) valp;
SM_REQUIRE_ISA(fp, SmFileMagic);
switch (what)
{
case SM_IO_WHAT_VECTORS:
if (valp == NULL)
{
errno = EINVAL;
return -1;
}
/* This is the "generic" available for all */
v->f_close = fp->f_close;
v->f_read = fp->f_read;
v->f_seek = fp->f_seek;
v->f_write = fp->f_write;
v->f_open = fp->f_open;
v->f_setinfo = fp->f_setinfo;
v->f_getinfo = fp->f_getinfo;
v->f_type = fp->f_type;
return 0;
case SM_IO_WHAT_ISTYPE:
if (valp == NULL)
{
errno = EINVAL;
return -1;
}
return strcmp(fp->f_type, valp) == 0;
case SM_IO_IS_READABLE:
/* if there is data in the buffer, it must be readable */
if (fp->f_r > 0)
return 1;
/* otherwise query the underlying file */
break;
case SM_IO_WHAT_TIMEOUT:
*((int *) valp) = fp->f_timeout;
return 0;
case SM_IO_WHAT_FD:
if (fp->f_file > -1)
return fp->f_file;
/* try the file type specific getinfo to see if it knows */
break;
}
/* Otherwise the vector will check it out */
if (fp->f_getinfo == NULL)
{
errno = EINVAL;
return -1;
}
return (*fp->f_getinfo)(fp, what, valp);
}
|