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