xref: /original-bsd/lib/libc/sys/close.2 (revision e58c8952)
1.\" Copyright (c) 1980, 1991, 1993, 1994
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
6.\"     @(#)close.2	8.2 (Berkeley) 04/19/94
7.\"
8.Dd
9.Dt CLOSE 2
10.Os BSD 4
11.Sh NAME
12.Nm close
13.Nd delete a descriptor
14.Sh SYNOPSIS
15.Fd #include <unistd.h>
16.Ft int
17.Fn close "int d"
18.Sh DESCRIPTION
19The
20.Fn close
21call deletes a descriptor from the per-process object
22reference table.
23If this is the last reference to the underlying object, the
24object will be deactivated.
25For example, on the last close of a file
26the current
27.Em seek
28pointer associated with the file is lost;
29on the last close of a
30.Xr socket 2
31associated naming information and queued data are discarded;
32on the last close of a file holding an advisory lock
33the lock is released (see further
34.Xr flock 2 ) .
35.Pp
36When a process exits,
37all associated file descriptors are freed, but since there is
38a limit on active descriptors per processes, the
39.Fn close
40function call
41is useful when a large quantity of file descriptors are being handled.
42.Pp
43When a process forks (see
44.Xr fork 2 ) ,
45all descriptors for the new child process reference the same
46objects as they did in the parent before the fork.
47If a new process is then to be run using
48.Xr execve 2 ,
49the process would normally inherit these descriptors.  Most
50of the descriptors can be rearranged with
51.Xr dup2 2
52or deleted with
53.Fn close
54before the
55.Xr execve
56is attempted, but if some of these descriptors will still
57be needed if the execve fails, it is necessary to arrange for them
58to be closed if the execve succeeds.
59For this reason, the call
60.Dq Li fcntl(d, F_SETFD, 1)
61is provided,
62which arranges that a descriptor will be closed after a successful
63execve; the call
64.Dq Li fcntl(d, F_SETFD, 0)
65restores the default,
66which is to not close the descriptor.
67.Sh RETURN VALUES
68Upon successful completion, a value of 0 is returned.
69Otherwise, a value of -1 is returned and the global integer variable
70.Va errno
71is set to indicate the error.
72.Sh ERRORS
73.Fn Close
74will fail if:
75.Bl -tag -width Er
76.It Bq Er EBADF
77.Fa D
78is not an active descriptor.
79.It Bq Er EINTR
80An interrupt was received.
81.El
82.Sh SEE ALSO
83.Xr accept 2 ,
84.Xr flock 2 ,
85.Xr open 2 ,
86.Xr pipe 2 ,
87.Xr socket 2 ,
88.Xr socketpair 2 ,
89.Xr execve 2 ,
90.Xr fcntl 2
91.Sh STANDARDS
92.Fn Close
93conforms to IEEE Std 1003.1-1988
94.Pq Dq Tn POSIX .
95