flush.c revision 2
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * CDDL HEADER START
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 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * See the License for the specific language governing permissions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * and limitations under the License.
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 * CDDL HEADER END
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* Copyright (c) 1988 AT&T */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* All Rights Reserved */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define FILE_ARY_SZ 8 /* a nice size for FILE array & end_buffer_ptrs */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Macros to declare and loop over a fp or fp/xfp combo to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * avoid some of the _LP64 ifdef hell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define NEXTFP(fp) (x##fp ? fp = &(++x##fp)->_iob : ++fp)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* The extended 32-bit file structure for use in link buffers */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintypedef struct xFILE {
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#endif /* _LP64 */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* NULL for the __first_link in ILP32 */
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 * 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.
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.
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chinstatic int _fflush_l_iops(void);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * All functions that understand the linked list of iob's follow.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin__cleanup(void) /* called at process end to flush ouput streams */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * For fork1-safety (see libc_prepare_atfork(), etc).
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * XXX: We should acquire all of the iob locks here.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * XXX: We should release all of the iob locks here.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin_flushlbf(void) /* fflush() all line-buffered streams */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Allow compiler to optimize the loop */
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 /* Recheck after locking */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* allocate an unused stream; NULL if cannot */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* used so there only needs to be one malloc() */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin typedef struct {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin typedef union {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin struct { /* Normal */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin struct { /* Reversed */
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 * 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 * Need to allocate another and put it in the linked list.
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 /* Use reversed structure */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* Use normal structure */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /* _LP64 */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin for (i = 0; i < FILE_ARY_SZ; i++)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin for (i = 0; i < FILE_ARY_SZ; i++) {
if (threaded)
return (fp);
int save_errno;
#ifdef _LP64
Uchar *
static struct xFILEdata *
return (NULL);
Uchar *
return (NULL);
rmutex_t *
return (NULL);
ssize_t n;
if (num_wrote <= 0) {
if (!cancel_active())
return (EOF);
n -= num_wrote;
int res;
if (iop) {
return (res);
int res = 0;
if (threaded)
if (threaded) {
if (threaded)
if (threaded)
return (res);
int res = 0;
return (res);
int res = 0;
return (EOF);
if (__libc_threaded)
fcloses++;
if (__libc_threaded)
return (res);
fcloseall(void)
if (__libc_threaded)
fcloses++;
if (__libc_threaded)
int res = 0;
return (EOF);
return (res);
static FILE *
#ifndef _LP64
return (fp);
return (NULL);
#ifndef _LP64
return (NULL);
int altfd;
abort();
return (altfd);
int Fflag;
if (action < 0)
if (fd < 0) {
for (i = 0; i < NTRIES; i++) {
if (fd >= 0)