thr_close.c revision 103388
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 103388 2002-09-16 08:45:36Z mini $ 3313546Sjulian */ 3448046Sjb#include <errno.h> 3517706Sjulian#include <stdlib.h> 3613546Sjulian#include <unistd.h> 3717706Sjulian#include <fcntl.h> 3817706Sjulian#include <sys/stat.h> 3913546Sjulian#include <pthread.h> 40103388Smini#include "thr_private.h" 4113546Sjulian 4275369Sdeischen__weak_reference(__close, close); 4371581Sdeischen 4413546Sjulianint 4556698Sjasone_close(int fd) 4613546Sjulian{ 4717706Sjulian int flags; 4817706Sjulian int ret; 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) && 6471581Sdeischen ((ret = __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 */ 85102590Sdeischen if ((S_ISREG(sb.st_mode) || S_ISCHR(sb.st_mode)) 86102590Sdeischen && (_thread_fd_getflags(fd) & O_NONBLOCK) == 0) { 8717706Sjulian /* Get the current flags: */ 8871581Sdeischen flags = __sys_fcntl(fd, F_GETFL, NULL); 8917706Sjulian /* Clear the nonblocking file descriptor flag: */ 9071581Sdeischen __sys_fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); 9117706Sjulian } 9217706Sjulian 9348046Sjb /* XXX: Assumes well behaved threads. */ 9448046Sjb /* XXX: Defer real close to avoid race condition */ 9548046Sjb entry = _thread_fd_table[fd]; 9648046Sjb _thread_fd_table[fd] = NULL; 9748046Sjb free(entry); 9848046Sjb 9917706Sjulian /* Close the file descriptor: */ 10071581Sdeischen ret = __sys_close(fd); 10113546Sjulian } 10213546Sjulian return (ret); 10313546Sjulian} 10455838Sjasone 10556698Sjasoneint 10671581Sdeischen__close(int fd) 10756698Sjasone{ 10856698Sjasone int ret; 10956698Sjasone 11056698Sjasone _thread_enter_cancellation_point(); 11156698Sjasone ret = _close(fd); 11256698Sjasone _thread_leave_cancellation_point(); 11356698Sjasone 11456698Sjasone return ret; 11556698Sjasone} 116