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. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" @(#)close.2 8.2 (Berkeley) 4/19/94 29.\" 30.Dd December 1, 2017 31.Dt CLOSE 2 32.Os 33.Sh NAME 34.Nm close 35.Nd delete a descriptor 36.Sh LIBRARY 37.Lb libc 38.Sh SYNOPSIS 39.In unistd.h 40.Ft int 41.Fn close "int fd" 42.Sh DESCRIPTION 43The 44.Fn close 45system call deletes a descriptor from the per-process object 46reference table. 47If this is the last reference to the underlying object, the 48object will be deactivated. 49For example, on the last close of a file 50the current 51.Em seek 52pointer associated with the file is lost; 53on the last close of a 54.Xr socket 2 55associated naming information and queued data are discarded; 56on the last close of a file holding an advisory lock 57the lock is released (see further 58.Xr flock 2 ) . 59However, the semantics of System V and 60.St -p1003.1-88 61dictate that all 62.Xr fcntl 2 63advisory record locks associated with a file for a given process 64are removed when 65.Em any 66file descriptor for that file is closed by that process. 67.Pp 68When a process exits, 69all associated file descriptors are freed, but since there is 70a limit on active descriptors per processes, the 71.Fn close 72system call 73is useful when a large quantity of file descriptors are being handled. 74.Pp 75When a process forks (see 76.Xr fork 2 ) , 77all descriptors for the new child process reference the same 78objects as they did in the parent before the fork. 79If a new process is then to be run using 80.Xr execve 2 , 81the process would normally inherit these descriptors. 82Most 83of 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 if some of these descriptors will still 90be needed if the execve fails, it is necessary to arrange for them 91to be closed if the execve succeeds. 92For this reason, the call 93.Dq Li fcntl(d, F_SETFD, FD_CLOEXEC) 94is provided, 95which arranges that a descriptor will be closed after a successful 96execve; the call 97.Dq Li fcntl(d, F_SETFD, 0) 98restores the default, 99which is to not close the descriptor. 100.Sh RETURN VALUES 101.Rv -std close 102.Sh ERRORS 103The 104.Fn close 105system call will fail if: 106.Bl -tag -width Er 107.It Bq Er EBADF 108The 109.Fa fd 110argument 111is not an active descriptor. 112.It Bq Er EINTR 113An interrupt was received. 114.It Bq Er ENOSPC 115The underlying object did not fit, cached data was lost. 116.It Bq Er ECONNRESET 117The underlying object was a stream socket that was shut down by the peer 118before all pending data was delivered. 119.El 120.Pp 121In case of any error except 122.Er EBADF , 123the supplied file descriptor is deallocated and therefore is no longer valid. 124.Sh SEE ALSO 125.Xr accept 2 , 126.Xr closefrom 2 , 127.Xr execve 2 , 128.Xr fcntl 2 , 129.Xr flock 2 , 130.Xr open 2 , 131.Xr pipe 2 , 132.Xr socket 2 , 133.Xr socketpair 2 134.Sh STANDARDS 135The 136.Fn close 137system call is expected to conform to 138.St -p1003.1-90 . 139.Sh HISTORY 140The 141.Fn close 142function appeared in 143.At v1 . 144