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