10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51672Sraf * Common Development and Distribution License (the "License").
61672Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211672Sraf
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
296812Sraf #include "lint.h"
300Sstevel@tonic-gate #include "mtlib.h"
310Sstevel@tonic-gate #include "file64.h"
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <thread.h>
360Sstevel@tonic-gate #include <synch.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include "stdiom.h"
400Sstevel@tonic-gate #include <wchar.h>
410Sstevel@tonic-gate #include <sys/stat.h>
420Sstevel@tonic-gate #include <stddef.h>
430Sstevel@tonic-gate #include <errno.h>
441846Scraigm #include <fcntl.h>
450Sstevel@tonic-gate
466812Sraf #define _iob __iob
476812Sraf
480Sstevel@tonic-gate #undef end
490Sstevel@tonic-gate
500Sstevel@tonic-gate #define FILE_ARY_SZ 8 /* a nice size for FILE array & end_buffer_ptrs */
510Sstevel@tonic-gate
520Sstevel@tonic-gate #ifdef _LP64
530Sstevel@tonic-gate
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate * Macros to declare and loop over a fp or fp/xfp combo to
560Sstevel@tonic-gate * avoid some of the _LP64 ifdef hell.
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #define FPDECL(fp) FILE *fp
600Sstevel@tonic-gate #define FIRSTFP(lp, fp) fp = lp->iobp
610Sstevel@tonic-gate #define NEXTFP(fp) fp++
621846Scraigm #define FPLOCK(fp) &fp->_lock
631846Scraigm #define FPSTATE(fp) &fp->_state
640Sstevel@tonic-gate
650Sstevel@tonic-gate #define xFILE FILE
660Sstevel@tonic-gate
670Sstevel@tonic-gate #else
680Sstevel@tonic-gate
690Sstevel@tonic-gate #define FPDECL(fp) FILE *fp; xFILE *x##fp
700Sstevel@tonic-gate #define FIRSTFP(lp, fp) x##fp = lp->iobp; \
710Sstevel@tonic-gate fp = x##fp ? &x##fp->_iob : &_iob[0]
720Sstevel@tonic-gate #define NEXTFP(fp) (x##fp ? fp = &(++x##fp)->_iob : ++fp)
731846Scraigm #define FPLOCK(fp) x##fp ? \
741846Scraigm &x##fp->xlock : &_xftab[IOPIND(fp)]._lock
751846Scraigm #define FPSTATE(fp) x##fp ? \
761846Scraigm &x##fp->xstate : &_xftab[IOPIND(fp)]._state
770Sstevel@tonic-gate
780Sstevel@tonic-gate /* The extended 32-bit file structure for use in link buffers */
790Sstevel@tonic-gate typedef struct xFILE {
800Sstevel@tonic-gate FILE _iob; /* must be first! */
810Sstevel@tonic-gate struct xFILEdata _xdat;
820Sstevel@tonic-gate } xFILE;
830Sstevel@tonic-gate
840Sstevel@tonic-gate #define xmagic _xdat._magic
850Sstevel@tonic-gate #define xend _xdat._end
860Sstevel@tonic-gate #define xlock _xdat._lock
870Sstevel@tonic-gate #define xstate _xdat._state
880Sstevel@tonic-gate
890Sstevel@tonic-gate #define FILEx(fp) ((struct xFILE *)(uintptr_t)fp)
900Sstevel@tonic-gate
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate * The magic number stored is actually the pointer scrambled with
930Sstevel@tonic-gate * a magic number. Pointers to data items live everywhere in memory
940Sstevel@tonic-gate * so we scramble the pointer in order to avoid accidental collisions.
950Sstevel@tonic-gate */
960Sstevel@tonic-gate #define XFILEMAGIC 0x63687367
970Sstevel@tonic-gate #define XMAGIC(xfp) ((uintptr_t)(xfp) ^ XFILEMAGIC)
980Sstevel@tonic-gate
990Sstevel@tonic-gate #endif /* _LP64 */
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate struct _link_ /* manages a list of streams */
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate xFILE *iobp; /* the array of (x)FILE's */
1040Sstevel@tonic-gate /* NULL for the __first_link in ILP32 */
1050Sstevel@tonic-gate int niob; /* length of the arrays */
1060Sstevel@tonic-gate struct _link_ *next; /* next in the list */
1070Sstevel@tonic-gate };
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate * With dynamic linking, iob may be in either the library or in the user's
1110Sstevel@tonic-gate * a.out, so the run time linker fixes up the first entry in __first_link at
1120Sstevel@tonic-gate * process startup time.
1130Sstevel@tonic-gate *
1140Sstevel@tonic-gate * In 32 bit processes, we don't have xFILE[FILE_ARY_SZ] but FILE[],
1150Sstevel@tonic-gate * and _xftab[] instead; this is denoted by having iobp set to NULL in
1160Sstevel@tonic-gate * 32 bit mode for the first link entry.
1170Sstevel@tonic-gate */
1180Sstevel@tonic-gate struct _link_ __first_link = /* first in linked list */
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate #if !defined(_LP64)
1210Sstevel@tonic-gate NULL,
1220Sstevel@tonic-gate #else
1230Sstevel@tonic-gate &_iob[0],
1240Sstevel@tonic-gate #endif
1250Sstevel@tonic-gate _NFILE,
1260Sstevel@tonic-gate NULL
1270Sstevel@tonic-gate };
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate * Information cached to speed up searches. We remember where we
1310Sstevel@tonic-gate * last found a free FILE* and we remember whether we saw any fcloses
1320Sstevel@tonic-gate * in between. We also count the number of chunks we allocated, see
1330Sstevel@tonic-gate * _findiop() for an explanation.
1340Sstevel@tonic-gate * These variables are all protected by _first_link_lock.
1350Sstevel@tonic-gate */
1360Sstevel@tonic-gate static struct _link_ *lastlink = NULL;
1370Sstevel@tonic-gate static int fcloses;
1380Sstevel@tonic-gate static int nchunks;
1390Sstevel@tonic-gate
1401846Scraigm static mutex_t _first_link_lock = DEFAULTMUTEX;
1410Sstevel@tonic-gate
1421846Scraigm static int _fflush_l_iops(void);
1431846Scraigm static FILE *getiop(FILE *, rmutex_t *, mbstate_t *);
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate * All functions that understand the linked list of iob's follow.
1470Sstevel@tonic-gate */
1480Sstevel@tonic-gate #pragma weak _cleanup = __cleanup
1490Sstevel@tonic-gate void
__cleanup(void)1500Sstevel@tonic-gate __cleanup(void) /* called at process end to flush ouput streams */
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate (void) fflush(NULL);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate * For fork1-safety (see libc_prepare_atfork(), etc).
1570Sstevel@tonic-gate */
1580Sstevel@tonic-gate void
stdio_locks()1590Sstevel@tonic-gate stdio_locks()
1600Sstevel@tonic-gate {
1616515Sraf (void) mutex_lock(&_first_link_lock);
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate * XXX: We should acquire all of the iob locks here.
1640Sstevel@tonic-gate */
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate void
stdio_unlocks()1680Sstevel@tonic-gate stdio_unlocks()
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate * XXX: We should release all of the iob locks here.
1720Sstevel@tonic-gate */
1736515Sraf (void) mutex_unlock(&_first_link_lock);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate void
_flushlbf(void)1770Sstevel@tonic-gate _flushlbf(void) /* fflush() all line-buffered streams */
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate FPDECL(fp);
1800Sstevel@tonic-gate int i;
1810Sstevel@tonic-gate struct _link_ *lp;
1821846Scraigm /* Allow compiler to optimize the loop */
1831846Scraigm int threaded = __libc_threaded;
1840Sstevel@tonic-gate
1851846Scraigm if (threaded)
1865891Sraf cancel_safe_mutex_lock(&_first_link_lock);
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate lp = &__first_link;
1890Sstevel@tonic-gate do {
1900Sstevel@tonic-gate FIRSTFP(lp, fp);
1910Sstevel@tonic-gate for (i = lp->niob; --i >= 0; NEXTFP(fp)) {
1921846Scraigm /*
1931846Scraigm * The additional _IONBF check guards againsts
1941846Scraigm * allocated but uninitialized iops (see _findiop).
1951846Scraigm * We also automatically skip non allocated iop's.
1961846Scraigm * Don't block on locks.
1971846Scraigm */
1981846Scraigm if ((fp->_flag & (_IOLBF | _IOWRT | _IONBF)) ==
1991846Scraigm (_IOLBF | _IOWRT)) {
2001846Scraigm if (threaded) {
2011846Scraigm rmutex_t *lk = FPLOCK(fp);
2025891Sraf if (cancel_safe_mutex_trylock(lk) != 0)
2031846Scraigm continue;
2041846Scraigm /* Recheck after locking */
2051846Scraigm if ((fp->_flag & (_IOLBF | _IOWRT)) ==
2061846Scraigm (_IOLBF | _IOWRT)) {
2071846Scraigm (void) _fflush_u(fp);
2081846Scraigm }
2095891Sraf cancel_safe_mutex_unlock(lk);
2101846Scraigm } else {
2111846Scraigm (void) _fflush_u(fp);
2121846Scraigm }
2131846Scraigm }
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate } while ((lp = lp->next) != NULL);
2160Sstevel@tonic-gate
2171846Scraigm if (threaded)
2185891Sraf cancel_safe_mutex_unlock(&_first_link_lock);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate /* allocate an unused stream; NULL if cannot */
2220Sstevel@tonic-gate FILE *
_findiop(void)2230Sstevel@tonic-gate _findiop(void)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate struct _link_ *lp, **prev;
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate /* used so there only needs to be one malloc() */
2280Sstevel@tonic-gate #ifdef _LP64
2290Sstevel@tonic-gate typedef struct {
2300Sstevel@tonic-gate struct _link_ hdr;
2310Sstevel@tonic-gate FILE iob[FILE_ARY_SZ];
2320Sstevel@tonic-gate } Pkg;
2330Sstevel@tonic-gate #else
2340Sstevel@tonic-gate typedef union {
2350Sstevel@tonic-gate struct { /* Normal */
2360Sstevel@tonic-gate struct _link_ hdr;
2370Sstevel@tonic-gate xFILE iob[FILE_ARY_SZ];
2380Sstevel@tonic-gate } Pkgn;
2390Sstevel@tonic-gate struct { /* Reversed */
2400Sstevel@tonic-gate xFILE iob[FILE_ARY_SZ];
2410Sstevel@tonic-gate struct _link_ hdr;
2420Sstevel@tonic-gate } Pkgr;
2430Sstevel@tonic-gate } Pkg;
2440Sstevel@tonic-gate uintptr_t delta;
2450Sstevel@tonic-gate #endif
2460Sstevel@tonic-gate Pkg *pkgp;
2470Sstevel@tonic-gate struct _link_ *hdr;
2480Sstevel@tonic-gate FPDECL(fp);
2490Sstevel@tonic-gate int i;
2501846Scraigm int threaded = __libc_threaded;
2510Sstevel@tonic-gate
2521846Scraigm if (threaded)
2535891Sraf cancel_safe_mutex_lock(&_first_link_lock);
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate if (lastlink == NULL) {
2560Sstevel@tonic-gate rescan:
2570Sstevel@tonic-gate fcloses = 0;
2580Sstevel@tonic-gate lastlink = &__first_link;
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate lp = lastlink;
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate * lock to make testing of fp->_flag == 0 and acquiring the fp atomic
2650Sstevel@tonic-gate * and for allocation of new links
2660Sstevel@tonic-gate * low contention expected on _findiop(), hence coarse locking.
2670Sstevel@tonic-gate * for finer granularity, use fp->_lock for allocating an iop
2680Sstevel@tonic-gate * and make the testing of lp->next and allocation of new link atomic
2690Sstevel@tonic-gate * using lp->_lock
2700Sstevel@tonic-gate */
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate do {
2730Sstevel@tonic-gate prev = &lp->next;
2740Sstevel@tonic-gate FIRSTFP(lp, fp);
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate for (i = lp->niob; --i >= 0; NEXTFP(fp)) {
2771846Scraigm FILE *ret;
2781846Scraigm if (threaded) {
2791846Scraigm ret = getiop(fp, FPLOCK(fp), FPSTATE(fp));
2801846Scraigm if (ret != NULL) {
2815891Sraf cancel_safe_mutex_unlock(
2821881Scasper &_first_link_lock);
2831881Scasper return (ret);
2841846Scraigm }
2851846Scraigm } else {
2861846Scraigm ret = getiop(fp, NULL, FPSTATE(fp));
2871846Scraigm if (ret != NULL)
2881846Scraigm return (ret);
2891846Scraigm }
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate } while ((lastlink = lp = lp->next) != NULL);
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate * If there was a sufficient number of fcloses since we last started
2950Sstevel@tonic-gate * at __first_link, we rescan all fp's again. We do not rescan for
2960Sstevel@tonic-gate * all fcloses; that would simplify the algorithm but would make
2970Sstevel@tonic-gate * search times near O(n) again.
2980Sstevel@tonic-gate * Worst case behaviour would still be pretty bad (open a full set,
2990Sstevel@tonic-gate * then continously opening and closing one FILE * gets you a full
3000Sstevel@tonic-gate * scan each time). That's why we over allocate 1 FILE for each
3010Sstevel@tonic-gate * 32 chunks. More over allocation is better; this is a nice
3020Sstevel@tonic-gate * empirical value which doesn't cost a lot of memory, doesn't
3030Sstevel@tonic-gate * overallocate until we reach 256 FILE *s and keeps the performance
3040Sstevel@tonic-gate * pretty close to the optimum.
3050Sstevel@tonic-gate */
3060Sstevel@tonic-gate if (fcloses > nchunks/32)
3070Sstevel@tonic-gate goto rescan;
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate * Need to allocate another and put it in the linked list.
3110Sstevel@tonic-gate */
3120Sstevel@tonic-gate if ((pkgp = malloc(sizeof (Pkg))) == NULL) {
3131846Scraigm if (threaded)
3145891Sraf cancel_safe_mutex_unlock(&_first_link_lock);
3150Sstevel@tonic-gate return (NULL);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate (void) memset(pkgp, 0, sizeof (Pkg));
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate #ifdef _LP64
3210Sstevel@tonic-gate hdr = &pkgp->hdr;
3220Sstevel@tonic-gate hdr->iobp = &pkgp->iob[0];
3230Sstevel@tonic-gate #else
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate * The problem with referencing a word after a FILE* is the possibility
3260Sstevel@tonic-gate * of a SIGSEGV if a non-stdio issue FILE structure ends on a page
3270Sstevel@tonic-gate * boundary. We run this check so we never need to run an expensive
3280Sstevel@tonic-gate * check like mincore() in order to know whether it is
3290Sstevel@tonic-gate * safe to dereference ((xFILE*)fp)->xmagic.
3300Sstevel@tonic-gate * We allocate the block with two alternative layouts; if one
3310Sstevel@tonic-gate * layout is not properly aligned for our purposes, the other layout
3320Sstevel@tonic-gate * will be because the size of _link_ is small compared to
3330Sstevel@tonic-gate * sizeof (xFILE).
3340Sstevel@tonic-gate * The check performed is this:
3350Sstevel@tonic-gate * If the distance from pkgp to the end of the page is
3360Sstevel@tonic-gate * less than the the offset of the last xmagic field in the
3370Sstevel@tonic-gate * xFILE structure, (the 0x1000 boundary is inside our just
3380Sstevel@tonic-gate * allocated structure) and the distance modulo the size of xFILE
3390Sstevel@tonic-gate * is identical to the offset of the first xmagic in the
3400Sstevel@tonic-gate * structure (i.e., XXXXXX000 points to an xmagic field),
3410Sstevel@tonic-gate * we need to use the reverse structure.
3420Sstevel@tonic-gate */
3430Sstevel@tonic-gate if ((delta = 0x1000 - ((uintptr_t)pkgp & 0xfff)) <=
3445891Sraf offsetof(Pkg, Pkgn.iob[FILE_ARY_SZ-1].xmagic) &&
3450Sstevel@tonic-gate delta % sizeof (struct xFILE) ==
3465891Sraf offsetof(Pkg, Pkgn.iob[0].xmagic)) {
3470Sstevel@tonic-gate /* Use reversed structure */
3480Sstevel@tonic-gate hdr = &pkgp->Pkgr.hdr;
3490Sstevel@tonic-gate hdr->iobp = &pkgp->Pkgr.iob[0];
3500Sstevel@tonic-gate } else {
3510Sstevel@tonic-gate /* Use normal structure */
3520Sstevel@tonic-gate hdr = &pkgp->Pkgn.hdr;
3530Sstevel@tonic-gate hdr->iobp = &pkgp->Pkgn.iob[0];
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate #endif /* _LP64 */
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate hdr->niob = FILE_ARY_SZ;
3580Sstevel@tonic-gate nchunks++;
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate #ifdef _LP64
3610Sstevel@tonic-gate fp = hdr->iobp;
3620Sstevel@tonic-gate for (i = 0; i < FILE_ARY_SZ; i++)
3636812Sraf (void) mutex_init(&fp[i]._lock,
3646812Sraf USYNC_THREAD | LOCK_RECURSIVE, NULL);
3650Sstevel@tonic-gate #else
3660Sstevel@tonic-gate xfp = hdr->iobp;
3670Sstevel@tonic-gate fp = &xfp->_iob;
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate for (i = 0; i < FILE_ARY_SZ; i++) {
3700Sstevel@tonic-gate xfp[i].xmagic = XMAGIC(&xfp[i]);
3716812Sraf (void) mutex_init(&xfp[i].xlock,
3726812Sraf USYNC_THREAD | LOCK_RECURSIVE, NULL);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate #endif /* _LP64 */
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate lastlink = *prev = hdr;
3770Sstevel@tonic-gate fp->_ptr = 0;
3780Sstevel@tonic-gate fp->_base = 0;
3790Sstevel@tonic-gate fp->_flag = 0377; /* claim the fp by setting low 8 bits */
3801846Scraigm if (threaded)
3815891Sraf cancel_safe_mutex_unlock(&_first_link_lock);
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate return (fp);
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate static void
isseekable(FILE * iop)3870Sstevel@tonic-gate isseekable(FILE *iop)
3880Sstevel@tonic-gate {
3890Sstevel@tonic-gate struct stat64 fstatbuf;
3900Sstevel@tonic-gate int save_errno;
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate save_errno = errno;
3930Sstevel@tonic-gate
3941846Scraigm if (fstat64(GET_FD(iop), &fstatbuf) != 0) {
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate * when we don't know what it is we'll
3970Sstevel@tonic-gate * do the old behaviour and flush
3980Sstevel@tonic-gate * the stream
3990Sstevel@tonic-gate */
4000Sstevel@tonic-gate SET_SEEKABLE(iop);
4010Sstevel@tonic-gate errno = save_errno;
4020Sstevel@tonic-gate return;
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate
4050Sstevel@tonic-gate /*
4060Sstevel@tonic-gate * check for what is non-SEEKABLE
4070Sstevel@tonic-gate * otherwise assume it's SEEKABLE so we get the old
4080Sstevel@tonic-gate * behaviour and flush the stream
4090Sstevel@tonic-gate */
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate if (S_ISFIFO(fstatbuf.st_mode) || S_ISCHR(fstatbuf.st_mode) ||
4120Sstevel@tonic-gate S_ISSOCK(fstatbuf.st_mode) || S_ISDOOR(fstatbuf.st_mode)) {
4130Sstevel@tonic-gate CLEAR_SEEKABLE(iop);
4140Sstevel@tonic-gate } else {
4150Sstevel@tonic-gate SET_SEEKABLE(iop);
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate errno = save_errno;
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate #ifdef _LP64
4220Sstevel@tonic-gate void
_setbufend(FILE * iop,Uchar * end)4230Sstevel@tonic-gate _setbufend(FILE *iop, Uchar *end) /* set the end pointer for this iop */
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate iop->_end = end;
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate isseekable(iop);
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate #undef _realbufend
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate Uchar *
_realbufend(FILE * iop)4330Sstevel@tonic-gate _realbufend(FILE *iop) /* get the end pointer for this iop */
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate return (iop->_end);
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate #else /* _LP64 */
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate /*
4410Sstevel@tonic-gate * Awkward functions not needed for the sane 64 bit environment.
4420Sstevel@tonic-gate */
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate * xmagic must not be aligned on a 4K boundary. We guarantee this in
4450Sstevel@tonic-gate * _findiop().
4460Sstevel@tonic-gate */
4470Sstevel@tonic-gate #define VALIDXFILE(xfp) \
4480Sstevel@tonic-gate (((uintptr_t)&(xfp)->xmagic & 0xfff) && \
4490Sstevel@tonic-gate (xfp)->xmagic == XMAGIC(FILEx(xfp)))
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate static struct xFILEdata *
getxfdat(FILE * iop)4520Sstevel@tonic-gate getxfdat(FILE *iop)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate if (STDIOP(iop))
4550Sstevel@tonic-gate return (&_xftab[IOPIND(iop)]);
4560Sstevel@tonic-gate else if (VALIDXFILE(FILEx(iop)))
4570Sstevel@tonic-gate return (&FILEx(iop)->_xdat);
4580Sstevel@tonic-gate else
4590Sstevel@tonic-gate return (NULL);
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate void
_setbufend(FILE * iop,Uchar * end)4630Sstevel@tonic-gate _setbufend(FILE *iop, Uchar *end) /* set the end pointer for this iop */
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate struct xFILEdata *dat = getxfdat(iop);
4660Sstevel@tonic-gate
4670Sstevel@tonic-gate if (dat != NULL)
4680Sstevel@tonic-gate dat->_end = end;
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate isseekable(iop);
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate /*
4730Sstevel@tonic-gate * For binary compatibility with user programs using the
4740Sstevel@tonic-gate * old _bufend macro. This is *so* broken, fileno()
4750Sstevel@tonic-gate * is not the proper index.
4760Sstevel@tonic-gate */
4771846Scraigm if (iop->_magic < _NFILE)
4781846Scraigm _bufendtab[iop->_magic] = end;
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate Uchar *
_realbufend(FILE * iop)4830Sstevel@tonic-gate _realbufend(FILE *iop) /* get the end pointer for this iop */
4840Sstevel@tonic-gate {
4850Sstevel@tonic-gate struct xFILEdata *dat = getxfdat(iop);
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate if (dat != NULL)
4880Sstevel@tonic-gate return (dat->_end);
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate return (NULL);
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate /*
4940Sstevel@tonic-gate * _reallock() is invoked in each stdio call through the IOB_LCK() macro,
4950Sstevel@tonic-gate * it is therefor extremely performance sensitive. We get better performance
4960Sstevel@tonic-gate * by inlining the STDIOP check in IOB_LCK and inlining a custom version
4970Sstevel@tonic-gate * of getfxdat() here.
4980Sstevel@tonic-gate */
4990Sstevel@tonic-gate rmutex_t *
_reallock(FILE * iop)5000Sstevel@tonic-gate _reallock(FILE *iop)
5010Sstevel@tonic-gate {
5020Sstevel@tonic-gate if (VALIDXFILE(FILEx(iop)))
5030Sstevel@tonic-gate return (&FILEx(iop)->xlock);
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate return (NULL);
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate #endif /* _LP64 */
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate /* make sure _cnt, _ptr are correct */
5110Sstevel@tonic-gate void
_bufsync(FILE * iop,Uchar * bufend)5120Sstevel@tonic-gate _bufsync(FILE *iop, Uchar *bufend)
5130Sstevel@tonic-gate {
5140Sstevel@tonic-gate ssize_t spaceleft;
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate spaceleft = bufend - iop->_ptr;
5170Sstevel@tonic-gate if (bufend < iop->_ptr) {
5180Sstevel@tonic-gate iop->_ptr = bufend;
5190Sstevel@tonic-gate iop->_cnt = 0;
5200Sstevel@tonic-gate } else if (spaceleft < iop->_cnt)
5210Sstevel@tonic-gate iop->_cnt = spaceleft;
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate /* really write out current buffer contents */
5250Sstevel@tonic-gate int
_xflsbuf(FILE * iop)5260Sstevel@tonic-gate _xflsbuf(FILE *iop)
5270Sstevel@tonic-gate {
5280Sstevel@tonic-gate ssize_t n;
5290Sstevel@tonic-gate Uchar *base = iop->_base;
5300Sstevel@tonic-gate Uchar *bufend;
5310Sstevel@tonic-gate ssize_t num_wrote;
5320Sstevel@tonic-gate
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate * Hopefully, be stable with respect to interrupts...
5350Sstevel@tonic-gate */
5360Sstevel@tonic-gate n = iop->_ptr - base;
5370Sstevel@tonic-gate iop->_ptr = base;
5380Sstevel@tonic-gate bufend = _bufend(iop);
5390Sstevel@tonic-gate if (iop->_flag & (_IOLBF | _IONBF))
5400Sstevel@tonic-gate iop->_cnt = 0; /* always go to a flush */
5410Sstevel@tonic-gate else
5420Sstevel@tonic-gate iop->_cnt = bufend - base;
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate if (_needsync(iop, bufend)) /* recover from interrupts */
5450Sstevel@tonic-gate _bufsync(iop, bufend);
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate if (n > 0) {
5481846Scraigm int fd = GET_FD(iop);
5495891Sraf while ((num_wrote = write(fd, base, (size_t)n)) != n) {
5500Sstevel@tonic-gate if (num_wrote <= 0) {
5515891Sraf if (!cancel_active())
5525891Sraf iop->_flag |= _IOERR;
5530Sstevel@tonic-gate return (EOF);
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate n -= num_wrote;
5560Sstevel@tonic-gate base += num_wrote;
5570Sstevel@tonic-gate }
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate return (0);
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate /* flush (write) buffer */
5630Sstevel@tonic-gate int
fflush(FILE * iop)5640Sstevel@tonic-gate fflush(FILE *iop)
5650Sstevel@tonic-gate {
5660Sstevel@tonic-gate int res;
5670Sstevel@tonic-gate rmutex_t *lk;
5680Sstevel@tonic-gate
5690Sstevel@tonic-gate if (iop) {
5700Sstevel@tonic-gate FLOCKFILE(lk, iop);
5710Sstevel@tonic-gate res = _fflush_u(iop);
5720Sstevel@tonic-gate FUNLOCKFILE(lk);
5730Sstevel@tonic-gate } else {
5741846Scraigm res = _fflush_l_iops(); /* flush all iops */
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate return (res);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate
5790Sstevel@tonic-gate static int
_fflush_l_iops(void)5801846Scraigm _fflush_l_iops(void) /* flush all buffers */
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate FPDECL(iop);
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate int i;
5850Sstevel@tonic-gate struct _link_ *lp;
5860Sstevel@tonic-gate int res = 0;
5871846Scraigm rmutex_t *lk;
5881846Scraigm /* Allow the compiler to optimize the load out of the loop */
5891846Scraigm int threaded = __libc_threaded;
5900Sstevel@tonic-gate
5911846Scraigm if (threaded)
5925891Sraf cancel_safe_mutex_lock(&_first_link_lock);
5930Sstevel@tonic-gate
5940Sstevel@tonic-gate lp = &__first_link;
5950Sstevel@tonic-gate
5960Sstevel@tonic-gate do {
5970Sstevel@tonic-gate /*
5981846Scraigm * We need to grab the file locks or file corruption
5991846Scraigm * will happen. But we first check the flags field
6001846Scraigm * knowing that when it is 0, it isn't allocated and
6011846Scraigm * cannot be allocated while we're holding the
6021846Scraigm * _first_link_lock. And when _IONBF is set (also the
6031846Scraigm * case when _flag is 0377, or alloc in progress), we
6041846Scraigm * also ignore it.
6051846Scraigm *
6061846Scraigm * Ignore locked streams; it will appear as if
6071846Scraigm * concurrent updates happened after fflush(NULL). Note
6081846Scraigm * that we even attempt to lock if the locking is set to
6091846Scraigm * "by caller". We don't want to penalize callers of
6101846Scraigm * __fsetlocking() by not flushing their files. Note: if
6111846Scraigm * __fsetlocking() callers don't employ any locking, they
6121846Scraigm * may still face corruption in fflush(NULL); but that's
6131846Scraigm * no change from earlier releases.
6140Sstevel@tonic-gate */
6150Sstevel@tonic-gate FIRSTFP(lp, iop);
6160Sstevel@tonic-gate for (i = lp->niob; --i >= 0; NEXTFP(iop)) {
6171846Scraigm unsigned int flag = iop->_flag;
6181846Scraigm
6191846Scraigm /* flag 0, flag 0377, or _IONBF set */
6201846Scraigm if (flag == 0 || (flag & _IONBF) != 0)
6211846Scraigm continue;
6221846Scraigm
6231846Scraigm if (threaded) {
6241846Scraigm lk = FPLOCK(iop);
6255891Sraf if (cancel_safe_mutex_trylock(lk) != 0)
6261846Scraigm continue;
6271846Scraigm }
6281846Scraigm
6291846Scraigm if (!(iop->_flag & _IONBF)) {
6300Sstevel@tonic-gate /*
6311846Scraigm * don't need to worry about the _IORW case
6321846Scraigm * since the iop will also marked with _IOREAD
6331846Scraigm * or _IOWRT whichever we are really doing
6340Sstevel@tonic-gate */
6351846Scraigm if (iop->_flag & _IOWRT) {
6361846Scraigm /* Flush write buffers */
6371846Scraigm res |= _fflush_u(iop);
6381846Scraigm } else if (iop->_flag & _IOREAD) {
6391846Scraigm /*
6401846Scraigm * flush seekable read buffers
6411846Scraigm * don't flush non-seekable read buffers
6421846Scraigm */
6431846Scraigm if (GET_SEEKABLE(iop)) {
6441846Scraigm res |= _fflush_u(iop);
6451846Scraigm }
6461846Scraigm }
6470Sstevel@tonic-gate }
6481846Scraigm if (threaded)
6495891Sraf cancel_safe_mutex_unlock(lk);
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate } while ((lp = lp->next) != NULL);
6521846Scraigm if (threaded)
6535891Sraf cancel_safe_mutex_unlock(&_first_link_lock);
6540Sstevel@tonic-gate return (res);
6550Sstevel@tonic-gate }
6560Sstevel@tonic-gate
6570Sstevel@tonic-gate /* flush buffer */
6580Sstevel@tonic-gate int
_fflush_u(FILE * iop)6590Sstevel@tonic-gate _fflush_u(FILE *iop)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate int res = 0;
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate /* this portion is always assumed locked */
6640Sstevel@tonic-gate if (!(iop->_flag & _IOWRT)) {
6651846Scraigm (void) lseek64(GET_FD(iop), -iop->_cnt, SEEK_CUR);
6660Sstevel@tonic-gate iop->_cnt = 0;
6670Sstevel@tonic-gate /* needed for ungetc & multibyte pushbacks */
6680Sstevel@tonic-gate iop->_ptr = iop->_base;
6690Sstevel@tonic-gate if (iop->_flag & _IORW) {
6700Sstevel@tonic-gate iop->_flag &= ~_IOREAD;
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate return (0);
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate if (iop->_base != NULL && iop->_ptr > iop->_base) {
6750Sstevel@tonic-gate res = _xflsbuf(iop);
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate if (iop->_flag & _IORW) {
6780Sstevel@tonic-gate iop->_flag &= ~_IOWRT;
6790Sstevel@tonic-gate iop->_cnt = 0;
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate return (res);
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate
6840Sstevel@tonic-gate /* flush buffer and close stream */
6850Sstevel@tonic-gate int
fclose(FILE * iop)6860Sstevel@tonic-gate fclose(FILE *iop)
6870Sstevel@tonic-gate {
6880Sstevel@tonic-gate int res = 0;
6890Sstevel@tonic-gate rmutex_t *lk;
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate if (iop == NULL) {
6920Sstevel@tonic-gate return (EOF); /* avoid passing zero to FLOCKFILE */
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate
6950Sstevel@tonic-gate FLOCKFILE(lk, iop);
6960Sstevel@tonic-gate if (iop->_flag == 0) {
6970Sstevel@tonic-gate FUNLOCKFILE(lk);
6980Sstevel@tonic-gate return (EOF);
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate /* Is not unbuffered and opened for read and/or write ? */
7010Sstevel@tonic-gate if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
7020Sstevel@tonic-gate res = _fflush_u(iop);
7031846Scraigm if (close(GET_FD(iop)) < 0)
7040Sstevel@tonic-gate res = EOF;
7050Sstevel@tonic-gate if (iop->_flag & _IOMYBUF) {
7060Sstevel@tonic-gate (void) free((char *)iop->_base - PUSHBACK);
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate iop->_base = NULL;
7090Sstevel@tonic-gate iop->_ptr = NULL;
7100Sstevel@tonic-gate iop->_cnt = 0;
7110Sstevel@tonic-gate iop->_flag = 0; /* marks it as available */
7120Sstevel@tonic-gate FUNLOCKFILE(lk);
7130Sstevel@tonic-gate
7141672Sraf if (__libc_threaded)
7155891Sraf cancel_safe_mutex_lock(&_first_link_lock);
7160Sstevel@tonic-gate fcloses++;
7171672Sraf if (__libc_threaded)
7185891Sraf cancel_safe_mutex_unlock(&_first_link_lock);
7190Sstevel@tonic-gate
7200Sstevel@tonic-gate return (res);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate
723*13093SRoger.Faulkner@Oracle.COM /* close all open streams */
724*13093SRoger.Faulkner@Oracle.COM int
fcloseall(void)725*13093SRoger.Faulkner@Oracle.COM fcloseall(void)
726*13093SRoger.Faulkner@Oracle.COM {
727*13093SRoger.Faulkner@Oracle.COM FPDECL(iop);
728*13093SRoger.Faulkner@Oracle.COM
729*13093SRoger.Faulkner@Oracle.COM struct _link_ *lp;
730*13093SRoger.Faulkner@Oracle.COM rmutex_t *lk;
731*13093SRoger.Faulkner@Oracle.COM
732*13093SRoger.Faulkner@Oracle.COM if (__libc_threaded)
733*13093SRoger.Faulkner@Oracle.COM cancel_safe_mutex_lock(&_first_link_lock);
734*13093SRoger.Faulkner@Oracle.COM
735*13093SRoger.Faulkner@Oracle.COM lp = &__first_link;
736*13093SRoger.Faulkner@Oracle.COM
737*13093SRoger.Faulkner@Oracle.COM do {
738*13093SRoger.Faulkner@Oracle.COM int i;
739*13093SRoger.Faulkner@Oracle.COM
740*13093SRoger.Faulkner@Oracle.COM FIRSTFP(lp, iop);
741*13093SRoger.Faulkner@Oracle.COM for (i = lp->niob; --i >= 0; NEXTFP(iop)) {
742*13093SRoger.Faulkner@Oracle.COM /* code stolen from fclose(), above */
743*13093SRoger.Faulkner@Oracle.COM
744*13093SRoger.Faulkner@Oracle.COM FLOCKFILE(lk, iop);
745*13093SRoger.Faulkner@Oracle.COM if (iop->_flag == 0) {
746*13093SRoger.Faulkner@Oracle.COM FUNLOCKFILE(lk);
747*13093SRoger.Faulkner@Oracle.COM continue;
748*13093SRoger.Faulkner@Oracle.COM }
749*13093SRoger.Faulkner@Oracle.COM
750*13093SRoger.Faulkner@Oracle.COM /* Not unbuffered and opened for read and/or write? */
751*13093SRoger.Faulkner@Oracle.COM if (!(iop->_flag & _IONBF) &&
752*13093SRoger.Faulkner@Oracle.COM (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
753*13093SRoger.Faulkner@Oracle.COM (void) _fflush_u(iop);
754*13093SRoger.Faulkner@Oracle.COM (void) close(GET_FD(iop));
755*13093SRoger.Faulkner@Oracle.COM if (iop->_flag & _IOMYBUF)
756*13093SRoger.Faulkner@Oracle.COM free((char *)iop->_base - PUSHBACK);
757*13093SRoger.Faulkner@Oracle.COM iop->_base = NULL;
758*13093SRoger.Faulkner@Oracle.COM iop->_ptr = NULL;
759*13093SRoger.Faulkner@Oracle.COM iop->_cnt = 0;
760*13093SRoger.Faulkner@Oracle.COM iop->_flag = 0; /* marks it as available */
761*13093SRoger.Faulkner@Oracle.COM FUNLOCKFILE(lk);
762*13093SRoger.Faulkner@Oracle.COM fcloses++;
763*13093SRoger.Faulkner@Oracle.COM }
764*13093SRoger.Faulkner@Oracle.COM } while ((lp = lp->next) != NULL);
765*13093SRoger.Faulkner@Oracle.COM
766*13093SRoger.Faulkner@Oracle.COM if (__libc_threaded)
767*13093SRoger.Faulkner@Oracle.COM cancel_safe_mutex_unlock(&_first_link_lock);
768*13093SRoger.Faulkner@Oracle.COM
769*13093SRoger.Faulkner@Oracle.COM return (0);
770*13093SRoger.Faulkner@Oracle.COM }
771*13093SRoger.Faulkner@Oracle.COM
7720Sstevel@tonic-gate /* flush buffer, close fd but keep the stream used by freopen() */
7730Sstevel@tonic-gate int
close_fd(FILE * iop)7740Sstevel@tonic-gate close_fd(FILE *iop)
7750Sstevel@tonic-gate {
7760Sstevel@tonic-gate int res = 0;
7770Sstevel@tonic-gate mbstate_t *mb;
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate if (iop == NULL || iop->_flag == 0)
7800Sstevel@tonic-gate return (EOF);
7810Sstevel@tonic-gate /* Is not unbuffered and opened for read and/or write ? */
7820Sstevel@tonic-gate if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
7830Sstevel@tonic-gate res = _fflush_u(iop);
7841846Scraigm if (close(GET_FD(iop)) < 0)
7850Sstevel@tonic-gate res = EOF;
7860Sstevel@tonic-gate if (iop->_flag & _IOMYBUF) {
7870Sstevel@tonic-gate (void) free((char *)iop->_base - PUSHBACK);
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate iop->_base = NULL;
7900Sstevel@tonic-gate iop->_ptr = NULL;
7910Sstevel@tonic-gate mb = _getmbstate(iop);
7920Sstevel@tonic-gate if (mb != NULL)
7930Sstevel@tonic-gate (void) memset(mb, 0, sizeof (mbstate_t));
7940Sstevel@tonic-gate iop->_cnt = 0;
7950Sstevel@tonic-gate _setorientation(iop, _NO_MODE);
7960Sstevel@tonic-gate return (res);
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate
7990Sstevel@tonic-gate static FILE *
getiop(FILE * fp,rmutex_t * lk,mbstate_t * mb)8000Sstevel@tonic-gate getiop(FILE *fp, rmutex_t *lk, mbstate_t *mb)
8010Sstevel@tonic-gate {
8025891Sraf if (lk != NULL && cancel_safe_mutex_trylock(lk) != 0)
8030Sstevel@tonic-gate return (NULL); /* locked: fp in use */
8040Sstevel@tonic-gate
8050Sstevel@tonic-gate if (fp->_flag == 0) { /* unused */
8060Sstevel@tonic-gate #ifndef _LP64
8070Sstevel@tonic-gate fp->__orientation = 0;
8080Sstevel@tonic-gate #endif /* _LP64 */
8090Sstevel@tonic-gate fp->_cnt = 0;
8100Sstevel@tonic-gate fp->_ptr = NULL;
8110Sstevel@tonic-gate fp->_base = NULL;
8120Sstevel@tonic-gate fp->_flag = 0377; /* claim the fp by setting low 8 bits */
8130Sstevel@tonic-gate (void) memset(mb, 0, sizeof (mbstate_t));
8140Sstevel@tonic-gate FUNLOCKFILE(lk);
8150Sstevel@tonic-gate return (fp);
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate FUNLOCKFILE(lk);
8180Sstevel@tonic-gate return (NULL);
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate
8210Sstevel@tonic-gate #ifndef _LP64
8220Sstevel@tonic-gate /*
8230Sstevel@tonic-gate * DESCRIPTION:
8240Sstevel@tonic-gate * This function gets the pointer to the mbstate_t structure associated
8250Sstevel@tonic-gate * with the specified iop.
8260Sstevel@tonic-gate *
8270Sstevel@tonic-gate * RETURNS:
8280Sstevel@tonic-gate * If the associated mbstate_t found, the pointer to the mbstate_t is
8290Sstevel@tonic-gate * returned. Otherwise, NULL is returned.
8300Sstevel@tonic-gate */
8310Sstevel@tonic-gate mbstate_t *
_getmbstate(FILE * iop)8320Sstevel@tonic-gate _getmbstate(FILE *iop)
8330Sstevel@tonic-gate {
8340Sstevel@tonic-gate struct xFILEdata *dat = getxfdat(iop);
8350Sstevel@tonic-gate
8360Sstevel@tonic-gate if (dat != NULL)
8370Sstevel@tonic-gate return (&dat->_state);
8380Sstevel@tonic-gate
8390Sstevel@tonic-gate return (NULL);
8400Sstevel@tonic-gate }
8411846Scraigm
8421846Scraigm /*
8431846Scraigm * More 32-bit only functions.
8441846Scraigm * They lookup/set large fd's for extended FILE support.
8451846Scraigm */
8461846Scraigm
8471846Scraigm /*
8481846Scraigm * The negative value indicates that Extended fd FILE's has not
8491846Scraigm * been enabled by the user.
8501846Scraigm */
8511846Scraigm static int bad_fd = -1;
8521846Scraigm
8531846Scraigm int
_file_get(FILE * iop)8541846Scraigm _file_get(FILE *iop)
8551846Scraigm {
8561846Scraigm int altfd;
8571846Scraigm
8581846Scraigm /*
8591846Scraigm * Failure indicates a FILE * not allocated through stdio;
8601846Scraigm * it means the flag values are probably bogus and that if
8611846Scraigm * a file descriptor is set, it's in _magic.
8621846Scraigm * Inline getxfdat() for performance reasons.
8631846Scraigm */
8641846Scraigm if (STDIOP(iop))
8651846Scraigm altfd = _xftab[IOPIND(iop)]._altfd;
8661846Scraigm else if (VALIDXFILE(FILEx(iop)))
8671846Scraigm altfd = FILEx(iop)->_xdat._altfd;
8681846Scraigm else
8691846Scraigm return (iop->_magic);
8701846Scraigm /*
8711846Scraigm * if this is not an internal extended FILE then check
8721846Scraigm * if _file is being changed from underneath us.
8731846Scraigm * It should not be because if
8741846Scraigm * it is then then we lose our ability to guard against
8751846Scraigm * silent data corruption.
8761846Scraigm */
8771846Scraigm if (!iop->__xf_nocheck && bad_fd > -1 && iop->_magic != bad_fd) {
8783468Scraigm (void) fprintf(stderr,
8791846Scraigm "Application violated extended FILE safety mechanism.\n"
8803468Scraigm "Please read the man page for extendedFILE.\nAborting\n");
8811846Scraigm abort();
8821846Scraigm }
8831846Scraigm return (altfd);
8841846Scraigm }
8851846Scraigm
8861846Scraigm int
_file_set(FILE * iop,int fd,const char * type)8871846Scraigm _file_set(FILE *iop, int fd, const char *type)
8881846Scraigm {
8891846Scraigm struct xFILEdata *dat;
8901846Scraigm int Fflag;
8911846Scraigm
8921846Scraigm /* Already known to contain at least one byte */
8931846Scraigm while (*++type != '\0')
8941846Scraigm ;
8951846Scraigm
8961846Scraigm Fflag = type[-1] == 'F';
8971846Scraigm if (!Fflag && bad_fd < 0) {
8981846Scraigm errno = EMFILE;
8991846Scraigm return (-1);
9001846Scraigm }
9011846Scraigm
9021846Scraigm dat = getxfdat(iop);
9031846Scraigm iop->__extendedfd = 1;
9041846Scraigm iop->__xf_nocheck = Fflag;
9051846Scraigm dat->_altfd = fd;
9061846Scraigm iop->_magic = (unsigned char)bad_fd;
9071846Scraigm return (0);
9081846Scraigm }
9091846Scraigm
9101846Scraigm /*
9111846Scraigm * Activates extended fd's in FILE's
9121846Scraigm */
9131846Scraigm
9141846Scraigm static const int tries[] = {196, 120, 60, 3};
9151846Scraigm #define NTRIES (sizeof (tries)/sizeof (int))
9161846Scraigm
9171846Scraigm int
enable_extended_FILE_stdio(int fd,int action)9181846Scraigm enable_extended_FILE_stdio(int fd, int action)
9191846Scraigm {
9201846Scraigm int i;
9211846Scraigm
9221846Scraigm if (action < 0)
9231846Scraigm action = SIGABRT; /* default signal */
9241846Scraigm
9251846Scraigm if (fd < 0) {
9261846Scraigm /*
9271846Scraigm * search for an available fd and make it the badfd
9281846Scraigm */
9291846Scraigm for (i = 0; i < NTRIES; i++) {
9301846Scraigm fd = fcntl(tries[i], F_BADFD, action);
9311846Scraigm if (fd >= 0)
9321846Scraigm break;
9331846Scraigm }
9341846Scraigm if (fd < 0) /* failed to find an available fd */
9351846Scraigm return (-1);
9361846Scraigm } else {
9371846Scraigm /* caller requests that fd be the chosen badfd */
9381846Scraigm int nfd = fcntl(fd, F_BADFD, action);
9391846Scraigm if (nfd < 0 || nfd != fd)
9401846Scraigm return (-1);
9411846Scraigm }
9421846Scraigm bad_fd = fd;
9431846Scraigm return (0);
9441846Scraigm }
9450Sstevel@tonic-gate #endif
946