]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/close.2
zfs: merge openzfs/zfs@03e9caaec
[FreeBSD/FreeBSD.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 .\"
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
43 The
44 .Fn close
45 system call deletes a descriptor from the per-process object
46 reference table.
47 If this is the last reference to the underlying object, the
48 object will be deactivated.
49 For example, on the last close of a file
50 the current
51 .Em seek
52 pointer associated with the file is lost;
53 on the last close of a
54 .Xr socket 2
55 associated naming information and queued data are discarded;
56 on the last close of a file holding an advisory lock
57 the lock is released (see further
58 .Xr flock 2 ) .
59 However, the semantics of System V and
60 .St -p1003.1-88
61 dictate that all
62 .Xr fcntl 2
63 advisory record locks associated with a file for a given process
64 are removed when
65 .Em any
66 file descriptor for that file is closed by that process.
67 .Pp
68 When a process exits,
69 all associated file descriptors are freed, but since there is
70 a limit on active descriptors per processes, the
71 .Fn close
72 system call
73 is useful when a large quantity of file descriptors are being handled.
74 .Pp
75 When a process forks (see
76 .Xr fork 2 ) ,
77 all descriptors for the new child process reference the same
78 objects as they did in the parent before the fork.
79 If a new process is then to be run using
80 .Xr execve 2 ,
81 the process would normally inherit these descriptors.
82 Most
83 of the descriptors can be rearranged with
84 .Xr dup2 2
85 or deleted with
86 .Fn close
87 before the
88 .Xr execve 2
89 is attempted, but if some of these descriptors will still
90 be needed if the execve fails, it is necessary to arrange for them
91 to be closed if the execve succeeds.
92 For this reason, the call
93 .Dq Li fcntl(d, F_SETFD, FD_CLOEXEC)
94 is provided,
95 which arranges that a descriptor will be closed after a successful
96 execve; the call
97 .Dq Li fcntl(d, F_SETFD, 0)
98 restores the default,
99 which is to not close the descriptor.
100 .Sh RETURN VALUES
101 .Rv -std close
102 .Sh ERRORS
103 The
104 .Fn close
105 system call will fail if:
106 .Bl -tag -width Er
107 .It Bq Er EBADF
108 The
109 .Fa fd
110 argument
111 is not an active descriptor.
112 .It Bq Er EINTR
113 An interrupt was received.
114 .It Bq Er ENOSPC
115 The underlying object did not fit, cached data was lost.
116 .It Bq Er ECONNRESET
117 The underlying object was a stream socket that was shut down by the peer
118 before all pending data was delivered.
119 .El
120 .Pp
121 In case of any error except
122 .Er EBADF ,
123 the 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
135 The
136 .Fn close
137 system call is expected to conform to
138 .St -p1003.1-90 .
139 .Sh HISTORY
140 The
141 .Fn close
142 function appeared in
143 .At v1 .