10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51672Sraf * Common Development and Distribution License (the "License"). 61672Sraf * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211672Sraf 220Sstevel@tonic-gate /* 23*5891Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 300Sstevel@tonic-gate /* All Rights Reserved */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include "synonyms.h" 340Sstevel@tonic-gate #include "mtlib.h" 350Sstevel@tonic-gate #include "file64.h" 360Sstevel@tonic-gate 370Sstevel@tonic-gate #define _iob __iob 380Sstevel@tonic-gate 390Sstevel@tonic-gate #include <sys/types.h> 400Sstevel@tonic-gate #include <stdlib.h> 410Sstevel@tonic-gate #include <stdio.h> 420Sstevel@tonic-gate #include <thread.h> 430Sstevel@tonic-gate #include <synch.h> 440Sstevel@tonic-gate #include <unistd.h> 450Sstevel@tonic-gate #include <string.h> 460Sstevel@tonic-gate #include "stdiom.h" 470Sstevel@tonic-gate #include <wchar.h> 480Sstevel@tonic-gate #include <sys/stat.h> 490Sstevel@tonic-gate #include <stddef.h> 500Sstevel@tonic-gate #include <errno.h> 511846Scraigm #include <fcntl.h> 520Sstevel@tonic-gate 530Sstevel@tonic-gate #undef end 540Sstevel@tonic-gate 550Sstevel@tonic-gate #define FILE_ARY_SZ 8 /* a nice size for FILE array & end_buffer_ptrs */ 560Sstevel@tonic-gate 570Sstevel@tonic-gate #ifdef _LP64 580Sstevel@tonic-gate 590Sstevel@tonic-gate /* 600Sstevel@tonic-gate * Macros to declare and loop over a fp or fp/xfp combo to 610Sstevel@tonic-gate * avoid some of the _LP64 ifdef hell. 620Sstevel@tonic-gate */ 630Sstevel@tonic-gate 640Sstevel@tonic-gate #define FPDECL(fp) FILE *fp 650Sstevel@tonic-gate #define FIRSTFP(lp, fp) fp = lp->iobp 660Sstevel@tonic-gate #define NEXTFP(fp) fp++ 671846Scraigm #define FPLOCK(fp) &fp->_lock 681846Scraigm #define FPSTATE(fp) &fp->_state 690Sstevel@tonic-gate 700Sstevel@tonic-gate #define xFILE FILE 710Sstevel@tonic-gate 720Sstevel@tonic-gate #else 730Sstevel@tonic-gate 740Sstevel@tonic-gate #define FPDECL(fp) FILE *fp; xFILE *x##fp 750Sstevel@tonic-gate #define FIRSTFP(lp, fp) x##fp = lp->iobp; \ 760Sstevel@tonic-gate fp = x##fp ? &x##fp->_iob : &_iob[0] 770Sstevel@tonic-gate #define NEXTFP(fp) (x##fp ? fp = &(++x##fp)->_iob : ++fp) 781846Scraigm #define FPLOCK(fp) x##fp ? \ 791846Scraigm &x##fp->xlock : &_xftab[IOPIND(fp)]._lock 801846Scraigm #define FPSTATE(fp) x##fp ? \ 811846Scraigm &x##fp->xstate : &_xftab[IOPIND(fp)]._state 820Sstevel@tonic-gate 830Sstevel@tonic-gate /* The extended 32-bit file structure for use in link buffers */ 840Sstevel@tonic-gate typedef struct xFILE { 850Sstevel@tonic-gate FILE _iob; /* must be first! */ 860Sstevel@tonic-gate struct xFILEdata _xdat; 870Sstevel@tonic-gate } xFILE; 880Sstevel@tonic-gate 890Sstevel@tonic-gate #define xmagic _xdat._magic 900Sstevel@tonic-gate #define xend _xdat._end 910Sstevel@tonic-gate #define xlock _xdat._lock 920Sstevel@tonic-gate #define xstate _xdat._state 930Sstevel@tonic-gate 940Sstevel@tonic-gate #define FILEx(fp) ((struct xFILE *)(uintptr_t)fp) 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* 970Sstevel@tonic-gate * The magic number stored is actually the pointer scrambled with 980Sstevel@tonic-gate * a magic number. Pointers to data items live everywhere in memory 990Sstevel@tonic-gate * so we scramble the pointer in order to avoid accidental collisions. 1000Sstevel@tonic-gate */ 1010Sstevel@tonic-gate #define XFILEMAGIC 0x63687367 1020Sstevel@tonic-gate #define XMAGIC(xfp) ((uintptr_t)(xfp) ^ XFILEMAGIC) 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate #endif /* _LP64 */ 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate struct _link_ /* manages a list of streams */ 1070Sstevel@tonic-gate { 1080Sstevel@tonic-gate xFILE *iobp; /* the array of (x)FILE's */ 1090Sstevel@tonic-gate /* NULL for the __first_link in ILP32 */ 1100Sstevel@tonic-gate int niob; /* length of the arrays */ 1110Sstevel@tonic-gate struct _link_ *next; /* next in the list */ 1120Sstevel@tonic-gate }; 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate /* 1150Sstevel@tonic-gate * With dynamic linking, iob may be in either the library or in the user's 1160Sstevel@tonic-gate * a.out, so the run time linker fixes up the first entry in __first_link at 1170Sstevel@tonic-gate * process startup time. 1180Sstevel@tonic-gate * 1190Sstevel@tonic-gate * In 32 bit processes, we don't have xFILE[FILE_ARY_SZ] but FILE[], 1200Sstevel@tonic-gate * and _xftab[] instead; this is denoted by having iobp set to NULL in 1210Sstevel@tonic-gate * 32 bit mode for the first link entry. 1220Sstevel@tonic-gate */ 1230Sstevel@tonic-gate struct _link_ __first_link = /* first in linked list */ 1240Sstevel@tonic-gate { 1250Sstevel@tonic-gate #if !defined(_LP64) 1260Sstevel@tonic-gate NULL, 1270Sstevel@tonic-gate #else 1280Sstevel@tonic-gate &_iob[0], 1290Sstevel@tonic-gate #endif 1300Sstevel@tonic-gate _NFILE, 1310Sstevel@tonic-gate NULL 1320Sstevel@tonic-gate }; 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate /* 1350Sstevel@tonic-gate * Information cached to speed up searches. We remember where we 1360Sstevel@tonic-gate * last found a free FILE* and we remember whether we saw any fcloses 1370Sstevel@tonic-gate * in between. We also count the number of chunks we allocated, see 1380Sstevel@tonic-gate * _findiop() for an explanation. 1390Sstevel@tonic-gate * These variables are all protected by _first_link_lock. 1400Sstevel@tonic-gate */ 1410Sstevel@tonic-gate static struct _link_ *lastlink = NULL; 1420Sstevel@tonic-gate static int fcloses; 1430Sstevel@tonic-gate static int nchunks; 1440Sstevel@tonic-gate 1451846Scraigm static mutex_t _first_link_lock = DEFAULTMUTEX; 1460Sstevel@tonic-gate 1471846Scraigm static int _fflush_l_iops(void); 1481846Scraigm static FILE *getiop(FILE *, rmutex_t *, mbstate_t *); 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate /* 1510Sstevel@tonic-gate * All functions that understand the linked list of iob's follow. 1520Sstevel@tonic-gate */ 1530Sstevel@tonic-gate #pragma weak _cleanup = __cleanup 1540Sstevel@tonic-gate void 1550Sstevel@tonic-gate __cleanup(void) /* called at process end to flush ouput streams */ 1560Sstevel@tonic-gate { 1570Sstevel@tonic-gate (void) fflush(NULL); 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate /* 1610Sstevel@tonic-gate * For fork1-safety (see libc_prepare_atfork(), etc). 1620Sstevel@tonic-gate */ 1630Sstevel@tonic-gate void 1640Sstevel@tonic-gate stdio_locks() 1650Sstevel@tonic-gate { 1661881Scasper (void) _private_mutex_lock(&_first_link_lock); 1670Sstevel@tonic-gate /* 1680Sstevel@tonic-gate * XXX: We should acquire all of the iob locks here. 1690Sstevel@tonic-gate */ 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate void 1730Sstevel@tonic-gate stdio_unlocks() 1740Sstevel@tonic-gate { 1750Sstevel@tonic-gate /* 1760Sstevel@tonic-gate * XXX: We should release all of the iob locks here. 1770Sstevel@tonic-gate */ 1781881Scasper (void) _private_mutex_unlock(&_first_link_lock); 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate void 1820Sstevel@tonic-gate _flushlbf(void) /* fflush() all line-buffered streams */ 1830Sstevel@tonic-gate { 1840Sstevel@tonic-gate FPDECL(fp); 1850Sstevel@tonic-gate int i; 1860Sstevel@tonic-gate struct _link_ *lp; 1871846Scraigm /* Allow compiler to optimize the loop */ 1881846Scraigm int threaded = __libc_threaded; 1890Sstevel@tonic-gate 1901846Scraigm if (threaded) 191*5891Sraf cancel_safe_mutex_lock(&_first_link_lock); 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate lp = &__first_link; 1940Sstevel@tonic-gate do { 1950Sstevel@tonic-gate FIRSTFP(lp, fp); 1960Sstevel@tonic-gate for (i = lp->niob; --i >= 0; NEXTFP(fp)) { 1971846Scraigm /* 1981846Scraigm * The additional _IONBF check guards againsts 1991846Scraigm * allocated but uninitialized iops (see _findiop). 2001846Scraigm * We also automatically skip non allocated iop's. 2011846Scraigm * Don't block on locks. 2021846Scraigm */ 2031846Scraigm if ((fp->_flag & (_IOLBF | _IOWRT | _IONBF)) == 2041846Scraigm (_IOLBF | _IOWRT)) { 2051846Scraigm if (threaded) { 2061846Scraigm rmutex_t *lk = FPLOCK(fp); 207*5891Sraf if (cancel_safe_mutex_trylock(lk) != 0) 2081846Scraigm continue; 2091846Scraigm /* Recheck after locking */ 2101846Scraigm if ((fp->_flag & (_IOLBF | _IOWRT)) == 2111846Scraigm (_IOLBF | _IOWRT)) { 2121846Scraigm (void) _fflush_u(fp); 2131846Scraigm } 214*5891Sraf cancel_safe_mutex_unlock(lk); 2151846Scraigm } else { 2161846Scraigm (void) _fflush_u(fp); 2171846Scraigm } 2181846Scraigm } 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate } while ((lp = lp->next) != NULL); 2210Sstevel@tonic-gate 2221846Scraigm if (threaded) 223*5891Sraf cancel_safe_mutex_unlock(&_first_link_lock); 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate /* allocate an unused stream; NULL if cannot */ 2270Sstevel@tonic-gate FILE * 2280Sstevel@tonic-gate _findiop(void) 2290Sstevel@tonic-gate { 2300Sstevel@tonic-gate struct _link_ *lp, **prev; 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate /* used so there only needs to be one malloc() */ 2330Sstevel@tonic-gate #ifdef _LP64 2340Sstevel@tonic-gate typedef struct { 2350Sstevel@tonic-gate struct _link_ hdr; 2360Sstevel@tonic-gate FILE iob[FILE_ARY_SZ]; 2370Sstevel@tonic-gate } Pkg; 2380Sstevel@tonic-gate #else 2390Sstevel@tonic-gate typedef union { 2400Sstevel@tonic-gate struct { /* Normal */ 2410Sstevel@tonic-gate struct _link_ hdr; 2420Sstevel@tonic-gate xFILE iob[FILE_ARY_SZ]; 2430Sstevel@tonic-gate } Pkgn; 2440Sstevel@tonic-gate struct { /* Reversed */ 2450Sstevel@tonic-gate xFILE iob[FILE_ARY_SZ]; 2460Sstevel@tonic-gate struct _link_ hdr; 2470Sstevel@tonic-gate } Pkgr; 2480Sstevel@tonic-gate } Pkg; 2490Sstevel@tonic-gate uintptr_t delta; 2500Sstevel@tonic-gate #endif 2510Sstevel@tonic-gate Pkg *pkgp; 2520Sstevel@tonic-gate struct _link_ *hdr; 2530Sstevel@tonic-gate FPDECL(fp); 2540Sstevel@tonic-gate int i; 2551846Scraigm int threaded = __libc_threaded; 2560Sstevel@tonic-gate 2571846Scraigm if (threaded) 258*5891Sraf cancel_safe_mutex_lock(&_first_link_lock); 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate if (lastlink == NULL) { 2610Sstevel@tonic-gate rescan: 2620Sstevel@tonic-gate fcloses = 0; 2630Sstevel@tonic-gate lastlink = &__first_link; 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate lp = lastlink; 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate /* 2690Sstevel@tonic-gate * lock to make testing of fp->_flag == 0 and acquiring the fp atomic 2700Sstevel@tonic-gate * and for allocation of new links 2710Sstevel@tonic-gate * low contention expected on _findiop(), hence coarse locking. 2720Sstevel@tonic-gate * for finer granularity, use fp->_lock for allocating an iop 2730Sstevel@tonic-gate * and make the testing of lp->next and allocation of new link atomic 2740Sstevel@tonic-gate * using lp->_lock 2750Sstevel@tonic-gate */ 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate do { 2780Sstevel@tonic-gate prev = &lp->next; 2790Sstevel@tonic-gate FIRSTFP(lp, fp); 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate for (i = lp->niob; --i >= 0; NEXTFP(fp)) { 2821846Scraigm FILE *ret; 2831846Scraigm if (threaded) { 2841846Scraigm ret = getiop(fp, FPLOCK(fp), FPSTATE(fp)); 2851846Scraigm if (ret != NULL) { 286*5891Sraf cancel_safe_mutex_unlock( 2871881Scasper &_first_link_lock); 2881881Scasper return (ret); 2891846Scraigm } 2901846Scraigm } else { 2911846Scraigm ret = getiop(fp, NULL, FPSTATE(fp)); 2921846Scraigm if (ret != NULL) 2931846Scraigm return (ret); 2941846Scraigm } 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate } while ((lastlink = lp = lp->next) != NULL); 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate /* 2990Sstevel@tonic-gate * If there was a sufficient number of fcloses since we last started 3000Sstevel@tonic-gate * at __first_link, we rescan all fp's again. We do not rescan for 3010Sstevel@tonic-gate * all fcloses; that would simplify the algorithm but would make 3020Sstevel@tonic-gate * search times near O(n) again. 3030Sstevel@tonic-gate * Worst case behaviour would still be pretty bad (open a full set, 3040Sstevel@tonic-gate * then continously opening and closing one FILE * gets you a full 3050Sstevel@tonic-gate * scan each time). That's why we over allocate 1 FILE for each 3060Sstevel@tonic-gate * 32 chunks. More over allocation is better; this is a nice 3070Sstevel@tonic-gate * empirical value which doesn't cost a lot of memory, doesn't 3080Sstevel@tonic-gate * overallocate until we reach 256 FILE *s and keeps the performance 3090Sstevel@tonic-gate * pretty close to the optimum. 3100Sstevel@tonic-gate */ 3110Sstevel@tonic-gate if (fcloses > nchunks/32) 3120Sstevel@tonic-gate goto rescan; 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate /* 3150Sstevel@tonic-gate * Need to allocate another and put it in the linked list. 3160Sstevel@tonic-gate */ 3170Sstevel@tonic-gate if ((pkgp = malloc(sizeof (Pkg))) == NULL) { 3181846Scraigm if (threaded) 319*5891Sraf cancel_safe_mutex_unlock(&_first_link_lock); 3200Sstevel@tonic-gate return (NULL); 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate (void) memset(pkgp, 0, sizeof (Pkg)); 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate #ifdef _LP64 3260Sstevel@tonic-gate hdr = &pkgp->hdr; 3270Sstevel@tonic-gate hdr->iobp = &pkgp->iob[0]; 3280Sstevel@tonic-gate #else 3290Sstevel@tonic-gate /* 3300Sstevel@tonic-gate * The problem with referencing a word after a FILE* is the possibility 3310Sstevel@tonic-gate * of a SIGSEGV if a non-stdio issue FILE structure ends on a page 3320Sstevel@tonic-gate * boundary. We run this check so we never need to run an expensive 3330Sstevel@tonic-gate * check like mincore() in order to know whether it is 3340Sstevel@tonic-gate * safe to dereference ((xFILE*)fp)->xmagic. 3350Sstevel@tonic-gate * We allocate the block with two alternative layouts; if one 3360Sstevel@tonic-gate * layout is not properly aligned for our purposes, the other layout 3370Sstevel@tonic-gate * will be because the size of _link_ is small compared to 3380Sstevel@tonic-gate * sizeof (xFILE). 3390Sstevel@tonic-gate * The check performed is this: 3400Sstevel@tonic-gate * If the distance from pkgp to the end of the page is 3410Sstevel@tonic-gate * less than the the offset of the last xmagic field in the 3420Sstevel@tonic-gate * xFILE structure, (the 0x1000 boundary is inside our just 3430Sstevel@tonic-gate * allocated structure) and the distance modulo the size of xFILE 3440Sstevel@tonic-gate * is identical to the offset of the first xmagic in the 3450Sstevel@tonic-gate * structure (i.e., XXXXXX000 points to an xmagic field), 3460Sstevel@tonic-gate * we need to use the reverse structure. 3470Sstevel@tonic-gate */ 3480Sstevel@tonic-gate if ((delta = 0x1000 - ((uintptr_t)pkgp & 0xfff)) <= 349*5891Sraf offsetof(Pkg, Pkgn.iob[FILE_ARY_SZ-1].xmagic) && 3500Sstevel@tonic-gate delta % sizeof (struct xFILE) == 351*5891Sraf offsetof(Pkg, Pkgn.iob[0].xmagic)) { 3520Sstevel@tonic-gate /* Use reversed structure */ 3530Sstevel@tonic-gate hdr = &pkgp->Pkgr.hdr; 3540Sstevel@tonic-gate hdr->iobp = &pkgp->Pkgr.iob[0]; 3550Sstevel@tonic-gate } else { 3560Sstevel@tonic-gate /* Use normal structure */ 3570Sstevel@tonic-gate hdr = &pkgp->Pkgn.hdr; 3580Sstevel@tonic-gate hdr->iobp = &pkgp->Pkgn.iob[0]; 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate #endif /* _LP64 */ 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate hdr->niob = FILE_ARY_SZ; 3630Sstevel@tonic-gate nchunks++; 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate #ifdef _LP64 3660Sstevel@tonic-gate fp = hdr->iobp; 3670Sstevel@tonic-gate for (i = 0; i < FILE_ARY_SZ; i++) 3680Sstevel@tonic-gate _private_mutex_init(&fp[i]._lock, 369*5891Sraf USYNC_THREAD|LOCK_RECURSIVE, NULL); 3700Sstevel@tonic-gate #else 3710Sstevel@tonic-gate xfp = hdr->iobp; 3720Sstevel@tonic-gate fp = &xfp->_iob; 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate for (i = 0; i < FILE_ARY_SZ; i++) { 3750Sstevel@tonic-gate xfp[i].xmagic = XMAGIC(&xfp[i]); 3760Sstevel@tonic-gate _private_mutex_init(&xfp[i].xlock, 377*5891Sraf USYNC_THREAD|LOCK_RECURSIVE, NULL); 3780Sstevel@tonic-gate } 3790Sstevel@tonic-gate #endif /* _LP64 */ 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate lastlink = *prev = hdr; 3820Sstevel@tonic-gate fp->_ptr = 0; 3830Sstevel@tonic-gate fp->_base = 0; 3840Sstevel@tonic-gate fp->_flag = 0377; /* claim the fp by setting low 8 bits */ 3851846Scraigm if (threaded) 386*5891Sraf cancel_safe_mutex_unlock(&_first_link_lock); 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate return (fp); 3890Sstevel@tonic-gate } 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate static void 3920Sstevel@tonic-gate isseekable(FILE *iop) 3930Sstevel@tonic-gate { 3940Sstevel@tonic-gate struct stat64 fstatbuf; 3950Sstevel@tonic-gate int save_errno; 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate save_errno = errno; 3980Sstevel@tonic-gate 3991846Scraigm if (fstat64(GET_FD(iop), &fstatbuf) != 0) { 4000Sstevel@tonic-gate /* 4010Sstevel@tonic-gate * when we don't know what it is we'll 4020Sstevel@tonic-gate * do the old behaviour and flush 4030Sstevel@tonic-gate * the stream 4040Sstevel@tonic-gate */ 4050Sstevel@tonic-gate SET_SEEKABLE(iop); 4060Sstevel@tonic-gate errno = save_errno; 4070Sstevel@tonic-gate return; 4080Sstevel@tonic-gate } 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate /* 4110Sstevel@tonic-gate * check for what is non-SEEKABLE 4120Sstevel@tonic-gate * otherwise assume it's SEEKABLE so we get the old 4130Sstevel@tonic-gate * behaviour and flush the stream 4140Sstevel@tonic-gate */ 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate if (S_ISFIFO(fstatbuf.st_mode) || S_ISCHR(fstatbuf.st_mode) || 4170Sstevel@tonic-gate S_ISSOCK(fstatbuf.st_mode) || S_ISDOOR(fstatbuf.st_mode)) { 4180Sstevel@tonic-gate CLEAR_SEEKABLE(iop); 4190Sstevel@tonic-gate } else { 4200Sstevel@tonic-gate SET_SEEKABLE(iop); 4210Sstevel@tonic-gate } 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate errno = save_errno; 4240Sstevel@tonic-gate } 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate #ifdef _LP64 4270Sstevel@tonic-gate void 4280Sstevel@tonic-gate _setbufend(FILE *iop, Uchar *end) /* set the end pointer for this iop */ 4290Sstevel@tonic-gate { 4300Sstevel@tonic-gate iop->_end = end; 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate isseekable(iop); 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate #undef _realbufend 4360Sstevel@tonic-gate 4370Sstevel@tonic-gate Uchar * 4380Sstevel@tonic-gate _realbufend(FILE *iop) /* get the end pointer for this iop */ 4390Sstevel@tonic-gate { 4400Sstevel@tonic-gate return (iop->_end); 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate #else /* _LP64 */ 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate /* 4460Sstevel@tonic-gate * Awkward functions not needed for the sane 64 bit environment. 4470Sstevel@tonic-gate */ 4480Sstevel@tonic-gate /* 4490Sstevel@tonic-gate * xmagic must not be aligned on a 4K boundary. We guarantee this in 4500Sstevel@tonic-gate * _findiop(). 4510Sstevel@tonic-gate */ 4520Sstevel@tonic-gate #define VALIDXFILE(xfp) \ 4530Sstevel@tonic-gate (((uintptr_t)&(xfp)->xmagic & 0xfff) && \ 4540Sstevel@tonic-gate (xfp)->xmagic == XMAGIC(FILEx(xfp))) 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate static struct xFILEdata * 4570Sstevel@tonic-gate getxfdat(FILE *iop) 4580Sstevel@tonic-gate { 4590Sstevel@tonic-gate if (STDIOP(iop)) 4600Sstevel@tonic-gate return (&_xftab[IOPIND(iop)]); 4610Sstevel@tonic-gate else if (VALIDXFILE(FILEx(iop))) 4620Sstevel@tonic-gate return (&FILEx(iop)->_xdat); 4630Sstevel@tonic-gate else 4640Sstevel@tonic-gate return (NULL); 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate void 4680Sstevel@tonic-gate _setbufend(FILE *iop, Uchar *end) /* set the end pointer for this iop */ 4690Sstevel@tonic-gate { 4700Sstevel@tonic-gate struct xFILEdata *dat = getxfdat(iop); 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate if (dat != NULL) 4730Sstevel@tonic-gate dat->_end = end; 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate isseekable(iop); 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate /* 4780Sstevel@tonic-gate * For binary compatibility with user programs using the 4790Sstevel@tonic-gate * old _bufend macro. This is *so* broken, fileno() 4800Sstevel@tonic-gate * is not the proper index. 4810Sstevel@tonic-gate */ 4821846Scraigm if (iop->_magic < _NFILE) 4831846Scraigm _bufendtab[iop->_magic] = end; 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate } 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate Uchar * 4880Sstevel@tonic-gate _realbufend(FILE *iop) /* get the end pointer for this iop */ 4890Sstevel@tonic-gate { 4900Sstevel@tonic-gate struct xFILEdata *dat = getxfdat(iop); 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate if (dat != NULL) 4930Sstevel@tonic-gate return (dat->_end); 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate return (NULL); 4960Sstevel@tonic-gate } 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate /* 4990Sstevel@tonic-gate * _reallock() is invoked in each stdio call through the IOB_LCK() macro, 5000Sstevel@tonic-gate * it is therefor extremely performance sensitive. We get better performance 5010Sstevel@tonic-gate * by inlining the STDIOP check in IOB_LCK and inlining a custom version 5020Sstevel@tonic-gate * of getfxdat() here. 5030Sstevel@tonic-gate */ 5040Sstevel@tonic-gate rmutex_t * 5050Sstevel@tonic-gate _reallock(FILE *iop) 5060Sstevel@tonic-gate { 5070Sstevel@tonic-gate if (VALIDXFILE(FILEx(iop))) 5080Sstevel@tonic-gate return (&FILEx(iop)->xlock); 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate return (NULL); 5110Sstevel@tonic-gate } 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate #endif /* _LP64 */ 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate /* make sure _cnt, _ptr are correct */ 5160Sstevel@tonic-gate void 5170Sstevel@tonic-gate _bufsync(FILE *iop, Uchar *bufend) 5180Sstevel@tonic-gate { 5190Sstevel@tonic-gate ssize_t spaceleft; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate spaceleft = bufend - iop->_ptr; 5220Sstevel@tonic-gate if (bufend < iop->_ptr) { 5230Sstevel@tonic-gate iop->_ptr = bufend; 5240Sstevel@tonic-gate iop->_cnt = 0; 5250Sstevel@tonic-gate } else if (spaceleft < iop->_cnt) 5260Sstevel@tonic-gate iop->_cnt = spaceleft; 5270Sstevel@tonic-gate } 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate /* really write out current buffer contents */ 5300Sstevel@tonic-gate int 5310Sstevel@tonic-gate _xflsbuf(FILE *iop) 5320Sstevel@tonic-gate { 5330Sstevel@tonic-gate ssize_t n; 5340Sstevel@tonic-gate Uchar *base = iop->_base; 5350Sstevel@tonic-gate Uchar *bufend; 5360Sstevel@tonic-gate ssize_t num_wrote; 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate /* 5390Sstevel@tonic-gate * Hopefully, be stable with respect to interrupts... 5400Sstevel@tonic-gate */ 5410Sstevel@tonic-gate n = iop->_ptr - base; 5420Sstevel@tonic-gate iop->_ptr = base; 5430Sstevel@tonic-gate bufend = _bufend(iop); 5440Sstevel@tonic-gate if (iop->_flag & (_IOLBF | _IONBF)) 5450Sstevel@tonic-gate iop->_cnt = 0; /* always go to a flush */ 5460Sstevel@tonic-gate else 5470Sstevel@tonic-gate iop->_cnt = bufend - base; 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate if (_needsync(iop, bufend)) /* recover from interrupts */ 5500Sstevel@tonic-gate _bufsync(iop, bufend); 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate if (n > 0) { 5531846Scraigm int fd = GET_FD(iop); 554*5891Sraf while ((num_wrote = write(fd, base, (size_t)n)) != n) { 5550Sstevel@tonic-gate if (num_wrote <= 0) { 556*5891Sraf if (!cancel_active()) 557*5891Sraf iop->_flag |= _IOERR; 5580Sstevel@tonic-gate return (EOF); 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate n -= num_wrote; 5610Sstevel@tonic-gate base += num_wrote; 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate } 5640Sstevel@tonic-gate return (0); 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate /* flush (write) buffer */ 5680Sstevel@tonic-gate int 5690Sstevel@tonic-gate fflush(FILE *iop) 5700Sstevel@tonic-gate { 5710Sstevel@tonic-gate int res; 5720Sstevel@tonic-gate rmutex_t *lk; 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate if (iop) { 5750Sstevel@tonic-gate FLOCKFILE(lk, iop); 5760Sstevel@tonic-gate res = _fflush_u(iop); 5770Sstevel@tonic-gate FUNLOCKFILE(lk); 5780Sstevel@tonic-gate } else { 5791846Scraigm res = _fflush_l_iops(); /* flush all iops */ 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate return (res); 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate static int 5851846Scraigm _fflush_l_iops(void) /* flush all buffers */ 5860Sstevel@tonic-gate { 5870Sstevel@tonic-gate FPDECL(iop); 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate int i; 5900Sstevel@tonic-gate struct _link_ *lp; 5910Sstevel@tonic-gate int res = 0; 5921846Scraigm rmutex_t *lk; 5931846Scraigm /* Allow the compiler to optimize the load out of the loop */ 5941846Scraigm int threaded = __libc_threaded; 5950Sstevel@tonic-gate 5961846Scraigm if (threaded) 597*5891Sraf cancel_safe_mutex_lock(&_first_link_lock); 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate lp = &__first_link; 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate do { 6020Sstevel@tonic-gate /* 6031846Scraigm * We need to grab the file locks or file corruption 6041846Scraigm * will happen. But we first check the flags field 6051846Scraigm * knowing that when it is 0, it isn't allocated and 6061846Scraigm * cannot be allocated while we're holding the 6071846Scraigm * _first_link_lock. And when _IONBF is set (also the 6081846Scraigm * case when _flag is 0377, or alloc in progress), we 6091846Scraigm * also ignore it. 6101846Scraigm * 6111846Scraigm * Ignore locked streams; it will appear as if 6121846Scraigm * concurrent updates happened after fflush(NULL). Note 6131846Scraigm * that we even attempt to lock if the locking is set to 6141846Scraigm * "by caller". We don't want to penalize callers of 6151846Scraigm * __fsetlocking() by not flushing their files. Note: if 6161846Scraigm * __fsetlocking() callers don't employ any locking, they 6171846Scraigm * may still face corruption in fflush(NULL); but that's 6181846Scraigm * no change from earlier releases. 6190Sstevel@tonic-gate */ 6200Sstevel@tonic-gate FIRSTFP(lp, iop); 6210Sstevel@tonic-gate for (i = lp->niob; --i >= 0; NEXTFP(iop)) { 6221846Scraigm unsigned int flag = iop->_flag; 6231846Scraigm 6241846Scraigm /* flag 0, flag 0377, or _IONBF set */ 6251846Scraigm if (flag == 0 || (flag & _IONBF) != 0) 6261846Scraigm continue; 6271846Scraigm 6281846Scraigm if (threaded) { 6291846Scraigm lk = FPLOCK(iop); 630*5891Sraf if (cancel_safe_mutex_trylock(lk) != 0) 6311846Scraigm continue; 6321846Scraigm } 6331846Scraigm 6341846Scraigm if (!(iop->_flag & _IONBF)) { 6350Sstevel@tonic-gate /* 6361846Scraigm * don't need to worry about the _IORW case 6371846Scraigm * since the iop will also marked with _IOREAD 6381846Scraigm * or _IOWRT whichever we are really doing 6390Sstevel@tonic-gate */ 6401846Scraigm if (iop->_flag & _IOWRT) { 6411846Scraigm /* Flush write buffers */ 6421846Scraigm res |= _fflush_u(iop); 6431846Scraigm } else if (iop->_flag & _IOREAD) { 6441846Scraigm /* 6451846Scraigm * flush seekable read buffers 6461846Scraigm * don't flush non-seekable read buffers 6471846Scraigm */ 6481846Scraigm if (GET_SEEKABLE(iop)) { 6491846Scraigm res |= _fflush_u(iop); 6501846Scraigm } 6511846Scraigm } 6520Sstevel@tonic-gate } 6531846Scraigm if (threaded) 654*5891Sraf cancel_safe_mutex_unlock(lk); 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate } while ((lp = lp->next) != NULL); 6571846Scraigm if (threaded) 658*5891Sraf cancel_safe_mutex_unlock(&_first_link_lock); 6590Sstevel@tonic-gate return (res); 6600Sstevel@tonic-gate } 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate /* flush buffer */ 6630Sstevel@tonic-gate int 6640Sstevel@tonic-gate _fflush_u(FILE *iop) 6650Sstevel@tonic-gate { 6660Sstevel@tonic-gate int res = 0; 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate /* this portion is always assumed locked */ 6690Sstevel@tonic-gate if (!(iop->_flag & _IOWRT)) { 6701846Scraigm (void) lseek64(GET_FD(iop), -iop->_cnt, SEEK_CUR); 6710Sstevel@tonic-gate iop->_cnt = 0; 6720Sstevel@tonic-gate /* needed for ungetc & multibyte pushbacks */ 6730Sstevel@tonic-gate iop->_ptr = iop->_base; 6740Sstevel@tonic-gate if (iop->_flag & _IORW) { 6750Sstevel@tonic-gate iop->_flag &= ~_IOREAD; 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate return (0); 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate if (iop->_base != NULL && iop->_ptr > iop->_base) { 6800Sstevel@tonic-gate res = _xflsbuf(iop); 6810Sstevel@tonic-gate } 6820Sstevel@tonic-gate if (iop->_flag & _IORW) { 6830Sstevel@tonic-gate iop->_flag &= ~_IOWRT; 6840Sstevel@tonic-gate iop->_cnt = 0; 6850Sstevel@tonic-gate } 6860Sstevel@tonic-gate return (res); 6870Sstevel@tonic-gate } 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate /* flush buffer and close stream */ 6900Sstevel@tonic-gate int 6910Sstevel@tonic-gate fclose(FILE *iop) 6920Sstevel@tonic-gate { 6930Sstevel@tonic-gate int res = 0; 6940Sstevel@tonic-gate rmutex_t *lk; 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate if (iop == NULL) { 6970Sstevel@tonic-gate return (EOF); /* avoid passing zero to FLOCKFILE */ 6980Sstevel@tonic-gate } 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate FLOCKFILE(lk, iop); 7010Sstevel@tonic-gate if (iop->_flag == 0) { 7020Sstevel@tonic-gate FUNLOCKFILE(lk); 7030Sstevel@tonic-gate return (EOF); 7040Sstevel@tonic-gate } 7050Sstevel@tonic-gate /* Is not unbuffered and opened for read and/or write ? */ 7060Sstevel@tonic-gate if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW))) 7070Sstevel@tonic-gate res = _fflush_u(iop); 7081846Scraigm if (close(GET_FD(iop)) < 0) 7090Sstevel@tonic-gate res = EOF; 7100Sstevel@tonic-gate if (iop->_flag & _IOMYBUF) { 7110Sstevel@tonic-gate (void) free((char *)iop->_base - PUSHBACK); 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate iop->_base = NULL; 7140Sstevel@tonic-gate iop->_ptr = NULL; 7150Sstevel@tonic-gate iop->_cnt = 0; 7160Sstevel@tonic-gate iop->_flag = 0; /* marks it as available */ 7170Sstevel@tonic-gate FUNLOCKFILE(lk); 7180Sstevel@tonic-gate 7191672Sraf if (__libc_threaded) 720*5891Sraf cancel_safe_mutex_lock(&_first_link_lock); 7210Sstevel@tonic-gate fcloses++; 7221672Sraf if (__libc_threaded) 723*5891Sraf cancel_safe_mutex_unlock(&_first_link_lock); 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate return (res); 7260Sstevel@tonic-gate } 7270Sstevel@tonic-gate 7280Sstevel@tonic-gate /* flush buffer, close fd but keep the stream used by freopen() */ 7290Sstevel@tonic-gate int 7300Sstevel@tonic-gate close_fd(FILE *iop) 7310Sstevel@tonic-gate { 7320Sstevel@tonic-gate int res = 0; 7330Sstevel@tonic-gate mbstate_t *mb; 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate if (iop == NULL || iop->_flag == 0) 7360Sstevel@tonic-gate return (EOF); 7370Sstevel@tonic-gate /* Is not unbuffered and opened for read and/or write ? */ 7380Sstevel@tonic-gate if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW))) 7390Sstevel@tonic-gate res = _fflush_u(iop); 7401846Scraigm if (close(GET_FD(iop)) < 0) 7410Sstevel@tonic-gate res = EOF; 7420Sstevel@tonic-gate if (iop->_flag & _IOMYBUF) { 7430Sstevel@tonic-gate (void) free((char *)iop->_base - PUSHBACK); 7440Sstevel@tonic-gate } 7450Sstevel@tonic-gate iop->_base = NULL; 7460Sstevel@tonic-gate iop->_ptr = NULL; 7470Sstevel@tonic-gate mb = _getmbstate(iop); 7480Sstevel@tonic-gate if (mb != NULL) 7490Sstevel@tonic-gate (void) memset(mb, 0, sizeof (mbstate_t)); 7500Sstevel@tonic-gate iop->_cnt = 0; 7510Sstevel@tonic-gate _setorientation(iop, _NO_MODE); 7520Sstevel@tonic-gate return (res); 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate static FILE * 7560Sstevel@tonic-gate getiop(FILE *fp, rmutex_t *lk, mbstate_t *mb) 7570Sstevel@tonic-gate { 758*5891Sraf if (lk != NULL && cancel_safe_mutex_trylock(lk) != 0) 7590Sstevel@tonic-gate return (NULL); /* locked: fp in use */ 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate if (fp->_flag == 0) { /* unused */ 7620Sstevel@tonic-gate #ifndef _LP64 7630Sstevel@tonic-gate fp->__orientation = 0; 7640Sstevel@tonic-gate #endif /* _LP64 */ 7650Sstevel@tonic-gate fp->_cnt = 0; 7660Sstevel@tonic-gate fp->_ptr = NULL; 7670Sstevel@tonic-gate fp->_base = NULL; 7680Sstevel@tonic-gate fp->_flag = 0377; /* claim the fp by setting low 8 bits */ 7690Sstevel@tonic-gate (void) memset(mb, 0, sizeof (mbstate_t)); 7700Sstevel@tonic-gate FUNLOCKFILE(lk); 7710Sstevel@tonic-gate return (fp); 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate FUNLOCKFILE(lk); 7740Sstevel@tonic-gate return (NULL); 7750Sstevel@tonic-gate } 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate #ifndef _LP64 7780Sstevel@tonic-gate /* 7790Sstevel@tonic-gate * DESCRIPTION: 7800Sstevel@tonic-gate * This function gets the pointer to the mbstate_t structure associated 7810Sstevel@tonic-gate * with the specified iop. 7820Sstevel@tonic-gate * 7830Sstevel@tonic-gate * RETURNS: 7840Sstevel@tonic-gate * If the associated mbstate_t found, the pointer to the mbstate_t is 7850Sstevel@tonic-gate * returned. Otherwise, NULL is returned. 7860Sstevel@tonic-gate */ 7870Sstevel@tonic-gate mbstate_t * 7880Sstevel@tonic-gate _getmbstate(FILE *iop) 7890Sstevel@tonic-gate { 7900Sstevel@tonic-gate struct xFILEdata *dat = getxfdat(iop); 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate if (dat != NULL) 7930Sstevel@tonic-gate return (&dat->_state); 7940Sstevel@tonic-gate 7950Sstevel@tonic-gate return (NULL); 7960Sstevel@tonic-gate } 7971846Scraigm 7981846Scraigm /* 7991846Scraigm * More 32-bit only functions. 8001846Scraigm * They lookup/set large fd's for extended FILE support. 8011846Scraigm */ 8021846Scraigm 8031846Scraigm /* 8041846Scraigm * The negative value indicates that Extended fd FILE's has not 8051846Scraigm * been enabled by the user. 8061846Scraigm */ 8071846Scraigm static int bad_fd = -1; 8081846Scraigm 8091846Scraigm int 8101846Scraigm _file_get(FILE *iop) 8111846Scraigm { 8121846Scraigm int altfd; 8131846Scraigm 8141846Scraigm /* 8151846Scraigm * Failure indicates a FILE * not allocated through stdio; 8161846Scraigm * it means the flag values are probably bogus and that if 8171846Scraigm * a file descriptor is set, it's in _magic. 8181846Scraigm * Inline getxfdat() for performance reasons. 8191846Scraigm */ 8201846Scraigm if (STDIOP(iop)) 8211846Scraigm altfd = _xftab[IOPIND(iop)]._altfd; 8221846Scraigm else if (VALIDXFILE(FILEx(iop))) 8231846Scraigm altfd = FILEx(iop)->_xdat._altfd; 8241846Scraigm else 8251846Scraigm return (iop->_magic); 8261846Scraigm /* 8271846Scraigm * if this is not an internal extended FILE then check 8281846Scraigm * if _file is being changed from underneath us. 8291846Scraigm * It should not be because if 8301846Scraigm * it is then then we lose our ability to guard against 8311846Scraigm * silent data corruption. 8321846Scraigm */ 8331846Scraigm if (!iop->__xf_nocheck && bad_fd > -1 && iop->_magic != bad_fd) { 8343468Scraigm (void) fprintf(stderr, 8351846Scraigm "Application violated extended FILE safety mechanism.\n" 8363468Scraigm "Please read the man page for extendedFILE.\nAborting\n"); 8371846Scraigm abort(); 8381846Scraigm } 8391846Scraigm return (altfd); 8401846Scraigm } 8411846Scraigm 8421846Scraigm int 8431846Scraigm _file_set(FILE *iop, int fd, const char *type) 8441846Scraigm { 8451846Scraigm struct xFILEdata *dat; 8461846Scraigm int Fflag; 8471846Scraigm 8481846Scraigm /* Already known to contain at least one byte */ 8491846Scraigm while (*++type != '\0') 8501846Scraigm ; 8511846Scraigm 8521846Scraigm Fflag = type[-1] == 'F'; 8531846Scraigm if (!Fflag && bad_fd < 0) { 8541846Scraigm errno = EMFILE; 8551846Scraigm return (-1); 8561846Scraigm } 8571846Scraigm 8581846Scraigm dat = getxfdat(iop); 8591846Scraigm iop->__extendedfd = 1; 8601846Scraigm iop->__xf_nocheck = Fflag; 8611846Scraigm dat->_altfd = fd; 8621846Scraigm iop->_magic = (unsigned char)bad_fd; 8631846Scraigm return (0); 8641846Scraigm } 8651846Scraigm 8661846Scraigm /* 8671846Scraigm * Activates extended fd's in FILE's 8681846Scraigm */ 8691846Scraigm 8701846Scraigm static const int tries[] = {196, 120, 60, 3}; 8711846Scraigm #define NTRIES (sizeof (tries)/sizeof (int)) 8721846Scraigm 8731846Scraigm int 8741846Scraigm enable_extended_FILE_stdio(int fd, int action) 8751846Scraigm { 8761846Scraigm int i; 8771846Scraigm 8781846Scraigm if (action < 0) 8791846Scraigm action = SIGABRT; /* default signal */ 8801846Scraigm 8811846Scraigm if (fd < 0) { 8821846Scraigm /* 8831846Scraigm * search for an available fd and make it the badfd 8841846Scraigm */ 8851846Scraigm for (i = 0; i < NTRIES; i++) { 8861846Scraigm fd = fcntl(tries[i], F_BADFD, action); 8871846Scraigm if (fd >= 0) 8881846Scraigm break; 8891846Scraigm } 8901846Scraigm if (fd < 0) /* failed to find an available fd */ 8911846Scraigm return (-1); 8921846Scraigm } else { 8931846Scraigm /* caller requests that fd be the chosen badfd */ 8941846Scraigm int nfd = fcntl(fd, F_BADFD, action); 8951846Scraigm if (nfd < 0 || nfd != fd) 8961846Scraigm return (-1); 8971846Scraigm } 8981846Scraigm bad_fd = fd; 8991846Scraigm return (0); 9001846Scraigm } 9010Sstevel@tonic-gate #endif 902