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.
24cd62a92Robert Mustacchi * Copyright 2020 Robert Mustacchi
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1988 AT&T	*/
28cd62a92Robert Mustacchi/*	  All Rights Reserved	*/
297c478bdstevel@tonic-gate
307257d1braf#include "lint.h"
317c478bdstevel@tonic-gate#include "mtlib.h"
327c478bdstevel@tonic-gate#include "file64.h"
337c478bdstevel@tonic-gate#include <sys/types.h>
347c478bdstevel@tonic-gate#include <stdlib.h>
357c478bdstevel@tonic-gate#include <stdio.h>
367c478bdstevel@tonic-gate#include <thread.h>
377c478bdstevel@tonic-gate#include <synch.h>
387c478bdstevel@tonic-gate#include <unistd.h>
397c478bdstevel@tonic-gate#include <string.h>
407c478bdstevel@tonic-gate#include "stdiom.h"
417c478bdstevel@tonic-gate#include <wchar.h>
427c478bdstevel@tonic-gate#include <sys/stat.h>
437c478bdstevel@tonic-gate#include <stddef.h>
447c478bdstevel@tonic-gate#include <errno.h>
45a5f6978craigm#include <fcntl.h>
46cd62a92Robert Mustacchi#include <sys/debug.h>
47cd62a92Robert Mustacchi#include <limits.h>
487c478bdstevel@tonic-gate
497257d1braf#define	_iob	__iob
507257d1braf
517c478bdstevel@tonic-gate#undef end
527c478bdstevel@tonic-gate
537c478bdstevel@tonic-gate#define	FILE_ARY_SZ	8 /* a nice size for FILE array & end_buffer_ptrs */
547c478bdstevel@tonic-gate
557c478bdstevel@tonic-gate#ifdef	_LP64
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate/*
587c478bdstevel@tonic-gate * Macros to declare and loop over a fp or fp/xfp combo to
597c478bdstevel@tonic-gate * avoid some of the _LP64 ifdef hell.
607c478bdstevel@tonic-gate */
617c478bdstevel@tonic-gate
627c478bdstevel@tonic-gate#define	FPDECL(fp)		FILE *fp
637c478bdstevel@tonic-gate#define	FIRSTFP(lp, fp)		fp = lp->iobp
647c478bdstevel@tonic-gate#define	NEXTFP(fp)		fp++
65a5f6978craigm#define	FPLOCK(fp)		&fp->_lock
66a5f6978craigm#define	FPSTATE(fp)		&fp->_state
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate#define	xFILE			FILE
697c478bdstevel@tonic-gate
707c478bdstevel@tonic-gate#else
717c478bdstevel@tonic-gate
727c478bdstevel@tonic-gate#define	FPDECL(fp)		FILE *fp; xFILE *x##fp
737c478bdstevel@tonic-gate#define	FIRSTFP(lp, fp)		x##fp = lp->iobp; \
747c478bdstevel@tonic-gate				fp = x##fp ? &x##fp->_iob : &_iob[0]
757c478bdstevel@tonic-gate#define	NEXTFP(fp)		(x##fp ? fp = &(++x##fp)->_iob : ++fp)
76a5f6978craigm#define	FPLOCK(fp)		x##fp ? \
77a5f6978craigm				    &x##fp->xlock : &_xftab[IOPIND(fp)]._lock
78a5f6978craigm#define	FPSTATE(fp)		x##fp ? \
79a5f6978craigm				    &x##fp->xstate : &_xftab[IOPIND(fp)]._state
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gate/* The extended 32-bit file structure for use in link buffers */
827c478bdstevel@tonic-gatetypedef struct xFILE {
837c478bdstevel@tonic-gate	FILE			_iob;		/* must be first! */
847c478bdstevel@tonic-gate	struct xFILEdata	_xdat;
857c478bdstevel@tonic-gate} xFILE;
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate#define	xmagic			_xdat._magic
887c478bdstevel@tonic-gate#define	xend			_xdat._end
897c478bdstevel@tonic-gate#define	xlock			_xdat._lock
907c478bdstevel@tonic-gate#define	xstate			_xdat._state
917c478bdstevel@tonic-gate
927c478bdstevel@tonic-gate#define	FILEx(fp)		((struct xFILE *)(uintptr_t)fp)
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gate/*
957c478bdstevel@tonic-gate * The magic number stored is actually the pointer scrambled with
967c478bdstevel@tonic-gate * a magic number.  Pointers to data items live everywhere in memory
977c478bdstevel@tonic-gate * so we scramble the pointer in order to avoid accidental collisions.
987c478bdstevel@tonic-gate */
997c478bdstevel@tonic-gate#define	XFILEMAGIC		0x63687367
1007c478bdstevel@tonic-gate#define	XMAGIC(xfp)		((uintptr_t)(xfp) ^ XFILEMAGIC)
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate#endif /* _LP64 */
1037c478bdstevel@tonic-gate
1047c478bdstevel@tonic-gatestruct _link_	/* manages a list of streams */
1057c478bdstevel@tonic-gate{
1067c478bdstevel@tonic-gate	xFILE *iobp;		/* the array of (x)FILE's */
1077c478bdstevel@tonic-gate				/* NULL for the __first_link in ILP32 */
1087c478bdstevel@tonic-gate	int	niob;		/* length of the arrays */
1097c478bdstevel@tonic-gate	struct _link_	*next;	/* next in the list */
1107c478bdstevel@tonic-gate};
1117c478bdstevel@tonic-gate
1127c478bdstevel@tonic-gate/*
1137c478bdstevel@tonic-gate * With dynamic linking, iob may be in either the library or in the user's
1147c478bdstevel@tonic-gate * a.out, so the run time linker fixes up the first entry in __first_link at
1157c478bdstevel@tonic-gate * process startup time.
1167c478bdstevel@tonic-gate *
1177c478bdstevel@tonic-gate * In 32 bit processes, we don't have xFILE[FILE_ARY_SZ] but FILE[],
1187c478bdstevel@tonic-gate * and _xftab[] instead; this is denoted by having iobp set to NULL in
1197c478bdstevel@tonic-gate * 32 bit mode for the first link entry.
1207c478bdstevel@tonic-gate */
1217c478bdstevel@tonic-gatestruct _link_ __first_link =	/* first in linked list */
1227c478bdstevel@tonic-gate{
1237c478bdstevel@tonic-gate#if !defined(_LP64)
1247c478bdstevel@tonic-gate	NULL,
1257c478bdstevel@tonic-gate#else
1267c478bdstevel@tonic-gate	&_iob[0],
1277c478bdstevel@tonic-gate#endif
1287c478bdstevel@tonic-gate	_NFILE,
1297c478bdstevel@tonic-gate	NULL
1307c478bdstevel@tonic-gate};
1317c478bdstevel@tonic-gate
1327c478bdstevel@tonic-gate/*
1337c478bdstevel@tonic-gate * Information cached to speed up searches.  We remember where we
1347c478bdstevel@tonic-gate * last found a free FILE* and we remember whether we saw any fcloses
1357c478bdstevel@tonic-gate * in between.  We also count the number of chunks we allocated, see
1367c478bdstevel@tonic-gate * _findiop() for an explanation.
1377c478bdstevel@tonic-gate * These variables are all protected by _first_link_lock.
1387c478bdstevel@tonic-gate */
1397c478bdstevel@tonic-gatestatic struct _link_ *lastlink = NULL;
1407c478bdstevel@tonic-gatestatic int fcloses;
1417c478bdstevel@tonic-gatestatic int nchunks;
1427c478bdstevel@tonic-gate
143a5f6978craigmstatic mutex_t _first_link_lock = DEFAULTMUTEX;
1447c478bdstevel@tonic-gate
145a5f6978craigmstatic int _fflush_l_iops(void);
1467c478bdstevel@tonic-gatestatic FILE *getiop(FILE *, rmutex_t *, mbstate_t *);
1477c478bdstevel@tonic-gate
1487c478bdstevel@tonic-gate/*
1497c478bdstevel@tonic-gate * All functions that understand the linked list of iob's follow.
1507c478bdstevel@tonic-gate */
1517c478bdstevel@tonic-gate#pragma weak _cleanup = __cleanup
1527c478bdstevel@tonic-gatevoid
1537c478bdstevel@tonic-gate__cleanup(void)		/* called at process end to flush ouput streams */
1547c478bdstevel@tonic-gate{
1557c478bdstevel@tonic-gate	(void) fflush(NULL);
1567c478bdstevel@tonic-gate}
1577c478bdstevel@tonic-gate
1587c478bdstevel@tonic-gate/*
1597c478bdstevel@tonic-gate * For fork1-safety (see libc_prepare_atfork(), etc).
1607c478bdstevel@tonic-gate */
1617c478bdstevel@tonic-gatevoid
1627c478bdstevel@tonic-gatestdio_locks()
1637c478bdstevel@tonic-gate{
1648cd4554raf	(void) mutex_lock(&_first_link_lock);
1657c478bdstevel@tonic-gate	/*
1667c478bdstevel@tonic-gate	 * XXX: We should acquire all of the iob locks here.
1677c478bdstevel@tonic-gate	 */
1687c478bdstevel@tonic-gate}
1697c478bdstevel@tonic-gate
1707c478bdstevel@tonic-gatevoid
1717c478bdstevel@tonic-gatestdio_unlocks()
1727c478bdstevel@tonic-gate{
1737c478bdstevel@tonic-gate	/*
1747c478bdstevel@tonic-gate	 * XXX: We should release all of the iob locks here.
1757c478bdstevel@tonic-gate	 */
1768cd4554raf	(void) mutex_unlock(&_first_link_lock);
1777c478bdstevel@tonic-gate}
1787c478bdstevel@tonic-gate
1797c478bdstevel@tonic-gatevoid
1807c478bdstevel@tonic-gate_flushlbf(void)		/* fflush() all line-buffered streams */
1817c478bdstevel@tonic-gate{
1827c478bdstevel@tonic-gate	FPDECL(fp);
1837c478bdstevel@tonic-gate	int i;
1847c478bdstevel@tonic-gate	struct _link_ *lp;
185a5f6978craigm	/* Allow compiler to optimize the loop */
186a5f6978craigm	int threaded = __libc_threaded;
1877c478bdstevel@tonic-gate
188a5f6978craigm	if (threaded)
189a574db8raf		cancel_safe_mutex_lock(&_first_link_lock);
1907c478bdstevel@tonic-gate
1917c478bdstevel@tonic-gate	lp = &__first_link;
1927c478bdstevel@tonic-gate	do {
1937c478bdstevel@tonic-gate		FIRSTFP(lp, fp);
1947c478bdstevel@tonic-gate		for (i = lp->niob; --i >= 0; NEXTFP(fp)) {
195a5f6978craigm			/*
196a5f6978craigm			 * The additional _IONBF check guards againsts
197a5f6978craigm			 * allocated but uninitialized iops (see _findiop).
198a5f6978craigm			 * We also automatically skip non allocated iop's.
199a5f6978craigm			 * Don't block on locks.
200a5f6978craigm			 */
201a5f6978craigm			if ((fp->_flag & (_IOLBF | _IOWRT | _IONBF)) ==
202a5f6978craigm			    (_IOLBF | _IOWRT)) {
203a5f6978craigm				if (threaded) {
204a5f6978craigm					rmutex_t *lk = FPLOCK(fp);
205a574db8raf					if (cancel_safe_mutex_trylock(lk) != 0)
206a5f6978craigm						continue;
207a5f6978craigm					/* Recheck after locking */
208a5f6978craigm					if ((fp->_flag & (_IOLBF | _IOWRT)) ==
209a5f6978craigm					    (_IOLBF | _IOWRT)) {
210a5f6978craigm						(void) _fflush_u(fp);
211a5f6978craigm					}
212a574db8raf					cancel_safe_mutex_unlock(lk);
213a5f6978craigm				} else {
214a5f6978craigm					(void) _fflush_u(fp);
215a5f6978craigm				}
216a5f6978craigm			}
2177c478bdstevel@tonic-gate		}
2187c478bdstevel@tonic-gate	} while ((lp = lp->next) != NULL);
2197c478bdstevel@tonic-gate
220a5f6978craigm	if (threaded)
221a574db8raf		cancel_safe_mutex_unlock(&_first_link_lock);
2227c478bdstevel@tonic-gate}
2237c478bdstevel@tonic-gate
2247c478bdstevel@tonic-gate/* allocate an unused stream; NULL if cannot */
2257c478bdstevel@tonic-gateFILE *
2267c478bdstevel@tonic-gate_findiop(void)
2277c478bdstevel@tonic-gate{
2287c478bdstevel@tonic-gate	struct _link_ *lp, **prev;
2297c478bdstevel@tonic-gate
2307c478bdstevel@tonic-gate	/* used so there only needs to be one malloc() */
2317c478bdstevel@tonic-gate#ifdef _LP64
2327c478bdstevel@tonic-gate	typedef	struct	{
2337c478bdstevel@tonic-gate		struct _link_	hdr;
2347c478bdstevel@tonic-gate		FILE	iob[FILE_ARY_SZ];
2357c478bdstevel@tonic-gate	} Pkg;
2367c478bdstevel@tonic-gate#else
2377c478bdstevel@tonic-gate	typedef union {
2387c478bdstevel@tonic-gate		struct {				/* Normal */
2397c478bdstevel@tonic-gate			struct _link_	hdr;
2407c478bdstevel@tonic-gate			xFILE	iob[FILE_ARY_SZ];
2417c478bdstevel@tonic-gate		} Pkgn;
2427c478bdstevel@tonic-gate		struct {				/* Reversed */
2437c478bdstevel@tonic-gate			xFILE	iob[FILE_ARY_SZ];
2447c478bdstevel@tonic-gate			struct _link_	hdr;
2457c478bdstevel@tonic-gate		} Pkgr;
2467c478bdstevel@tonic-gate	} Pkg;
2477c478bdstevel@tonic-gate	uintptr_t delta;
2487c478bdstevel@tonic-gate#endif
2497c478bdstevel@tonic-gate	Pkg *pkgp;
2507c478bdstevel@tonic-gate	struct _link_ *hdr;
2517c478bdstevel@tonic-gate	FPDECL(fp);
2527c478bdstevel@tonic-gate	int i;
253a5f6978craigm	int threaded = __libc_threaded;
2547c478bdstevel@tonic-gate
255a5f6978craigm	if (threaded)
256a574db8raf		cancel_safe_mutex_lock(&_first_link_lock);
2577c478bdstevel@tonic-gate
2587c478bdstevel@tonic-gate	if (lastlink == NULL) {
2597c478bdstevel@tonic-gaterescan:
2607c478bdstevel@tonic-gate		fcloses = 0;
2617c478bdstevel@tonic-gate		lastlink = &__first_link;
2627c478bdstevel@tonic-gate	}
2637c478bdstevel@tonic-gate
2647c478bdstevel@tonic-gate	lp = lastlink;
2657c478bdstevel@tonic-gate
2667c478bdstevel@tonic-gate	/*
2677c478bdstevel@tonic-gate	 * lock to make testing of fp->_flag == 0 and acquiring the fp atomic
2687c478bdstevel@tonic-gate	 * and for allocation of new links
2697c478bdstevel@tonic-gate	 * low contention expected on _findiop(), hence coarse locking.
2707c478bdstevel@tonic-gate	 * for finer granularity, use fp->_lock for allocating an iop
2717c478bdstevel@tonic-gate	 * and make the testing of lp->next and allocation of new link atomic
2727c478bdstevel@tonic-gate	 * using lp->_lock
2737c478bdstevel@tonic-gate	 */
2747c478bdstevel@tonic-gate
2757c478bdstevel@tonic-gate	do {
2767c478bdstevel@tonic-gate		prev = &lp->next;
2777c478bdstevel@tonic-gate		FIRSTFP(lp, fp);
2787c478bdstevel@tonic-gate
2797c478bdstevel@tonic-gate		for (i = lp->niob; --i >= 0; NEXTFP(fp)) {
280a5f6978craigm			FILE *ret;
281a5f6978craigm			if (threaded) {
282a5f6978craigm				ret = getiop(fp, FPLOCK(fp), FPSTATE(fp));
283a5f6978craigm				if (ret != NULL) {
284a574db8raf					cancel_safe_mutex_unlock(
285b2e86e7casper					    &_first_link_lock);
286b2e86e7casper					return (ret);
287a5f6978craigm				}
288a5f6978craigm			} else {
289a5f6978craigm				ret = getiop(fp, NULL, FPSTATE(fp));
290a5f6978craigm				if (ret != NULL)
291a5f6978craigm					return (ret);
292a5f6978craigm			}
2937c478bdstevel@tonic-gate		}
2947c478bdstevel@tonic-gate	} while ((lastlink = lp = lp->next) != NULL);
2957c478bdstevel@tonic-gate
2967c478bdstevel@tonic-gate	/*
2977c478bdstevel@tonic-gate	 * If there was a sufficient number of  fcloses since we last started
2987c478bdstevel@tonic-gate	 * at __first_link, we rescan all fp's again.  We do not rescan for
2997c478bdstevel@tonic-gate	 * all fcloses; that would simplify the algorithm but would make
3007c478bdstevel@tonic-gate	 * search times near O(n) again.
3017c478bdstevel@tonic-gate	 * Worst case behaviour would still be pretty bad (open a full set,
3027c478bdstevel@tonic-gate	 * then continously opening and closing one FILE * gets you a full
3037c478bdstevel@tonic-gate	 * scan each time).  That's why we over allocate 1 FILE for each
3047c478bdstevel@tonic-gate	 * 32 chunks.  More over allocation is better; this is a nice
3057c478bdstevel@tonic-gate	 * empirical value which doesn't cost a lot of memory, doesn't
3067c478bdstevel@tonic-gate	 * overallocate until we reach 256 FILE *s and keeps the performance
3077c478bdstevel@tonic-gate	 * pretty close to the optimum.
3087c478bdstevel@tonic-gate	 */
3097c478bdstevel@tonic-gate	if (fcloses > nchunks/32)
3107c478bdstevel@tonic-gate		goto rescan;
3117c478bdstevel@tonic-gate
3127c478bdstevel@tonic-gate	/*
3137c478bdstevel@tonic-gate	 * Need to allocate another and put it in the linked list.
3147c478bdstevel@tonic-gate	 */
3157c478bdstevel@tonic-gate	if ((pkgp = malloc(sizeof (Pkg))) == NULL) {
316a5f6978craigm		if (threaded)
317a574db8raf			cancel_safe_mutex_unlock(&_first_link_lock);
3187c478bdstevel@tonic-gate		return (NULL);
3197c478bdstevel@tonic-gate	}
3207c478bdstevel@tonic-gate
3217c478bdstevel@tonic-gate	(void) memset(pkgp, 0, sizeof (Pkg));
3227c478bdstevel@tonic-gate
3237c478bdstevel@tonic-gate#ifdef _LP64
3247c478bdstevel@tonic-gate	hdr = &pkgp->hdr;
3257c478bdstevel@tonic-gate	hdr->iobp = &pkgp->iob[0];
3267c478bdstevel@tonic-gate#else
3277c478bdstevel@tonic-gate	/*
3287c478bdstevel@tonic-gate	 * The problem with referencing a word after a FILE* is the possibility
3297c478bdstevel@tonic-gate	 * of a SIGSEGV if a non-stdio issue FILE structure ends on a page
3307c478bdstevel@tonic-gate	 * boundary.  We run this check so we never need to run an expensive
3317c478bdstevel@tonic-gate	 * check like mincore() in order to know whether it is
3327c478bdstevel@tonic-gate	 * safe to dereference ((xFILE*)fp)->xmagic.
3337c478bdstevel@tonic-gate	 * We allocate the block with two alternative layouts; if one
3347c478bdstevel@tonic-gate	 * layout is not properly aligned for our purposes, the other layout
3357c478bdstevel@tonic-gate	 * will be because the size of _link_ is small compared to
3367c478bdstevel@tonic-gate	 * sizeof (xFILE).
3377c478bdstevel@tonic-gate	 * The check performed is this:
3387c478bdstevel@tonic-gate	 *	If the distance from pkgp to the end of the page is
3397c478bdstevel@tonic-gate	 *	less than the the offset of the last xmagic field in the
3407c478bdstevel@tonic-gate	 *	xFILE structure, (the 0x1000 boundary is inside our just
3417c478bdstevel@tonic-gate	 *	allocated structure) and the distance modulo the size of xFILE
3427c478bdstevel@tonic-gate	 *	is identical to the offset of the first xmagic in the
3437c478bdstevel@tonic-gate	 *	structure (i.e., XXXXXX000 points to an xmagic field),
3447c478bdstevel@tonic-gate	 *	we need to use the reverse structure.
3457c478bdstevel@tonic-gate	 */
3467c478bdstevel@tonic-gate	if ((delta = 0x1000 - ((uintptr_t)pkgp & 0xfff)) <=
347a574db8raf	    offsetof(Pkg, Pkgn.iob[FILE_ARY_SZ-1].xmagic) &&
3487c478bdstevel@tonic-gate	    delta % sizeof (struct xFILE) ==
349a574db8raf	    offsetof(Pkg, Pkgn.iob[0].xmagic)) {
3507c478bdstevel@tonic-gate		/* Use reversed structure */
3517c478bdstevel@tonic-gate		hdr = &pkgp->Pkgr.hdr;
3527c478bdstevel@tonic-gate		hdr->iobp = &pkgp->Pkgr.iob[0];
3537c478bdstevel@tonic-gate	} else {
3547c478bdstevel@tonic-gate		/* Use normal structure */
3557c478bdstevel@tonic-gate		hdr = &pkgp->Pkgn.hdr;
3567c478bdstevel@tonic-gate		hdr->iobp = &pkgp->Pkgn.iob[0];
3577c478bdstevel@tonic-gate	}
3587c478bdstevel@tonic-gate#endif /* _LP64 */
3597c478bdstevel@tonic-gate
3607c478bdstevel@tonic-gate	hdr->niob = FILE_ARY_SZ;
3617c478bdstevel@tonic-gate	nchunks++;
3627c478bdstevel@tonic-gate
3637c478bdstevel@tonic-gate#ifdef	_LP64
3647c478bdstevel@tonic-gate	fp = hdr->iobp;
3657c478bdstevel@tonic-gate	for (i = 0; i < FILE_ARY_SZ; i++)
3667257d1braf		(void) mutex_init(&fp[i]._lock,
3677257d1braf		    USYNC_THREAD | LOCK_RECURSIVE, NULL);
3687c478bdstevel@tonic-gate#else
3697c478bdstevel@tonic-gate	xfp = hdr->iobp;
3707c478bdstevel@tonic-gate	fp = &xfp->_iob;
3717c478bdstevel@tonic-gate
3727c478bdstevel@tonic-gate	for (i = 0; i < FILE_ARY_SZ; i++) {
3737c478bdstevel@tonic-gate		xfp[i].xmagic = XMAGIC(&xfp[i]);
3747257d1braf		(void) mutex_init(&xfp[i].xlock,
3757257d1braf		    USYNC_THREAD | LOCK_RECURSIVE, NULL);
3767c478bdstevel@tonic-gate	}
3777c478bdstevel@tonic-gate#endif	/*	_LP64	*/
3787c478bdstevel@tonic-gate
3797c478bdstevel@tonic-gate	lastlink = *prev = hdr;
3807c478bdstevel@tonic-gate	fp->_ptr = 0;
3817c478bdstevel@tonic-gate	fp->_base = 0;
382cd62a92Robert Mustacchi	/* claim the fp by setting low 8 bits */
383cd62a92Robert Mustacchi	fp->_flag = _DEF_FLAG_MASK;
384a5f6978craigm	if (threaded)
385a574db8raf		cancel_safe_mutex_unlock(&_first_link_lock);
3867c478bdstevel@tonic-gate
3877c478bdstevel@tonic-gate	return (fp);
3887c478bdstevel@tonic-gate}
3897c478bdstevel@tonic-gate
3907c478bdstevel@tonic-gatestatic void
3917c478bdstevel@tonic-gateisseekable(FILE *iop)
3927c478bdstevel@tonic-gate{
3937c478bdstevel@tonic-gate	struct stat64 fstatbuf;
394cd62a92Robert Mustacchi	int fd, save_errno;
3957c478bdstevel@tonic-gate
3967c478bdstevel@tonic-gate	save_errno = errno;
3977c478bdstevel@tonic-gate
398cd62a92Robert Mustacchi	/*
399cd62a92Robert Mustacchi	 * non-FILE based STREAMS are required to declare their own seekability
400cd62a92Robert Mustacchi	 * and therefore we should not try and test them below.
401cd62a92Robert Mustacchi	 */
402cd62a92Robert Mustacchi	fd = _get_fd(iop);
403cd62a92Robert Mustacchi	if (fd == -1) {
404cd62a92Robert Mustacchi		return;
405cd62a92Robert Mustacchi	}
406cd62a92Robert Mustacchi	if (fstat64(fd, &fstatbuf) != 0) {
4077c478bdstevel@tonic-gate		/*
4087c478bdstevel@tonic-gate		 * when we don't know what it is we'll
4097c478bdstevel@tonic-gate		 * do the old behaviour and flush
4107c478bdstevel@tonic-gate		 * the stream
4117c478bdstevel@tonic-gate		 */
4127c478bdstevel@tonic-gate		SET_SEEKABLE(iop);
4137c478bdstevel@tonic-gate		errno = save_errno;
4147c478bdstevel@tonic-gate		return;
4157c478bdstevel@tonic-gate	}
4167c478bdstevel@tonic-gate
4177c478bdstevel@tonic-gate	/*
4187c478bdstevel@tonic-gate	 * check for what is non-SEEKABLE
4197c478bdstevel@tonic-gate	 * otherwise assume it's SEEKABLE so we get the old
4207c478bdstevel@tonic-gate	 * behaviour and flush the stream
4217c478bdstevel@tonic-gate	 */
4227c478bdstevel@tonic-gate
4237c478bdstevel@tonic-gate	if (S_ISFIFO(fstatbuf.st_mode) || S_ISCHR(fstatbuf.st_mode) ||
4247c478bdstevel@tonic-gate	    S_ISSOCK(fstatbuf.st_mode) || S_ISDOOR(fstatbuf.st_mode)) {
4257c478bdstevel@tonic-gate		CLEAR_SEEKABLE(iop);
4267c478bdstevel@tonic-gate	} else {
4277c478bdstevel@tonic-gate		SET_SEEKABLE(iop);
4287c478bdstevel@tonic-gate	}
4297c478bdstevel@tonic-gate
4307c478bdstevel@tonic-gate	errno = save_errno;
4317c478bdstevel@tonic-gate}
4327c478bdstevel@tonic-gate
4337c478bdstevel@tonic-gate#ifdef	_LP64
4347c478bdstevel@tonic-gatevoid
4357c478bdstevel@tonic-gate_setbufend(FILE *iop, Uchar *end)	/* set the end pointer for this iop */
4367c478bdstevel@tonic-gate{
4377c478bdstevel@tonic-gate	iop->_end = end;
4387c478bdstevel@tonic-gate
4397c478bdstevel@tonic-gate	isseekable(iop);
4407c478bdstevel@tonic-gate}
4417c478bdstevel@tonic-gate
4427c478bdstevel@tonic-gate#undef _realbufend
4437c478bdstevel@tonic-gate
4447c478bdstevel@tonic-gateUchar *
4457c478bdstevel@tonic-gate_realbufend(FILE *iop)		/* get the end pointer for this iop */
4467c478bdstevel@tonic-gate{
4477c478bdstevel@tonic-gate	return (iop->_end);
4487c478bdstevel@tonic-gate}
4497c478bdstevel@tonic-gate
4507c478bdstevel@tonic-gate#else /* _LP64 */
4517c478bdstevel@tonic-gate
4527c478bdstevel@tonic-gate/*
4537c478bdstevel@tonic-gate * Awkward functions not needed for the sane 64 bit environment.
4547c478bdstevel@tonic-gate */
4557c478bdstevel@tonic-gate/*
4567c478bdstevel@tonic-gate * xmagic must not be aligned on a 4K boundary. We guarantee this in
4577c478bdstevel@tonic-gate * _findiop().
4587c478bdstevel@tonic-gate */
4597c478bdstevel@tonic-gate#define	VALIDXFILE(xfp) \
4607c478bdstevel@tonic-gate	(((uintptr_t)&(xfp)->xmagic & 0xfff) && \
4617c478bdstevel@tonic-gate	    (xfp)->xmagic == XMAGIC(FILEx(xfp)))
4627c478bdstevel@tonic-gate
4637c478bdstevel@tonic-gatestatic struct xFILEdata *
4647c478bdstevel@tonic-gategetxfdat(FILE *iop)
4657c478bdstevel@tonic-gate{
4667c478bdstevel@tonic-gate	if (STDIOP(iop))
4677c478bdstevel@tonic-gate		return (&_xftab[IOPIND(iop)]);
4687c478bdstevel@tonic-gate	else if (VALIDXFILE(FILEx(iop)))
4697c478bdstevel@tonic-gate		return (&FILEx(iop)->_xdat);
4707c478bdstevel@tonic-gate	else
4717c478bdstevel@tonic-gate		return (NULL);
4727c478bdstevel@tonic-gate}
4737c478bdstevel@tonic-gate
4747c478bdstevel@tonic-gatevoid
4757c478bdstevel@tonic-gate_setbufend(FILE *iop, Uchar *end)	/* set the end pointer for this iop */
4767c478bdstevel@tonic-gate{
4777c478bdstevel@tonic-gate	struct xFILEdata *dat = getxfdat(iop);
4787c478bdstevel@tonic-gate
4797c478bdstevel@tonic-gate	if (dat != NULL)
4807c478bdstevel@tonic-gate		dat->_end = end;
4817c478bdstevel@tonic-gate
4827c478bdstevel@tonic-gate	isseekable(iop);
4837c478bdstevel@tonic-gate
4847c478bdstevel@tonic-gate	/*
4857c478bdstevel@tonic-gate	 * For binary compatibility with user programs using the
4867c478bdstevel@tonic-gate	 * old _bufend macro.  This is *so* broken, fileno()
4877c478bdstevel@tonic-gate	 * is not the proper index.
4887c478bdstevel@tonic-gate	 */
489a5f6978craigm	if (iop->_magic < _NFILE)
490a5f6978craigm		_bufendtab[iop->_magic] = end;
4917c478bdstevel@tonic-gate
4927c478bdstevel@tonic-gate}
4937c478bdstevel@tonic-gate
4947c478bdstevel@tonic-gateUchar *
4957c478bdstevel@tonic-gate_realbufend(FILE *iop)		/* get the end pointer for this iop */
4967c478bdstevel@tonic-gate{
4977c478bdstevel@tonic-gate	struct xFILEdata *dat = getxfdat(iop);
4987c478bdstevel@tonic-gate
4997c478bdstevel@tonic-gate	if (dat != NULL)
5007c478bdstevel@tonic-gate		return (dat->_end);
5017c478bdstevel@tonic-gate
5027c478bdstevel@tonic-gate	return (NULL);
5037c478bdstevel@tonic-gate}
5047c478bdstevel@tonic-gate
5057c478bdstevel@tonic-gate/*
5067c478bdstevel@tonic-gate * _reallock() is invoked in each stdio call through the IOB_LCK() macro,
5077c478bdstevel@tonic-gate * it is therefor extremely performance sensitive.  We get better performance
5087c478bdstevel@tonic-gate * by inlining the STDIOP check in IOB_LCK and inlining a custom version
5097c478bdstevel@tonic-gate * of getfxdat() here.
5107c478bdstevel@tonic-gate */
5117c478bdstevel@tonic-gatermutex_t *
5127c478bdstevel@tonic-gate_reallock(FILE *iop)
5137c478bdstevel@tonic-gate{
5147c478bdstevel@tonic-gate	if (VALIDXFILE(FILEx(iop)))
5157c478bdstevel@tonic-gate		return (&FILEx(iop)->xlock);
5167c478bdstevel@tonic-gate
5177c478bdstevel@tonic-gate	return (NULL);
5187c478bdstevel@tonic-gate}
5197c478bdstevel@tonic-gate
5207c478bdstevel@tonic-gate#endif	/*	_LP64	*/
5217c478bdstevel@tonic-gate
5227c478bdstevel@tonic-gate/* make sure _cnt, _ptr are correct */
5237c478bdstevel@tonic-gatevoid
5247c478bdstevel@tonic-gate_bufsync(FILE *iop, Uchar *bufend)
5257c478bdstevel@tonic-gate{
5267c478bdstevel@tonic-gate	ssize_t spaceleft;
5277c478bdstevel@tonic-gate
5287c478bdstevel@tonic-gate	spaceleft = bufend - iop->_ptr;
5297c478bdstevel@tonic-gate	if (bufend < iop->_ptr) {
5307c478bdstevel@tonic-gate		iop->_ptr = bufend;
5317c478bdstevel@tonic-gate		iop->_cnt = 0;
5327c478bdstevel@tonic-gate	} else if (spaceleft < iop->_cnt)
5337c478bdstevel@tonic-gate		iop->_cnt = spaceleft;
5347c478bdstevel@tonic-gate}
5357c478bdstevel@tonic-gate
5367c478bdstevel@tonic-gate/* really write out current buffer contents */
5377c478bdstevel@tonic-gateint
5387c478bdstevel@tonic-gate_xflsbuf(FILE *iop)
5397c478bdstevel@tonic-gate{
5407c478bdstevel@tonic-gate	ssize_t n;
5417c478bdstevel@tonic-gate	Uchar *base = iop->_base;
5427c478bdstevel@tonic-gate	Uchar *bufend;
5437c478bdstevel@tonic-gate	ssize_t num_wrote;
5447c478bdstevel@tonic-gate
5457c478bdstevel@tonic-gate	/*
5467c478bdstevel@tonic-gate	 * Hopefully, be stable with respect to interrupts...
5477c478bdstevel@tonic-gate	 */
5487c478bdstevel@tonic-gate	n = iop->_ptr - base;
5497c478bdstevel@tonic-gate	iop->_ptr = base;
5507c478bdstevel@tonic-gate	bufend = _bufend(iop);
5517c478bdstevel@tonic-gate	if (iop->_flag & (_IOLBF | _IONBF))
5527c478bdstevel@tonic-gate		iop->_cnt = 0;		/* always go to a flush */
5537c478bdstevel@tonic-gate	else
5547c478bdstevel@tonic-gate		iop->_cnt = bufend - base;
5557c478bdstevel@tonic-gate
5567c478bdstevel@tonic-gate	if (_needsync(iop, bufend))	/* recover from interrupts */
5577c478bdstevel@tonic-gate		_bufsync(iop, bufend);
5587c478bdstevel@tonic-gate
5597c478bdstevel@tonic-gate	if (n > 0) {
560cd62a92Robert Mustacchi		while ((num_wrote = _xwrite(iop, base, (size_t)n)) != n) {
5617c478bdstevel@tonic-gate			if (num_wrote <= 0) {
562a574db8raf				if (!cancel_active())
563a574db8raf					iop->_flag |= _IOERR;
5647c478bdstevel@tonic-gate				return (EOF);
5657c478bdstevel@tonic-gate			}
5667c478bdstevel@tonic-gate			n -= num_wrote;
5677c478bdstevel@tonic-gate			base += num_wrote;
5687c478bdstevel@tonic-gate		}
5697c478bdstevel@tonic-gate	}
5707c478bdstevel@tonic-gate	return (0);
5717c478bdstevel@tonic-gate}
5727c478bdstevel@tonic-gate
5737c478bdstevel@tonic-gate/* flush (write) buffer */
5747c478bdstevel@tonic-gateint
5757c478bdstevel@tonic-gatefflush(FILE *iop)
5767c478bdstevel@tonic-gate{
5777c478bdstevel@tonic-gate	int res;
5787c478bdstevel@tonic-gate	rmutex_t *lk;
5797c478bdstevel@tonic-gate
5807c478bdstevel@tonic-gate	if (iop) {
5817c478bdstevel@tonic-gate		FLOCKFILE(lk, iop);
5827c478bdstevel@tonic-gate		res = _fflush_u(iop);
5837c478bdstevel@tonic-gate		FUNLOCKFILE(lk);
5847c478bdstevel@tonic-gate	} else {
585a5f6978craigm		res = _fflush_l_iops();		/* flush all iops */
5867c478bdstevel@tonic-gate	}
5877c478bdstevel@tonic-gate	return (res);
5887c478bdstevel@tonic-gate}
5897c478bdstevel@tonic-gate
5907c478bdstevel@tonic-gatestatic int
591a5f6978craigm_fflush_l_iops(void)		/* flush all buffers */
5927c478bdstevel@tonic-gate{
5937c478bdstevel@tonic-gate	FPDECL(iop);
5947c478bdstevel@tonic-gate
5957c478bdstevel@tonic-gate	int i;
5967c478bdstevel@tonic-gate	struct _link_ *lp;
5977c478bdstevel@tonic-gate	int res = 0;
598a5f6978craigm	rmutex_t *lk;
599a5f6978craigm	/* Allow the compiler to optimize the load out of the loop */
600a5f6978craigm	int threaded = __libc_threaded;
6017c478bdstevel@tonic-gate
602a5f6978craigm	if (threaded)
603a574db8raf		cancel_safe_mutex_lock(&_first_link_lock);
6047c478bdstevel@tonic-gate
6057c478bdstevel@tonic-gate	lp = &__first_link;
6067c478bdstevel@tonic-gate
6077c478bdstevel@tonic-gate	do {
6087c478bdstevel@tonic-gate		/*
609a5f6978craigm		 * We need to grab the file locks or file corruption
610a5f6978craigm		 * will happen.  But we first check the flags field
611a5f6978craigm		 * knowing that when it is 0, it isn't allocated and
612a5f6978craigm		 * cannot be allocated while we're holding the
613a5f6978craigm		 * _first_link_lock.  And when _IONBF is set (also the
614cd62a92Robert Mustacchi		 * case when _flag is 0377 -- _DEF_FLAG_MASK, or alloc in
615cd62a92Robert Mustacchi		 * progress), we also ignore it.
616a5f6978craigm		 *
617a5f6978craigm		 * Ignore locked streams; it will appear as if
618a5f6978craigm		 * concurrent updates happened after fflush(NULL).  Note
619a5f6978craigm		 * that we even attempt to lock if the locking is set to
620a5f6978craigm		 * "by caller".  We don't want to penalize callers of
621a5f6978craigm		 * __fsetlocking() by not flushing their files.  Note: if
622a5f6978craigm		 * __fsetlocking() callers don't employ any locking, they
623a5f6978craigm		 * may still face corruption in fflush(NULL); but that's
624a5f6978craigm		 * no change from earlier releases.
6257c478bdstevel@tonic-gate		 */
6267c478bdstevel@tonic-gate		FIRSTFP(lp, iop);
6277c478bdstevel@tonic-gate		for (i = lp->niob; --i >= 0; NEXTFP(iop)) {
628a5f6978craigm			unsigned int flag = iop->_flag;
629a5f6978craigm
630a5f6978craigm			/* flag 0, flag 0377, or _IONBF set */
631a5f6978craigm			if (flag == 0 || (flag & _IONBF) != 0)
632a5f6978craigm				continue;
633a5f6978craigm
634a5f6978craigm			if (threaded) {
635a5f6978craigm				lk = FPLOCK(iop);
636a574db8raf				if (cancel_safe_mutex_trylock(lk) != 0)
637a5f6978craigm					continue;
638a5f6978craigm			}
639a5f6978craigm
640a5f6978craigm			if (!(iop->_flag & _IONBF)) {
6417c478bdstevel@tonic-gate				/*
642a5f6978craigm				 * don't need to worry about the _IORW case
643a5f6978craigm				 * since the iop will also marked with _IOREAD
644a5f6978craigm				 * or _IOWRT whichever we are really doing
6457c478bdstevel@tonic-gate				 */
646a5f6978craigm				if (iop->_flag & _IOWRT) {
647a5f6978craigm					/* Flush write buffers */
648a5f6978craigm					res |= _fflush_u(iop);
649a5f6978craigm				} else if (iop->_flag & _IOREAD) {
650a5f6978craigm					/*
651a5f6978craigm					 * flush seekable read buffers
652a5f6978craigm					 * don't flush non-seekable read buffers
653a5f6978craigm					 */
654a5f6978craigm					if (GET_SEEKABLE(iop)) {
655a5f6978craigm						res |= _fflush_u(iop);
656a5f6978craigm					}
657a5f6978craigm				}
6587c478bdstevel@tonic-gate			}
659a5f6978craigm			if (threaded)
660a574db8raf				cancel_safe_mutex_unlock(lk);
6617c478bdstevel@tonic-gate		}
6627c478bdstevel@tonic-gate	} while ((lp = lp->next) != NULL);
663a5f6978craigm	if (threaded)
664a574db8raf		cancel_safe_mutex_unlock(&_first_link_lock);
6657c478bdstevel@tonic-gate	return (res);
6667c478bdstevel@tonic-gate}
6677c478bdstevel@tonic-gate
6687c478bdstevel@tonic-gate/* flush buffer */
6697c478bdstevel@tonic-gateint
6707c478bdstevel@tonic-gate_fflush_u(FILE *iop)
6717c478bdstevel@tonic-gate{
6727c478bdstevel@tonic-gate	int res = 0;
6737c478bdstevel@tonic-gate
6747c478bdstevel@tonic-gate	/* this portion is always assumed locked */
6757c478bdstevel@tonic-gate	if (!(iop->_flag & _IOWRT)) {
676cd62a92Robert Mustacchi		(void) _xseek64(iop, -iop->_cnt, SEEK_CUR);
6777c478bdstevel@tonic-gate		iop->_cnt = 0;
6787c478bdstevel@tonic-gate		/* needed for ungetc & multibyte pushbacks */
6797c478bdstevel@tonic-gate		iop->_ptr = iop->_base;
6807c478bdstevel@tonic-gate		if (iop->_flag & _IORW) {
6817c478bdstevel@tonic-gate			iop->_flag &= ~_IOREAD;
6827c478bdstevel@tonic-gate		}
6837c478bdstevel@tonic-gate		return (0);
6847c478bdstevel@tonic-gate	}
6857c478bdstevel@tonic-gate	if (iop->_base != NULL && iop->_ptr > iop->_base) {
6867c478bdstevel@tonic-gate		res = _xflsbuf(iop);
6877c478bdstevel@tonic-gate	}
6887c478bdstevel@tonic-gate	if (iop->_flag & _IORW) {
6897c478bdstevel@tonic-gate		iop->_flag &= ~_IOWRT;
6907c478bdstevel@tonic-gate		iop->_cnt = 0;
6917c478bdstevel@tonic-gate	}
6927c478bdstevel@tonic-gate	return (res);
6937c478bdstevel@tonic-gate}
6947c478bdstevel@tonic-gate
6957c478bdstevel@tonic-gate/* flush buffer and close stream */
6967c478bdstevel@tonic-gateint
6977c478bdstevel@tonic-gatefclose(FILE *iop)
6987c478bdstevel@tonic-gate{
6997c478bdstevel@tonic-gate	int res = 0;
7007c478bdstevel@tonic-gate	rmutex_t *lk;
7017c478bdstevel@tonic-gate
7027c478bdstevel@tonic-gate	if (iop == NULL) {
7037c478bdstevel@tonic-gate		return (EOF);		/* avoid passing zero to FLOCKFILE */
7047c478bdstevel@tonic-gate	}
7057c478bdstevel@tonic-gate
7067c478bdstevel@tonic-gate	FLOCKFILE(lk, iop);
7077c478bdstevel@tonic-gate	if (iop->_flag == 0) {
7087c478bdstevel@tonic-gate		FUNLOCKFILE(lk);
7097c478bdstevel@tonic-gate		return (EOF);
7107c478bdstevel@tonic-gate	}
7117c478bdstevel@tonic-gate	/* Is not unbuffered and opened for read and/or write ? */
7127c478bdstevel@tonic-gate	if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
7137c478bdstevel@tonic-gate		res = _fflush_u(iop);
714cd62a92Robert Mustacchi	if (_xclose(iop) < 0)
7157c478bdstevel@tonic-gate		res = EOF;
7167c478bdstevel@tonic-gate	if (iop->_flag & _IOMYBUF) {
7177c478bdstevel@tonic-gate		(void) free((char *)iop->_base - PUSHBACK);
7187c478bdstevel@tonic-gate	}
7197c478bdstevel@tonic-gate	iop->_base = NULL;
7207c478bdstevel@tonic-gate	iop->_ptr = NULL;
7217c478bdstevel@tonic-gate	iop->_cnt = 0;
7227c478bdstevel@tonic-gate	iop->_flag = 0;			/* marks it as available */
7237c478bdstevel@tonic-gate	FUNLOCKFILE(lk);
7247c478bdstevel@tonic-gate
7251d2738araf	if (__libc_threaded)
726a574db8raf		cancel_safe_mutex_lock(&_first_link_lock);
7277c478bdstevel@tonic-gate	fcloses++;
7281d2738araf	if (__libc_threaded)
729a574db8raf		cancel_safe_mutex_unlock(&_first_link_lock);
7307c478bdstevel@tonic-gate
7317c478bdstevel@tonic-gate	return (res);
7327c478bdstevel@tonic-gate}
7337c478bdstevel@tonic-gate
73423a1cceRoger A. Faulkner/* close all open streams */
73523a1cceRoger A. Faulknerint
73623a1cceRoger A. Faulknerfcloseall(void)
73723a1cceRoger A. Faulkner{
73823a1cceRoger A. Faulkner	FPDECL(iop);
73923a1cceRoger A. Faulkner
74023a1cceRoger A. Faulkner	struct _link_ *lp;
74123a1cceRoger A. Faulkner	rmutex_t *lk;
74223a1cceRoger A. Faulkner
74323a1cceRoger A. Faulkner	if (__libc_threaded)
74423a1cceRoger A. Faulkner		cancel_safe_mutex_lock(&_first_link_lock);
74523a1cceRoger A. Faulkner
74623a1cceRoger A. Faulkner	lp = &__first_link;
74723a1cceRoger A. Faulkner
74823a1cceRoger A. Faulkner	do {
74923a1cceRoger A. Faulkner		int i;
75023a1cceRoger A. Faulkner
75123a1cceRoger A. Faulkner		FIRSTFP(lp, iop);
75223a1cceRoger A. Faulkner		for (i = lp->niob; --i >= 0; NEXTFP(iop)) {
75323a1cceRoger A. Faulkner			/* code stolen from fclose(), above */
75423a1cceRoger A. Faulkner
75523a1cceRoger A. Faulkner			FLOCKFILE(lk, iop);
75623a1cceRoger A. Faulkner			if (iop->_flag == 0) {
75723a1cceRoger A. Faulkner				FUNLOCKFILE(lk);
75823a1cceRoger A. Faulkner				continue;
75923a1cceRoger A. Faulkner			}
76023a1cceRoger A. Faulkner
76123a1cceRoger A. Faulkner			/* Not unbuffered and opened for read and/or write? */
76223a1cceRoger A. Faulkner			if (!(iop->_flag & _IONBF) &&
76323a1cceRoger A. Faulkner			    (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
76423a1cceRoger A. Faulkner				(void) _fflush_u(iop);
765cd62a92Robert Mustacchi			(void) _xclose(iop);
76623a1cceRoger A. Faulkner			if (iop->_flag & _IOMYBUF)
76723a1cceRoger A. Faulkner				free((char *)iop->_base - PUSHBACK);
76823a1cceRoger A. Faulkner			iop->_base = NULL;
76923a1cceRoger A. Faulkner			iop->_ptr = NULL;
77023a1cceRoger A. Faulkner			iop->_cnt = 0;
77123a1cceRoger A. Faulkner			iop->_flag = 0;		/* marks it as available */
77223a1cceRoger A. Faulkner			FUNLOCKFILE(lk);
77323a1cceRoger A. Faulkner			fcloses++;
77423a1cceRoger A. Faulkner		}
77523a1cceRoger A. Faulkner	} while ((lp = lp->next) != NULL);
77623a1cceRoger A. Faulkner
77723a1cceRoger A. Faulkner	if (__libc_threaded)
77823a1cceRoger A. Faulkner		cancel_safe_mutex_unlock(&_first_link_lock);
77923a1cceRoger A. Faulkner
78023a1cceRoger A. Faulkner	return (0);
78123a1cceRoger A. Faulkner}
78223a1cceRoger A. Faulkner
7837c478bdstevel@tonic-gate/* flush buffer, close fd but keep the stream used by freopen() */
7847c478bdstevel@tonic-gateint
7857c478bdstevel@tonic-gateclose_fd(FILE *iop)
7867c478bdstevel@tonic-gate{
7877c478bdstevel@tonic-gate	int res = 0;
7887c478bdstevel@tonic-gate	mbstate_t *mb;
7897c478bdstevel@tonic-gate
7907c478bdstevel@tonic-gate	if (iop == NULL || iop->_flag == 0)
7917c478bdstevel@tonic-gate		return (EOF);
7927c478bdstevel@tonic-gate	/* Is not unbuffered and opened for read and/or write ? */
7937c478bdstevel@tonic-gate	if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
7947c478bdstevel@tonic-gate		res = _fflush_u(iop);
795cd62a92Robert Mustacchi	if (_xclose(iop) < 0)
7967c478bdstevel@tonic-gate		res = EOF;
7977c478bdstevel@tonic-gate	if (iop->_flag & _IOMYBUF) {
7987c478bdstevel@tonic-gate		(void) free((char *)iop->_base - PUSHBACK);
7997c478bdstevel@tonic-gate	}
8007c478bdstevel@tonic-gate	iop->_base = NULL;
8017c478bdstevel@tonic-gate	iop->_ptr = NULL;
8027c478bdstevel@tonic-gate	mb = _getmbstate(iop);
8037c478bdstevel@tonic-gate	if (mb != NULL)
8047c478bdstevel@tonic-gate		(void) memset(mb, 0, sizeof (mbstate_t));
8057c478bdstevel@tonic-gate	iop->_cnt = 0;
8067c478bdstevel@tonic-gate	_setorientation(iop, _NO_MODE);
8077c478bdstevel@tonic-gate	return (res);
8087c478bdstevel@tonic-gate}
8097c478bdstevel@tonic-gate
8107c478bdstevel@tonic-gatestatic FILE *
8117c478bdstevel@tonic-gategetiop(FILE *fp, rmutex_t *lk, mbstate_t *mb)
8127c478bdstevel@tonic-gate{
813a574db8raf	if (lk != NULL && cancel_safe_mutex_trylock(lk) != 0)
8147c478bdstevel@tonic-gate		return (NULL);	/* locked: fp in use */
8157c478bdstevel@tonic-gate
8167c478bdstevel@tonic-gate	if (fp->_flag == 0) {	/* unused */
8177c478bdstevel@tonic-gate#ifndef	_LP64
8187c478bdstevel@tonic-gate		fp->__orientation = 0;
8197c478bdstevel@tonic-gate#endif /* _LP64 */
8207c478bdstevel@tonic-gate		fp->_cnt = 0;
8217c478bdstevel@tonic-gate		fp->_ptr = NULL;
8227c478bdstevel@tonic-gate		fp->_base = NULL;
823cd62a92Robert Mustacchi		/* claim the fp by setting low 8 bits */
824cd62a92Robert Mustacchi		fp->_flag = _DEF_FLAG_MASK;
8257c478bdstevel@tonic-gate		(void) memset(mb, 0, sizeof (mbstate_t));
8267c478bdstevel@tonic-gate		FUNLOCKFILE(lk);
8277c478bdstevel@tonic-gate		return (fp);
8287c478bdstevel@tonic-gate	}
8297c478bdstevel@tonic-gate	FUNLOCKFILE(lk);
8307c478bdstevel@tonic-gate	return (NULL);
8317c478bdstevel@tonic-gate}
8327c478bdstevel@tonic-gate
8337c478bdstevel@tonic-gate#ifndef	_LP64
8347c478bdstevel@tonic-gate/*
8357c478bdstevel@tonic-gate * DESCRIPTION:
8367c478bdstevel@tonic-gate * This function gets the pointer to the mbstate_t structure associated
8377c478bdstevel@tonic-gate * with the specified iop.
8387c478bdstevel@tonic-gate *
8397c478bdstevel@tonic-gate * RETURNS:
8407c478bdstevel@tonic-gate * If the associated mbstate_t found, the pointer to the mbstate_t is
8417c478bdstevel@tonic-gate * returned.  Otherwise, NULL is returned.
8427c478bdstevel@tonic-gate */
8437c478bdstevel@tonic-gatembstate_t *
8447c478bdstevel@tonic-gate_getmbstate(FILE *iop)
8457c478bdstevel@tonic-gate{
8467c478bdstevel@tonic-gate	struct xFILEdata *dat = getxfdat(iop);
8477c478bdstevel@tonic-gate
8487c478bdstevel@tonic-gate	if (dat != NULL)
8497c478bdstevel@tonic-gate		return (&dat->_state);
8507c478bdstevel@tonic-gate
8517c478bdstevel@tonic-gate	return (NULL);
8527c478bdstevel@tonic-gate}
853a5f6978craigm
854a5f6978craigm/*
855a5f6978craigm * More 32-bit only functions.
856a5f6978craigm * They lookup/set large fd's for extended FILE support.
857a5f6978craigm */
858a5f6978craigm
859a5f6978craigm/*
860a5f6978craigm * The negative value indicates that Extended fd FILE's has not
861a5f6978craigm * been enabled by the user.
862a5f6978craigm */
863a5f6978craigmstatic int bad_fd = -1;
864a5f6978craigm
865a5f6978craigmint
866a5f6978craigm_file_get(FILE *iop)
867a5f6978craigm{
868a5f6978craigm	int altfd;
869a5f6978craigm
870a5f6978craigm	/*
871a5f6978craigm	 * Failure indicates a FILE * not allocated through stdio;
872a5f6978craigm	 * it means the flag values are probably bogus and that if
873a5f6978craigm	 * a file descriptor is set, it's in _magic.
874a5f6978craigm	 * Inline getxfdat() for performance reasons.
875a5f6978craigm	 */
876a5f6978craigm	if (STDIOP(iop))
877a5f6978craigm		altfd = _xftab[IOPIND(iop)]._altfd;
878a5f6978craigm	else if (VALIDXFILE(FILEx(iop)))
879a5f6978craigm		altfd = FILEx(iop)->_xdat._altfd;
880a5f6978craigm	else
881a5f6978craigm		return (iop->_magic);
882a5f6978craigm	/*
883a5f6978craigm	 * if this is not an internal extended FILE then check
884a5f6978craigm	 * if _file is being changed from underneath us.
885a5f6978craigm	 * It should not be because if
886a5f6978craigm	 * it is then then we lose our ability to guard against
887a5f6978craigm	 * silent data corruption.
888a5f6978craigm	 */
889a5f6978craigm	if (!iop->__xf_nocheck && bad_fd > -1 && iop->_magic != bad_fd) {
890e1d65dbcraigm		(void) fprintf(stderr,
891a5f6978craigm		    "Application violated extended FILE safety mechanism.\n"
892e1d65dbcraigm		    "Please read the man page for extendedFILE.\nAborting\n");
893a5f6978craigm		abort();
894a5f6978craigm	}
895a5f6978craigm	return (altfd);
896a5f6978craigm}
897a5f6978craigm
898a5f6978craigmint
899a5f6978craigm_file_set(FILE *iop, int fd, const char *type)
900a5f6978craigm{
901a5f6978craigm	struct xFILEdata *dat;
902a5f6978craigm	int Fflag;
903a5f6978craigm
904a5f6978craigm	/* Already known to contain at least one byte */
905a5f6978craigm	while (*++type != '\0')
906a5f6978craigm		;
907a5f6978craigm
908a5f6978craigm	Fflag = type[-1] == 'F';
909a5f6978craigm	if (!Fflag && bad_fd < 0) {
910a5f6978craigm		errno = EMFILE;
911a5f6978craigm		return (-1);
912a5f6978craigm	}
913a5f6978craigm
914a5f6978craigm	dat = getxfdat(iop);
915a5f6978craigm	iop->__extendedfd = 1;
916a5f6978craigm	iop->__xf_nocheck = Fflag;
917a5f6978craigm	dat->_altfd = fd;
918a5f6978craigm	iop->_magic = (unsigned char)bad_fd;
919a5f6978craigm	return (0);
920a5f6978craigm}
921a5f6978craigm
922a5f6978craigm/*
923a5f6978craigm * Activates extended fd's in FILE's
924a5f6978craigm */
925a5f6978craigm
926a5f6978craigmstatic const int tries[] = {196, 120, 60, 3};
927a5f6978craigm#define	NTRIES	(sizeof (tries)/sizeof (int))
928a5f6978craigm
929a5f6978craigmint
930a5f6978craigmenable_extended_FILE_stdio(int fd, int action)
931a5f6978craigm{
932a5f6978craigm	int i;
933a5f6978craigm
934a5f6978craigm	if (action < 0)
935a5f6978craigm		action = SIGABRT;	/* default signal */
936a5f6978craigm
937a5f6978craigm	if (fd < 0) {
938a5f6978craigm		/*
939a5f6978craigm		 * search for an available fd and make it the badfd
940a5f6978craigm		 */
941a5f6978craigm		for (i = 0; i < NTRIES; i++) {
942a5f6978craigm			fd = fcntl(tries[i], F_BADFD, action);
943a5f6978craigm			if (fd >= 0)
944a5f6978craigm				break;
945a5f6978craigm		}
946a5f6978craigm		if (fd < 0)	/* failed to find an available fd */
947a5f6978craigm			return (-1);
948a5f6978craigm	} else {
949a5f6978craigm		/* caller requests that fd be the chosen badfd */
950a5f6978craigm		int nfd = fcntl(fd, F_BADFD, action);
951a5f6978craigm		if (nfd < 0 || nfd != fd)
952a5f6978craigm			return (-1);
953a5f6978craigm	}
954a5f6978craigm	bad_fd = fd;
955a5f6978craigm	return (0);
956a5f6978craigm}
9577c478bdstevel@tonic-gate#endif
958cd62a92Robert Mustacchi
959cd62a92Robert Mustacchi/*
960cd62a92Robert Mustacchi * Wrappers around the various system calls that stdio needs to make on a file
961cd62a92Robert Mustacchi * descriptor.
962cd62a92Robert Mustacchi */
963cd62a92Robert Mustacchistatic stdio_ops_t *
964cd62a92Robert Mustacchiget_stdops(FILE *iop)
965cd62a92Robert Mustacchi{
966cd62a92Robert Mustacchi#ifdef	_LP64
967cd62a92Robert Mustacchi	return (iop->_ops);
968cd62a92Robert Mustacchi#else
969cd62a92Robert Mustacchi	struct xFILEdata *dat = getxfdat(iop);
970cd62a92Robert Mustacchi	return (dat->_ops);
971cd62a92Robert Mustacchi#endif
972cd62a92Robert Mustacchi}
973cd62a92Robert Mustacchi
974cd62a92Robert Mustacchistatic void
975cd62a92Robert Mustacchiset_stdops(FILE *iop, stdio_ops_t *ops)
976cd62a92Robert Mustacchi{
977cd62a92Robert Mustacchi#ifdef	_LP64
978cd62a92Robert Mustacchi	ASSERT3P(iop->_ops, ==, NULL);
979cd62a92Robert Mustacchi	iop->_ops = ops;
980cd62a92Robert Mustacchi#else
981cd62a92Robert Mustacchi	struct xFILEdata *dat = getxfdat(iop);
982cd62a92Robert Mustacchi	ASSERT3P(dat->_ops, ==, NULL);
983cd62a92Robert Mustacchi	dat->_ops = ops;
984cd62a92Robert Mustacchi#endif
985cd62a92Robert Mustacchi
986cd62a92Robert Mustacchi}
987cd62a92Robert Mustacchi
988cd62a92Robert Mustacchissize_t
989cd62a92Robert Mustacchi_xread(FILE *iop, void *buf, size_t nbytes)
990cd62a92Robert Mustacchi{
991cd62a92Robert Mustacchi	stdio_ops_t *ops = get_stdops(iop);
992cd62a92Robert Mustacchi	if (ops != NULL) {
993cd62a92Robert Mustacchi		return (ops->std_read(iop, buf, nbytes));
994cd62a92Robert Mustacchi	}
995cd62a92Robert Mustacchi
996cd62a92Robert Mustacchi	return (read(_get_fd(iop), buf, nbytes));
997cd62a92Robert Mustacchi}
998cd62a92Robert Mustacchi
999cd62a92Robert Mustacchissize_t
1000cd62a92Robert Mustacchi_xwrite(FILE *iop, const void *buf, size_t nbytes)
1001cd62a92Robert Mustacchi{
1002cd62a92Robert Mustacchi	stdio_ops_t *ops = get_stdops(iop);
1003cd62a92Robert Mustacchi	if (ops != NULL) {
1004cd62a92Robert Mustacchi		return (ops->std_write(iop, buf, nbytes));
1005cd62a92Robert Mustacchi	}
1006cd62a92Robert Mustacchi	return (write(_get_fd(iop), buf, nbytes));
1007cd62a92Robert Mustacchi}
1008cd62a92Robert Mustacchi
1009cd62a92Robert Mustacchioff_t
1010cd62a92Robert Mustacchi_xseek(FILE *iop, off_t off, int whence)
1011cd62a92Robert Mustacchi{
1012cd62a92Robert Mustacchi	stdio_ops_t *ops = get_stdops(iop);
1013cd62a92Robert Mustacchi	if (ops != NULL) {
1014cd62a92Robert Mustacchi		return (ops->std_seek(iop, off, whence));
1015cd62a92Robert Mustacchi	}
1016cd62a92Robert Mustacchi
1017cd62a92Robert Mustacchi	return (lseek(_get_fd(iop), off, whence));
1018cd62a92Robert Mustacchi}
1019cd62a92Robert Mustacchi
1020cd62a92Robert Mustacchioff64_t
1021cd62a92Robert Mustacchi_xseek64(FILE *iop, off64_t off, int whence)
1022cd62a92Robert Mustacchi{
1023cd62a92Robert Mustacchi	stdio_ops_t *ops = get_stdops(iop);
1024cd62a92Robert Mustacchi	if (ops != NULL) {
1025cd62a92Robert Mustacchi		/*
1026cd62a92Robert Mustacchi		 * The internal APIs only operate with an off_t. An off64_t in
1027cd62a92Robert Mustacchi		 * an ILP32 environment may represent a value larger than they
1028cd62a92Robert Mustacchi		 * can accept. As such, we try and catch such cases and error
1029cd62a92Robert Mustacchi		 * about it before we get there.
1030cd62a92Robert Mustacchi		 */
1031cd62a92Robert Mustacchi		if (off > LONG_MAX || off < LONG_MIN) {
1032cd62a92Robert Mustacchi			errno = EOVERFLOW;
1033cd62a92Robert Mustacchi			return (-1);
1034cd62a92Robert Mustacchi		}
1035cd62a92Robert Mustacchi		return (ops->std_seek(iop, off, whence));
1036cd62a92Robert Mustacchi	}
1037cd62a92Robert Mustacchi
1038cd62a92Robert Mustacchi	return (lseek64(_get_fd(iop), off, whence));
1039cd62a92Robert Mustacchi}
1040cd62a92Robert Mustacchi
1041cd62a92Robert Mustacchiint
1042cd62a92Robert Mustacchi_xclose(FILE *iop)
1043cd62a92Robert Mustacchi{
1044cd62a92Robert Mustacchi	stdio_ops_t *ops = get_stdops(iop);
1045cd62a92Robert Mustacchi	if (ops != NULL) {
1046cd62a92Robert Mustacchi		return (ops->std_close(iop));
1047cd62a92Robert Mustacchi	}
1048cd62a92Robert Mustacchi
1049cd62a92Robert Mustacchi	return (close(_get_fd(iop)));
1050cd62a92Robert Mustacchi}
1051cd62a92Robert Mustacchi
1052cd62a92Robert Mustacchivoid *
1053cd62a92Robert Mustacchi_xdata(FILE *iop)
1054cd62a92Robert Mustacchi{
1055cd62a92Robert Mustacchi	stdio_ops_t *ops = get_stdops(iop);
1056cd62a92Robert Mustacchi	if (ops != NULL) {
1057cd62a92Robert Mustacchi		return (ops->std_data);
1058cd62a92Robert Mustacchi	}
1059cd62a92Robert Mustacchi
1060cd62a92Robert Mustacchi	return (NULL);
1061cd62a92Robert Mustacchi}
1062cd62a92Robert Mustacchi
1063cd62a92Robert Mustacchiint
1064cd62a92Robert Mustacchi_xassoc(FILE *iop, fread_t readf, fwrite_t writef, fseek_t seekf,
1065cd62a92Robert Mustacchi    fclose_t closef, void *data)
1066cd62a92Robert Mustacchi{
1067cd62a92Robert Mustacchi	stdio_ops_t *ops = get_stdops(iop);
1068cd62a92Robert Mustacchi
1069cd62a92Robert Mustacchi	if (ops == NULL) {
1070cd62a92Robert Mustacchi		ops = malloc(sizeof (*ops));
1071cd62a92Robert Mustacchi		if (ops == NULL) {
1072cd62a92Robert Mustacchi			return (-1);
1073cd62a92Robert Mustacchi		}
1074cd62a92Robert Mustacchi		set_stdops(iop, ops);
1075cd62a92Robert Mustacchi	}
1076cd62a92Robert Mustacchi
1077cd62a92Robert Mustacchi	ops->std_read = readf;
1078cd62a92Robert Mustacchi	ops->std_write = writef;
1079cd62a92Robert Mustacchi	ops->std_seek = seekf;
1080cd62a92Robert Mustacchi	ops->std_close = closef;
1081cd62a92Robert Mustacchi	ops->std_data = data;
1082cd62a92Robert Mustacchi
1083cd62a92Robert Mustacchi	return (0);
1084cd62a92Robert Mustacchi}
1085cd62a92Robert Mustacchi
1086cd62a92Robert Mustacchivoid
1087cd62a92Robert Mustacchi_xunassoc(FILE *iop)
1088cd62a92Robert Mustacchi{
1089cd62a92Robert Mustacchi	stdio_ops_t *ops = get_stdops(iop);
1090cd62a92Robert Mustacchi	if (ops == NULL) {
1091cd62a92Robert Mustacchi		return;
1092cd62a92Robert Mustacchi	}
1093cd62a92Robert Mustacchi	set_stdops(iop, NULL);
1094cd62a92Robert Mustacchi	free(ops);
1095cd62a92Robert Mustacchi}
1096cd62a92Robert Mustacchi
1097cd62a92Robert Mustacchiint
1098cd62a92Robert Mustacchi_get_fd(FILE *iop)
1099cd62a92Robert Mustacchi{
1100cd62a92Robert Mustacchi	/*
1101cd62a92Robert Mustacchi	 * Streams with an ops vector (currently the memory stream family) do
1102cd62a92Robert Mustacchi	 * not have an underlying file descriptor that we can give back to the
1103cd62a92Robert Mustacchi	 * user. In such cases, return -1 to explicitly make sure that they'll
1104cd62a92Robert Mustacchi	 * get an ebadf from things.
1105cd62a92Robert Mustacchi	 */
1106cd62a92Robert Mustacchi	if (get_stdops(iop) != NULL) {
1107cd62a92Robert Mustacchi		return (-1);
1108cd62a92Robert Mustacchi	}
1109cd62a92Robert Mustacchi#ifdef  _LP64
1110cd62a92Robert Mustacchi	return (iop->_file);
1111cd62a92Robert Mustacchi#else
1112cd62a92Robert Mustacchi	if (iop->__extendedfd) {
1113cd62a92Robert Mustacchi		return (_file_get(iop));
1114cd62a92Robert Mustacchi	} else {
1115cd62a92Robert Mustacchi		return (iop->_magic);
1116cd62a92Robert Mustacchi	}
1117cd62a92Robert Mustacchi#endif
1118cd62a92Robert Mustacchi}
1119