/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
#include "lint.h"
#include "mtlib.h"
#include "file64.h"
#include <stdlib.h>
#include <stdio.h>
#include <thread.h>
#include <synch.h>
#include <unistd.h>
#include <string.h>
#include "stdiom.h"
#include <wchar.h>
#include <stddef.h>
#include <errno.h>
#include <fcntl.h>
#ifdef _LP64
/*
* avoid some of the _LP64 ifdef hell.
*/
#else
/* The extended 32-bit file structure for use in link buffers */
typedef struct xFILE {
} xFILE;
/*
* The magic number stored is actually the pointer scrambled with
* a magic number. Pointers to data items live everywhere in memory
* so we scramble the pointer in order to avoid accidental collisions.
*/
#endif /* _LP64 */
{
/* NULL for the __first_link in ILP32 */
};
/*
* With dynamic linking, iob may be in either the library or in the user's
* a.out, so the run time linker fixes up the first entry in __first_link at
* process startup time.
*
* In 32 bit processes, we don't have xFILE[FILE_ARY_SZ] but FILE[],
* and _xftab[] instead; this is denoted by having iobp set to NULL in
* 32 bit mode for the first link entry.
*/
{
#if !defined(_LP64)
NULL,
#else
&_iob[0],
#endif
};
/*
* Information cached to speed up searches. We remember where we
* last found a free FILE* and we remember whether we saw any fcloses
* in between. We also count the number of chunks we allocated, see
* _findiop() for an explanation.
* These variables are all protected by _first_link_lock.
*/
static int fcloses;
static int nchunks;
static int _fflush_l_iops(void);
/*
* All functions that understand the linked list of iob's follow.
*/
void
{
}
/*
* For fork1-safety (see libc_prepare_atfork(), etc).
*/
void
{
(void) mutex_lock(&_first_link_lock);
/*
* XXX: We should acquire all of the iob locks here.
*/
}
void
{
/*
* XXX: We should release all of the iob locks here.
*/
(void) mutex_unlock(&_first_link_lock);
}
void
{
int i;
/* Allow compiler to optimize the loop */
if (threaded)
lp = &__first_link;
do {
/*
* The additional _IONBF check guards againsts
* allocated but uninitialized iops (see _findiop).
* We also automatically skip non allocated iop's.
* Don't block on locks.
*/
if (threaded) {
if (cancel_safe_mutex_trylock(lk) != 0)
continue;
/* Recheck after locking */
}
} else {
}
}
}
if (threaded)
}
/* allocate an unused stream; NULL if cannot */
FILE *
_findiop(void)
{
/* used so there only needs to be one malloc() */
#ifdef _LP64
typedef struct {
} Pkg;
#else
typedef union {
struct { /* Normal */
} Pkgn;
struct { /* Reversed */
} Pkgr;
} Pkg;
#endif
int i;
if (threaded)
fcloses = 0;
lastlink = &__first_link;
}
/*
* lock to make testing of fp->_flag == 0 and acquiring the fp atomic
* and for allocation of new links
* low contention expected on _findiop(), hence coarse locking.
* for finer granularity, use fp->_lock for allocating an iop
* and make the testing of lp->next and allocation of new link atomic
* using lp->_lock
*/
do {
if (threaded) {
return (ret);
}
} else {
return (ret);
}
}
/*
* If there was a sufficient number of fcloses since we last started
* at __first_link, we rescan all fp's again. We do not rescan for
* all fcloses; that would simplify the algorithm but would make
* search times near O(n) again.
* Worst case behaviour would still be pretty bad (open a full set,
* then continously opening and closing one FILE * gets you a full
* scan each time). That's why we over allocate 1 FILE for each
* 32 chunks. More over allocation is better; this is a nice
* empirical value which doesn't cost a lot of memory, doesn't
* overallocate until we reach 256 FILE *s and keeps the performance
* pretty close to the optimum.
*/
goto rescan;
/*
* Need to allocate another and put it in the linked list.
*/
if (threaded)
return (NULL);
}
#ifdef _LP64
#else
/*
* The problem with referencing a word after a FILE* is the possibility
* of a SIGSEGV if a non-stdio issue FILE structure ends on a page
* boundary. We run this check so we never need to run an expensive
* check like mincore() in order to know whether it is
* safe to dereference ((xFILE*)fp)->xmagic.
* We allocate the block with two alternative layouts; if one
* layout is not properly aligned for our purposes, the other layout
* will be because the size of _link_ is small compared to
* sizeof (xFILE).
* The check performed is this:
* If the distance from pkgp to the end of the page is
* less than the the offset of the last xmagic field in the
* xFILE structure, (the 0x1000 boundary is inside our just
* allocated structure) and the distance modulo the size of xFILE
* is identical to the offset of the first xmagic in the
* structure (i.e., XXXXXX000 points to an xmagic field),
* we need to use the reverse structure.
*/
/* Use reversed structure */
} else {
/* Use normal structure */
}
#endif /* _LP64 */
nchunks++;
#ifdef _LP64
for (i = 0; i < FILE_ARY_SZ; i++)
#else
for (i = 0; i < FILE_ARY_SZ; i++) {
}
#endif /* _LP64 */
if (threaded)
return (fp);
}
static void
{
int save_errno;
save_errno = errno;
/*
* when we don't know what it is we'll
* do the old behaviour and flush
* the stream
*/
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
*/
} else {
}
errno = save_errno;
}
#ifdef _LP64
void
{
}
Uchar *
{
}
#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().
*/
static struct xFILEdata *
{
else
return (NULL);
}
void
{
/*
* For binary compatibility with user programs using the
* old _bufend macro. This is *so* broken, fileno()
* is not the proper index.
*/
}
Uchar *
{
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 *
{
return (NULL);
}
#endif /* _LP64 */
/* make sure _cnt, _ptr are correct */
void
{
}
/* really write out current buffer contents */
int
{
ssize_t n;
/*
* Hopefully, be stable with respect to interrupts...
*/
else
if (n > 0) {
if (num_wrote <= 0) {
if (!cancel_active())
return (EOF);
}
n -= num_wrote;
}
}
return (0);
}
/* flush (write) buffer */
int
{
int res;
if (iop) {
} else {
}
return (res);
}
static int
{
int i;
int res = 0;
/* Allow the compiler to optimize the load out of the loop */
if (threaded)
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.
*/
/* flag 0, flag 0377, or _IONBF set */
continue;
if (threaded) {
if (cancel_safe_mutex_trylock(lk) != 0)
continue;
}
/*
* don't need to worry about the _IORW case
* since the iop will also marked with _IOREAD
* or _IOWRT whichever we are really doing
*/
/* Flush write buffers */
/*
* flush seekable read buffers
* don't flush non-seekable read buffers
*/
if (GET_SEEKABLE(iop)) {
}
}
}
if (threaded)
}
if (threaded)
return (res);
}
/* flush buffer */
int
{
int res = 0;
/* this portion is always assumed locked */
/* needed for ungetc & multibyte pushbacks */
}
return (0);
}
}
}
return (res);
}
/* flush buffer and close stream */
int
{
int res = 0;
return (EOF); /* avoid passing zero to FLOCKFILE */
}
return (EOF);
}
}
if (__libc_threaded)
fcloses++;
if (__libc_threaded)
return (res);
}
/* close all open streams */
int
fcloseall(void)
{
if (__libc_threaded)
lp = &__first_link;
do {
int i;
/* code stolen from fclose(), above */
continue;
}
fcloses++;
}
if (__libc_threaded)
return (0);
}
/* flush buffer, close fd but keep the stream used by freopen() */
int
{
int res = 0;
return (EOF);
}
return (res);
}
static FILE *
{
return (NULL); /* locked: fp in use */
#ifndef _LP64
fp->__orientation = 0;
#endif /* _LP64 */
return (fp);
}
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.
*/
{
return (NULL);
}
/*
* More 32-bit only functions.
*/
/*
* The negative value indicates that Extended fd FILE's has not
* been enabled by the user.
*/
int
{
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.
*/
else
/*
* 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.
*/
"Application violated extended FILE safety mechanism.\n"
"Please read the man page for extendedFILE.\nAborting\n");
abort();
}
return (altfd);
}
int
{
int Fflag;
/* Already known to contain at least one byte */
while (*++type != '\0')
;
return (-1);
}
return (0);
}
/*
* Activates extended fd's in FILE's
*/
int
{
int i;
if (action < 0)
if (fd < 0) {
/*
* search for an available fd and make it the badfd
*/
for (i = 0; i < NTRIES; i++) {
if (fd >= 0)
break;
}
if (fd < 0) /* failed to find an available fd */
return (-1);
} else {
/* caller requests that fd be the chosen badfd */
return (-1);
}
return (0);
}
#endif