thr_close.c revision 56698
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 2349439Sdeischen * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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 * 3250476Speter * $FreeBSD: head/lib/libkse/thread/thr_close.c 56698 2000-01-27 23:07:25Z jasone $ 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 4456698Sjasone_close(int fd) 4513546Sjulian{ 4617706Sjulian int flags; 4717706Sjulian int ret; 4817706Sjulian struct stat sb; 4948046Sjb struct fd_table_entry *entry; 5017706Sjulian 5148046Sjb if ((fd == _thread_kern_pipe[0]) || (fd == _thread_kern_pipe[1])) { 5217706Sjulian /* 5348046Sjb * Don't allow silly programs to close the kernel pipe. 5448046Sjb */ 5548046Sjb errno = EBADF; 5648046Sjb ret = -1; 5748046Sjb } 5848046Sjb /* 5948046Sjb * Lock the file descriptor while the file is closed and get 6048046Sjb * the file descriptor status: 6148046Sjb */ 6248046Sjb else if (((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) && 6348046Sjb ((ret = _thread_sys_fstat(fd, &sb)) == 0)) { 6448046Sjb /* 6517706Sjulian * Check if the file should be left as blocking. 6617706Sjulian * 6717706Sjulian * This is so that the file descriptors shared with a parent 6817706Sjulian * process aren't left set to non-blocking if the child 6917706Sjulian * closes them prior to exit. An example where this causes 7017706Sjulian * problems with /bin/sh is when a child closes stdin. 7117706Sjulian * 7217706Sjulian * Setting a file as blocking causes problems if a threaded 7317706Sjulian * parent accesses the file descriptor before the child exits. 7417706Sjulian * Once the threaded parent receives a SIGCHLD then it resets 7517706Sjulian * all of its files to non-blocking, and so it is then safe 7617706Sjulian * to access them. 7717706Sjulian * 7817706Sjulian * Pipes are not set to blocking when they are closed, as 7917706Sjulian * the parent and child will normally close the file 8017706Sjulian * descriptor of the end of the pipe that they are not 8117706Sjulian * using, which would then cause any reads to block 8217706Sjulian * indefinitely. 8317706Sjulian */ 8417706Sjulian if ((S_ISREG(sb.st_mode) || S_ISCHR(sb.st_mode)) && (_thread_fd_table[fd]->flags & O_NONBLOCK) == 0) { 8517706Sjulian /* Get the current flags: */ 8617706Sjulian flags = _thread_sys_fcntl(fd, F_GETFL, NULL); 8717706Sjulian /* Clear the nonblocking file descriptor flag: */ 8817706Sjulian _thread_sys_fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); 8917706Sjulian } 9017706Sjulian 9148046Sjb /* XXX: Assumes well behaved threads. */ 9248046Sjb /* XXX: Defer real close to avoid race condition */ 9348046Sjb entry = _thread_fd_table[fd]; 9448046Sjb _thread_fd_table[fd] = NULL; 9548046Sjb free(entry); 9648046Sjb 9717706Sjulian /* Close the file descriptor: */ 9813546Sjulian ret = _thread_sys_close(fd); 9913546Sjulian } 10013546Sjulian return (ret); 10113546Sjulian} 10255838Sjasone 10356698Sjasoneint 10456698Sjasoneclose(int fd) 10556698Sjasone{ 10656698Sjasone int ret; 10756698Sjasone 10856698Sjasone _thread_enter_cancellation_point(); 10956698Sjasone ret = _close(fd); 11056698Sjasone _thread_leave_cancellation_point(); 11156698Sjasone 11256698Sjasone return ret; 11356698Sjasone} 11413546Sjulian#endif 115