xref: /netbsd-src/lib/libc/sys/close.2 (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1.\" Copyright (c) 1980, 1991 Regents of the University of California.
2.\" 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. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     from: @(#)close.2	6.4 (Berkeley) 3/10/91
33.\"	$Id: close.2,v 1.4 1994/01/10 23:54:16 jtc Exp $
34.\"
35.Dd March 10, 1991
36.Dt CLOSE 2
37.Os BSD 4
38.Sh NAME
39.Nm close
40.Nd delete a descriptor
41.Sh SYNOPSIS
42.Fd #include <unistd.h>
43.Ft int
44.Fn close "int d"
45.Sh DESCRIPTION
46The
47.Fn close
48call deletes a descriptor from 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;
59on the last close of a file holding an advisory lock
60the lock is released (see further
61.Xr flock 2 ) .
62.Pp
63When a process exits,
64all associated file descriptors are freed, but since there is
65a limit on active descriptors per processes, the
66.Fn close
67function call
68is useful when a large quantity of file descriptors are being handled.
69.Pp
70When a process forks (see
71.Xr fork 2 ) ,
72all descriptors for the new child process reference the same
73objects as they did in the parent before the fork.
74If a new process is then to be run using
75.Xr execve 2 ,
76the process would normally inherit these descriptors.  Most
77of the descriptors can be rearranged with
78.Xr dup2 2
79or deleted with
80.Fn close
81before the
82.Xr execve
83is attempted, but if some of these descriptors will still
84be needed if the execve fails, it is necessary to arrange for them
85to be closed if the execve succeeds.
86For this reason, the call
87.Dq Li fcntl(d, F_SETFD, 1)
88is provided,
89which arranges that a descriptor will be closed after a successful
90execve; the call
91.Dq Li fcntl(d, F_SETFD, 0)
92restores the default,
93which is to not close the descriptor.
94.Sh RETURN VALUES
95Upon successful completion, a value of 0 is returned.
96Otherwise, a value of -1 is returned and the global integer variable
97.Va errno
98is set to indicate the error.
99.Sh ERRORS
100.Fn Close
101will fail if:
102.Bl -tag -width Er
103.It Bq Er EBADF
104.Fa D
105is not an active descriptor.
106.It Bq Er EINTR
107An interrupt was received.
108.El
109.Sh SEE ALSO
110.Xr accept 2 ,
111.Xr flock 2 ,
112.Xr open 2 ,
113.Xr pipe 2 ,
114.Xr socket 2 ,
115.Xr socketpair 2 ,
116.Xr execve 2 ,
117.Xr fcntl 2
118.Sh STANDARDS
119.Fn Close
120conforms to
121.St -p1003.1-88 .
122