Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[dragonfly.git] / lib / libc / sys / close.2
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
45 The
46 .Fn close
47 call deletes a descriptor from the per-process object
48 reference table.
49 If this is the last reference to the underlying object, the
50 object will be deactivated.
51 For example, on the last close of a file
52 the current
53 .Em seek
54 pointer associated with the file is lost;
55 on the last close of a
56 .Xr socket 2
57 associated naming information and queued data are discarded;
58 on the last close of a file holding an advisory lock
59 the lock is released (see further
60 .Xr flock 2 ) .
61 However, the semantics of System V and
62 .St -p1003.1-88
63 dictate that all
64 .Xr fcntl 2
65 advisory record locks associated with a file for a given process
66 are removed when
67 .Em any
68 file descriptor for that file is closed by that process.
69 .Pp
70 When a process exits,
71 all associated file descriptors are freed, but since there is
72 a limit on active descriptors per processes, the
73 .Fn close
74 function call
75 is useful when a large quantity of file descriptors are being handled.
76 .Pp
77 When a process forks (see
78 .Xr fork 2 ) ,
79 all descriptors for the new child process reference the same
80 objects as they did in the parent before the fork.
81 If a new process is then to be run using
82 .Xr execve 2 ,
83 the process would normally inherit these descriptors.  Most
84 of the descriptors can be rearranged with
85 .Xr dup2 2
86 or deleted with
87 .Fn close
88 before the
89 .Xr execve 2
90 is attempted, but if some of these descriptors will still
91 be needed if the execve fails, it is necessary to arrange for them
92 to be closed if the execve succeeds.
93 For this reason, the call
94 .Dq Li fcntl(d, F_SETFD, 1)
95 is provided,
96 which arranges that a descriptor will be closed after a successful
97 execve; the call
98 .Dq Li fcntl(d, F_SETFD, 0)
99 restores the default,
100 which is to not close the descriptor.
101 .Sh RETURN VALUES
102 .Rv -std close
103 .Sh ERRORS
104 .Fn Close
105 will fail if:
106 .Bl -tag -width Er
107 .It Bq Er EBADF
108 .Fa D
109 is not an active descriptor.
110 .It Bq Er EINTR
111 An 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
123 The
124 .Fn close
125 function call is expected to conform to
126 .St -p1003.1-90 .
127 .Sh HISTORY
128 A
129 .Fn close
130 function call appeared in
131 .At v7 .