1.\" $OpenBSD: close.2,v 1.18 2014/12/10 19:19:00 schwarze 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: December 10 2014 $ 34.Dt CLOSE 2 35.Os 36.Sh NAME 37.Nm close 38.Nd delete a descriptor 39.Sh SYNOPSIS 40.In unistd.h 41.Ft int 42.Fn close "int d" 43.Sh DESCRIPTION 44The 45.Fn close 46call deletes a descriptor 47.Fa d 48from 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; 59and on the last close of a file holding an advisory lock, 60the lock is released (see 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 process, 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 image is to then be run using 83.Xr execve 2 , 84the process would normally inherit these descriptors. 85Most of 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 since some of these descriptors may still 92be needed should the 93.Xr execve 2 94fail, it is necessary to arrange for them 95to be closed when the 96.Xr execve 2 97succeeds. 98For this reason, the call 99.Fn fcntl d F_SETFD FD_CLOEXEC 100is provided, 101which arranges that a descriptor will be closed after a successful 102.Xr execve 2 ; 103the call 104.Fn fcntl d F_SETFD 0 105restores the default, 106which is to not close the descriptor. 107.Sh RETURN VALUES 108.Rv -std 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.It Bq Er EIO 119An I/O error occurred while writing to the file system. 120.El 121.Sh SEE ALSO 122.Xr accept 2 , 123.Xr closefrom 2 , 124.Xr dup2 2 , 125.Xr execve 2 , 126.Xr fcntl 2 , 127.Xr flock 2 , 128.Xr open 2 , 129.Xr pipe 2 , 130.Xr socket 2 , 131.Xr socketpair 2 132.Sh STANDARDS 133.Fn close 134conforms to 135.St -p1003.1-2008 . 136.Sh HISTORY 137The 138.Fn close 139system call first appeared in 140.At v1 . 141