17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
51d2738araf * Common Development and Distribution License (the "License").
61d2738araf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
211d2738araf
227c478bdstevel@tonic-gate/*
2323a1cceRoger A. Faulkner * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate/*	Copyright (c) 1988 AT&T	*/
277c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
287c478bdstevel@tonic-gate
297257d1braf#include "lint.h"
307c478bdstevel@tonic-gate#include "mtlib.h"
317c478bdstevel@tonic-gate#include "file64.h"
327c478bdstevel@tonic-gate#include <sys/types.h>
337c478bdstevel@tonic-gate#include <stdlib.h>
347c478bdstevel@tonic-gate#include <stdio.h>
357c478bdstevel@tonic-gate#include <thread.h>
367c478bdstevel@tonic-gate#include <synch.h>
377c478bdstevel@tonic-gate#include <unistd.h>
387c478bdstevel@tonic-gate#include <string.h>
397c478bdstevel@tonic-gate#include "stdiom.h"
407c478bdstevel@tonic-gate#include <wchar.h>
417c478bdstevel@tonic-gate#include <sys/stat.h>
427c478bdstevel@tonic-gate#include <stddef.h>
437c478bdstevel@tonic-gate#include <errno.h>
44a5f6978craigm#include <fcntl.h>
457c478bdstevel@tonic-gate
467257d1braf#define	_iob	__iob
477257d1braf
487c478bdstevel@tonic-gate#undef end
497c478bdstevel@tonic-gate
507c478bdstevel@tonic-gate#define	FILE_ARY_SZ	8 /* a nice size for FILE array & end_buffer_ptrs */
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gate#ifdef	_LP64
537c478bdstevel@tonic-gate
547c478bdstevel@tonic-gate/*
557c478bdstevel@tonic-gate * Macros to declare and loop over a fp or fp/xfp combo to
567c478bdstevel@tonic-gate * avoid some of the _LP64 ifdef hell.
577c478bdstevel@tonic-gate */
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gate#define	FPDECL(fp)		FILE *fp
607c478bdstevel@tonic-gate#define	FIRSTFP(lp, fp)		fp = lp->iobp
617c478bdstevel@tonic-gate#define	NEXTFP(fp)		fp++
62a5f6978craigm#define	FPLOCK(fp)		&fp->_lock
63a5f6978craigm#define	FPSTATE(fp)		&fp->_state
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate#define	xFILE			FILE
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gate#else
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate#define	FPDECL(fp)		FILE *fp; xFILE *x##fp
707c478bdstevel@tonic-gate#define	FIRSTFP(lp, fp)		x##fp = lp->iobp; \
717c478bdstevel@tonic-gate				fp = x##fp ? &x##fp->_iob : &_iob[0]
727c478bdstevel@tonic-gate#define	NEXTFP(fp)		(x##fp ? fp = &(++x##fp)->_iob : ++fp)
73a5f6978craigm#define	FPLOCK(fp)		x##fp ? \
74a5f6978craigm				    &x##fp->xlock : &_xftab[IOPIND(fp)]._lock
75a5f6978craigm#define	FPSTATE(fp)		x##fp ? \
76a5f6978craigm				    &x##fp->xstate : &_xftab[IOPIND(fp)]._state
777c478bdstevel@tonic-gate
787c478bdstevel@tonic-gate/* The extended 32-bit file structure for use in link buffers */
797c478bdstevel@tonic-gatetypedef struct xFILE {
807c478bdstevel@tonic-gate	FILE			_iob;		/* must be first! */
817c478bdstevel@tonic-gate	struct xFILEdata	_xdat;
827c478bdstevel@tonic-gate} xFILE;
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gate#define	xmagic			_xdat._magic
857c478bdstevel@tonic-gate#define	xend			_xdat._end
867c478bdstevel@tonic-gate#define	xlock			_xdat._lock
877c478bdstevel@tonic-gate#define	xstate			_xdat._state
887c478bdstevel@tonic-gate
897c478bdstevel@tonic-gate#define	FILEx(fp)		((struct xFILE *)(uintptr_t)fp)
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gate/*
927c478bdstevel@tonic-gate * The magic number stored is actually the pointer scrambled with
937c478bdstevel@tonic-gate * a magic number.  Pointers to data items live everywhere in memory
947c478bdstevel@tonic-gate * so we scramble the pointer in order to avoid accidental collisions.
957c478bdstevel@tonic-gate */
967c478bdstevel@tonic-gate#define	XFILEMAGIC		0x63687367
977c478bdstevel@tonic-gate#define	XMAGIC(xfp)		((uintptr_t)(xfp) ^ XFILEMAGIC)
987c478bdstevel@tonic-gate
997c478bdstevel@tonic-gate#endif /* _LP64 */
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gatestruct _link_	/* manages a list of streams */
1027c478bdstevel@tonic-gate{
1037c478bdstevel@tonic-gate	xFILE *iobp;		/* the array of (x)FILE's */
1047c478bdstevel@tonic-gate				/* NULL for the __first_link in ILP32 */
1057c478bdstevel@tonic-gate	int	niob;		/* length of the arrays */
1067c478bdstevel@tonic-gate	struct _link_	*next;	/* next in the list */
1077c478bdstevel@tonic-gate};
1087c478bdstevel@tonic-gate
1097c478bdstevel@tonic-gate/*
1107c478bdstevel@tonic-gate * With dynamic linking, iob may be in either the library or in the user's
1117c478bdstevel@tonic-gate * a.out, so the run time linker fixes up the first entry in __first_link at
1127c478bdstevel@tonic-gate * process startup time.
1137c478bdstevel@tonic-gate *
1147c478bdstevel@tonic-gate * In 32 bit processes, we don't have xFILE[FILE_ARY_SZ] but FILE[],
1157c478bdstevel@tonic-gate * and _xftab[] instead; this is denoted by having iobp set to NULL in
1167c478bdstevel@tonic-gate * 32 bit mode for the first link entry.
1177c478bdstevel@tonic-gate */
1187c478bdstevel@tonic-gatestruct _link_ __first_link =	/* first in linked list */
1197c478bdstevel@tonic-gate{
1207c478bdstevel@tonic-gate#if !defined(_LP64)
1217c478bdstevel@tonic-gate	NULL,
1227c478bdstevel@tonic-gate#else
1237c478bdstevel@tonic-gate	&_iob[0],
1247c478bdstevel@tonic-gate#endif
1257c478bdstevel@tonic-gate	_NFILE,
1267c478bdstevel@tonic-gate	NULL
1277c478bdstevel@tonic-gate};
1287c478bdstevel@tonic-gate
1297c478bdstevel@tonic-gate/*
1307c478bdstevel@tonic-gate * Information cached to speed up searches.  We remember where we
1317c478bdstevel@tonic-gate * last found a free FILE* and we remember whether we saw any fcloses
1327c478bdstevel@tonic-gate * in between.  We also count the number of chunks we allocated, see
1337c478bdstevel@tonic-gate * _findiop() for an explanation.
1347c478bdstevel@tonic-gate * These variables are all protected by _first_link_lock.
1357c478bdstevel@tonic-gate */
1367c478bdstevel@tonic-gatestatic struct _link_ *lastlink = NULL;
1377c478bdstevel@tonic-gatestatic int fcloses;
1387c478bdstevel@tonic-gatestatic int nchunks;
1397c478bdstevel@tonic-gate
140a5f6978craigmstatic mutex_t _first_link_lock = DEFAULTMUTEX;
1417c478bdstevel@tonic-gate
142a5f6978craigmstatic int _fflush_l_iops(void);
1437c478bdstevel@tonic-gatestatic FILE *getiop(FILE *, rmutex_t *, mbstate_t *);
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate/*
1467c478bdstevel@tonic-gate * All functions that understand the linked list of iob's follow.
1477c478bdstevel@tonic-gate */
1487c478bdstevel@tonic-gate#pragma weak _cleanup = __cleanup
1497c478bdstevel@tonic-gatevoid
1507c478bdstevel@tonic-gate__cleanup(void)		/* called at process end to flush ouput streams */
1517c478bdstevel@tonic-gate{
1527c478bdstevel@tonic-gate	(void) fflush(NULL);
1537c478bdstevel@tonic-gate}
1547c478bdstevel@tonic-gate
1557c478bdstevel@tonic-gate/*
1567c478bdstevel@tonic-gate * For fork1-safety (see libc_prepare_atfork(), etc).
1577c478bdstevel@tonic-gate */
1587c478bdstevel@tonic-gatevoid
1597c478bdstevel@tonic-gatestdio_locks()
1607c478bdstevel@tonic-gate{
1618cd4554raf	(void) mutex_lock(&_first_link_lock);
1627c478bdstevel@tonic-gate	/*
1637c478bdstevel@tonic-gate	 * XXX: We should acquire all of the iob locks here.
1647c478bdstevel@tonic-gate	 */
1657c478bdstevel@tonic-gate}
1667c478bdstevel@tonic-gate
1677c478bdstevel@tonic-gatevoid
1687c478bdstevel@tonic-gatestdio_unlocks()
1697c478bdstevel@tonic-gate{
1707c478bdstevel@tonic-gate	/*
1717c478bdstevel@tonic-gate	 * XXX: We should release all of the iob locks here.
1727c478bdstevel@tonic-gate	 */
1738cd4554raf	(void) mutex_unlock(&_first_link_lock);
1747c478bdstevel@tonic-gate}
1757c478bdstevel@tonic-gate
1767c478bdstevel@tonic-gatevoid
1777c478bdstevel@tonic-gate_flushlbf(void)		/* fflush() all line-buffered streams */
1787c478bdstevel@tonic-gate{
1797c478bdstevel@tonic-gate	FPDECL(fp);
1807c478bdstevel@tonic-gate	int i;
1817c478bdstevel@tonic-gate	struct _link_ *lp;
182a5f6978craigm	/* Allow compiler to optimize the loop */
183a5f6978craigm	int threaded = __libc_threaded;
1847c478bdstevel@tonic-gate
185a5f6978craigm	if (threaded)
186a574db8raf		cancel_safe_mutex_lock(&_first_link_lock);
1877c478bdstevel@tonic-gate
1887c478bdstevel@tonic-gate	lp = &__first_link;
1897c478bdstevel@tonic-gate	do {
1907c478bdstevel@tonic-gate		FIRSTFP(lp, fp);
1917c478bdstevel@tonic-gate		for (i = lp->niob; --i >= 0; NEXTFP(fp)) {
192a5f6978craigm			/*
193a5f6978craigm			 * The additional _IONBF check guards againsts
194a5f6978craigm			 * allocated but uninitialized iops (see _findiop).
195a5f6978craigm			 * We also automatically skip non allocated iop's.
196a5f6978craigm			 * Don't block on locks.
197a5f6978craigm			 */
198a5f6978craigm			if ((fp->_flag & (_IOLBF | _IOWRT | _IONBF)) ==
199a5f6978craigm			    (_IOLBF | _IOWRT)) {
200a5f6978craigm				if (threaded) {
201a5f6978craigm					rmutex_t *lk = FPLOCK(fp);
202a574db8raf					if (cancel_safe_mutex_trylock(lk) != 0)
203a5f6978craigm						continue;
204a5f6978craigm					/* Recheck after locking */
205a5f6978craigm					if ((fp->_flag & (_IOLBF | _IOWRT)) ==
206a5f6978craigm					    (_IOLBF | _IOWRT)) {
207a5f6978craigm						(void) _fflush_u(fp);
208a5f6978craigm					}
209a574db8raf					cancel_safe_mutex_unlock(lk);
210a5f6978craigm				} else {
211a5f6978craigm					(void) _fflush_u(fp);
212a5f6978craigm				}
213a5f6978craigm			}
2147c478bdstevel@tonic-gate		}
2157c478bdstevel@tonic-gate	} while ((lp = lp->next) != NULL);
2167c478bdstevel@tonic-gate
217a5f6978craigm	if (threaded)
218a574db8raf		cancel_safe_mutex_unlock(&_first_link_lock);
2197c478bdstevel@tonic-gate}
2207c478bdstevel@tonic-gate
2217c478bdstevel@tonic-gate/* allocate an unused stream; NULL if cannot */
2227c478bdstevel@tonic-gateFILE *
2237c478bdstevel@tonic-gate_findiop(void)
2247c478bdstevel@tonic-gate{
2257c478bdstevel@tonic-gate	struct _link_ *lp, **prev;
2267c478bdstevel@tonic-gate
2277c478bdstevel@tonic-gate	/* used so there only needs to be one malloc() */
2287c478bdstevel@tonic-gate#ifdef _LP64
2297c478bdstevel@tonic-gate	typedef	struct	{
2307c478bdstevel@tonic-gate		struct _link_	hdr;
2317c478bdstevel@tonic-gate		FILE	iob[FILE_ARY_SZ];
2327c478bdstevel@tonic-gate	} Pkg;
2337c478bdstevel@tonic-gate#else
2347c478bdstevel@tonic-gate	typedef union {
2357c478bdstevel@tonic-gate		struct {				/* Normal */
2367c478bdstevel@tonic-gate			struct _link_	hdr;
2377c478bdstevel@tonic-gate			xFILE	iob[FILE_ARY_SZ];
2387c478bdstevel@tonic-gate		} Pkgn;
2397c478bdstevel@tonic-gate		struct {				/* Reversed */
2407c478bdstevel@tonic-gate			xFILE	iob[FILE_ARY_SZ];
2417c478bdstevel@tonic-gate			struct _link_	hdr;
2427c478bdstevel@tonic-gate		} Pkgr;
2437c478bdstevel@tonic-gate	} Pkg;
2447c478bdstevel@tonic-gate	uintptr_t delta;
2457c478bdstevel@tonic-gate#endif
2467c478bdstevel@tonic-gate	Pkg *pkgp;
2477c478bdstevel@tonic-gate	struct _link_ *hdr;
2487c478bdstevel@tonic-gate	FPDECL(fp);
2497c478bdstevel@tonic-gate	int i;
250a5f6978craigm	int threaded = __libc_threaded;
2517c478bdstevel@tonic-gate
252a5f6978craigm	if (threaded)
253a574db8raf		cancel_safe_mutex_lock(&_first_link_lock);
2547c478bdstevel@tonic-gate
2557c478bdstevel@tonic-gate	if (lastlink == NULL) {
2567c478bdstevel@tonic-gaterescan:
2577c478bdstevel@tonic-gate		fcloses = 0;
2587c478bdstevel@tonic-gate		lastlink = &__first_link;
2597c478bdstevel@tonic-gate	}
2607c478bdstevel@tonic-gate
2617c478bdstevel@tonic-gate	lp = lastlink;
2627c478bdstevel@tonic-gate
2637c478bdstevel@tonic-gate	/*
2647c478bdstevel@tonic-gate	 * lock to make testing of fp->_flag == 0 and acquiring the fp atomic
2657c478bdstevel@tonic-gate	 * and for allocation of new links
2667c478bdstevel@tonic-gate	 * low contention expected on _findiop(), hence coarse locking.
2677c478bdstevel@tonic-gate	 * for finer granularity, use fp->_lock for allocating an iop
2687c478bdstevel@tonic-gate	 * and make the testing of lp->next and allocation of new link atomic
2697c478bdstevel@tonic-gate	 * using lp->_lock
2707c478bdstevel@tonic-gate	 */
2717c478bdstevel@tonic-gate
2727c478bdstevel@tonic-gate	do {
2737c478bdstevel@tonic-gate		prev = &lp->next;
2747c478bdstevel@tonic-gate		FIRSTFP(lp, fp);
2757c478bdstevel@tonic-gate
2767c478bdstevel@tonic-gate		for (i = lp->niob; --i >= 0; NEXTFP(fp)) {
277a5f6978craigm			FILE *ret;
278a5f6978craigm			if (threaded) {
279a5f6978craigm				ret = getiop(fp, FPLOCK(fp), FPSTATE(fp));
280a5f6978craigm				if (ret != NULL) {
281a574db8raf					cancel_safe_mutex_unlock(
282b2e86e7casper					    &_first_link_lock);
283b2e86e7casper					return (ret);
284a5f6978craigm				}
285a5f6978craigm			} else {
286a5f6978craigm				ret = getiop(fp, NULL, FPSTATE(fp));
287a5f6978craigm				if (ret != NULL)
288a5f6978craigm					return (ret);
289a5f6978craigm			}
2907c478bdstevel@tonic-gate		}
2917c478bdstevel@tonic-gate	} while ((lastlink = lp = lp->next) != NULL);
2927c478bdstevel@tonic-gate
2937c478bdstevel@tonic-gate	/*
2947c478bdstevel@tonic-gate	 * If there was a sufficient number of  fcloses since we last started
2957c478bdstevel@tonic-gate	 * at __first_link, we rescan all fp's again.  We do not rescan for
2967c478bdstevel@tonic-gate	 * all fcloses; that would simplify the algorithm but would make
2977c478bdstevel@tonic-gate	 * search times near O(n) again.
2987c478bdstevel@tonic-gate	 * Worst case behaviour would still be pretty bad (open a full set,
2997c478bdstevel@tonic-gate	 * then continously opening and closing one FILE * gets you a full
3007c478bdstevel@tonic-gate	 * scan each time).  That's why we over allocate 1 FILE for each
3017c478bdstevel@tonic-gate	 * 32 chunks.  More over allocation is better; this is a nice
3027c478bdstevel@tonic-gate	 * empirical value which doesn't cost a lot of memory, doesn't
3037c478bdstevel@tonic-gate	 * overallocate until we reach 256 FILE *s and keeps the performance
3047c478bdstevel@tonic-gate	 * pretty close to the optimum.
3057c478bdstevel@tonic-gate	 */
3067c478bdstevel@tonic-gate	if (fcloses > nchunks/32)
3077c478bdstevel@tonic-gate		goto rescan;
3087c478bdstevel@tonic-gate
3097c478bdstevel@tonic-gate	/*
3107c478bdstevel@tonic-gate	 * Need to allocate another and put it in the linked list.
3117c478bdstevel@tonic-gate	 */
3127c478bdstevel@tonic-gate	if ((pkgp = malloc(sizeof (Pkg))) == NULL) {
313a5f6978craigm		if (threaded)
314a574db8raf			cancel_safe_mutex_unlock(&_first_link_lock);
3157c478bdstevel@tonic-gate		return (NULL);
3167c478bdstevel@tonic-gate	}
3177c478bdstevel@tonic-gate
3187c478bdstevel@tonic-gate	(void) memset(pkgp, 0, sizeof (Pkg));
3197c478bdstevel@tonic-gate
3207c478bdstevel@tonic-gate#ifdef _LP64
3217c478bdstevel@tonic-gate	hdr = &pkgp->hdr;
3227c478bdstevel@tonic-gate	hdr->iobp = &pkgp->iob[0];
3237c478bdstevel@tonic-gate#else
3247c478bdstevel@tonic-gate	/*
3257c478bdstevel@tonic-gate	 * The problem with referencing a word after a FILE* is the possibility
3267c478bdstevel@tonic-gate	 * of a SIGSEGV if a non-stdio issue FILE structure ends on a page
3277c478bdstevel@tonic-gate	 * boundary.  We run this check so we never need to run an expensive
3287c478bdstevel@tonic-gate	 * check like mincore() in order to know whether it is
3297c478bdstevel@tonic-gate	 * safe to dereference ((xFILE*)fp)->xmagic.
3307c478bdstevel@tonic-gate	 * We allocate the block with two alternative layouts; if one
3317c478bdstevel@tonic-gate	 * layout is not properly aligned for our purposes, the other layout
3327c478bdstevel@tonic-gate	 * will be because the size of _link_ is small compared to
3337c478bdstevel@tonic-gate	 * sizeof (xFILE).
3347c478bdstevel@tonic-gate	 * The check performed is this:
3357c478bdstevel@tonic-gate	 *	If the distance from pkgp to the end of the page is
3367c478bdstevel@tonic-gate	 *	less than the the offset of the last xmagic field in the
3377c478bdstevel@tonic-gate	 *	xFILE structure, (the 0x1000 boundary is inside our just
3387c478bdstevel@tonic-gate	 *	allocated structure) and the distance modulo the size of xFILE
3397c478bdstevel@tonic-gate	 *	is identical to the offset of the first xmagic in the
3407c478bdstevel@tonic-gate	 *	structure (i.e., XXXXXX000 points to an xmagic field),
3417c478bdstevel@tonic-gate	 *	we need to use the reverse structure.
3427c478bdstevel@tonic-gate	 */
3437c478bdstevel@tonic-gate	if ((delta = 0x1000 - ((uintptr_t)pkgp & 0xfff)) <=
344a574db8raf	    offsetof(Pkg, Pkgn.iob[FILE_ARY_SZ-1].xmagic) &&
3457c478bdstevel@tonic-gate	    delta % sizeof (struct xFILE) ==
346a574db8raf	    offsetof(Pkg, Pkgn.iob[0].xmagic)) {
3477c478bdstevel@tonic-gate		/* Use reversed structure */
3487c478bdstevel@tonic-gate		hdr = &pkgp->Pkgr.hdr;
3497c478bdstevel@tonic-gate		hdr->iobp = &pkgp->Pkgr.iob[0];
3507c478bdstevel@tonic-gate	} else {
3517c478bdstevel@tonic-gate		/* Use normal structure */
3527c478bdstevel@tonic-gate		hdr = &pkgp->Pkgn.hdr;
3537c478bdstevel@tonic-gate		hdr->iobp = &pkgp->Pkgn.iob[0];
3547c478bdstevel@tonic-gate	}
3557c478bdstevel@tonic-gate#endif /* _LP64 */
3567c478bdstevel@tonic-gate
3577c478bdstevel@tonic-gate	hdr->niob = FILE_ARY_SZ;
3587c478bdstevel@tonic-gate	nchunks++;
3597c478bdstevel@tonic-gate
3607c478bdstevel@tonic-gate#ifdef	_LP64
3617c478bdstevel@tonic-gate	fp = hdr->iobp;
3627c478bdstevel@tonic-gate	for (i = 0; i < FILE_ARY_SZ; i++)
3637257d1braf		(void) mutex_init(&fp[i]._lock,
3647257d1braf		    USYNC_THREAD | LOCK_RECURSIVE, NULL);
3657c478bdstevel@tonic-gate#else
3667c478bdstevel@tonic-gate	xfp = hdr->iobp;
3677c478bdstevel@tonic-gate	fp = &xfp->_iob;
3687c478bdstevel@tonic-gate
3697c478bdstevel@tonic-gate	for (i = 0; i < FILE_ARY_SZ; i++) {
3707c478bdstevel@tonic-gate		xfp[i].xmagic = XMAGIC(&xfp[i]);
3717257d1braf		(void) mutex_init(&xfp[i].xlock,
3727257d1braf		    USYNC_THREAD | LOCK_RECURSIVE, NULL);
3737c478bdstevel@tonic-gate	}
3747c478bdstevel@tonic-gate#endif	/*	_LP64	*/
3757c478bdstevel@tonic-gate
3767c478bdstevel@tonic-gate	lastlink = *prev = hdr;
3777c478bdstevel@tonic-gate	fp->_ptr = 0;
3787c478bdstevel@tonic-gate	fp->_base = 0;
3797c478bdstevel@tonic-gate	fp->_flag = 0377; /* claim the fp by setting low 8 bits */
380a5f6978craigm	if (threaded)
381a574db8raf		cancel_safe_mutex_unlock(&_first_link_lock);
3827c478bdstevel@tonic-gate
3837c478bdstevel@tonic-gate	return (fp);
3847c478bdstevel@tonic-gate}
3857c478bdstevel@tonic-gate
3867c478bdstevel@tonic-gatestatic void
3877c478bdstevel@tonic-gateisseekable(FILE *iop)
3887c478bdstevel@tonic-gate{
3897c478bdstevel@tonic-gate	struct stat64 fstatbuf;
3907c478bdstevel@tonic-gate	int save_errno;
3917c478bdstevel@tonic-gate
3927c478bdstevel@tonic-gate	save_errno = errno;
3937c478bdstevel@tonic-gate
394a5f6978craigm	if (fstat64(GET_FD(iop), &fstatbuf) != 0) {
3957c478bdstevel@tonic-gate		/*
3967c478bdstevel@tonic-gate		 * when we don't know what it is we'll
3977c478bdstevel@tonic-gate		 * do the old behaviour and flush
3987c478bdstevel@tonic-gate		 * the stream
3997c478bdstevel@tonic-gate		 */
4007c478bdstevel@tonic-gate		SET_SEEKABLE(iop);
4017c478bdstevel@tonic-gate		errno = save_errno;
4027c478bdstevel@tonic-gate		return;
4037c478bdstevel@tonic-gate	}
4047c478bdstevel@tonic-gate
4057c478bdstevel@tonic-gate	/*
4067c478bdstevel@tonic-gate	 * check for what is non-SEEKABLE
4077c478bdstevel@tonic-gate	 * otherwise assume it's SEEKABLE so we get the old
4087c478bdstevel@tonic-gate	 * behaviour and flush the stream
4097c478bdstevel@tonic-gate	 */
4107c478bdstevel@tonic-gate
4117c478bdstevel@tonic-gate	if (S_ISFIFO(fstatbuf.st_mode) || S_ISCHR(fstatbuf.st_mode) ||
4127c478bdstevel@tonic-gate	    S_ISSOCK(fstatbuf.st_mode) || S_ISDOOR(fstatbuf.st_mode)) {
4137c478bdstevel@tonic-gate		CLEAR_SEEKABLE(iop);
4147c478bdstevel@tonic-gate	} else {
4157c478bdstevel@tonic-gate		SET_SEEKABLE(iop);
4167c478bdstevel@tonic-gate	}
4177c478bdstevel@tonic-gate
4187c478bdstevel@tonic-gate	errno = save_errno;
4197c478bdstevel@tonic-gate}
4207c478bdstevel@tonic-gate
4217c478bdstevel@tonic-gate#ifdef	_LP64
4227c478bdstevel@tonic-gatevoid
4237c478bdstevel@tonic-gate_setbufend(FILE *iop, Uchar *end)	/* set the end pointer for this iop */
4247c478bdstevel@tonic-gate{
4257c478bdstevel@tonic-gate	iop->_end = end;
4267c478bdstevel@tonic-gate
4277c478bdstevel@tonic-gate	isseekable(iop);
4287c478bdstevel@tonic-gate}
4297c478bdstevel@tonic-gate
4307c478bdstevel@tonic-gate#undef _realbufend
4317c478bdstevel@tonic-gate
4327c478bdstevel@tonic-gateUchar *
4337c478bdstevel@tonic-gate_realbufend(FILE *iop)		/* get the end pointer for this iop */
4347c478bdstevel@tonic-gate{
4357c478bdstevel@tonic-gate	return (iop->_end);
4367c478bdstevel@tonic-gate}
4377c478bdstevel@tonic-gate
4387c478bdstevel@tonic-gate#else /* _LP64 */
4397c478bdstevel@tonic-gate
4407c478bdstevel@tonic-gate/*
4417c478bdstevel@tonic-gate * Awkward functions not needed for the sane 64 bit environment.
4427c478bdstevel@tonic-gate */
4437c478bdstevel@tonic-gate/*
4447c478bdstevel@tonic-gate * xmagic must not be aligned on a 4K boundary. We guarantee this in
4457c478bdstevel@tonic-gate * _findiop().
4467c478bdstevel@tonic-gate */
4477c478bdstevel@tonic-gate#define	VALIDXFILE(xfp) \
4487c478bdstevel@tonic-gate	(((uintptr_t)&(xfp)->xmagic & 0xfff) && \
4497c478bdstevel@tonic-gate	    (xfp)->xmagic == XMAGIC(FILEx(xfp)))
4507c478bdstevel@tonic-gate
4517c478bdstevel@tonic-gatestatic struct xFILEdata *
4527c478bdstevel@tonic-gategetxfdat(FILE *iop)
4537c478bdstevel@tonic-gate{
4547c478bdstevel@tonic-gate	if (STDIOP(iop))
4557c478bdstevel@tonic-gate		return (&_xftab[IOPIND(iop)]);
4567c478bdstevel@tonic-gate	else if (VALIDXFILE(FILEx(iop)))
4577c478bdstevel@tonic-gate		return (&FILEx(iop)->_xdat);
4587c478bdstevel@tonic-gate	else
4597c478bdstevel@tonic-gate		return (NULL);
4607c478bdstevel@tonic-gate}
4617c478bdstevel@tonic-gate
4627c478bdstevel@tonic-gatevoid
4637c478bdstevel@tonic-gate_setbufend(FILE *iop, Uchar *end)	/* set the end pointer for this iop */
4647c478bdstevel@tonic-gate{
4657c478bdstevel@tonic-gate	struct xFILEdata *dat = getxfdat(iop);
4667c478bdstevel@tonic-gate
4677c478bdstevel@tonic-gate	if (dat != NULL)
4687c478bdstevel@tonic-gate		dat->_end = end;
4697c478bdstevel@tonic-gate
4707c478bdstevel@tonic-gate	isseekable(iop);
4717c478bdstevel@tonic-gate
4727c478bdstevel@tonic-gate	/*
4737c478bdstevel@tonic-gate	 * For binary compatibility with user programs using the
4747c478bdstevel@tonic-gate	 * old _bufend macro.  This is *so* broken, fileno()
4757c478bdstevel@tonic-gate	 * is not the proper index.
4767c478bdstevel@tonic-gate	 */
477a5f6978craigm	if (iop->_magic < _NFILE)
478a5f6978craigm		_bufendtab[iop->_magic] = end;
4797c478bdstevel@tonic-gate
4807c478bdstevel@tonic-gate}
4817c478bdstevel@tonic-gate
4827c478bdstevel@tonic-gateUchar *
4837c478bdstevel@tonic-gate_realbufend(FILE *iop)		/* get the end pointer for this iop */
4847c478bdstevel@tonic-gate{
4857c478bdstevel@tonic-gate	struct xFILEdata *dat = getxfdat(iop);
4867c478bdstevel@tonic-gate
4877c478bdstevel@tonic-gate	if (dat != NULL)
4887c478bdstevel@tonic-gate		return (dat->_end);
4897c478bdstevel@tonic-gate
4907c478bdstevel@tonic-gate	return (NULL);
4917c478bdstevel@tonic-gate}
4927c478bdstevel@tonic-gate
4937c478bdstevel@tonic-gate/*
4947c478bdstevel@tonic-gate * _reallock() is invoked in each stdio call through the IOB_LCK() macro,
4957c478bdstevel@tonic-gate * it is therefor extremely performance sensitive.  We get better performance
4967c478bdstevel@tonic-gate * by inlining the STDIOP check in IOB_LCK and inlining a custom version
4977c478bdstevel@tonic-gate * of getfxdat() here.
4987c478bdstevel@tonic-gate */
4997c478bdstevel@tonic-gatermutex_t *
5007c478bdstevel@tonic-gate_reallock(FILE *iop)
5017c478bdstevel@tonic-gate{
5027c478bdstevel@tonic-gate	if (VALIDXFILE(FILEx(iop)))
5037c478bdstevel@tonic-gate		return (&FILEx(iop)->xlock);
5047c478bdstevel@tonic-gate
5057c478bdstevel@tonic-gate	return (NULL);
5067c478bdstevel@tonic-gate}
5077c478bdstevel@tonic-gate
5087c478bdstevel@tonic-gate#endif	/*	_LP64	*/
5097c478bdstevel@tonic-gate
5107c478bdstevel@tonic-gate/* make sure _cnt, _ptr are correct */
5117c478bdstevel@tonic-gatevoid
5127c478bdstevel@tonic-gate_bufsync(FILE *iop, Uchar *bufend)
5137c478bdstevel@tonic-gate{
5147c478bdstevel@tonic-gate	ssize_t spaceleft;
5157c478bdstevel@tonic-gate
5167c478bdstevel@tonic-gate	spaceleft = bufend - iop->_ptr;
5177c478bdstevel@tonic-gate	if (bufend < iop->_ptr) {
5187c478bdstevel@tonic-gate		iop->_ptr = bufend;
5197c478bdstevel@tonic-gate		iop->_cnt = 0;
5207c478bdstevel@tonic-gate	} else if (spaceleft < iop->_cnt)
5217c478bdstevel@tonic-gate		iop->_cnt = spaceleft;
5227c478bdstevel@tonic-gate}
5237c478bdstevel@tonic-gate
5247c478bdstevel@tonic-gate/* really write out current buffer contents */
5257c478bdstevel@tonic-gateint
5267c478bdstevel@tonic-gate_xflsbuf(FILE *iop)
5277c478bdstevel@tonic-gate{
5287c478bdstevel@tonic-gate	ssize_t n;
5297c478bdstevel@tonic-gate	Uchar *base = iop->_base;
5307c478bdstevel@tonic-gate	Uchar *bufend;
5317c478bdstevel@tonic-gate	ssize_t num_wrote;
5327c478bdstevel@tonic-gate
5337c478bdstevel@tonic-gate	/*
5347c478bdstevel@tonic-gate	 * Hopefully, be stable with respect to interrupts...
5357c478bdstevel@tonic-gate	 */
5367c478bdstevel@tonic-gate	n = iop->_ptr - base;
5377c478bdstevel@tonic-gate	iop->_ptr = base;
5387c478bdstevel@tonic-gate	bufend = _bufend(iop);
5397c478bdstevel@tonic-gate	if (iop->_flag & (_IOLBF | _IONBF))
5407c478bdstevel@tonic-gate		iop->_cnt = 0;		/* always go to a flush */
5417c478bdstevel@tonic-gate	else
5427c478bdstevel@tonic-gate		iop->_cnt = bufend - base;
5437c478bdstevel@tonic-gate
5447c478bdstevel@tonic-gate	if (_needsync(iop, bufend))	/* recover from interrupts */
5457c478bdstevel@tonic-gate		_bufsync(iop, bufend);
5467c478bdstevel@tonic-gate
5477c478bdstevel@tonic-gate	if (n > 0) {
548a5f6978craigm		int fd = GET_FD(iop);
549a574db8raf		while ((num_wrote = write(fd, base, (size_t)n)) != n) {
5507c478bdstevel@tonic-gate			if (num_wrote <= 0) {
551a574db8raf				if (!cancel_active())
552a574db8raf					iop->_flag |= _IOERR;
5537c478bdstevel@tonic-gate				return (EOF);
5547c478bdstevel@tonic-gate			}
5557c478bdstevel@tonic-gate			n -= num_wrote;
5567c478bdstevel@tonic-gate			base += num_wrote;
5577c478bdstevel@tonic-gate		}
5587c478bdstevel@tonic-gate	}
5597c478bdstevel@tonic-gate	return (0);
5607c478bdstevel@tonic-gate}
5617c478bdstevel@tonic-gate
5627c478bdstevel@tonic-gate/* flush (write) buffer */
5637c478bdstevel@tonic-gateint
5647c478bdstevel@tonic-gatefflush(FILE *iop)
5657c478bdstevel@tonic-gate{
5667c478bdstevel@tonic-gate	int res;
5677c478bdstevel@tonic-gate	rmutex_t *lk;
5687c478bdstevel@tonic-gate
5697c478bdstevel@tonic-gate	if (iop) {
5707c478bdstevel@tonic-gate		FLOCKFILE(lk, iop);
5717c478bdstevel@tonic-gate		res = _fflush_u(iop);
5727c478bdstevel@tonic-gate		FUNLOCKFILE(lk);
5737c478bdstevel@tonic-gate	} else {
574a5f6978craigm		res = _fflush_l_iops();		/* flush all iops */
5757c478bdstevel@tonic-gate	}
5767c478bdstevel@tonic-gate	return (res);
5777c478bdstevel@tonic-gate}
5787c478bdstevel@tonic-gate
5797c478bdstevel@tonic-gatestatic int
580a5f6978craigm_fflush_l_iops(void)		/* flush all buffers */
5817c478bdstevel@tonic-gate{
5827c478bdstevel@tonic-gate	FPDECL(iop);
5837c478bdstevel@tonic-gate
5847c478bdstevel@tonic-gate	int i;
5857c478bdstevel@tonic-gate	struct _link_ *lp;
5867c478bdstevel@tonic-gate	int res = 0;
587a5f6978craigm	rmutex_t *lk;
588a5f6978craigm	/* Allow the compiler to optimize the load out of the loop */
589a5f6978craigm	int threaded = __libc_threaded;
5907c478bdstevel@tonic-gate
591a5f6978craigm	if (threaded)
592a574db8raf		cancel_safe_mutex_lock(&_first_link_lock);
5937c478bdstevel@tonic-gate
5947c478bdstevel@tonic-gate	lp = &__first_link;
5957c478bdstevel@tonic-gate
5967c478bdstevel@tonic-gate	do {
5977c478bdstevel@tonic-gate		/*
598a5f6978craigm		 * We need to grab the file locks or file corruption
599a5f6978craigm		 * will happen.  But we first check the flags field
600a5f6978craigm		 * knowing that when it is 0, it isn't allocated and
601a5f6978craigm		 * cannot be allocated while we're holding the
602a5f6978craigm		 * _first_link_lock.  And when _IONBF is set (also the
603a5f6978craigm		 * case when _flag is 0377, or alloc in progress), we
604a5f6978craigm		 * also ignore it.
605a5f6978craigm		 *
606a5f6978craigm		 * Ignore locked streams; it will appear as if
607a5f6978craigm		 * concurrent updates happened after fflush(NULL).  Note
608a5f6978craigm		 * that we even attempt to lock if the locking is set to
609a5f6978craigm		 * "by caller".  We don't want to penalize callers of
610a5f6978craigm		 * __fsetlocking() by not flushing their files.  Note: if
611a5f6978craigm		 * __fsetlocking() callers don't employ any locking, they
612a5f6978craigm		 * may still face corruption in fflush(NULL); but that's
613a5f6978craigm		 * no change from earlier releases.
6147c478bdstevel@tonic-gate		 */
6157c478bdstevel@tonic-gate		FIRSTFP(lp, iop);
6167c478bdstevel@tonic-gate		for (i = lp->niob; --i >= 0; NEXTFP(iop)) {
617a5f6978craigm			unsigned int flag = iop->_flag;
618a5f6978craigm
619a5f6978craigm			/* flag 0, flag 0377, or _IONBF set */
620a5f6978craigm			if (flag == 0 || (flag & _IONBF) != 0)
621a5f6978craigm				continue;
622a5f6978craigm
623a5f6978craigm			if (threaded) {
624a5f6978craigm				lk = FPLOCK(iop);
625a574db8raf				if (cancel_safe_mutex_trylock(lk) != 0)
626a5f6978craigm					continue;
627a5f6978craigm			}
628a5f6978craigm
629a5f6978craigm			if (!(iop->_flag & _IONBF)) {
6307c478bdstevel@tonic-gate				/*
631a5f6978craigm				 * don't need to worry about the _IORW case
632a5f6978craigm				 * since the iop will also marked with _IOREAD
633a5f6978craigm				 * or _IOWRT whichever we are really doing
6347c478bdstevel@tonic-gate				 */
635a5f6978craigm				if (iop->_flag & _IOWRT) {
636a5f6978craigm					/* Flush write buffers */
637a5f6978craigm					res |= _fflush_u(iop);
638a5f6978craigm				} else if (iop->_flag & _IOREAD) {
639a5f6978craigm					/*
640a5f6978craigm					 * flush seekable read buffers
641a5f6978craigm					 * don't flush non-seekable read buffers
642a5f6978craigm					 */
643a5f6978craigm					if (GET_SEEKABLE(iop)) {
644a5f6978craigm						res |= _fflush_u(iop);
645a5f6978craigm					}
646a5f6978craigm				}
6477c478bdstevel@tonic-gate			}
648a5f6978craigm			if (threaded)
649a574db8raf				cancel_safe_mutex_unlock(lk);
6507c478bdstevel@tonic-gate		}
6517c478bdstevel@tonic-gate	} while ((lp = lp->next) != NULL);
652a5f6978craigm	if (threaded)
653a574db8raf		cancel_safe_mutex_unlock(&_first_link_lock);
6547c478bdstevel@tonic-gate	return (res);
6557c478bdstevel@tonic-gate}
6567c478bdstevel@tonic-gate
6577c478bdstevel@tonic-gate/* flush buffer */
6587c478bdstevel@tonic-gateint
6597c478bdstevel@tonic-gate_fflush_u(FILE *iop)
6607c478bdstevel@tonic-gate{
6617c478bdstevel@tonic-gate	int res = 0;
6627c478bdstevel@tonic-gate
6637c478bdstevel@tonic-gate	/* this portion is always assumed locked */
6647c478bdstevel@tonic-gate	if (!(iop->_flag & _IOWRT)) {
665a5f6978craigm		(void) lseek64(GET_FD(iop), -iop->_cnt, SEEK_CUR);
6667c478bdstevel@tonic-gate		iop->_cnt = 0;
6677c478bdstevel@tonic-gate		/* needed for ungetc & multibyte pushbacks */
6687c478bdstevel@tonic-gate		iop->_ptr = iop->_base;
6697c478bdstevel@tonic-gate		if (iop->_flag & _IORW) {
6707c478bdstevel@tonic-gate			iop->_flag &= ~_IOREAD;
6717c478bdstevel@tonic-gate		}
6727c478bdstevel@tonic-gate		return (0);
6737c478bdstevel@tonic-gate	}
6747c478bdstevel@tonic-gate	if (iop->_base != NULL && iop->_ptr > iop->_base) {
6757c478bdstevel@tonic-gate		res = _xflsbuf(iop);
6767c478bdstevel@tonic-gate	}
6777c478bdstevel@tonic-gate	if (iop->_flag & _IORW) {
6787c478bdstevel@tonic-gate		iop->_flag &= ~_IOWRT;
6797c478bdstevel@tonic-gate		iop->_cnt = 0;
6807c478bdstevel@tonic-gate	}
6817c478bdstevel@tonic-gate	return (res);
6827c478bdstevel@tonic-gate}
6837c478bdstevel@tonic-gate
6847c478bdstevel@tonic-gate/* flush buffer and close stream */
6857c478bdstevel@tonic-gateint
6867c478bdstevel@tonic-gatefclose(FILE *iop)
6877c478bdstevel@tonic-gate{
6887c478bdstevel@tonic-gate	int res = 0;
6897c478bdstevel@tonic-gate	rmutex_t *lk;
6907c478bdstevel@tonic-gate
6917c478bdstevel@tonic-gate	if (iop == NULL) {
6927c478bdstevel@tonic-gate		return (EOF);		/* avoid passing zero to FLOCKFILE */
6937c478bdstevel@tonic-gate	}
6947c478bdstevel@tonic-gate
6957c478bdstevel@tonic-gate	FLOCKFILE(lk, iop);
6967c478bdstevel@tonic-gate	if (iop->_flag == 0) {
6977c478bdstevel@tonic-gate		FUNLOCKFILE(lk);
6987c478bdstevel@tonic-gate		return (EOF);
6997c478bdstevel@tonic-gate	}
7007c478bdstevel@tonic-gate	/* Is not unbuffered and opened for read and/or write ? */
7017c478bdstevel@tonic-gate	if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
7027c478bdstevel@tonic-gate		res = _fflush_u(iop);
703a5f6978craigm	if (close(GET_FD(iop)) < 0)
7047c478bdstevel@tonic-gate		res = EOF;
7057c478bdstevel@tonic-gate	if (iop->_flag & _IOMYBUF) {
7067c478bdstevel@tonic-gate		(void) free((char *)iop->_base - PUSHBACK);
7077c478bdstevel@tonic-gate	}
7087c478bdstevel@tonic-gate	iop->_base = NULL;
7097c478bdstevel@tonic-gate	iop->_ptr = NULL;
7107c478bdstevel@tonic-gate	iop->_cnt = 0;
7117c478bdstevel@tonic-gate	iop->_flag = 0;			/* marks it as available */
7127c478bdstevel@tonic-gate	FUNLOCKFILE(lk);
7137c478bdstevel@tonic-gate
7141d2738araf	if (__libc_threaded)
715a574db8raf		cancel_safe_mutex_lock(&_first_link_lock);
7167c478bdstevel@tonic-gate	fcloses++;
7171d2738araf	if (__libc_threaded)
718a574db8raf		cancel_safe_mutex_unlock(&_first_link_lock);
7197c478bdstevel@tonic-gate
7207c478bdstevel@tonic-gate	return (res);
7217c478bdstevel@tonic-gate}
7227c478bdstevel@tonic-gate
72323a1cceRoger A. Faulkner/* close all open streams */
72423a1cceRoger A. Faulknerint
72523a1cceRoger A. Faulknerfcloseall(void)
72623a1cceRoger A. Faulkner{
72723a1cceRoger A. Faulkner	FPDECL(iop);
72823a1cceRoger A. Faulkner
72923a1cceRoger A. Faulkner	struct _link_ *lp;
73023a1cceRoger A. Faulkner	rmutex_t *lk;
73123a1cceRoger A. Faulkner
73223a1cceRoger A. Faulkner	if (__libc_threaded)
73323a1cceRoger A. Faulkner		cancel_safe_mutex_lock(&_first_link_lock);
73423a1cceRoger A. Faulkner
73523a1cceRoger A. Faulkner	lp = &__first_link;
73623a1cceRoger A. Faulkner
73723a1cceRoger A. Faulkner	do {
73823a1cceRoger A. Faulkner		int i;
73923a1cceRoger A. Faulkner
74023a1cceRoger A. Faulkner		FIRSTFP(lp, iop);
74123a1cceRoger A. Faulkner		for (i = lp->niob; --i >= 0; NEXTFP(iop)) {
74223a1cceRoger A. Faulkner			/* code stolen from fclose(), above */
74323a1cceRoger A. Faulkner
74423a1cceRoger A. Faulkner			FLOCKFILE(lk, iop);
74523a1cceRoger A. Faulkner			if (iop->_flag == 0) {
74623a1cceRoger A. Faulkner				FUNLOCKFILE(lk);
74723a1cceRoger A. Faulkner				continue;
74823a1cceRoger A. Faulkner			}
74923a1cceRoger A. Faulkner
75023a1cceRoger A. Faulkner			/* Not unbuffered and opened for read and/or write? */
75123a1cceRoger A. Faulkner			if (!(iop->_flag & _IONBF) &&
75223a1cceRoger A. Faulkner			    (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
75323a1cceRoger A. Faulkner				(void) _fflush_u(iop);
75423a1cceRoger A. Faulkner			(void) close(GET_FD(iop));
75523a1cceRoger A. Faulkner			if (iop->_flag & _IOMYBUF)
75623a1cceRoger A. Faulkner				free((char *)iop->_base - PUSHBACK);
75723a1cceRoger A. Faulkner			iop->_base = NULL;
75823a1cceRoger A. Faulkner			iop->_ptr = NULL;
75923a1cceRoger A. Faulkner			iop->_cnt = 0;
76023a1cceRoger A. Faulkner			iop->_flag = 0;		/* marks it as available */
76123a1cceRoger A. Faulkner			FUNLOCKFILE(lk);
76223a1cceRoger A. Faulkner			fcloses++;
76323a1cceRoger A. Faulkner		}
76423a1cceRoger A. Faulkner	} while ((lp = lp->next) != NULL);
76523a1cceRoger A. Faulkner
76623a1cceRoger A. Faulkner	if (__libc_threaded)
76723a1cceRoger A. Faulkner		cancel_safe_mutex_unlock(&_first_link_lock);
76823a1cceRoger A. Faulkner
76923a1cceRoger A. Faulkner	return (0);
77023a1cceRoger A. Faulkner}
77123a1cceRoger A. Faulkner
7727c478bdstevel@tonic-gate/* flush buffer, close fd but keep the stream used by freopen() */
7737c478bdstevel@tonic-gateint
7747c478bdstevel@tonic-gateclose_fd(FILE *iop)
7757c478bdstevel@tonic-gate{
7767c478bdstevel@tonic-gate	int res = 0;
7777c478bdstevel@tonic-gate	mbstate_t *mb;
7787c478bdstevel@tonic-gate
7797c478bdstevel@tonic-gate	if (iop == NULL || iop->_flag == 0)
7807c478bdstevel@tonic-gate		return (EOF);
7817c478bdstevel@tonic-gate	/* Is not unbuffered and opened for read and/or write ? */
7827c478bdstevel@tonic-gate	if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
7837c478bdstevel@tonic-gate		res = _fflush_u(iop);
784a5f6978craigm	if (close(GET_FD(iop)) < 0)
7857c478bdstevel@tonic-gate		res = EOF;
7867c478bdstevel@tonic-gate	if (iop->_flag & _IOMYBUF) {
7877c478bdstevel@tonic-gate		(void) free((char *)iop->_base - PUSHBACK);
7887c478bdstevel@tonic-gate	}
7897c478bdstevel@tonic-gate	iop->_base = NULL;
7907c478bdstevel@tonic-gate	iop->_ptr = NULL;
7917c478bdstevel@tonic-gate	mb = _getmbstate(iop);
7927c478bdstevel@tonic-gate	if (mb != NULL)
7937c478bdstevel@tonic-gate		(void) memset(mb, 0, sizeof (mbstate_t));
7947c478bdstevel@tonic-gate	iop->_cnt = 0;
7957c478bdstevel@tonic-gate	_setorientation(iop, _NO_MODE);
7967c478bdstevel@tonic-gate	return (res);
7977c478bdstevel@tonic-gate}
7987c478bdstevel@tonic-gate
7997c478bdstevel@tonic-gatestatic FILE *
8007c478bdstevel@tonic-gategetiop(FILE *fp, rmutex_t *lk, mbstate_t *mb)
8017c478bdstevel@tonic-gate{
802a574db8raf	if (lk != NULL && cancel_safe_mutex_trylock(lk) != 0)
8037c478bdstevel@tonic-gate		return (NULL);	/* locked: fp in use */
8047c478bdstevel@tonic-gate
8057c478bdstevel@tonic-gate	if (fp->_flag == 0) {	/* unused */
8067c478bdstevel@tonic-gate#ifndef	_LP64
8077c478bdstevel@tonic-gate		fp->__orientation = 0;
8087c478bdstevel@tonic-gate#endif /* _LP64 */
8097c478bdstevel@tonic-gate		fp->_cnt = 0;
8107c478bdstevel@tonic-gate		fp->_ptr = NULL;
8117c478bdstevel@tonic-gate		fp->_base = NULL;
8127c478bdstevel@tonic-gate		fp->_flag = 0377;	/* claim the fp by setting low 8 bits */
8137c478bdstevel@tonic-gate		(void) memset(mb, 0, sizeof (mbstate_t));
8147c478bdstevel@tonic-gate		FUNLOCKFILE(lk);
8157c478bdstevel@tonic-gate		return (fp);
8167c478bdstevel@tonic-gate	}
8177c478bdstevel@tonic-gate	FUNLOCKFILE(lk);
8187c478bdstevel@tonic-gate	return (NULL);
8197c478bdstevel@tonic-gate}
8207c478bdstevel@tonic-gate
8217c478bdstevel@tonic-gate#ifndef	_LP64
8227c478bdstevel@tonic-gate/*
8237c478bdstevel@tonic-gate * DESCRIPTION:
8247c478bdstevel@tonic-gate * This function gets the pointer to the mbstate_t structure associated
8257c478bdstevel@tonic-gate * with the specified iop.
8267c478bdstevel@tonic-gate *
8277c478bdstevel@tonic-gate * RETURNS:
8287c478bdstevel@tonic-gate * If the associated mbstate_t found, the pointer to the mbstate_t is
8297c478bdstevel@tonic-gate * returned.  Otherwise, NULL is returned.
8307c478bdstevel@tonic-gate */
8317c478bdstevel@tonic-gatembstate_t *
8327c478bdstevel@tonic-gate_getmbstate(FILE *iop)
8337c478bdstevel@tonic-gate{
8347c478bdstevel@tonic-gate	struct xFILEdata *dat = getxfdat(iop);
8357c478bdstevel@tonic-gate
8367c478bdstevel@tonic-gate	if (dat != NULL)
8377c478bdstevel@tonic-gate		return (&dat->_state);
8387c478bdstevel@tonic-gate
8397c478bdstevel@tonic-gate	return (NULL);
8407c478bdstevel@tonic-gate}
841a5f6978craigm
842a5f6978craigm/*
843a5f6978craigm * More 32-bit only functions.
844a5f6978craigm * They lookup/set large fd's for extended FILE support.
845a5f6978craigm */
846a5f6978craigm
847a5f6978craigm/*
848a5f6978craigm * The negative value indicates that Extended fd FILE's has not
849a5f6978craigm * been enabled by the user.
850a5f6978craigm */
851a5f6978craigmstatic int bad_fd = -1;
852a5f6978craigm
853a5f6978craigmint
854a5f6978craigm_file_get(FILE *iop)
855a5f6978craigm{
856a5f6978craigm	int altfd;
857a5f6978craigm
858a5f6978craigm	/*
859a5f6978craigm	 * Failure indicates a FILE * not allocated through stdio;
860a5f6978craigm	 * it means the flag values are probably bogus and that if
861a5f6978craigm	 * a file descriptor is set, it's in _magic.
862a5f6978craigm	 * Inline getxfdat() for performance reasons.
863a5f6978craigm	 */
864a5f6978craigm	if (STDIOP(iop))
865a5f6978craigm		altfd = _xftab[IOPIND(iop)]._altfd;
866a5f6978craigm	else if (VALIDXFILE(FILEx(iop)))
867a5f6978craigm		altfd = FILEx(iop)->_xdat._altfd;
868a5f6978craigm	else
869a5f6978craigm		return (iop->_magic);
870a5f6978craigm	/*
871a5f6978craigm	 * if this is not an internal extended FILE then check
872a5f6978craigm	 * if _file is being changed from underneath us.
873a5f6978craigm	 * It should not be because if
874a5f6978craigm	 * it is then then we lose our ability to guard against
875a5f6978craigm	 * silent data corruption.
876a5f6978craigm	 */
877a5f6978craigm	if (!iop->__xf_nocheck && bad_fd > -1 && iop->_magic != bad_fd) {
878e1d65dbcraigm		(void) fprintf(stderr,
879a5f6978craigm		    "Application violated extended FILE safety mechanism.\n"
880e1d65dbcraigm		    "Please read the man page for extendedFILE.\nAborting\n");
881a5f6978craigm		abort();
882a5f6978craigm	}
883a5f6978craigm	return (altfd);
884a5f6978craigm}
885a5f6978craigm
886a5f6978craigmint
887a5f6978craigm_file_set(FILE *iop, int fd, const char *type)
888a5f6978craigm{
889a5f6978craigm	struct xFILEdata *dat;
890a5f6978craigm	int Fflag;
891a5f6978craigm
892a5f6978craigm	/* Already known to contain at least one byte */
893a5f6978craigm	while (*++type != '\0')
894a5f6978craigm		;
895a5f6978craigm
896a5f6978craigm	Fflag = type[-1] == 'F';
897a5f6978craigm	if (!Fflag && bad_fd < 0) {
898a5f6978craigm		errno = EMFILE;
899a5f6978craigm		return (-1);
900a5f6978craigm	}
901a5f6978craigm
902a5f6978craigm	dat = getxfdat(iop);
903a5f6978craigm	iop->__extendedfd = 1;
904a5f6978craigm	iop->__xf_nocheck = Fflag;
905a5f6978craigm	dat->_altfd = fd;
906a5f6978craigm	iop->_magic = (unsigned char)bad_fd;
907a5f6978craigm	return (0);
908a5f6978craigm}
909a5f6978craigm
910a5f6978craigm/*
911a5f6978craigm * Activates extended fd's in FILE's
912a5f6978craigm */
913a5f6978craigm
914a5f6978craigmstatic const int tries[] = {196, 120, 60, 3};
915a5f6978craigm#define	NTRIES	(sizeof (tries)/sizeof (int))
916a5f6978craigm
917a5f6978craigmint
918a5f6978craigmenable_extended_FILE_stdio(int fd, int action)
919a5f6978craigm{
920a5f6978craigm	int i;
921a5f6978craigm
922a5f6978craigm	if (action < 0)
923a5f6978craigm		action = SIGABRT;	/* default signal */
924a5f6978craigm
925a5f6978craigm	if (fd < 0) {
926a5f6978craigm		/*
927a5f6978craigm		 * search for an available fd and make it the badfd
928a5f6978craigm		 */
929a5f6978craigm		for (i = 0; i < NTRIES; i++) {
930a5f6978craigm			fd = fcntl(tries[i], F_BADFD, action);
931a5f6978craigm			if (fd >= 0)
932a5f6978craigm				break;
933a5f6978craigm		}
934a5f6978craigm		if (fd < 0)	/* failed to find an available fd */
935a5f6978craigm			return (-1);
936a5f6978craigm	} else {
937a5f6978craigm		/* caller requests that fd be the chosen badfd */
938a5f6978craigm		int nfd = fcntl(fd, F_BADFD, action);
939a5f6978craigm		if (nfd < 0 || nfd != fd)
940a5f6978craigm			return (-1);
941a5f6978craigm	}
942a5f6978craigm	bad_fd = fd;
943a5f6978craigm	return (0);
944a5f6978craigm}
9457c478bdstevel@tonic-gate#endif
946