1.\" Copyright (c) 1980, 1991, 1993, 1994 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)close.2 8.2 (Berkeley) 4/19/94 33.\" $Id: close.2,v 1.8 1997/02/22 15:03:47 peter Exp $ 34.\" 35.Dd April 19, 1994 36.Dt CLOSE 2 37.Os BSD 4 38.Sh NAME 39.Nm close 40.Nd delete a descriptor 41.Sh SYNOPSIS 42.Fd #include <unistd.h> 43.Ft int 44.Fn close "int d" 45.Sh DESCRIPTION 46The 47.Fn close 48call deletes a descriptor from the per-process object 49reference table. 50If this is the last reference to the underlying object, the 51object will be deactivated. 52For example, on the last close of a file 53the current 54.Em seek 55pointer associated with the file is lost; 56on the last close of a 57.Xr socket 2 58associated naming information and queued data are discarded; 59on the last close of a file holding an advisory lock 60the lock is released (see further 61.Xr flock 2 ) . 62However, the semantics of System V and 63.St -p1003.1-88 64dictate that all 65.Xr fcntl 2 66advisory record locks associated with a file for a given process 67are removed when 68.Em any 69file descriptor for that file is closed by that process. 70.Pp 71When a process exits, 72all associated file descriptors are freed, but since there is 73a limit on active descriptors per processes, the 74.Fn close 75function call 76is useful when a large quantity of file descriptors are being handled. 77.Pp 78When a process forks (see 79.Xr fork 2 ) , 80all descriptors for the new child process reference the same 81objects as they did in the parent before the fork. 82If a new process is then to be run using 83.Xr execve 2 , 84the process would normally inherit these descriptors. Most 85of the descriptors can be rearranged with 86.Xr dup2 2 87or deleted with 88.Fn close 89before the 90.Xr execve 2 91is attempted, but if some of these descriptors will still 92be needed if the execve fails, it is necessary to arrange for them 93to be closed if the execve succeeds. 94For this reason, the call 95.Dq Li fcntl(d, F_SETFD, 1) 96is provided, 97which arranges that a descriptor will be closed after a successful 98execve; the call 99.Dq Li fcntl(d, F_SETFD, 0) 100restores the default, 101which is to not close the descriptor. 102.Sh IMPLEMENTATION NOTES 103.Pp 104In the non-threaded library 105.Fn close 106is implemented as the 107.Va close 108syscall. 109.Pp 110In the threaded library, the 111.Va close 112syscall is assembled to 113.Fn _thread_sys_close 114and 115.Fn close 116is implemented as a function which locks 117.Va d 118for read and write, then calls 119.Fn _thread_sys_close . 120Before returning, 121.Fn close 122unlocks 123.Va d . 124.Sh RETURN VALUES 125Upon successful completion, a value of 0 is returned. 126Otherwise, a value of -1 is returned and the global integer variable 127.Va errno 128is set to indicate the error. 129.Sh ERRORS 130.Fn Close 131will fail if: 132.Bl -tag -width Er 133.It Bq Er EBADF 134.Fa D 135is not an active descriptor. 136.It Bq Er EINTR 137An interrupt was received. 138.El 139.Sh SEE ALSO 140.Xr accept 2 , 141.Xr execve 2 , 142.Xr fcntl 2 , 143.Xr flock 2 , 144.Xr open 2 , 145.Xr pipe 2 , 146.Xr socket 2 , 147.Xr socketpair 2 148.Sh STANDARDS 149The 150.Fn close 151function call is expected to conform to 152.St -p1003.1-90 . 153.Sh HISTORY 154A 155.Fn close 156function call appeared in 157.At v7 . 158