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.\" 34.Dd April 19, 1994 35.Dt CLOSE 2 36.Os BSD 4 37.Sh NAME 38.Nm close 39.Nd delete a descriptor 40.Sh SYNOPSIS 41.Fd #include <unistd.h> 42.Ft int 43.Fn close "int d" 44.Sh DESCRIPTION 45The 46.Fn close 47call deletes a descriptor from the per-process object 48reference table. 49If this is the last reference to the underlying object, the 50object will be deactivated. 51For example, on the last close of a file 52the current 53.Em seek 54pointer associated with the file is lost; 55on the last close of a 56.Xr socket 2 57associated naming information and queued data are discarded; 58on the last close of a file holding an advisory lock 59the lock is released (see further 60.Xr flock 2 ) . 61.Pp 62When a process exits, 63all associated file descriptors are freed, but since there is 64a limit on active descriptors per processes, the 65.Fn close 66function call 67is useful when a large quantity of file descriptors are being handled. 68.Pp 69When a process forks (see 70.Xr fork 2 ) , 71all descriptors for the new child process reference the same 72objects as they did in the parent before the fork. 73If a new process is then to be run using 74.Xr execve 2 , 75the process would normally inherit these descriptors. Most 76of the descriptors can be rearranged with 77.Xr dup2 2 78or deleted with 79.Fn close 80before the 81.Xr execve 82is attempted, but if some of these descriptors will still 83be needed if the execve fails, it is necessary to arrange for them 84to be closed if the execve succeeds. 85For this reason, the call 86.Dq Li fcntl(d, F_SETFD, 1) 87is provided, 88which arranges that a descriptor will be closed after a successful 89execve; the call 90.Dq Li fcntl(d, F_SETFD, 0) 91restores the default, 92which is to not close the descriptor. 93.Sh RETURN VALUES 94Upon successful completion, a value of 0 is returned. 95Otherwise, a value of -1 is returned and the global integer variable 96.Va errno 97is set to indicate the error. 98.Sh ERRORS 99.Fn Close 100will fail if: 101.Bl -tag -width Er 102.It Bq Er EBADF 103.Fa D 104is not an active descriptor. 105.It Bq Er EINTR 106An interrupt was received. 107.El 108.Sh SEE ALSO 109.Xr accept 2 , 110.Xr flock 2 , 111.Xr open 2 , 112.Xr pipe 2 , 113.Xr socket 2 , 114.Xr socketpair 2 , 115.Xr execve 2 , 116.Xr fcntl 2 117.Sh STANDARDS 118.Fn Close 119conforms to IEEE Std 1003.1-1988 120.Pq Dq Tn POSIX . 121