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