| 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 | .\"
|
|---|
| 5 | .\" @(#)close.2 6.3 (Berkeley) 5/22/86
|
|---|
| 6 | .\"
|
|---|
| 7 | .TH CLOSE 2 "May 22, 1986"
|
|---|
| 8 | .UC 4
|
|---|
| 9 | .SH NAME
|
|---|
| 10 | close \- delete a descriptor
|
|---|
| 11 | .SH SYNOPSIS
|
|---|
| 12 | .nf
|
|---|
| 13 | .ft B
|
|---|
| 14 | #include <unistd.h>
|
|---|
| 15 |
|
|---|
| 16 | int close(int \fId\fP)
|
|---|
| 17 | .ft R
|
|---|
| 18 | .fi
|
|---|
| 19 | .SH DESCRIPTION
|
|---|
| 20 | The
|
|---|
| 21 | .B close
|
|---|
| 22 | call deletes a descriptor from the per-process object
|
|---|
| 23 | reference table.
|
|---|
| 24 | If this is the last reference to the underlying object, then
|
|---|
| 25 | it will be deactivated.
|
|---|
| 26 | For example, on the last close of a file
|
|---|
| 27 | the current \fIseek\fP pointer associated with the file is lost;
|
|---|
| 28 | on the last close of a TCP/IP descriptor
|
|---|
| 29 | associated naming information and queued data are discarded;
|
|---|
| 30 | on the last close of a file holding an advisory lock
|
|---|
| 31 | the lock is released (see further
|
|---|
| 32 | .BR fcntl (2)).
|
|---|
| 33 | .PP
|
|---|
| 34 | A close of all of a process's descriptors is automatic on
|
|---|
| 35 | .IR exit ,
|
|---|
| 36 | but since
|
|---|
| 37 | there is a limit on the number of active descriptors per process,
|
|---|
| 38 | .B close
|
|---|
| 39 | is necessary for programs that deal with many descriptors.
|
|---|
| 40 | .PP
|
|---|
| 41 | When a process forks (see
|
|---|
| 42 | .BR fork (2)),
|
|---|
| 43 | all descriptors for the new child process reference the same
|
|---|
| 44 | objects as they did in the parent before the fork.
|
|---|
| 45 | If a new process is then to be run using
|
|---|
| 46 | .BR execve (2),
|
|---|
| 47 | the process would normally inherit these descriptors. Most
|
|---|
| 48 | of the descriptors can be rearranged with
|
|---|
| 49 | .BR dup2 (2)
|
|---|
| 50 | or deleted with
|
|---|
| 51 | .B close
|
|---|
| 52 | before the
|
|---|
| 53 | .B execve
|
|---|
| 54 | is attempted, but if some of these descriptors will still
|
|---|
| 55 | be needed if the
|
|---|
| 56 | .B execve
|
|---|
| 57 | fails, it is necessary to arrange for them to be closed if the
|
|---|
| 58 | .B execve
|
|---|
| 59 | succeeds.
|
|---|
| 60 | For this reason, the call ``fcntl(d, F_SETFD, \fIflags\fR)'' is provided,
|
|---|
| 61 | that can be used to mark a descriptor "close on exec" by setting
|
|---|
| 62 | the
|
|---|
| 63 | .B FD_CLOEXEC
|
|---|
| 64 | flag:
|
|---|
| 65 | .PP
|
|---|
| 66 | .RS
|
|---|
| 67 | fcntl(d, F_SETFD, fcntl(d, F_GETFD) | FD_CLOEXEC);
|
|---|
| 68 | .RE
|
|---|
| 69 | .SH "RETURN VALUE
|
|---|
| 70 | Upon successful completion, a value of 0 is returned.
|
|---|
| 71 | Otherwise, a value of \-1 is returned and the global integer variable
|
|---|
| 72 | .B errno
|
|---|
| 73 | is set to indicate the error.
|
|---|
| 74 | .SH ERRORS
|
|---|
| 75 | .B Close
|
|---|
| 76 | will fail if:
|
|---|
| 77 | .TP 15
|
|---|
| 78 | [EBADF]
|
|---|
| 79 | \fID\fP is not an active descriptor.
|
|---|
| 80 | .SH "SEE ALSO"
|
|---|
| 81 | .BR open (2),
|
|---|
| 82 | .BR pipe (2),
|
|---|
| 83 | .BR execve (2),
|
|---|
| 84 | .BR fcntl (2).
|
|---|