xref: /dragonfly/lib/libc/sys/close.2 (revision 6e5c5008)
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.\" $FreeBSD: src/lib/libc/sys/close.2,v 1.10.2.6 2001/12/14 18:34:00 ru Exp $
30.\" $DragonFly: src/lib/libc/sys/close.2,v 1.2 2003/06/17 04:26:47 dillon Exp $
31.\"
32.Dd April 19, 1994
33.Dt CLOSE 2
34.Os
35.Sh NAME
36.Nm close
37.Nd delete a descriptor
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In unistd.h
42.Ft int
43.Fn close "int d"
44.Sh DESCRIPTION
45The
46.Fn close
47call deletes a descriptor from the per-process object
48reference table.
49If this is the last reference to the underlying object, the
50object will be deactivated.
51For example, on the last close of a file
52the current
53.Em seek
54pointer associated with the file is lost;
55on the last close of a
56.Xr socket 2
57associated naming information and queued data are discarded;
58on the last close of a file holding an advisory lock
59the lock is released (see further
60.Xr flock 2 ) .
61However, the semantics of System V and
62.St -p1003.1-88
63dictate that all
64.Xr fcntl 2
65advisory record locks associated with a file for a given process
66are removed when
67.Em any
68file descriptor for that file is closed by that process.
69.Pp
70When a process exits,
71all associated file descriptors are freed, but since there is
72a limit on active descriptors per processes, the
73.Fn close
74function call
75is useful when a large quantity of file descriptors are being handled.
76.Pp
77When a process forks (see
78.Xr fork 2 ) ,
79all descriptors for the new child process reference the same
80objects as they did in the parent before the fork.
81If a new process is then to be run using
82.Xr execve 2 ,
83the process would normally inherit these descriptors.  Most
84of the descriptors can be rearranged with
85.Xr dup2 2
86or deleted with
87.Fn close
88before the
89.Xr execve 2
90is attempted, but if some of these descriptors will still
91be needed if the execve fails, it is necessary to arrange for them
92to be closed if the execve succeeds.
93For this reason, the call
94.Dq Li fcntl(d, F_SETFD, 1)
95is provided,
96which arranges that a descriptor will be closed after a successful
97execve; the call
98.Dq Li fcntl(d, F_SETFD, 0)
99restores the default,
100which is to not close the descriptor.
101.Sh RETURN VALUES
102.Rv -std close
103.Sh ERRORS
104.Fn Close
105will fail if:
106.Bl -tag -width Er
107.It Bq Er EBADF
108.Fa D
109is not an active descriptor.
110.It Bq Er EINTR
111An interrupt was received.
112.El
113.Sh SEE ALSO
114.Xr accept 2 ,
115.Xr execve 2 ,
116.Xr fcntl 2 ,
117.Xr flock 2 ,
118.Xr open 2 ,
119.Xr pipe 2 ,
120.Xr socket 2 ,
121.Xr socketpair 2
122.Sh STANDARDS
123The
124.Fn close
125function call is expected to conform to
126.St -p1003.1-90 .
127.Sh HISTORY
128A
129.Fn close
130function call appeared in
131.At v7 .
132