flush.c revision 2
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * CDDL HEADER START
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The contents of this file are subject to the terms of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Common Development and Distribution License (the "License").
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz * You may not use this file except in compliance with the License.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin * or http://www.opensolaris.org/os/licensing.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * See the License for the specific language governing permissions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * and limitations under the License.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * When distributing Covered Code, include this CDDL HEADER in each
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * If applicable, add the following below this CDDL HEADER, with the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * fields enclosed by brackets "[]" replaced with your own identifying
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * information: Portions Copyright [yyyy] [name of copyright owner]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * CDDL HEADER END
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* Copyright (c) 1988 AT&T */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* All Rights Reserved */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "lint.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "mtlib.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "file64.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <sys/types.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <stdlib.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <stdio.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <thread.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <synch.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <unistd.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <string.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "stdiom.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <wchar.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <sys/stat.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <stddef.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <errno.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <fcntl.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define _iob __iob
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#undef end
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define FILE_ARY_SZ 8 /* a nice size for FILE array & end_buffer_ptrs */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#ifdef _LP64
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Macros to declare and loop over a fp or fp/xfp combo to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * avoid some of the _LP64 ifdef hell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define FPDECL(fp) FILE *fp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define FIRSTFP(lp, fp) fp = lp->iobp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define NEXTFP(fp) fp++
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define FPLOCK(fp) &fp->_lock
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define FPSTATE(fp) &fp->_state
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define xFILE FILE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define FPDECL(fp) FILE *fp; xFILE *x##fp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define FIRSTFP(lp, fp) x##fp = lp->iobp; \
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fp = x##fp ? &x##fp->_iob : &_iob[0]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define NEXTFP(fp) (x##fp ? fp = &(++x##fp)->_iob : ++fp)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define FPLOCK(fp) x##fp ? \
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin &x##fp->xlock : &_xftab[IOPIND(fp)]._lock
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define FPSTATE(fp) x##fp ? \
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin &x##fp->xstate : &_xftab[IOPIND(fp)]._state
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* The extended 32-bit file structure for use in link buffers */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintypedef struct xFILE {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin FILE _iob; /* must be first! */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin struct xFILEdata _xdat;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin} xFILE;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define xmagic _xdat._magic
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define xend _xdat._end
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define xlock _xdat._lock
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define xstate _xdat._state
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define FILEx(fp) ((struct xFILE *)(uintptr_t)fp)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The magic number stored is actually the pointer scrambled with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * a magic number. Pointers to data items live everywhere in memory
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * so we scramble the pointer in order to avoid accidental collisions.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define XFILEMAGIC 0x63687367
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define XMAGIC(xfp) ((uintptr_t)(xfp) ^ XFILEMAGIC)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /* _LP64 */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstruct _link_ /* manages a list of streams */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin xFILE *iobp; /* the array of (x)FILE's */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* NULL for the __first_link in ILP32 */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int niob; /* length of the arrays */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin struct _link_ *next; /* next in the list */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin};
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * With dynamic linking, iob may be in either the library or in the user's
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * a.out, so the run time linker fixes up the first entry in __first_link at
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * process startup time.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * In 32 bit processes, we don't have xFILE[FILE_ARY_SZ] but FILE[],
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * and _xftab[] instead; this is denoted by having iobp set to NULL in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 32 bit mode for the first link entry.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstruct _link_ __first_link = /* first in linked list */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz#if !defined(_LP64)
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz NULL,
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin#else
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin &_iob[0],
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin#endif
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz _NFILE,
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz NULL
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz};
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz/*
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz * Information cached to speed up searches. We remember where we
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz * last found a free FILE* and we remember whether we saw any fcloses
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * in between. We also count the number of chunks we allocated, see
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * _findiop() for an explanation.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * These variables are all protected by _first_link_lock.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic struct _link_ *lastlink = NULL;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int fcloses;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int nchunks;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic mutex_t _first_link_lock = DEFAULTMUTEX;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chinstatic int _fflush_l_iops(void);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic FILE *getiop(FILE *, rmutex_t *, mbstate_t *);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * All functions that understand the linked list of iob's follow.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#pragma weak _cleanup = __cleanup
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvoid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin__cleanup(void) /* called at process end to flush ouput streams */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) fflush(NULL);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * For fork1-safety (see libc_prepare_atfork(), etc).
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvoid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstdio_locks()
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) mutex_lock(&_first_link_lock);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * XXX: We should acquire all of the iob locks here.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainzvoid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstdio_unlocks()
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * XXX: We should release all of the iob locks here.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) mutex_unlock(&_first_link_lock);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvoid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin_flushlbf(void) /* fflush() all line-buffered streams */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin FPDECL(fp);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int i;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin struct _link_ *lp;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Allow compiler to optimize the loop */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int threaded = __libc_threaded;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (threaded)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin cancel_safe_mutex_lock(&_first_link_lock);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin lp = &__first_link;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin do {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin FIRSTFP(lp, fp);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin for (i = lp->niob; --i >= 0; NEXTFP(fp)) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The additional _IONBF check guards againsts
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * allocated but uninitialized iops (see _findiop).
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * We also automatically skip non allocated iop's.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Don't block on locks.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ((fp->_flag & (_IOLBF | _IOWRT | _IONBF)) ==
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (_IOLBF | _IOWRT)) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (threaded) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin rmutex_t *lk = FPLOCK(fp);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (cancel_safe_mutex_trylock(lk) != 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin continue;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Recheck after locking */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ((fp->_flag & (_IOLBF | _IOWRT)) ==
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (_IOLBF | _IOWRT)) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) _fflush_u(fp);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin cancel_safe_mutex_unlock(lk);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin } else {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) _fflush_u(fp);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin } while ((lp = lp->next) != NULL);
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz if (threaded)
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz cancel_safe_mutex_unlock(&_first_link_lock);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* allocate an unused stream; NULL if cannot */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFILE *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin_findiop(void)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin struct _link_ *lp, **prev;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* used so there only needs to be one malloc() */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#ifdef _LP64
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin typedef struct {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin struct _link_ hdr;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin FILE iob[FILE_ARY_SZ];
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin } Pkg;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin typedef union {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin struct { /* Normal */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin struct _link_ hdr;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin xFILE iob[FILE_ARY_SZ];
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin } Pkgn;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin struct { /* Reversed */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin xFILE iob[FILE_ARY_SZ];
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin struct _link_ hdr;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin } Pkgr;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin } Pkg;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin uintptr_t delta;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Pkg *pkgp;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin struct _link_ *hdr;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin FPDECL(fp);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int i;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int threaded = __libc_threaded;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (threaded)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin cancel_safe_mutex_lock(&_first_link_lock);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (lastlink == NULL) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrescan:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fcloses = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin lastlink = &__first_link;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin lp = lastlink;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * lock to make testing of fp->_flag == 0 and acquiring the fp atomic
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * and for allocation of new links
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * low contention expected on _findiop(), hence coarse locking.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * for finer granularity, use fp->_lock for allocating an iop
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * and make the testing of lp->next and allocation of new link atomic
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * using lp->_lock
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin do {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin prev = &lp->next;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin FIRSTFP(lp, fp);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin for (i = lp->niob; --i >= 0; NEXTFP(fp)) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin FILE *ret;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (threaded) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin ret = getiop(fp, FPLOCK(fp), FPSTATE(fp));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (ret != NULL) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin cancel_safe_mutex_unlock(
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin &_first_link_lock);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (ret);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin } else {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin ret = getiop(fp, NULL, FPSTATE(fp));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (ret != NULL)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (ret);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin } while ((lastlink = lp = lp->next) != NULL);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * If there was a sufficient number of fcloses since we last started
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * at __first_link, we rescan all fp's again. We do not rescan for
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * all fcloses; that would simplify the algorithm but would make
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * search times near O(n) again.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Worst case behaviour would still be pretty bad (open a full set,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * then continously opening and closing one FILE * gets you a full
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * scan each time). That's why we over allocate 1 FILE for each
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 32 chunks. More over allocation is better; this is a nice
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * empirical value which doesn't cost a lot of memory, doesn't
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * overallocate until we reach 256 FILE *s and keeps the performance
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * pretty close to the optimum.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (fcloses > nchunks/32)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin goto rescan;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Need to allocate another and put it in the linked list.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ((pkgp = malloc(sizeof (Pkg))) == NULL) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (threaded)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin cancel_safe_mutex_unlock(&_first_link_lock);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (NULL);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) memset(pkgp, 0, sizeof (Pkg));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#ifdef _LP64
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin hdr = &pkgp->hdr;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin hdr->iobp = &pkgp->iob[0];
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The problem with referencing a word after a FILE* is the possibility
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * of a SIGSEGV if a non-stdio issue FILE structure ends on a page
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * boundary. We run this check so we never need to run an expensive
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * check like mincore() in order to know whether it is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * safe to dereference ((xFILE*)fp)->xmagic.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * We allocate the block with two alternative layouts; if one
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * layout is not properly aligned for our purposes, the other layout
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * will be because the size of _link_ is small compared to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * sizeof (xFILE).
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The check performed is this:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * If the distance from pkgp to the end of the page is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * less than the the offset of the last xmagic field in the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * xFILE structure, (the 0x1000 boundary is inside our just
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * allocated structure) and the distance modulo the size of xFILE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * is identical to the offset of the first xmagic in the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * structure (i.e., XXXXXX000 points to an xmagic field),
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * we need to use the reverse structure.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ((delta = 0x1000 - ((uintptr_t)pkgp & 0xfff)) <=
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin offsetof(Pkg, Pkgn.iob[FILE_ARY_SZ-1].xmagic) &&
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin delta % sizeof (struct xFILE) ==
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin offsetof(Pkg, Pkgn.iob[0].xmagic)) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Use reversed structure */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin hdr = &pkgp->Pkgr.hdr;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin hdr->iobp = &pkgp->Pkgr.iob[0];
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin } else {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Use normal structure */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin hdr = &pkgp->Pkgn.hdr;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin hdr->iobp = &pkgp->Pkgn.iob[0];
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /* _LP64 */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin hdr->niob = FILE_ARY_SZ;
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin nchunks++;
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin#ifdef _LP64
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin fp = hdr->iobp;
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin for (i = 0; i < FILE_ARY_SZ; i++)
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin (void) mutex_init(&fp[i]._lock,
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin USYNC_THREAD | LOCK_RECURSIVE, NULL);
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin xfp = hdr->iobp;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fp = &xfp->_iob;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin for (i = 0; i < FILE_ARY_SZ; i++) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin xfp[i].xmagic = XMAGIC(&xfp[i]);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void) mutex_init(&xfp[i].xlock,
USYNC_THREAD | LOCK_RECURSIVE, NULL);
}
#endif /* _LP64 */
lastlink = *prev = hdr;
fp->_ptr = 0;
fp->_base = 0;
fp->_flag = 0377; /* claim the fp by setting low 8 bits */
if (threaded)
cancel_safe_mutex_unlock(&_first_link_lock);
return (fp);
}
static void
isseekable(FILE *iop)
{
struct stat64 fstatbuf;
int save_errno;
save_errno = errno;
if (fstat64(GET_FD(iop), &fstatbuf) != 0) {
/*
* when we don't know what it is we'll
* do the old behaviour and flush
* the stream
*/
SET_SEEKABLE(iop);
errno = save_errno;
return;
}
/*
* check for what is non-SEEKABLE
* otherwise assume it's SEEKABLE so we get the old
* behaviour and flush the stream
*/
if (S_ISFIFO(fstatbuf.st_mode) || S_ISCHR(fstatbuf.st_mode) ||
S_ISSOCK(fstatbuf.st_mode) || S_ISDOOR(fstatbuf.st_mode)) {
CLEAR_SEEKABLE(iop);
} else {
SET_SEEKABLE(iop);
}
errno = save_errno;
}
#ifdef _LP64
void
_setbufend(FILE *iop, Uchar *end) /* set the end pointer for this iop */
{
iop->_end = end;
isseekable(iop);
}
#undef _realbufend
Uchar *
_realbufend(FILE *iop) /* get the end pointer for this iop */
{
return (iop->_end);
}
#else /* _LP64 */
/*
* Awkward functions not needed for the sane 64 bit environment.
*/
/*
* xmagic must not be aligned on a 4K boundary. We guarantee this in
* _findiop().
*/
#define VALIDXFILE(xfp) \
(((uintptr_t)&(xfp)->xmagic & 0xfff) && \
(xfp)->xmagic == XMAGIC(FILEx(xfp)))
static struct xFILEdata *
getxfdat(FILE *iop)
{
if (STDIOP(iop))
return (&_xftab[IOPIND(iop)]);
else if (VALIDXFILE(FILEx(iop)))
return (&FILEx(iop)->_xdat);
else
return (NULL);
}
void
_setbufend(FILE *iop, Uchar *end) /* set the end pointer for this iop */
{
struct xFILEdata *dat = getxfdat(iop);
if (dat != NULL)
dat->_end = end;
isseekable(iop);
/*
* For binary compatibility with user programs using the
* old _bufend macro. This is *so* broken, fileno()
* is not the proper index.
*/
if (iop->_magic < _NFILE)
_bufendtab[iop->_magic] = end;
}
Uchar *
_realbufend(FILE *iop) /* get the end pointer for this iop */
{
struct xFILEdata *dat = getxfdat(iop);
if (dat != NULL)
return (dat->_end);
return (NULL);
}
/*
* _reallock() is invoked in each stdio call through the IOB_LCK() macro,
* it is therefor extremely performance sensitive. We get better performance
* by inlining the STDIOP check in IOB_LCK and inlining a custom version
* of getfxdat() here.
*/
rmutex_t *
_reallock(FILE *iop)
{
if (VALIDXFILE(FILEx(iop)))
return (&FILEx(iop)->xlock);
return (NULL);
}
#endif /* _LP64 */
/* make sure _cnt, _ptr are correct */
void
_bufsync(FILE *iop, Uchar *bufend)
{
ssize_t spaceleft;
spaceleft = bufend - iop->_ptr;
if (bufend < iop->_ptr) {
iop->_ptr = bufend;
iop->_cnt = 0;
} else if (spaceleft < iop->_cnt)
iop->_cnt = spaceleft;
}
/* really write out current buffer contents */
int
_xflsbuf(FILE *iop)
{
ssize_t n;
Uchar *base = iop->_base;
Uchar *bufend;
ssize_t num_wrote;
/*
* Hopefully, be stable with respect to interrupts...
*/
n = iop->_ptr - base;
iop->_ptr = base;
bufend = _bufend(iop);
if (iop->_flag & (_IOLBF | _IONBF))
iop->_cnt = 0; /* always go to a flush */
else
iop->_cnt = bufend - base;
if (_needsync(iop, bufend)) /* recover from interrupts */
_bufsync(iop, bufend);
if (n > 0) {
int fd = GET_FD(iop);
while ((num_wrote = write(fd, base, (size_t)n)) != n) {
if (num_wrote <= 0) {
if (!cancel_active())
iop->_flag |= _IOERR;
return (EOF);
}
n -= num_wrote;
base += num_wrote;
}
}
return (0);
}
/* flush (write) buffer */
int
fflush(FILE *iop)
{
int res;
rmutex_t *lk;
if (iop) {
FLOCKFILE(lk, iop);
res = _fflush_u(iop);
FUNLOCKFILE(lk);
} else {
res = _fflush_l_iops(); /* flush all iops */
}
return (res);
}
static int
_fflush_l_iops(void) /* flush all buffers */
{
FPDECL(iop);
int i;
struct _link_ *lp;
int res = 0;
rmutex_t *lk;
/* Allow the compiler to optimize the load out of the loop */
int threaded = __libc_threaded;
if (threaded)
cancel_safe_mutex_lock(&_first_link_lock);
lp = &__first_link;
do {
/*
* We need to grab the file locks or file corruption
* will happen. But we first check the flags field
* knowing that when it is 0, it isn't allocated and
* cannot be allocated while we're holding the
* _first_link_lock. And when _IONBF is set (also the
* case when _flag is 0377, or alloc in progress), we
* also ignore it.
*
* Ignore locked streams; it will appear as if
* concurrent updates happened after fflush(NULL). Note
* that we even attempt to lock if the locking is set to
* "by caller". We don't want to penalize callers of
* __fsetlocking() by not flushing their files. Note: if
* __fsetlocking() callers don't employ any locking, they
* may still face corruption in fflush(NULL); but that's
* no change from earlier releases.
*/
FIRSTFP(lp, iop);
for (i = lp->niob; --i >= 0; NEXTFP(iop)) {
unsigned int flag = iop->_flag;
/* flag 0, flag 0377, or _IONBF set */
if (flag == 0 || (flag & _IONBF) != 0)
continue;
if (threaded) {
lk = FPLOCK(iop);
if (cancel_safe_mutex_trylock(lk) != 0)
continue;
}
if (!(iop->_flag & _IONBF)) {
/*
* don't need to worry about the _IORW case
* since the iop will also marked with _IOREAD
* or _IOWRT whichever we are really doing
*/
if (iop->_flag & _IOWRT) {
/* Flush write buffers */
res |= _fflush_u(iop);
} else if (iop->_flag & _IOREAD) {
/*
* flush seekable read buffers
* don't flush non-seekable read buffers
*/
if (GET_SEEKABLE(iop)) {
res |= _fflush_u(iop);
}
}
}
if (threaded)
cancel_safe_mutex_unlock(lk);
}
} while ((lp = lp->next) != NULL);
if (threaded)
cancel_safe_mutex_unlock(&_first_link_lock);
return (res);
}
/* flush buffer */
int
_fflush_u(FILE *iop)
{
int res = 0;
/* this portion is always assumed locked */
if (!(iop->_flag & _IOWRT)) {
(void) lseek64(GET_FD(iop), -iop->_cnt, SEEK_CUR);
iop->_cnt = 0;
/* needed for ungetc & multibyte pushbacks */
iop->_ptr = iop->_base;
if (iop->_flag & _IORW) {
iop->_flag &= ~_IOREAD;
}
return (0);
}
if (iop->_base != NULL && iop->_ptr > iop->_base) {
res = _xflsbuf(iop);
}
if (iop->_flag & _IORW) {
iop->_flag &= ~_IOWRT;
iop->_cnt = 0;
}
return (res);
}
/* flush buffer and close stream */
int
fclose(FILE *iop)
{
int res = 0;
rmutex_t *lk;
if (iop == NULL) {
return (EOF); /* avoid passing zero to FLOCKFILE */
}
FLOCKFILE(lk, iop);
if (iop->_flag == 0) {
FUNLOCKFILE(lk);
return (EOF);
}
/* Is not unbuffered and opened for read and/or write ? */
if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
res = _fflush_u(iop);
if (close(GET_FD(iop)) < 0)
res = EOF;
if (iop->_flag & _IOMYBUF) {
(void) free((char *)iop->_base - PUSHBACK);
}
iop->_base = NULL;
iop->_ptr = NULL;
iop->_cnt = 0;
iop->_flag = 0; /* marks it as available */
FUNLOCKFILE(lk);
if (__libc_threaded)
cancel_safe_mutex_lock(&_first_link_lock);
fcloses++;
if (__libc_threaded)
cancel_safe_mutex_unlock(&_first_link_lock);
return (res);
}
/* close all open streams */
int
fcloseall(void)
{
FPDECL(iop);
struct _link_ *lp;
rmutex_t *lk;
if (__libc_threaded)
cancel_safe_mutex_lock(&_first_link_lock);
lp = &__first_link;
do {
int i;
FIRSTFP(lp, iop);
for (i = lp->niob; --i >= 0; NEXTFP(iop)) {
/* code stolen from fclose(), above */
FLOCKFILE(lk, iop);
if (iop->_flag == 0) {
FUNLOCKFILE(lk);
continue;
}
/* Not unbuffered and opened for read and/or write? */
if (!(iop->_flag & _IONBF) &&
(iop->_flag & (_IOWRT | _IOREAD | _IORW)))
(void) _fflush_u(iop);
(void) close(GET_FD(iop));
if (iop->_flag & _IOMYBUF)
free((char *)iop->_base - PUSHBACK);
iop->_base = NULL;
iop->_ptr = NULL;
iop->_cnt = 0;
iop->_flag = 0; /* marks it as available */
FUNLOCKFILE(lk);
fcloses++;
}
} while ((lp = lp->next) != NULL);
if (__libc_threaded)
cancel_safe_mutex_unlock(&_first_link_lock);
return (0);
}
/* flush buffer, close fd but keep the stream used by freopen() */
int
close_fd(FILE *iop)
{
int res = 0;
mbstate_t *mb;
if (iop == NULL || iop->_flag == 0)
return (EOF);
/* Is not unbuffered and opened for read and/or write ? */
if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
res = _fflush_u(iop);
if (close(GET_FD(iop)) < 0)
res = EOF;
if (iop->_flag & _IOMYBUF) {
(void) free((char *)iop->_base - PUSHBACK);
}
iop->_base = NULL;
iop->_ptr = NULL;
mb = _getmbstate(iop);
if (mb != NULL)
(void) memset(mb, 0, sizeof (mbstate_t));
iop->_cnt = 0;
_setorientation(iop, _NO_MODE);
return (res);
}
static FILE *
getiop(FILE *fp, rmutex_t *lk, mbstate_t *mb)
{
if (lk != NULL && cancel_safe_mutex_trylock(lk) != 0)
return (NULL); /* locked: fp in use */
if (fp->_flag == 0) { /* unused */
#ifndef _LP64
fp->__orientation = 0;
#endif /* _LP64 */
fp->_cnt = 0;
fp->_ptr = NULL;
fp->_base = NULL;
fp->_flag = 0377; /* claim the fp by setting low 8 bits */
(void) memset(mb, 0, sizeof (mbstate_t));
FUNLOCKFILE(lk);
return (fp);
}
FUNLOCKFILE(lk);
return (NULL);
}
#ifndef _LP64
/*
* DESCRIPTION:
* This function gets the pointer to the mbstate_t structure associated
* with the specified iop.
*
* RETURNS:
* If the associated mbstate_t found, the pointer to the mbstate_t is
* returned. Otherwise, NULL is returned.
*/
mbstate_t *
_getmbstate(FILE *iop)
{
struct xFILEdata *dat = getxfdat(iop);
if (dat != NULL)
return (&dat->_state);
return (NULL);
}
/*
* More 32-bit only functions.
* They lookup/set large fd's for extended FILE support.
*/
/*
* The negative value indicates that Extended fd FILE's has not
* been enabled by the user.
*/
static int bad_fd = -1;
int
_file_get(FILE *iop)
{
int altfd;
/*
* Failure indicates a FILE * not allocated through stdio;
* it means the flag values are probably bogus and that if
* a file descriptor is set, it's in _magic.
* Inline getxfdat() for performance reasons.
*/
if (STDIOP(iop))
altfd = _xftab[IOPIND(iop)]._altfd;
else if (VALIDXFILE(FILEx(iop)))
altfd = FILEx(iop)->_xdat._altfd;
else
return (iop->_magic);
/*
* if this is not an internal extended FILE then check
* if _file is being changed from underneath us.
* It should not be because if
* it is then then we lose our ability to guard against
* silent data corruption.
*/
if (!iop->__xf_nocheck && bad_fd > -1 && iop->_magic != bad_fd) {
(void) fprintf(stderr,
"Application violated extended FILE safety mechanism.\n"
"Please read the man page for extendedFILE.\nAborting\n");
abort();
}
return (altfd);
}
int
_file_set(FILE *iop, int fd, const char *type)
{
struct xFILEdata *dat;
int Fflag;
/* Already known to contain at least one byte */
while (*++type != '\0')
;
Fflag = type[-1] == 'F';
if (!Fflag && bad_fd < 0) {
errno = EMFILE;
return (-1);
}
dat = getxfdat(iop);
iop->__extendedfd = 1;
iop->__xf_nocheck = Fflag;
dat->_altfd = fd;
iop->_magic = (unsigned char)bad_fd;
return (0);
}
/*
* Activates extended fd's in FILE's
*/
static const int tries[] = {196, 120, 60, 3};
#define NTRIES (sizeof (tries)/sizeof (int))
int
enable_extended_FILE_stdio(int fd, int action)
{
int i;
if (action < 0)
action = SIGABRT; /* default signal */
if (fd < 0) {
/*
* search for an available fd and make it the badfd
*/
for (i = 0; i < NTRIES; i++) {
fd = fcntl(tries[i], F_BADFD, action);
if (fd >= 0)
break;
}
if (fd < 0) /* failed to find an available fd */
return (-1);
} else {
/* caller requests that fd be the chosen badfd */
int nfd = fcntl(fd, F_BADFD, action);
if (nfd < 0 || nfd != fd)
return (-1);
}
bad_fd = fd;
return (0);
}
#endif