1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * Copyright (c) 2000-2002 Sendmail, Inc. and its suppliers. 3*7c478bd9Sstevel@tonic-gate * All rights reserved. 4*7c478bd9Sstevel@tonic-gate * Copyright (c) 1990, 1993 5*7c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 6*7c478bd9Sstevel@tonic-gate * 7*7c478bd9Sstevel@tonic-gate * This code is derived from software contributed to Berkeley by 8*7c478bd9Sstevel@tonic-gate * Chris Torek. 9*7c478bd9Sstevel@tonic-gate * 10*7c478bd9Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set 11*7c478bd9Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of 12*7c478bd9Sstevel@tonic-gate * the sendmail distribution. 13*7c478bd9Sstevel@tonic-gate */ 14*7c478bd9Sstevel@tonic-gate 15*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 16*7c478bd9Sstevel@tonic-gate 17*7c478bd9Sstevel@tonic-gate #include <sm/gen.h> 18*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: findfp.c,v 1.66 2002/02/20 02:40:24 ca Exp $") 19*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 20*7c478bd9Sstevel@tonic-gate #include <unistd.h> 21*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 22*7c478bd9Sstevel@tonic-gate #include <errno.h> 23*7c478bd9Sstevel@tonic-gate #include <string.h> 24*7c478bd9Sstevel@tonic-gate #include <syslog.h> 25*7c478bd9Sstevel@tonic-gate #include <sm/io.h> 26*7c478bd9Sstevel@tonic-gate #include <sm/assert.h> 27*7c478bd9Sstevel@tonic-gate #include <sm/heap.h> 28*7c478bd9Sstevel@tonic-gate #include <sm/string.h> 29*7c478bd9Sstevel@tonic-gate #include <sm/conf.h> 30*7c478bd9Sstevel@tonic-gate #include "local.h" 31*7c478bd9Sstevel@tonic-gate #include "glue.h" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate bool Sm_IO_DidInit; /* IO system has been initialized? */ 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate const char SmFileMagic[] = "sm_file"; 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate /* An open type to map to fopen()-like behavior */ 38*7c478bd9Sstevel@tonic-gate SM_FILE_T SmFtStdio_def = 39*7c478bd9Sstevel@tonic-gate {SmFileMagic, 0, 0, 0, (SMRW|SMFBF), -1, {0, 0}, 0, 0, 0, 40*7c478bd9Sstevel@tonic-gate sm_stdclose, sm_stdread, sm_stdseek, sm_stdwrite, 41*7c478bd9Sstevel@tonic-gate sm_stdopen, sm_stdsetinfo, sm_stdgetinfo, SM_TIME_FOREVER, 42*7c478bd9Sstevel@tonic-gate SM_TIME_BLOCK, "stdio" }; 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate /* An open type to map to fdopen()-like behavior */ 45*7c478bd9Sstevel@tonic-gate SM_FILE_T SmFtStdiofd_def = 46*7c478bd9Sstevel@tonic-gate {SmFileMagic, 0, 0, 0, (SMRW|SMFBF), -1, {0, 0}, 0, 0, 0, 47*7c478bd9Sstevel@tonic-gate sm_stdclose, sm_stdread, sm_stdseek, sm_stdwrite, 48*7c478bd9Sstevel@tonic-gate sm_stdfdopen, sm_stdsetinfo, sm_stdgetinfo, SM_TIME_FOREVER, 49*7c478bd9Sstevel@tonic-gate SM_TIME_BLOCK, "stdiofd" }; 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate /* A string file type */ 52*7c478bd9Sstevel@tonic-gate SM_FILE_T SmFtString_def = 53*7c478bd9Sstevel@tonic-gate {SmFileMagic, 0, 0, 0, (SMRW|SMNBF), -1, {0, 0}, 0, 0, 0, 54*7c478bd9Sstevel@tonic-gate sm_strclose, sm_strread, sm_strseek, sm_strwrite, 55*7c478bd9Sstevel@tonic-gate sm_stropen, sm_strsetinfo, sm_strgetinfo, SM_TIME_FOREVER, 56*7c478bd9Sstevel@tonic-gate SM_TIME_BLOCK, "string" }; 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate #if 0 59*7c478bd9Sstevel@tonic-gate /* A file type for syslog communications */ 60*7c478bd9Sstevel@tonic-gate SM_FILE_T SmFtSyslog_def = 61*7c478bd9Sstevel@tonic-gate {SmFileMagic, 0, 0, 0, (SMRW|SMNBF), -1, {0, 0}, 0, 0, 0, 62*7c478bd9Sstevel@tonic-gate sm_syslogclose, sm_syslogread, sm_syslogseek, sm_syslogwrite, 63*7c478bd9Sstevel@tonic-gate sm_syslogopen, sm_syslogsetinfo, sm_sysloggetinfo, SM_TIME_FOREVER, 64*7c478bd9Sstevel@tonic-gate SM_TIME_BLOCK, "syslog" }; 65*7c478bd9Sstevel@tonic-gate #endif /* 0 */ 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate #define NDYNAMIC 10 /* add ten more whenever necessary */ 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate #define smio(flags, file, name) \ 70*7c478bd9Sstevel@tonic-gate {SmFileMagic, 0, 0, 0, flags, file, {0}, 0, SmIoF+file, 0, \ 71*7c478bd9Sstevel@tonic-gate sm_stdclose, sm_stdread, sm_stdseek, sm_stdwrite, \ 72*7c478bd9Sstevel@tonic-gate sm_stdopen, sm_stdsetinfo, sm_stdgetinfo, SM_TIME_FOREVER, \ 73*7c478bd9Sstevel@tonic-gate SM_TIME_BLOCK, name} 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate /* sm_magic p r w flags file bf lbfsize cookie ival */ 76*7c478bd9Sstevel@tonic-gate #define smstd(flags, file, name) \ 77*7c478bd9Sstevel@tonic-gate {SmFileMagic, 0, 0, 0, flags, -1, {0}, 0, 0, file, \ 78*7c478bd9Sstevel@tonic-gate sm_stdioclose, sm_stdioread, sm_stdioseek, sm_stdiowrite, \ 79*7c478bd9Sstevel@tonic-gate sm_stdioopen, sm_stdiosetinfo, sm_stdiogetinfo, SM_TIME_FOREVER,\ 80*7c478bd9Sstevel@tonic-gate SM_TIME_BLOCK, name} 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate /* A file type for interfacing to stdio FILE* streams. */ 83*7c478bd9Sstevel@tonic-gate SM_FILE_T SmFtRealStdio_def = smstd(SMRW|SMNBF, -1, "realstdio"); 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate /* the usual - (stdin + stdout + stderr) */ 86*7c478bd9Sstevel@tonic-gate static SM_FILE_T usual[SM_IO_OPEN_MAX - 3]; 87*7c478bd9Sstevel@tonic-gate static struct sm_glue smuglue = { 0, SM_IO_OPEN_MAX - 3, usual }; 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate /* List of builtin automagically already open file pointers */ 90*7c478bd9Sstevel@tonic-gate SM_FILE_T SmIoF[6] = 91*7c478bd9Sstevel@tonic-gate { 92*7c478bd9Sstevel@tonic-gate smio(SMRD|SMLBF, SMIOIN_FILENO, "smioin"), /* smioin */ 93*7c478bd9Sstevel@tonic-gate smio(SMWR|SMLBF, SMIOOUT_FILENO, "smioout"), /* smioout */ 94*7c478bd9Sstevel@tonic-gate smio(SMWR|SMNBF, SMIOERR_FILENO, "smioerr"), /* smioerr */ 95*7c478bd9Sstevel@tonic-gate smstd(SMRD|SMNBF, SMIOIN_FILENO, "smiostdin"), /* smiostdin */ 96*7c478bd9Sstevel@tonic-gate smstd(SMWR|SMNBF, SMIOOUT_FILENO, "smiostdout"),/* smiostdout */ 97*7c478bd9Sstevel@tonic-gate smstd(SMWR|SMNBF, SMIOERR_FILENO, "smiostderr") /* smiostderr */ 98*7c478bd9Sstevel@tonic-gate }; 99*7c478bd9Sstevel@tonic-gate 100*7c478bd9Sstevel@tonic-gate /* Structure containing list of currently open file pointers */ 101*7c478bd9Sstevel@tonic-gate struct sm_glue smglue = { &smuglue, 3, SmIoF }; 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate /* 104*7c478bd9Sstevel@tonic-gate ** SM_MOREGLUE -- adds more space for open file pointers 105*7c478bd9Sstevel@tonic-gate ** 106*7c478bd9Sstevel@tonic-gate ** Parameters: 107*7c478bd9Sstevel@tonic-gate ** n -- number of new spaces for file pointers 108*7c478bd9Sstevel@tonic-gate ** 109*7c478bd9Sstevel@tonic-gate ** Returns: 110*7c478bd9Sstevel@tonic-gate ** Raises an exception if no more memory. 111*7c478bd9Sstevel@tonic-gate ** Otherwise, returns a pointer to new spaces. 112*7c478bd9Sstevel@tonic-gate */ 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate static struct sm_glue *sm_moreglue_x __P((int)); 115*7c478bd9Sstevel@tonic-gate static SM_FILE_T empty; 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate static struct sm_glue * 118*7c478bd9Sstevel@tonic-gate sm_moreglue_x(n) 119*7c478bd9Sstevel@tonic-gate register int n; 120*7c478bd9Sstevel@tonic-gate { 121*7c478bd9Sstevel@tonic-gate register struct sm_glue *g; 122*7c478bd9Sstevel@tonic-gate register SM_FILE_T *p; 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate g = (struct sm_glue *) sm_pmalloc_x(sizeof(*g) + SM_ALIGN_BITS + 125*7c478bd9Sstevel@tonic-gate n * sizeof(SM_FILE_T)); 126*7c478bd9Sstevel@tonic-gate p = (SM_FILE_T *) SM_ALIGN(g + 1); 127*7c478bd9Sstevel@tonic-gate g->gl_next = NULL; 128*7c478bd9Sstevel@tonic-gate g->gl_niobs = n; 129*7c478bd9Sstevel@tonic-gate g->gl_iobs = p; 130*7c478bd9Sstevel@tonic-gate while (--n >= 0) 131*7c478bd9Sstevel@tonic-gate *p++ = empty; 132*7c478bd9Sstevel@tonic-gate return g; 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate /* 136*7c478bd9Sstevel@tonic-gate ** SM_FP -- allocate and initialize an SM_FILE structure 137*7c478bd9Sstevel@tonic-gate ** 138*7c478bd9Sstevel@tonic-gate ** Parameters: 139*7c478bd9Sstevel@tonic-gate ** t -- file type requested to be opened. 140*7c478bd9Sstevel@tonic-gate ** flags -- control flags for file type behavior 141*7c478bd9Sstevel@tonic-gate ** oldfp -- file pointer to reuse if available (optional) 142*7c478bd9Sstevel@tonic-gate ** 143*7c478bd9Sstevel@tonic-gate ** Returns: 144*7c478bd9Sstevel@tonic-gate ** Raises exception on memory exhaustion. 145*7c478bd9Sstevel@tonic-gate ** Aborts if type is invalid. 146*7c478bd9Sstevel@tonic-gate ** Otherwise, returns file pointer for requested file type. 147*7c478bd9Sstevel@tonic-gate */ 148*7c478bd9Sstevel@tonic-gate 149*7c478bd9Sstevel@tonic-gate SM_FILE_T * 150*7c478bd9Sstevel@tonic-gate sm_fp(t, flags, oldfp) 151*7c478bd9Sstevel@tonic-gate const SM_FILE_T *t; 152*7c478bd9Sstevel@tonic-gate const int flags; 153*7c478bd9Sstevel@tonic-gate SM_FILE_T *oldfp; 154*7c478bd9Sstevel@tonic-gate { 155*7c478bd9Sstevel@tonic-gate register SM_FILE_T *fp; 156*7c478bd9Sstevel@tonic-gate register int n; 157*7c478bd9Sstevel@tonic-gate register struct sm_glue *g; 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate SM_REQUIRE(t->f_open && t->f_close && (t->f_read || t->f_write)); 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate if (!Sm_IO_DidInit) 162*7c478bd9Sstevel@tonic-gate sm_init(); 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate if (oldfp != NULL) 165*7c478bd9Sstevel@tonic-gate { 166*7c478bd9Sstevel@tonic-gate fp = oldfp; 167*7c478bd9Sstevel@tonic-gate goto found; /* for opening reusing an 'fp' */ 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate for (g = &smglue;; g = g->gl_next) 171*7c478bd9Sstevel@tonic-gate { 172*7c478bd9Sstevel@tonic-gate for (fp = g->gl_iobs, n = g->gl_niobs; --n >= 0; fp++) 173*7c478bd9Sstevel@tonic-gate if (fp->sm_magic == NULL) 174*7c478bd9Sstevel@tonic-gate goto found; 175*7c478bd9Sstevel@tonic-gate if (g->gl_next == NULL) 176*7c478bd9Sstevel@tonic-gate g->gl_next = sm_moreglue_x(NDYNAMIC); 177*7c478bd9Sstevel@tonic-gate } 178*7c478bd9Sstevel@tonic-gate found: 179*7c478bd9Sstevel@tonic-gate fp->sm_magic = SmFileMagic; /* 'fp' now valid and in-use */ 180*7c478bd9Sstevel@tonic-gate fp->f_p = NULL; /* no current pointer */ 181*7c478bd9Sstevel@tonic-gate fp->f_w = 0; /* nothing to write */ 182*7c478bd9Sstevel@tonic-gate fp->f_r = 0; /* nothing to read */ 183*7c478bd9Sstevel@tonic-gate fp->f_flags = flags; 184*7c478bd9Sstevel@tonic-gate fp->f_file = -1; /* no file */ 185*7c478bd9Sstevel@tonic-gate fp->f_bf.smb_base = NULL; /* no buffer */ 186*7c478bd9Sstevel@tonic-gate fp->f_bf.smb_size = 0; /* no buffer size with no buffer */ 187*7c478bd9Sstevel@tonic-gate fp->f_lbfsize = 0; /* not line buffered */ 188*7c478bd9Sstevel@tonic-gate fp->f_flushfp = NULL; /* no associated flush file */ 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate fp->f_cookie = fp; /* default: *open* overrides cookie setting */ 191*7c478bd9Sstevel@tonic-gate fp->f_close = t->f_close; /* assign close function */ 192*7c478bd9Sstevel@tonic-gate fp->f_read = t->f_read; /* assign read function */ 193*7c478bd9Sstevel@tonic-gate fp->f_seek = t->f_seek; /* assign seek function */ 194*7c478bd9Sstevel@tonic-gate fp->f_write = t->f_write; /* assign write function */ 195*7c478bd9Sstevel@tonic-gate fp->f_open = t->f_open; /* assign open function */ 196*7c478bd9Sstevel@tonic-gate fp->f_setinfo = t->f_setinfo; /* assign setinfo function */ 197*7c478bd9Sstevel@tonic-gate fp->f_getinfo = t->f_getinfo; /* assign getinfo function */ 198*7c478bd9Sstevel@tonic-gate fp->f_type = t->f_type; /* file type */ 199*7c478bd9Sstevel@tonic-gate 200*7c478bd9Sstevel@tonic-gate fp->f_ub.smb_base = NULL; /* no ungetc buffer */ 201*7c478bd9Sstevel@tonic-gate fp->f_ub.smb_size = 0; /* no size for no ungetc buffer */ 202*7c478bd9Sstevel@tonic-gate 203*7c478bd9Sstevel@tonic-gate if (fp->f_timeout == SM_TIME_DEFAULT) 204*7c478bd9Sstevel@tonic-gate fp->f_timeout = SM_TIME_FOREVER; 205*7c478bd9Sstevel@tonic-gate else 206*7c478bd9Sstevel@tonic-gate fp->f_timeout = t->f_timeout; /* traditional behavior */ 207*7c478bd9Sstevel@tonic-gate fp->f_timeoutstate = SM_TIME_BLOCK; /* by default */ 208*7c478bd9Sstevel@tonic-gate 209*7c478bd9Sstevel@tonic-gate return fp; 210*7c478bd9Sstevel@tonic-gate } 211*7c478bd9Sstevel@tonic-gate 212*7c478bd9Sstevel@tonic-gate /* 213*7c478bd9Sstevel@tonic-gate ** SM_CLEANUP -- cleanup function when exit called. 214*7c478bd9Sstevel@tonic-gate ** 215*7c478bd9Sstevel@tonic-gate ** This function is registered via atexit(). 216*7c478bd9Sstevel@tonic-gate ** 217*7c478bd9Sstevel@tonic-gate ** Parameters: 218*7c478bd9Sstevel@tonic-gate ** none 219*7c478bd9Sstevel@tonic-gate ** 220*7c478bd9Sstevel@tonic-gate ** Returns: 221*7c478bd9Sstevel@tonic-gate ** nothing. 222*7c478bd9Sstevel@tonic-gate ** 223*7c478bd9Sstevel@tonic-gate ** Side Effects: 224*7c478bd9Sstevel@tonic-gate ** flushes open files before they are forced closed 225*7c478bd9Sstevel@tonic-gate */ 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate void 228*7c478bd9Sstevel@tonic-gate sm_cleanup() 229*7c478bd9Sstevel@tonic-gate { 230*7c478bd9Sstevel@tonic-gate int timeout = SM_TIME_DEFAULT; 231*7c478bd9Sstevel@tonic-gate 232*7c478bd9Sstevel@tonic-gate (void) sm_fwalk(sm_flush, &timeout); /* `cheating' */ 233*7c478bd9Sstevel@tonic-gate } 234*7c478bd9Sstevel@tonic-gate 235*7c478bd9Sstevel@tonic-gate /* 236*7c478bd9Sstevel@tonic-gate ** SM_INIT -- called whenever sm_io's internal variables must be set up. 237*7c478bd9Sstevel@tonic-gate ** 238*7c478bd9Sstevel@tonic-gate ** Parameters: 239*7c478bd9Sstevel@tonic-gate ** none 240*7c478bd9Sstevel@tonic-gate ** 241*7c478bd9Sstevel@tonic-gate ** Returns: 242*7c478bd9Sstevel@tonic-gate ** none 243*7c478bd9Sstevel@tonic-gate ** 244*7c478bd9Sstevel@tonic-gate ** Side Effects: 245*7c478bd9Sstevel@tonic-gate ** Registers sm_cleanup() using atexit(). 246*7c478bd9Sstevel@tonic-gate */ 247*7c478bd9Sstevel@tonic-gate 248*7c478bd9Sstevel@tonic-gate void 249*7c478bd9Sstevel@tonic-gate sm_init() 250*7c478bd9Sstevel@tonic-gate { 251*7c478bd9Sstevel@tonic-gate if (Sm_IO_DidInit) /* paranoia */ 252*7c478bd9Sstevel@tonic-gate return; 253*7c478bd9Sstevel@tonic-gate 254*7c478bd9Sstevel@tonic-gate /* more paranoia: initialize pointers in a static variable */ 255*7c478bd9Sstevel@tonic-gate empty.f_type = NULL; 256*7c478bd9Sstevel@tonic-gate empty.sm_magic = NULL; 257*7c478bd9Sstevel@tonic-gate 258*7c478bd9Sstevel@tonic-gate /* make sure we clean up on exit */ 259*7c478bd9Sstevel@tonic-gate atexit(sm_cleanup); /* conservative */ 260*7c478bd9Sstevel@tonic-gate Sm_IO_DidInit = true; 261*7c478bd9Sstevel@tonic-gate } 262*7c478bd9Sstevel@tonic-gate 263*7c478bd9Sstevel@tonic-gate /* 264*7c478bd9Sstevel@tonic-gate ** SM_IO_SETINFO -- change info for an open file type (fp) 265*7c478bd9Sstevel@tonic-gate ** 266*7c478bd9Sstevel@tonic-gate ** The generic SM_IO_WHAT_VECTORS is auto supplied for all file types. 267*7c478bd9Sstevel@tonic-gate ** If the request is to set info other than SM_IO_WHAT_VECTORS then 268*7c478bd9Sstevel@tonic-gate ** the request is passed on to the file type's specific setinfo vector. 269*7c478bd9Sstevel@tonic-gate ** WARNING: this is working on an active/open file type. 270*7c478bd9Sstevel@tonic-gate ** 271*7c478bd9Sstevel@tonic-gate ** Parameters: 272*7c478bd9Sstevel@tonic-gate ** fp -- file to make the setting on 273*7c478bd9Sstevel@tonic-gate ** what -- type of information to set 274*7c478bd9Sstevel@tonic-gate ** valp -- structure to obtain info from 275*7c478bd9Sstevel@tonic-gate ** 276*7c478bd9Sstevel@tonic-gate ** Returns: 277*7c478bd9Sstevel@tonic-gate ** 0 on success 278*7c478bd9Sstevel@tonic-gate ** -1 on error and sets errno: 279*7c478bd9Sstevel@tonic-gate ** - when what != SM_IO_WHAT_VECTORS and setinfo vector 280*7c478bd9Sstevel@tonic-gate ** not set 281*7c478bd9Sstevel@tonic-gate ** - when vectored setinfo returns -1 282*7c478bd9Sstevel@tonic-gate */ 283*7c478bd9Sstevel@tonic-gate 284*7c478bd9Sstevel@tonic-gate int 285*7c478bd9Sstevel@tonic-gate sm_io_setinfo(fp, what, valp) 286*7c478bd9Sstevel@tonic-gate SM_FILE_T *fp; 287*7c478bd9Sstevel@tonic-gate int what; 288*7c478bd9Sstevel@tonic-gate void *valp; 289*7c478bd9Sstevel@tonic-gate { 290*7c478bd9Sstevel@tonic-gate SM_FILE_T *v = (SM_FILE_T *) valp; 291*7c478bd9Sstevel@tonic-gate 292*7c478bd9Sstevel@tonic-gate SM_REQUIRE_ISA(fp, SmFileMagic); 293*7c478bd9Sstevel@tonic-gate switch (what) 294*7c478bd9Sstevel@tonic-gate { 295*7c478bd9Sstevel@tonic-gate case SM_IO_WHAT_VECTORS: 296*7c478bd9Sstevel@tonic-gate 297*7c478bd9Sstevel@tonic-gate /* 298*7c478bd9Sstevel@tonic-gate ** This is the "generic" available for all. 299*7c478bd9Sstevel@tonic-gate ** This allows the function vectors to be replaced 300*7c478bd9Sstevel@tonic-gate ** while the file type is active. 301*7c478bd9Sstevel@tonic-gate */ 302*7c478bd9Sstevel@tonic-gate 303*7c478bd9Sstevel@tonic-gate fp->f_close = v->f_close; 304*7c478bd9Sstevel@tonic-gate fp->f_read = v->f_read; 305*7c478bd9Sstevel@tonic-gate fp->f_seek = v->f_seek; 306*7c478bd9Sstevel@tonic-gate fp->f_write = v->f_write; 307*7c478bd9Sstevel@tonic-gate fp->f_open = v->f_open; 308*7c478bd9Sstevel@tonic-gate fp->f_setinfo = v->f_setinfo; 309*7c478bd9Sstevel@tonic-gate fp->f_getinfo = v->f_getinfo; 310*7c478bd9Sstevel@tonic-gate sm_free(fp->f_type); 311*7c478bd9Sstevel@tonic-gate fp->f_type = sm_strdup_x(v->f_type); 312*7c478bd9Sstevel@tonic-gate return 0; 313*7c478bd9Sstevel@tonic-gate case SM_IO_WHAT_TIMEOUT: 314*7c478bd9Sstevel@tonic-gate fp->f_timeout = *((int *)valp); 315*7c478bd9Sstevel@tonic-gate return 0; 316*7c478bd9Sstevel@tonic-gate } 317*7c478bd9Sstevel@tonic-gate 318*7c478bd9Sstevel@tonic-gate /* Otherwise the vector will check it out */ 319*7c478bd9Sstevel@tonic-gate if (fp->f_setinfo == NULL) 320*7c478bd9Sstevel@tonic-gate { 321*7c478bd9Sstevel@tonic-gate errno = EINVAL; 322*7c478bd9Sstevel@tonic-gate return -1; 323*7c478bd9Sstevel@tonic-gate } 324*7c478bd9Sstevel@tonic-gate else 325*7c478bd9Sstevel@tonic-gate return (*fp->f_setinfo)(fp, what, valp); 326*7c478bd9Sstevel@tonic-gate } 327*7c478bd9Sstevel@tonic-gate 328*7c478bd9Sstevel@tonic-gate /* 329*7c478bd9Sstevel@tonic-gate ** SM_IO_GETINFO -- get information for an active file type (fp) 330*7c478bd9Sstevel@tonic-gate ** 331*7c478bd9Sstevel@tonic-gate ** This function supplies for all file types the answers for the 332*7c478bd9Sstevel@tonic-gate ** three requests SM_IO_WHAT_VECTORS, SM_IO_WHAT_TYPE and 333*7c478bd9Sstevel@tonic-gate ** SM_IO_WHAT_ISTYPE. Other requests are handled by the getinfo 334*7c478bd9Sstevel@tonic-gate ** vector if available for the open file type. 335*7c478bd9Sstevel@tonic-gate ** SM_IO_WHAT_VECTORS returns information for the file pointer vectors. 336*7c478bd9Sstevel@tonic-gate ** SM_IO_WHAT_TYPE returns the type identifier for the file pointer 337*7c478bd9Sstevel@tonic-gate ** SM_IO_WHAT_ISTYPE returns >0 if the passed in type matches the 338*7c478bd9Sstevel@tonic-gate ** file pointer's type. 339*7c478bd9Sstevel@tonic-gate ** SM_IO_IS_READABLE returns 1 if there is data available for reading, 340*7c478bd9Sstevel@tonic-gate ** 0 otherwise. 341*7c478bd9Sstevel@tonic-gate ** 342*7c478bd9Sstevel@tonic-gate ** Parameters: 343*7c478bd9Sstevel@tonic-gate ** fp -- file pointer for active file type 344*7c478bd9Sstevel@tonic-gate ** what -- type of information request 345*7c478bd9Sstevel@tonic-gate ** valp -- structure to place obtained info into 346*7c478bd9Sstevel@tonic-gate ** 347*7c478bd9Sstevel@tonic-gate ** Returns: 348*7c478bd9Sstevel@tonic-gate ** -1 on error and sets errno: 349*7c478bd9Sstevel@tonic-gate ** - when valp==NULL and request expects otherwise 350*7c478bd9Sstevel@tonic-gate ** - when request is not SM_IO_WHAT_VECTORS and not 351*7c478bd9Sstevel@tonic-gate ** SM_IO_WHAT_TYPE and not SM_IO_WHAT_ISTYPE 352*7c478bd9Sstevel@tonic-gate ** and getinfo vector is NULL 353*7c478bd9Sstevel@tonic-gate ** - when getinfo type vector returns -1 354*7c478bd9Sstevel@tonic-gate ** >=0 on success 355*7c478bd9Sstevel@tonic-gate */ 356*7c478bd9Sstevel@tonic-gate 357*7c478bd9Sstevel@tonic-gate int 358*7c478bd9Sstevel@tonic-gate sm_io_getinfo(fp, what, valp) 359*7c478bd9Sstevel@tonic-gate SM_FILE_T *fp; 360*7c478bd9Sstevel@tonic-gate int what; 361*7c478bd9Sstevel@tonic-gate void *valp; 362*7c478bd9Sstevel@tonic-gate { 363*7c478bd9Sstevel@tonic-gate SM_FILE_T *v = (SM_FILE_T *) valp; 364*7c478bd9Sstevel@tonic-gate 365*7c478bd9Sstevel@tonic-gate SM_REQUIRE_ISA(fp, SmFileMagic); 366*7c478bd9Sstevel@tonic-gate 367*7c478bd9Sstevel@tonic-gate switch (what) 368*7c478bd9Sstevel@tonic-gate { 369*7c478bd9Sstevel@tonic-gate case SM_IO_WHAT_VECTORS: 370*7c478bd9Sstevel@tonic-gate 371*7c478bd9Sstevel@tonic-gate /* This is the "generic" available for all */ 372*7c478bd9Sstevel@tonic-gate v->f_close = fp->f_close; 373*7c478bd9Sstevel@tonic-gate v->f_read = fp->f_read; 374*7c478bd9Sstevel@tonic-gate v->f_seek = fp->f_seek; 375*7c478bd9Sstevel@tonic-gate v->f_write = fp->f_write; 376*7c478bd9Sstevel@tonic-gate v->f_open = fp->f_open; 377*7c478bd9Sstevel@tonic-gate v->f_setinfo = fp->f_setinfo; 378*7c478bd9Sstevel@tonic-gate v->f_getinfo = fp->f_getinfo; 379*7c478bd9Sstevel@tonic-gate v->f_type = fp->f_type; 380*7c478bd9Sstevel@tonic-gate return 0; 381*7c478bd9Sstevel@tonic-gate 382*7c478bd9Sstevel@tonic-gate case SM_IO_WHAT_TYPE: 383*7c478bd9Sstevel@tonic-gate if (valp == NULL) 384*7c478bd9Sstevel@tonic-gate { 385*7c478bd9Sstevel@tonic-gate errno = EINVAL; 386*7c478bd9Sstevel@tonic-gate return -1; 387*7c478bd9Sstevel@tonic-gate } 388*7c478bd9Sstevel@tonic-gate valp = sm_strdup_x(fp->f_type); 389*7c478bd9Sstevel@tonic-gate return 0; 390*7c478bd9Sstevel@tonic-gate 391*7c478bd9Sstevel@tonic-gate case SM_IO_WHAT_ISTYPE: 392*7c478bd9Sstevel@tonic-gate if (valp == NULL) 393*7c478bd9Sstevel@tonic-gate { 394*7c478bd9Sstevel@tonic-gate errno = EINVAL; 395*7c478bd9Sstevel@tonic-gate return -1; 396*7c478bd9Sstevel@tonic-gate } 397*7c478bd9Sstevel@tonic-gate return strcmp(fp->f_type, valp) == 0; 398*7c478bd9Sstevel@tonic-gate 399*7c478bd9Sstevel@tonic-gate case SM_IO_IS_READABLE: 400*7c478bd9Sstevel@tonic-gate 401*7c478bd9Sstevel@tonic-gate /* if there is data in the buffer, it must be readable */ 402*7c478bd9Sstevel@tonic-gate if (fp->f_r > 0) 403*7c478bd9Sstevel@tonic-gate return 1; 404*7c478bd9Sstevel@tonic-gate 405*7c478bd9Sstevel@tonic-gate /* otherwise query the underlying file */ 406*7c478bd9Sstevel@tonic-gate break; 407*7c478bd9Sstevel@tonic-gate 408*7c478bd9Sstevel@tonic-gate case SM_IO_WHAT_TIMEOUT: 409*7c478bd9Sstevel@tonic-gate *((int *) valp) = fp->f_timeout; 410*7c478bd9Sstevel@tonic-gate return 0; 411*7c478bd9Sstevel@tonic-gate 412*7c478bd9Sstevel@tonic-gate case SM_IO_WHAT_FD: 413*7c478bd9Sstevel@tonic-gate if (fp->f_file > -1) 414*7c478bd9Sstevel@tonic-gate return fp->f_file; 415*7c478bd9Sstevel@tonic-gate 416*7c478bd9Sstevel@tonic-gate /* try the file type specific getinfo to see if it knows */ 417*7c478bd9Sstevel@tonic-gate break; 418*7c478bd9Sstevel@tonic-gate } 419*7c478bd9Sstevel@tonic-gate 420*7c478bd9Sstevel@tonic-gate /* Otherwise the vector will check it out */ 421*7c478bd9Sstevel@tonic-gate if (fp->f_getinfo == NULL) 422*7c478bd9Sstevel@tonic-gate { 423*7c478bd9Sstevel@tonic-gate errno = EINVAL; 424*7c478bd9Sstevel@tonic-gate return -1; 425*7c478bd9Sstevel@tonic-gate } 426*7c478bd9Sstevel@tonic-gate return (*fp->f_getinfo)(fp, what, valp); 427*7c478bd9Sstevel@tonic-gate } 428