xref: /openbsd/lib/libc/sys/close.2 (revision 952f302f)
1.\"	$OpenBSD: close.2,v 1.19 2022/04/15 03:04:52 deraadt 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: April 15 2022 $
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 forks (see
72.Xr fork 2 ) ,
73all descriptors for the new child process reference the same
74objects as they did in the parent before the fork.
75If a new process image is to then be run using
76.Xr execve 2 ,
77the process would normally inherit these descriptors.
78Most of the descriptors can be rearranged with
79.Xr dup2 2
80or deleted with
81.Fn close
82before the
83.Xr execve 2
84is attempted, but since some of these descriptors may still
85be needed should the
86.Xr execve 2
87fail, it is necessary to arrange for them
88to be closed when the
89.Xr execve 2
90succeeds.
91For this reason, the call
92.Fn fcntl d F_SETFD FD_CLOEXEC
93is provided,
94which arranges that a descriptor will be closed after a successful
95.Xr execve 2 ;
96the call
97.Fn fcntl d F_SETFD 0
98restores the default,
99which is to not close the descriptor.
100.Sh RETURN VALUES
101.Rv -std
102.Sh ERRORS
103.Fn close
104will fail if:
105.Bl -tag -width Er
106.It Bq Er EBADF
107.Fa d
108is not an active descriptor.
109.It Bq Er EINTR
110An interrupt was received.
111.It Bq Er EIO
112An I/O error occurred while writing to the file system.
113.El
114.Sh SEE ALSO
115.Xr accept 2 ,
116.Xr closefrom 2 ,
117.Xr dup2 2 ,
118.Xr execve 2 ,
119.Xr fcntl 2 ,
120.Xr flock 2 ,
121.Xr open 2 ,
122.Xr pipe 2 ,
123.Xr socket 2 ,
124.Xr socketpair 2
125.Sh STANDARDS
126.Fn close
127conforms to
128.St -p1003.1-2008 .
129.Sh HISTORY
130The
131.Fn close
132system call first appeared in
133.At v1 .
134