no SCCS file; new copyright; att/bsd/shared
[unix-history] / usr / src / lib / libc / sys / close.2
CommitLineData
931b8415
CL
1.\" Copyright (c) 1980, 1991 Regents of the University of California.
2.\" All rights reserved.
6fd5737e 3.\"
931b8415 4.\" %sccs.include.redist.man%
6fd5737e 5.\"
931b8415
CL
6.\" @(#)close.2 6.4 (Berkeley) %G%
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
447c53e0 19The
931b8415
CL
20.Fn close
21call deletes a descriptor from the per-process object
447c53e0 22reference table.
931b8415
CL
23If this is the last reference to the underlying object, the
24object will be deactivated.
447c53e0 25For example, on the last close of a file
931b8415
CL
26the current
27.Em seek
28pointer associated with the file is lost;
447c53e0 29on the last close of a
931b8415 30.Xr socket 2
447c53e0
KM
31associated naming information and queued data are discarded;
32on the last close of a file holding an advisory lock
d4bad45b 33the lock is released (see further
931b8415
CL
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 quanitity of file descriptors are being handled.
42.Pp
447c53e0 43When a process forks (see
931b8415 44.Xr fork 2 ) ,
447c53e0
KM
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
931b8415 48.Xr execve 2 ,
447c53e0
KM
49the process would normally inherit these descriptors. Most
50of the descriptors can be rearranged with
931b8415 51.Xr dup2 2
447c53e0 52or deleted with
931b8415 53.Fn close
447c53e0 54before the
931b8415 55.Xr execve
447c53e0
KM
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.
931b8415
CL
59For this reason, the call
60.Dq Li fcntl(d, F_SETFD, 1)
61is provided,
447c53e0 62which arranges that a descriptor will be closed after a successful
931b8415
CL
63execve; the call
64.Dq Li fcntl(d, F_SETFD, 0)
65restores the default,
447c53e0 66which is to not close the descriptor.
931b8415 67.Sh RETURN VALUES
447c53e0 68Upon successful completion, a value of 0 is returned.
931b8415
CL
69Otherwise, a value of -1 is returned and the global integer variable
70.Va errno
447c53e0 71is set to indicate the error.
931b8415
CL
72.Sh ERRORS
73.Fn Close
447c53e0 74will fail if:
931b8415
CL
75.Bl -tag -width Er
76.It Bq Er EBADF
77.Fa D
78is not an active descriptor.
79.It Bq Er EINTR
80An interupt 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 .