xref: /csrg-svn/lib/libc/sys/close.2 (revision 66929)
1*66929Sbostic.\" Copyright (c) 1980, 1991, 1993, 1994
261181Sbostic.\"	The Regents of the University of California.  All rights reserved.
320020Smckusick.\"
447208Scael.\" %sccs.include.redist.man%
520020Smckusick.\"
6*66929Sbostic.\"     @(#)close.2	8.2 (Berkeley) 04/19/94
747208Scael.\"
847208Scael.Dd
947208Scael.Dt CLOSE 2
1047208Scael.Os BSD 4
1147208Scael.Sh NAME
1247208Scael.Nm close
1347208Scael.Nd delete a descriptor
1447208Scael.Sh SYNOPSIS
1547208Scael.Fd #include <unistd.h>
1647208Scael.Ft int
1747208Scael.Fn close "int d"
1847208Scael.Sh DESCRIPTION
1920021SmckusickThe
2047208Scael.Fn close
2147208Scaelcall deletes a descriptor from the per-process object
2220021Smckusickreference table.
2347208ScaelIf this is the last reference to the underlying object, the
2447208Scaelobject will be deactivated.
2520021SmckusickFor example, on the last close of a file
2647208Scaelthe current
2747208Scael.Em seek
2847208Scaelpointer associated with the file is lost;
2920021Smckusickon the last close of a
3047208Scael.Xr socket 2
3120021Smckusickassociated naming information and queued data are discarded;
3220021Smckusickon the last close of a file holding an advisory lock
3328361Sannethe lock is released (see further
3447208Scael.Xr flock 2 ) .
3547208Scael.Pp
3647208ScaelWhen a process exits,
3747208Scaelall associated file descriptors are freed, but since there is
3847208Scaela limit on active descriptors per processes, the
3947208Scael.Fn close
4047208Scaelfunction call
41*66929Sbosticis useful when a large quantity of file descriptors are being handled.
4247208Scael.Pp
4320021SmckusickWhen a process forks (see
4447208Scael.Xr fork 2 ) ,
4520021Smckusickall descriptors for the new child process reference the same
4620021Smckusickobjects as they did in the parent before the fork.
4720021SmckusickIf a new process is then to be run using
4847208Scael.Xr execve 2 ,
4920021Smckusickthe process would normally inherit these descriptors.  Most
5020021Smckusickof the descriptors can be rearranged with
5147208Scael.Xr dup2 2
5220021Smckusickor deleted with
5347208Scael.Fn close
5420021Smckusickbefore the
5547208Scael.Xr execve
5620021Smckusickis attempted, but if some of these descriptors will still
5720021Smckusickbe needed if the execve fails, it is necessary to arrange for them
5820021Smckusickto be closed if the execve succeeds.
5947208ScaelFor this reason, the call
6047208Scael.Dq Li fcntl(d, F_SETFD, 1)
6147208Scaelis provided,
6220021Smckusickwhich arranges that a descriptor will be closed after a successful
6347208Scaelexecve; the call
6447208Scael.Dq Li fcntl(d, F_SETFD, 0)
6547208Scaelrestores the default,
6620021Smckusickwhich is to not close the descriptor.
6747208Scael.Sh RETURN VALUES
6820021SmckusickUpon successful completion, a value of 0 is returned.
6947208ScaelOtherwise, a value of -1 is returned and the global integer variable
7047208Scael.Va errno
7120021Smckusickis set to indicate the error.
7247208Scael.Sh ERRORS
7347208Scael.Fn Close
7420021Smckusickwill fail if:
7547208Scael.Bl -tag -width Er
7647208Scael.It Bq Er EBADF
7747208Scael.Fa D
7847208Scaelis not an active descriptor.
7947208Scael.It Bq Er EINTR
80*66929SbosticAn interrupt was received.
8147208Scael.El
8247208Scael.Sh SEE ALSO
8347208Scael.Xr accept 2 ,
8447208Scael.Xr flock 2 ,
8547208Scael.Xr open 2 ,
8647208Scael.Xr pipe 2 ,
8747208Scael.Xr socket 2 ,
8847208Scael.Xr socketpair 2 ,
8947208Scael.Xr execve 2 ,
9047208Scael.Xr fcntl 2
9147208Scael.Sh STANDARDS
9247208Scael.Fn Close
9347208Scaelconforms to IEEE Std 1003.1-1988
9447208Scael.Pq Dq Tn POSIX .
95