thr_close.c revision 48046
113546Sjulian/* 235509Sjb * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> 313546Sjulian * All rights reserved. 413546Sjulian * 513546Sjulian * Redistribution and use in source and binary forms, with or without 613546Sjulian * modification, are permitted provided that the following conditions 713546Sjulian * are met: 813546Sjulian * 1. Redistributions of source code must retain the above copyright 913546Sjulian * notice, this list of conditions and the following disclaimer. 1013546Sjulian * 2. Redistributions in binary form must reproduce the above copyright 1113546Sjulian * notice, this list of conditions and the following disclaimer in the 1213546Sjulian * documentation and/or other materials provided with the distribution. 1313546Sjulian * 3. All advertising materials mentioning features or use of this software 1413546Sjulian * must display the following acknowledgement: 1513546Sjulian * This product includes software developed by John Birrell. 1613546Sjulian * 4. Neither the name of the author nor the names of any co-contributors 1713546Sjulian * may be used to endorse or promote products derived from this software 1813546Sjulian * without specific prior written permission. 1913546Sjulian * 2013546Sjulian * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 2113546Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2213546Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2313546Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2413546Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2513546Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2613546Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2713546Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2813546Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2913546Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3013546Sjulian * SUCH DAMAGE. 3113546Sjulian * 3248046Sjb * $Id$ 3313546Sjulian */ 3448046Sjb#include <errno.h> 3517706Sjulian#include <stdlib.h> 3613546Sjulian#include <unistd.h> 3717706Sjulian#include <fcntl.h> 3817706Sjulian#include <sys/stat.h> 3913546Sjulian#ifdef _THREAD_SAFE 4013546Sjulian#include <pthread.h> 4113546Sjulian#include "pthread_private.h" 4213546Sjulian 4313546Sjulianint 4413546Sjulianclose(int fd) 4513546Sjulian{ 4617706Sjulian int flags; 4717706Sjulian int ret; 4817706Sjulian int status; 4917706Sjulian struct stat sb; 5048046Sjb struct fd_table_entry *entry; 5117706Sjulian 5248046Sjb if ((fd == _thread_kern_pipe[0]) || (fd == _thread_kern_pipe[1])) { 5317706Sjulian /* 5448046Sjb * Don't allow silly programs to close the kernel pipe. 5548046Sjb */ 5648046Sjb errno = EBADF; 5748046Sjb ret = -1; 5848046Sjb } 5948046Sjb /* 6048046Sjb * Lock the file descriptor while the file is closed and get 6148046Sjb * the file descriptor status: 6248046Sjb */ 6348046Sjb else if (((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) && 6448046Sjb ((ret = _thread_sys_fstat(fd, &sb)) == 0)) { 6548046Sjb /* 6617706Sjulian * Check if the file should be left as blocking. 6717706Sjulian * 6817706Sjulian * This is so that the file descriptors shared with a parent 6917706Sjulian * process aren't left set to non-blocking if the child 7017706Sjulian * closes them prior to exit. An example where this causes 7117706Sjulian * problems with /bin/sh is when a child closes stdin. 7217706Sjulian * 7317706Sjulian * Setting a file as blocking causes problems if a threaded 7417706Sjulian * parent accesses the file descriptor before the child exits. 7517706Sjulian * Once the threaded parent receives a SIGCHLD then it resets 7617706Sjulian * all of its files to non-blocking, and so it is then safe 7717706Sjulian * to access them. 7817706Sjulian * 7917706Sjulian * Pipes are not set to blocking when they are closed, as 8017706Sjulian * the parent and child will normally close the file 8117706Sjulian * descriptor of the end of the pipe that they are not 8217706Sjulian * using, which would then cause any reads to block 8317706Sjulian * indefinitely. 8417706Sjulian */ 8517706Sjulian if ((S_ISREG(sb.st_mode) || S_ISCHR(sb.st_mode)) && (_thread_fd_table[fd]->flags & O_NONBLOCK) == 0) { 8617706Sjulian /* Get the current flags: */ 8717706Sjulian flags = _thread_sys_fcntl(fd, F_GETFL, NULL); 8817706Sjulian /* Clear the nonblocking file descriptor flag: */ 8917706Sjulian _thread_sys_fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); 9017706Sjulian } 9117706Sjulian 9248046Sjb /* XXX: Assumes well behaved threads. */ 9348046Sjb /* XXX: Defer real close to avoid race condition */ 9448046Sjb entry = _thread_fd_table[fd]; 9548046Sjb _thread_fd_table[fd] = NULL; 9648046Sjb free(entry); 9748046Sjb 9817706Sjulian /* Close the file descriptor: */ 9913546Sjulian ret = _thread_sys_close(fd); 10013546Sjulian } 10113546Sjulian return (ret); 10213546Sjulian} 10313546Sjulian#endif 104