manual page distributed with 4.2BSD
[unix-history] / usr / src / lib / libc / sys / close.2
CommitLineData
6fd5737e
KM
1.\" Copyright (c) 1980 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
447c53e0 5.\" @(#)close.2 5.1 (Berkeley) %G%
6fd5737e 6.\"
447c53e0 7.TH CLOSE 2 "27 July 1983"
6fd5737e
KM
8.UC 4
9.SH NAME
447c53e0 10close \- delete a descriptor
6fd5737e 11.SH SYNOPSIS
447c53e0
KM
12.B close(d)
13.br
14.B "int d;"
6fd5737e 15.SH DESCRIPTION
447c53e0
KM
16The
17\fIclose\fP call deletes a descriptor from the per-process object
18reference table.
19If this is the last reference to the underlying object, then
20it will be deactivated.
21For example, on the last close of a file
22the current \fIseek\fP pointer associated with the file is lost;
23on the last close of a
24.IR socket (2)
25associated naming information and queued data are discarded;
26on the last close of a file holding an advisory lock
27the lock is released; see further
28.IR flock (2).
29.PP
30A close of all of a process's descriptors is automatic on
31.IR exit ,
6fd5737e 32but since
447c53e0 33there is a limit on the number of active descriptors per process,
6fd5737e 34.I close
447c53e0 35is necessary for programs which deal with many descriptors.
6fd5737e 36.PP
447c53e0
KM
37When a process forks (see
38.IR fork (2)),
39all descriptors for the new child process reference the same
40objects as they did in the parent before the fork.
41If a new process is then to be run using
42.IR execve (2),
43the process would normally inherit these descriptors. Most
44of the descriptors can be rearranged with
45.IR dup2 (2)
46or deleted with
47.I close
48before the
49.I execve
50is attempted, but if some of these descriptors will still
51be needed if the execve fails, it is necessary to arrange for them
52to be closed if the execve succeeds.
53For this reason, the call ``fcntl(d, F_SETFD, 1)'' is provided
54which arranges that a descriptor will be closed after a successful
55execve; the call ``fcntl(d, F_SETFD, 0)'' restores the default,
56which is to not close the descriptor.
57.SH "RETURN VALUE
58Upon successful completion, a value of 0 is returned.
59Otherwise, a value of \-1 is returned and the global integer variable
60.I errno
61is set to indicate the error.
62.SH ERRORS
63.I Close
64will fail if:
65.TP 15
66[EBADF]
67\fID\fP is not an active descriptor.
6fd5737e 68.SH "SEE ALSO"
447c53e0
KM
69accept(2), flock(2), open(2), pipe(2), socket(2), socketpair(2),
70execve(2), fcntl(2)