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