#include "lint.h"
#include "mtlib.h"
#include "file64.h"
#include <sys/types.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 <sys/stat.h>
#include <stddef.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/debug.h>
#include <limits.h>
#define _iob __iob
#undef end
#define FILE_ARY_SZ 8
#ifdef _LP64
#define FPDECL(fp) FILE *fp
#define FIRSTFP(lp, fp) fp = lp->iobp
#define NEXTFP(fp) fp++
#define FPLOCK(fp) &fp->_lock
#define FPSTATE(fp) &fp->_state
#define xFILE FILE
#else
#define FPDECL(fp) FILE *fp; xFILE *x##fp
#define FIRSTFP(lp, fp) x##fp = lp->iobp; \
fp = x##fp ? &x##fp->_iob : &_iob[0]
#define NEXTFP(fp) (x##fp ? fp = &(++x##fp)->_iob : ++fp)
#define FPLOCK(fp) x##fp ? \
&x##fp->xlock : &_xftab[IOPIND(fp)]._lock
#define FPSTATE(fp) x##fp ? \
&x##fp->xstate : &_xftab[IOPIND(fp)]._state
typedef struct xFILE {
FILE _iob;
struct xFILEdata _xdat;
} xFILE;
#define xmagic _xdat._magic
#define xend _xdat._end
#define xlock _xdat._lock
#define xstate _xdat._state
#define FILEx(fp) ((struct xFILE *)(uintptr_t)fp)
#define XFILEMAGIC 0x63687367
#define XMAGIC(xfp) ((uintptr_t)(xfp) ^ XFILEMAGIC)
#endif
struct _link_
{
xFILE *iobp;
int niob;
struct _link_ *next;
};
struct _link_ __first_link =
{
#if !defined(_LP64)
NULL,
#else
&_iob[0],
#endif
_NFILE,
NULL
};
static struct _link_ *lastlink = NULL;
static int fcloses;
static int nchunks;
static mutex_t _first_link_lock = DEFAULTMUTEX;
static int _fflush_l_iops(void);
static FILE *getiop(FILE *, rmutex_t *, mbstate_t *);
#pragma weak _cleanup = __cleanup
void
__cleanup(void)
{
(void) fflush(NULL);
}
void
stdio_locks()
{
(void) mutex_lock(&_first_link_lock);
}
void
stdio_unlocks()
{
(void) mutex_unlock(&_first_link_lock);
}
void
_flushlbf(void)
{
FPDECL(fp);
int i;
struct _link_ *lp;
int threaded = __libc_threaded;
if (threaded)
cancel_safe_mutex_lock(&_first_link_lock);
lp = &__first_link;
do {
FIRSTFP(lp, fp);
for (i = lp->niob; --i >= 0; NEXTFP(fp)) {
if ((fp->_flag & (_IOLBF | _IOWRT | _IONBF)) ==
(_IOLBF | _IOWRT)) {
if (threaded) {
rmutex_t *lk = FPLOCK(fp);
if (cancel_safe_mutex_trylock(lk) != 0)
continue;
if ((fp->_flag & (_IOLBF | _IOWRT)) ==
(_IOLBF | _IOWRT)) {
(void) _fflush_u(fp);
}
cancel_safe_mutex_unlock(lk);
} else {
(void) _fflush_u(fp);
}
}
}
} while ((lp = lp->next) != NULL);
if (threaded)
cancel_safe_mutex_unlock(&_first_link_lock);
}
FILE *
_findiop(void)
{
struct _link_ *lp, **prev;
#ifdef _LP64
typedef struct {
struct _link_ hdr;
FILE iob[FILE_ARY_SZ];
} Pkg;
#else
typedef union {
struct {
struct _link_ hdr;
xFILE iob[FILE_ARY_SZ];
} Pkgn;
struct {
xFILE iob[FILE_ARY_SZ];
struct _link_ hdr;
} Pkgr;
} Pkg;
uintptr_t delta;
#endif
Pkg *pkgp;
struct _link_ *hdr;
FPDECL(fp);
int i;
int threaded = __libc_threaded;
if (threaded)
cancel_safe_mutex_lock(&_first_link_lock);
if (lastlink == NULL) {
rescan:
fcloses = 0;
lastlink = &__first_link;
}
lp = lastlink;
do {
prev = &lp->next;
FIRSTFP(lp, fp);
for (i = lp->niob; --i >= 0; NEXTFP(fp)) {
FILE *ret;
if (threaded) {
ret = getiop(fp, FPLOCK(fp), FPSTATE(fp));
if (ret != NULL) {
cancel_safe_mutex_unlock(
&_first_link_lock);
return (ret);
}
} else {
ret = getiop(fp, NULL, FPSTATE(fp));
if (ret != NULL)
return (ret);
}
}
} while ((lastlink = lp = lp->next) != NULL);
if (fcloses > nchunks/32)
goto rescan;
if ((pkgp = malloc(sizeof (Pkg))) == NULL) {
if (threaded)
cancel_safe_mutex_unlock(&_first_link_lock);
return (NULL);
}
(void) memset(pkgp, 0, sizeof (Pkg));
#ifdef _LP64
hdr = &pkgp->hdr;
hdr->iobp = &pkgp->iob[0];
#else
if ((delta = 0x1000 - ((uintptr_t)pkgp & 0xfff)) <=
offsetof(Pkg, Pkgn.iob[FILE_ARY_SZ-1].xmagic) &&
delta % sizeof (struct xFILE) ==
offsetof(Pkg, Pkgn.iob[0].xmagic)) {
hdr = &pkgp->Pkgr.hdr;
hdr->iobp = &pkgp->Pkgr.iob[0];
} else {
hdr = &pkgp->Pkgn.hdr;
hdr->iobp = &pkgp->Pkgn.iob[0];
}
#endif
hdr->niob = FILE_ARY_SZ;
nchunks++;
#ifdef _LP64
fp = hdr->iobp;
for (i = 0; i < FILE_ARY_SZ; i++)
(void) mutex_init(&fp[i]._lock,
USYNC_THREAD | LOCK_RECURSIVE, NULL);
#else
xfp = hdr->iobp;
fp = &xfp->_iob;
for (i = 0; i < FILE_ARY_SZ; i++) {
xfp[i].xmagic = XMAGIC(&xfp[i]);
(void) mutex_init(&xfp[i].xlock,
USYNC_THREAD | LOCK_RECURSIVE, NULL);
}
#endif
lastlink = *prev = hdr;
fp->_ptr = 0;
fp->_base = 0;
fp->_flag = _DEF_FLAG_MASK;
if (threaded)
cancel_safe_mutex_unlock(&_first_link_lock);
return (fp);
}
static void
isseekable(FILE *iop)
{
struct stat64 fstatbuf;
int fd, save_errno;
save_errno = errno;
fd = _get_fd(iop);
if (fd == -1) {
return;
}
if (fstat64(fd, &fstatbuf) != 0) {
SET_SEEKABLE(iop);
errno = save_errno;
return;
}
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)
{
iop->_end = end;
isseekable(iop);
}
#undef _realbufend
Uchar *
_realbufend(FILE *iop)
{
return (iop->_end);
}
#else
#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)
{
struct xFILEdata *dat = getxfdat(iop);
if (dat != NULL)
dat->_end = end;
isseekable(iop);
if (iop->_magic < _NFILE)
_bufendtab[iop->_magic] = end;
}
Uchar *
_realbufend(FILE *iop)
{
struct xFILEdata *dat = getxfdat(iop);
if (dat != NULL)
return (dat->_end);
return (NULL);
}
rmutex_t *
_reallock(FILE *iop)
{
if (VALIDXFILE(FILEx(iop)))
return (&FILEx(iop)->xlock);
return (NULL);
}
#endif
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;
}
int
_xflsbuf(FILE *iop)
{
ssize_t n;
Uchar *base = iop->_base;
Uchar *bufend;
ssize_t num_wrote;
n = iop->_ptr - base;
iop->_ptr = base;
bufend = _bufend(iop);
if (iop->_flag & (_IOLBF | _IONBF))
iop->_cnt = 0;
else
iop->_cnt = bufend - base;
if (_needsync(iop, bufend))
_bufsync(iop, bufend);
if (n > 0) {
while ((num_wrote = _xwrite(iop, 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);
}
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();
}
return (res);
}
static int
_fflush_l_iops(void)
{
FPDECL(iop);
int i;
struct _link_ *lp;
int res = 0;
rmutex_t *lk;
int threaded = __libc_threaded;
if (threaded)
cancel_safe_mutex_lock(&_first_link_lock);
lp = &__first_link;
do {
FIRSTFP(lp, iop);
for (i = lp->niob; --i >= 0; NEXTFP(iop)) {
unsigned int flag = iop->_flag;
if (flag == 0 || (flag & _IONBF) != 0)
continue;
if (threaded) {
lk = FPLOCK(iop);
if (cancel_safe_mutex_trylock(lk) != 0)
continue;
}
if (!(iop->_flag & _IONBF)) {
if (iop->_flag & _IOWRT) {
res |= _fflush_u(iop);
} else if (iop->_flag & _IOREAD) {
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);
}
int
_fflush_u(FILE *iop)
{
int res = 0;
if (!(iop->_flag & _IOWRT)) {
(void) _xseek64(iop, -iop->_cnt, SEEK_CUR);
iop->_cnt = 0;
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);
}
static int
fclose_helper(FILE *iop, boolean_t doclose)
{
int res = 0;
rmutex_t *lk;
if (iop == NULL) {
return (EOF);
}
FLOCKFILE(lk, iop);
if (iop->_flag == 0) {
FUNLOCKFILE(lk);
return (EOF);
}
if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
res = _fflush_u(iop);
if (doclose)
if (_xclose(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;
FUNLOCKFILE(lk);
return (res);
}
int
fclose(FILE *iop)
{
int res = 0;
res = fclose_helper(iop, B_TRUE);
if (__libc_threaded)
cancel_safe_mutex_lock(&_first_link_lock);
fcloses++;
if (__libc_threaded)
cancel_safe_mutex_unlock(&_first_link_lock);
return (res);
}
int
fdclose(FILE *iop, int *fdp)
{
int res = 0;
int fd = _get_fd(iop);
if (fd == -1) {
res = fclose_helper(iop, B_TRUE);
errno = ENOTSUP;
} else {
res = fclose_helper(iop, B_FALSE);
}
if (__libc_threaded)
cancel_safe_mutex_lock(&_first_link_lock);
fcloses++;
if (__libc_threaded)
cancel_safe_mutex_unlock(&_first_link_lock);
if (fdp != NULL)
*fdp = fd;
if (fd == -1)
res = EOF;
return (res);
}
int
fcloseall(void)
{
FPDECL(iop);
struct _link_ *lp;
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)) {
(void) fclose_helper(iop, B_TRUE);
fcloses++;
}
} while ((lp = lp->next) != NULL);
if (__libc_threaded)
cancel_safe_mutex_unlock(&_first_link_lock);
return (0);
}
int
close_fd(FILE *iop)
{
int res = 0;
mbstate_t *mb;
if (iop == NULL || iop->_flag == 0)
return (EOF);
if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
res = _fflush_u(iop);
if (_xclose(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);
if (fp->_flag == 0) {
#ifndef _LP64
fp->__orientation = 0;
#endif
fp->_cnt = 0;
fp->_ptr = NULL;
fp->_base = NULL;
fp->_flag = _DEF_FLAG_MASK;
(void) memset(mb, 0, sizeof (mbstate_t));
FUNLOCKFILE(lk);
return (fp);
}
FUNLOCKFILE(lk);
return (NULL);
}
#ifndef _LP64
mbstate_t *
_getmbstate(FILE *iop)
{
struct xFILEdata *dat = getxfdat(iop);
if (dat != NULL)
return (&dat->_state);
return (NULL);
}
static int bad_fd = -1;
int
_file_get(FILE *iop)
{
int altfd;
if (STDIOP(iop))
altfd = _xftab[IOPIND(iop)]._altfd;
else if (VALIDXFILE(FILEx(iop)))
altfd = FILEx(iop)->_xdat._altfd;
else
return (iop->_magic);
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;
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);
}
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;
if (fd < 0) {
for (i = 0; i < NTRIES; i++) {
fd = fcntl(tries[i], F_BADFD, action);
if (fd >= 0)
break;
}
if (fd < 0)
return (-1);
} else {
int nfd = fcntl(fd, F_BADFD, action);
if (nfd < 0 || nfd != fd)
return (-1);
}
bad_fd = fd;
return (0);
}
#endif
static stdio_ops_t *
get_stdops(FILE *iop)
{
#ifdef _LP64
return (iop->_ops);
#else
struct xFILEdata *dat = getxfdat(iop);
return (dat->_ops);
#endif
}
static void
set_stdops(FILE *iop, stdio_ops_t *ops)
{
#ifdef _LP64
ASSERT3P(iop->_ops, ==, NULL);
iop->_ops = ops;
#else
struct xFILEdata *dat = getxfdat(iop);
ASSERT3P(dat->_ops, ==, NULL);
dat->_ops = ops;
#endif
}
static void
clr_stdops(FILE *iop)
{
#ifdef _LP64
iop->_ops = NULL;
#else
struct xFILEdata *dat = getxfdat(iop);
dat->_ops = NULL;
#endif
}
ssize_t
_xread(FILE *iop, void *buf, size_t nbytes)
{
stdio_ops_t *ops = get_stdops(iop);
if (ops != NULL) {
return (ops->std_read(iop, buf, nbytes));
}
return (read(_get_fd(iop), buf, nbytes));
}
ssize_t
_xwrite(FILE *iop, const void *buf, size_t nbytes)
{
stdio_ops_t *ops = get_stdops(iop);
if (ops != NULL) {
return (ops->std_write(iop, buf, nbytes));
}
return (write(_get_fd(iop), buf, nbytes));
}
off_t
_xseek(FILE *iop, off_t off, int whence)
{
stdio_ops_t *ops = get_stdops(iop);
if (ops != NULL) {
return (ops->std_seek(iop, off, whence));
}
return (lseek(_get_fd(iop), off, whence));
}
off64_t
_xseek64(FILE *iop, off64_t off, int whence)
{
stdio_ops_t *ops = get_stdops(iop);
if (ops != NULL) {
if (off > LONG_MAX || off < LONG_MIN) {
errno = EOVERFLOW;
return (-1);
}
return (ops->std_seek(iop, off, whence));
}
return (lseek64(_get_fd(iop), off, whence));
}
int
_xclose(FILE *iop)
{
stdio_ops_t *ops = get_stdops(iop);
if (ops != NULL) {
return (ops->std_close(iop));
}
return (close(_get_fd(iop)));
}
void *
_xdata(FILE *iop)
{
stdio_ops_t *ops = get_stdops(iop);
if (ops != NULL) {
return (ops->std_data);
}
return (NULL);
}
int
_xassoc(FILE *iop, fread_t readf, fwrite_t writef, fseek_t seekf,
fclose_t closef, void *data)
{
stdio_ops_t *ops = get_stdops(iop);
if (ops == NULL) {
ops = malloc(sizeof (*ops));
if (ops == NULL) {
return (-1);
}
set_stdops(iop, ops);
}
ops->std_read = readf;
ops->std_write = writef;
ops->std_seek = seekf;
ops->std_close = closef;
ops->std_data = data;
return (0);
}
void
_xunassoc(FILE *iop)
{
stdio_ops_t *ops = get_stdops(iop);
if (ops == NULL) {
return;
}
clr_stdops(iop);
free(ops);
}
int
_get_fd(FILE *iop)
{
if (get_stdops(iop) != NULL) {
return (-1);
}
#ifdef _LP64
return (iop->_file);
#else
if (iop->__extendedfd) {
return (_file_get(iop));
} else {
return (iop->_magic);
}
#endif
}