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