NetBSD/lib/libc/sys/close.2

137 lines
4.2 KiB
Groff
Raw Normal View History

.\" $NetBSD: close.2,v 1.10 1999/12/02 21:42:36 kleink Exp $
.\"
.\" Copyright (c) 1980, 1991, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)close.2 8.2 (Berkeley) 4/19/94
1993-03-21 12:45:37 +03:00
.\"
.Dd April 19, 1994
1993-03-21 12:45:37 +03:00
.Dt CLOSE 2
.Os
1993-03-21 12:45:37 +03:00
.Sh NAME
.Nm close
.Nd delete a descriptor
.Sh LIBRARY
.Lb libc
1993-03-21 12:45:37 +03:00
.Sh SYNOPSIS
.Fd #include <unistd.h>
.Ft int
.Fn close "int d"
.Sh DESCRIPTION
The
.Fn close
1998-04-29 02:28:30 +04:00
system call deletes a descriptor from the per-process object
1993-03-21 12:45:37 +03:00
reference table.
If this is the last reference to the underlying object, the
object will be deactivated.
1998-04-29 02:28:30 +04:00
For example, on the last close of a file the current
1993-03-21 12:45:37 +03:00
.Em seek
1998-04-29 02:28:30 +04:00
pointer associated with the file is lost; on the last close of a
1993-03-21 12:45:37 +03:00
.Xr socket 2
1998-04-29 02:28:30 +04:00
associated naming information and queued data are discarded; on
the last close of a file holding an advisory lock the lock is
released (see
1993-03-21 12:45:37 +03:00
.Xr flock 2 ) .
.Pp
1998-04-29 02:28:30 +04:00
When a process exits, all associated descriptors are freed, but
since there is a limit on active descriptors per processes, the
1993-03-21 12:45:37 +03:00
.Fn close
1998-04-29 02:28:30 +04:00
system call
1994-01-11 02:54:08 +03:00
is useful when a large quantity of file descriptors are being handled.
1993-03-21 12:45:37 +03:00
.Pp
1998-04-29 02:28:30 +04:00
When a process calls
.Xr fork 2 ,
1993-03-21 12:45:37 +03:00
all descriptors for the new child process reference the same
1998-04-29 02:28:30 +04:00
objects as they did in the parent before the
.Fn fork .
1993-03-21 12:45:37 +03:00
If a new process is then to be run using
.Xr execve 2 ,
1998-04-29 02:28:30 +04:00
the process would normally inherit these descriptors.
Most of the descriptors can be rearranged with
1993-03-21 12:45:37 +03:00
.Xr dup2 2
or deleted with
.Fn close
before the
1998-04-29 02:28:30 +04:00
.Fn execve
is attempted, but if some of these descriptors will still be needed if the
.Fn execve
fails, it is necessary to arrange for them to be closed only if the
.Fn execve
succeeds.
For this reason, the system call
.Bl -item -offset indent
.It
.Fn fcntl d F_SETFD 1 ;
.El
.Pp
1993-03-21 12:45:37 +03:00
is provided,
1998-04-29 02:28:30 +04:00
which arranges that a descriptor
.Dq Fa d
will be closed after a successful
.Fn execve ;
the system call
.Bl -item -offset indent
.It
.Fn fcntl d F_SETFD 0 ;
.El
.Pp
restores the default, which is to not close descriptor
.Dq Fa d .
1993-03-21 12:45:37 +03:00
.Sh RETURN VALUES
Upon successful completion, a value of 0 is returned.
Otherwise, a value of -1 is returned and the global integer variable
.Va errno
is set to indicate the error.
.Sh ERRORS
.Fn close
1993-03-21 12:45:37 +03:00
will fail if:
.Bl -tag -width Er
.It Bq Er EBADF
.Fa d
1993-03-21 12:45:37 +03:00
is not an active descriptor.
.It Bq Er EINTR
1994-01-11 02:54:08 +03:00
An interrupt was received.
1993-03-21 12:45:37 +03:00
.El
.Sh SEE ALSO
.Xr accept 2 ,
1998-04-29 02:28:30 +04:00
.Xr execve 2 ,
.Xr fcntl 2 ,
1993-03-21 12:45:37 +03:00
.Xr flock 2 ,
.Xr open 2 ,
.Xr pipe 2 ,
.Xr socket 2 ,
1998-04-29 02:28:30 +04:00
.Xr socketpair 2
1993-03-21 12:45:37 +03:00
.Sh STANDARDS
The
.Fn close
function conforms to
.St -p1003.1-90 .