]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libc/sys/close.2
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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 .\" 4. 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$
30 .\"
31 .Dd January 22, 2012
32 .Dt CLOSE 2
33 .Os
34 .Sh NAME
35 .Nm close
36 .Nd delete a descriptor
37 .Sh LIBRARY
38 .Lb libc
39 .Sh SYNOPSIS
40 .In unistd.h
41 .Ft int
42 .Fn close "int d"
43 .Sh DESCRIPTION
44 The
45 .Fn close
46 system call deletes a descriptor from the per-process object
47 reference table.
48 If this is the last reference to the underlying object, the
49 object will be deactivated.
50 For example, on the last close of a file
51 the current
52 .Em seek
53 pointer associated with the file is lost;
54 on the last close of a
55 .Xr socket 2
56 associated naming information and queued data are discarded;
57 on the last close of a file holding an advisory lock
58 the lock is released (see further
59 .Xr flock 2 ) .
60 However, the semantics of System V and
61 .St -p1003.1-88
62 dictate that all
63 .Xr fcntl 2
64 advisory record locks associated with a file for a given process
65 are removed when
66 .Em any
67 file descriptor for that file is closed by that process.
68 .Pp
69 When a process exits,
70 all associated file descriptors are freed, but since there is
71 a limit on active descriptors per processes, the
72 .Fn close
73 system call
74 is useful when a large quantity of file descriptors are being handled.
75 .Pp
76 When a process forks (see
77 .Xr fork 2 ) ,
78 all descriptors for the new child process reference the same
79 objects as they did in the parent before the fork.
80 If a new process is then to be run using
81 .Xr execve 2 ,
82 the process would normally inherit these descriptors.
83 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, FD_CLOEXEC)
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 The
105 .Fn close
106 system call will fail if:
107 .Bl -tag -width Er
108 .It Bq Er EBADF
109 The
110 .Fa d
111 argument
112 is not an active descriptor.
113 .It Bq Er EINTR
114 An interrupt was received.
115 .It Bq Er ENOSPC
116 The underlying object did not fit, cached data was lost.
117 .It Bq Er ECONNRESET
118 The underlying object was a stream socket that was shut down by the peer
119 before all pending data was delivered.
120 .El
121 .Pp
122 In case of any error except
123 .Er EBADF ,
124 the supplied file descriptor is deallocated and therefore is no longer valid.
125 .Sh SEE ALSO
126 .Xr accept 2 ,
127 .Xr closefrom 2 ,
128 .Xr execve 2 ,
129 .Xr fcntl 2 ,
130 .Xr flock 2 ,
131 .Xr open 2 ,
132 .Xr pipe 2 ,
133 .Xr socket 2 ,
134 .Xr socketpair 2
135 .Sh STANDARDS
136 The
137 .Fn close
138 system call is expected to conform to
139 .St -p1003.1-90 .
140 .Sh HISTORY
141 The
142 .Fn close
143 function appeared in
144 .At v7 .