/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 1993 The Regents of the University of California.4* Copyright (c) 2013 Mariusz Zaborski <[email protected]>5* All rights reserved.6*7* This code is derived from software contributed to Berkeley by8* Chris Torek.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18* 3. Neither the name of the University nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include "namespace.h"36#include <errno.h>37#include <stdbool.h>38#include <stdio.h>39#include <stdlib.h>40#include "un-namespace.h"41#include <spinlock.h>42#include "libc_private.h"43#include "local.h"4445static int46cleanfile(FILE *fp, bool c)47{48int r;4950r = fp->_flags & __SWR ? __sflush(fp) : 0;51if (c) {52if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0)53r = EOF;54}5556if (fp->_flags & __SMBF)57free((char *)fp->_bf._base);58if (HASUB(fp))59FREEUB(fp);60if (HASLB(fp))61FREELB(fp);62fp->_file = -1;63fp->_r = fp->_w = 0; /* Mess up if reaccessed. */6465/*66* Lock the spinlock used to protect __sglue list walk in67* __sfp(). The __sfp() uses fp->_flags == 0 test as an68* indication of the unused FILE.69*70* Taking the lock prevents possible compiler or processor71* reordering of the writes performed before the final _flags72* cleanup, making sure that we are done with the FILE before73* it is considered available.74*/75STDIO_THREAD_LOCK();76fp->_flags = 0; /* Release this FILE for reuse. */77STDIO_THREAD_UNLOCK();7879return (r);80}8182int83fdclose(FILE *fp, int *fdp)84{85int r, err;8687if (fdp != NULL)88*fdp = -1;8990if (fp->_flags == 0) { /* not open! */91errno = EBADF;92return (EOF);93}9495FLOCKFILE_CANCELSAFE(fp);96r = 0;97if (fp->_close != __sclose) {98r = EOF;99errno = EOPNOTSUPP;100} else if (fp->_file < 0) {101r = EOF;102errno = EBADF;103}104if (r == EOF) {105err = errno;106(void)cleanfile(fp, true);107errno = err;108} else {109if (fdp != NULL)110*fdp = fp->_file;111r = cleanfile(fp, false);112}113FUNLOCKFILE_CANCELSAFE();114115return (r);116}117118int119fclose(FILE *fp)120{121int r;122123if (fp->_flags == 0) { /* not open! */124errno = EBADF;125return (EOF);126}127128FLOCKFILE_CANCELSAFE(fp);129r = cleanfile(fp, true);130FUNLOCKFILE_CANCELSAFE();131132return (r);133}134135136