xref: /illumos-gate/usr/src/man/man2/close.2 (revision bbf21555)
166492cf0SYuri Pankov.\"
266492cf0SYuri Pankov.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
366492cf0SYuri Pankov.\" permission to reproduce portions of its copyrighted documentation.
466492cf0SYuri Pankov.\" Original documentation from The Open Group can be obtained online at
5c10c16deSRichard Lowe.\" http://www.opengroup.org/bookstore/.
666492cf0SYuri Pankov.\"
766492cf0SYuri Pankov.\" The Institute of Electrical and Electronics Engineers and The Open
866492cf0SYuri Pankov.\" Group, have given us permission to reprint portions of their
966492cf0SYuri Pankov.\" documentation.
1066492cf0SYuri Pankov.\"
1166492cf0SYuri Pankov.\" In the following statement, the phrase ``this text'' refers to portions
1266492cf0SYuri Pankov.\" of the system documentation.
1366492cf0SYuri Pankov.\"
1466492cf0SYuri Pankov.\" Portions of this text are reprinted and reproduced in electronic form
1566492cf0SYuri Pankov.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
1666492cf0SYuri Pankov.\" Standard for Information Technology -- Portable Operating System
1766492cf0SYuri Pankov.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
1866492cf0SYuri Pankov.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
1966492cf0SYuri Pankov.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
2066492cf0SYuri Pankov.\" between these versions and the original IEEE and The Open Group
2166492cf0SYuri Pankov.\" Standard, the original IEEE and The Open Group Standard is the referee
2266492cf0SYuri Pankov.\" document.  The original Standard can be obtained online at
2366492cf0SYuri Pankov.\" http://www.opengroup.org/unix/online.html.
2466492cf0SYuri Pankov.\"
2566492cf0SYuri Pankov.\" This notice shall appear on any product containing this material.
2666492cf0SYuri Pankov.\"
2766492cf0SYuri Pankov.\" The contents of this file are subject to the terms of the
2866492cf0SYuri Pankov.\" Common Development and Distribution License (the "License").
2966492cf0SYuri Pankov.\" You may not use this file except in compliance with the License.
3066492cf0SYuri Pankov.\"
3166492cf0SYuri Pankov.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
3266492cf0SYuri Pankov.\" or http://www.opensolaris.org/os/licensing.
3366492cf0SYuri Pankov.\" See the License for the specific language governing permissions
3466492cf0SYuri Pankov.\" and limitations under the License.
3566492cf0SYuri Pankov.\"
3666492cf0SYuri Pankov.\" When distributing Covered Code, include this CDDL HEADER in each
3766492cf0SYuri Pankov.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
3866492cf0SYuri Pankov.\" If applicable, add the following below this CDDL HEADER, with the
3966492cf0SYuri Pankov.\" fields enclosed by brackets "[]" replaced with your own identifying
4066492cf0SYuri Pankov.\" information: Portions Copyright [yyyy] [name of copyright owner]
4166492cf0SYuri Pankov.\"
4266492cf0SYuri Pankov.\"
4366492cf0SYuri Pankov.\" Copyright 1989 AT&T
4466492cf0SYuri Pankov.\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
4566492cf0SYuri Pankov.\" Copyright (c) 2005, Sun Microsystems, Inc.  All Rights Reserved.
461fa2a664SJoshua M. Clulow.\" Copyright 2022 Oxide Computer Company
4766492cf0SYuri Pankov.\"
481fa2a664SJoshua M. Clulow.Dd February 5, 2022
491fa2a664SJoshua M. Clulow.Dt CLOSE 2
501fa2a664SJoshua M. Clulow.Os
511fa2a664SJoshua M. Clulow.Sh NAME
521fa2a664SJoshua M. Clulow.Nm close
531fa2a664SJoshua M. Clulow.Nd close a file descriptor
541fa2a664SJoshua M. Clulow.Sh SYNOPSIS
551fa2a664SJoshua M. Clulow.In unistd.h
561fa2a664SJoshua M. Clulow.Ft int
571fa2a664SJoshua M. Clulow.Fo close
581fa2a664SJoshua M. Clulow.Fa "int fildes"
591fa2a664SJoshua M. Clulow.Fc
601fa2a664SJoshua M. Clulow.Sh DESCRIPTION
611fa2a664SJoshua M. ClulowThe
621fa2a664SJoshua M. Clulow.Fn close
631fa2a664SJoshua M. Clulowfunction deallocates the file descriptor indicated by
641fa2a664SJoshua M. Clulow.Fa fildes .
651fa2a664SJoshua M. ClulowTo deallocate means to make the file descriptor available for return by
661fa2a664SJoshua M. Clulowsubsequent calls to
671fa2a664SJoshua M. Clulow.Xr open 2
681fa2a664SJoshua M. Clulowor other functions that allocate file descriptors.
691fa2a664SJoshua M. ClulowAll outstanding record locks owned by the process on the file associated with
701fa2a664SJoshua M. Clulowthe file descriptor will be removed
711fa2a664SJoshua M. Clulow.Pq "that is, unlocked" .
721fa2a664SJoshua M. Clulow.Pp
731fa2a664SJoshua M. ClulowIf
741fa2a664SJoshua M. Clulow.Fn close
751fa2a664SJoshua M. Clulowis interrupted by a signal that is to be caught, it will return
761fa2a664SJoshua M. Clulow.Sy -1
771fa2a664SJoshua M. Clulowwith
781fa2a664SJoshua M. Clulow.Va errno
791fa2a664SJoshua M. Clulowset to
801fa2a664SJoshua M. Clulow.Er EINTR
811fa2a664SJoshua M. Clulowand the state of
821fa2a664SJoshua M. Clulow.Fa fildes
831fa2a664SJoshua M. Clulowis unspecified.
841fa2a664SJoshua M. ClulowIf an I/O error occurred while reading from or writing to the file system during
851fa2a664SJoshua M. Clulow.Fn close ,
861fa2a664SJoshua M. Clulowit returns
871fa2a664SJoshua M. Clulow.Sy -1 ,
881fa2a664SJoshua M. Clulowsets
891fa2a664SJoshua M. Clulow.Va errno
901fa2a664SJoshua M. Clulowto
911fa2a664SJoshua M. Clulow.Er EIO ,
921fa2a664SJoshua M. Clulowand the state of
931fa2a664SJoshua M. Clulow.Fa fildes
941fa2a664SJoshua M. Clulowis unspecified.
951fa2a664SJoshua M. Clulow.Pp
961fa2a664SJoshua M. ClulowWhen all file descriptors associated with a pipe or FIFO special file are
971fa2a664SJoshua M. Clulowclosed, any data remaining in the pipe or FIFO will be discarded.
981fa2a664SJoshua M. Clulow.Pp
99c10c16deSRichard LoweWhen all file descriptors associated with an open file description have been
100c10c16deSRichard Loweclosed the open file description will be freed.
1011fa2a664SJoshua M. Clulow.Pp
1021fa2a664SJoshua M. ClulowIf the link count of the file is
1031fa2a664SJoshua M. Clulow.Sy 0 ,
1041fa2a664SJoshua M. Clulowwhen all file descriptors associated with the file are closed, the space
1051fa2a664SJoshua M. Clulowoccupied by the file will be freed and the file will no longer be accessible.
1061fa2a664SJoshua M. Clulow.Pp
1071fa2a664SJoshua M. ClulowIf a streams-based
1081fa2a664SJoshua M. Clulow.Po
1091fa2a664SJoshua M. Clulowsee
1101fa2a664SJoshua M. Clulow.Xr Intro 2
1111fa2a664SJoshua M. Clulow.Pc
1121fa2a664SJoshua M. Clulow.Fa fildes
1131fa2a664SJoshua M. Clulowis closed and the calling process was previously registered to receive a
1141fa2a664SJoshua M. Clulow.Dv SIGPOLL
1151fa2a664SJoshua M. Clulowsignal
1161fa2a664SJoshua M. Clulow.Po
1171fa2a664SJoshua M. Clulowsee
1181fa2a664SJoshua M. Clulow.Xr signal 3C
1191fa2a664SJoshua M. Clulow.Pc
1201fa2a664SJoshua M. Clulowfor events associated with that stream
1211fa2a664SJoshua M. Clulow.Po
1221fa2a664SJoshua M. Clulowsee
1231fa2a664SJoshua M. Clulow.Dv I_SETSIG
1241fa2a664SJoshua M. Clulowin
125*bbf21555SRichard Lowe.Xr streamio 4I
1261fa2a664SJoshua M. Clulow.Pc ,
1271fa2a664SJoshua M. Clulowthe calling process will be unregistered for events associated with the stream.
1281fa2a664SJoshua M. ClulowThe last
1291fa2a664SJoshua M. Clulow.Fn close
1301fa2a664SJoshua M. Clulowfor a stream causes the stream associated with
1311fa2a664SJoshua M. Clulow.Fa fildes
1321fa2a664SJoshua M. Clulowto be dismantled.
1331fa2a664SJoshua M. ClulowIf
1341fa2a664SJoshua M. Clulow.Dv O_NONBLOCK
1351fa2a664SJoshua M. Clulowand
1361fa2a664SJoshua M. Clulow.Dv O_NDELAY
1371fa2a664SJoshua M. Cluloware not set and there have been no signals posted for the stream, and if there
1381fa2a664SJoshua M. Clulowis data on the module's write queue,
1391fa2a664SJoshua M. Clulow.Fn close
1401fa2a664SJoshua M. Clulowwaits up to 15 seconds
1411fa2a664SJoshua M. Clulow.Pq for each module and driver
1421fa2a664SJoshua M. Clulowfor any output to drain
1431fa2a664SJoshua M. Clulowbefore dismantling the stream.
1441fa2a664SJoshua M. ClulowThe time delay can be changed via an
1451fa2a664SJoshua M. Clulow.Dv I_SETCLTIME
1461fa2a664SJoshua M. Clulow.Xr ioctl 2
1471fa2a664SJoshua M. Clulowrequest
1481fa2a664SJoshua M. Clulow.Po
1491fa2a664SJoshua M. Clulowsee
150*bbf21555SRichard Lowe.Xr streamio 4I
1511fa2a664SJoshua M. Clulow.Pc .
1521fa2a664SJoshua M. ClulowIf the
1531fa2a664SJoshua M. Clulow.Dv O_NONBLOCK
1541fa2a664SJoshua M. Clulowor
1551fa2a664SJoshua M. Clulow.Dv O_NDELAY
1561fa2a664SJoshua M. Clulowflag is set, or if there are any pending signals,
1571fa2a664SJoshua M. Clulow.Fn close
158c10c16deSRichard Lowedoes not wait for output to drain, and dismantles the stream immediately.
1591fa2a664SJoshua M. Clulow.Pp
1601fa2a664SJoshua M. ClulowIf
1611fa2a664SJoshua M. Clulow.Fa fildes
1621fa2a664SJoshua M. Clulowis associated with one end of a pipe, the last
1631fa2a664SJoshua M. Clulow.Fn close
1641fa2a664SJoshua M. Clulowcauses a hangup to occur on the other end of the pipe.
1651fa2a664SJoshua M. ClulowIn addition, if the other end of the pipe has been named by
1661fa2a664SJoshua M. Clulow.Xr fattach 3C ,
1671fa2a664SJoshua M. Clulowthen the last
1681fa2a664SJoshua M. Clulow.Fn close
1691fa2a664SJoshua M. Clulowforces the named end to be detached by
1701fa2a664SJoshua M. Clulow.Xr fdetach 3C .
1711fa2a664SJoshua M. ClulowIf the named end has no open file descriptors associated with it and gets
1721fa2a664SJoshua M. Clulowdetached, the stream associated with that end is also dismantled.
1731fa2a664SJoshua M. Clulow.Pp
1741fa2a664SJoshua M. ClulowIf
1751fa2a664SJoshua M. Clulow.Fa fildes
1761fa2a664SJoshua M. Clulowrefers to the manager side of a pseudo-terminal, a
1771fa2a664SJoshua M. Clulow.Dv SIGHUP
1781fa2a664SJoshua M. Clulowsignal is sent to the session leader, if any, for which the subsidiary side of
1791fa2a664SJoshua M. Clulowthe pseudo-terminal is the controlling terminal.
1801fa2a664SJoshua M. ClulowIt is unspecified whether closing the manager side of the pseudo-terminal
1811fa2a664SJoshua M. Clulowflushes all queued input and output.
1821fa2a664SJoshua M. Clulow.Pp
1831fa2a664SJoshua M. ClulowIf
1841fa2a664SJoshua M. Clulow.Fa fildes
1851fa2a664SJoshua M. Clulowrefers to the subsidiary side of a streams-based pseudo-terminal, a zero-length
1861fa2a664SJoshua M. Clulowmessage may be sent to the manager.
1871fa2a664SJoshua M. Clulow.Pp
188c10c16deSRichard LoweWhen there is an outstanding cancelable asynchronous I/O operation against
1891fa2a664SJoshua M. Clulow.Fa fildes
1901fa2a664SJoshua M. Clulowwhen
1911fa2a664SJoshua M. Clulow.Fn close
1921fa2a664SJoshua M. Clulowis called, that I/O operation is canceled.
1931fa2a664SJoshua M. ClulowAn I/O operation that is not canceled completes as if the
1941fa2a664SJoshua M. Clulow.Fn close
1951fa2a664SJoshua M. Clulowoperation had not yet occurred.
1961fa2a664SJoshua M. ClulowAll operations that are not canceled will complete as if the
1971fa2a664SJoshua M. Clulow.Fn close
1981fa2a664SJoshua M. Clulowblocked until the operations completed.
1991fa2a664SJoshua M. Clulow.Pp
200c10c16deSRichard LoweIf a shared memory object or a memory mapped file remains referenced at the
2011fa2a664SJoshua M. Clulowlast close
2021fa2a664SJoshua M. Clulow.Pq "that is, a process has it mapped" ,
2031fa2a664SJoshua M. Clulowthen the entire contents of the memory object will persist until the memory
2041fa2a664SJoshua M. Clulowobject becomes unreferenced.
2051fa2a664SJoshua M. ClulowIf this is the last close of a shared memory object or a memory mapped file and
206c10c16deSRichard Lowethe close results in the memory object becoming unreferenced, and the memory
207c10c16deSRichard Loweobject has been unlinked, then the memory object will be removed.
2081fa2a664SJoshua M. Clulow.Pp
2091fa2a664SJoshua M. ClulowIf
2101fa2a664SJoshua M. Clulow.Fa fildes
2111fa2a664SJoshua M. Clulowrefers to a socket,
2121fa2a664SJoshua M. Clulow.Fn close
2131fa2a664SJoshua M. Clulowcauses the socket to be destroyed.
2141fa2a664SJoshua M. ClulowIf the socket is connection-mode, and the
2151fa2a664SJoshua M. Clulow.Dv SO_LINGER
2161fa2a664SJoshua M. Clulowoption is set for the socket with non-zero linger time, and the socket has
2171fa2a664SJoshua M. Clulowuntransmitted data, then
2181fa2a664SJoshua M. Clulow.Fn close
2191fa2a664SJoshua M. Clulowwill block for up to the current linger interval until all data is transmitted.
2201fa2a664SJoshua M. Clulow.Sh RETURN VALUES
2211fa2a664SJoshua M. Clulow.Rv -std close
2221fa2a664SJoshua M. Clulow.Sh EXAMPLES
2231fa2a664SJoshua M. Clulow.Sy Example 1
2241fa2a664SJoshua M. ClulowReassign a file descriptor.
2251fa2a664SJoshua M. Clulow.Pp
226c10c16deSRichard LoweThe following example closes the file descriptor associated with standard
227c10c16deSRichard Loweoutput for the current process, re-assigns standard output to a new file
2281fa2a664SJoshua M. Clulowdescriptor, and closes the original file descriptor to clean up.
2291fa2a664SJoshua M. ClulowThis example assumes that the file descriptor
2301fa2a664SJoshua M. Clulow.Sy 0 ,
2311fa2a664SJoshua M. Clulowwhich is the descriptor for standard input, is not closed.
2321fa2a664SJoshua M. Clulow.Bd -literal -offset Ds
233c10c16deSRichard Lowe#include <unistd.h>
234c10c16deSRichard Lowe\&...
235c10c16deSRichard Loweint pfd;
236c10c16deSRichard Lowe\&...
237c10c16deSRichard Loweclose(1);
238c10c16deSRichard Lowedup(pfd);
239c10c16deSRichard Loweclose(pfd);
240c10c16deSRichard Lowe\&...
2411fa2a664SJoshua M. Clulow.Ed
2421fa2a664SJoshua M. Clulow.Pp
243c10c16deSRichard LoweIncidentally, this is exactly what could be achieved using:
2441fa2a664SJoshua M. Clulow.Bd -literal -offset Ds
245c10c16deSRichard Lowedup2(pfd, 1);
246c10c16deSRichard Loweclose(pfd);
2471fa2a664SJoshua M. Clulow.Ed
2481fa2a664SJoshua M. Clulow.Pp
2491fa2a664SJoshua M. Clulow.Sy Example 2
2501fa2a664SJoshua M. ClulowClose a file descriptor.
2511fa2a664SJoshua M. Clulow.Pp
2521fa2a664SJoshua M. ClulowIn the following example,
2531fa2a664SJoshua M. Clulow.Fn close
2541fa2a664SJoshua M. Clulowis used to close a file descriptor after an unsuccessful attempt is made to
2551fa2a664SJoshua M. Clulowassociate that file descriptor with a stream.
2561fa2a664SJoshua M. Clulow.Bd -literal -offset Ds
257c10c16deSRichard Lowe#include <stdio.h>
258c10c16deSRichard Lowe#include <unistd.h>
259c10c16deSRichard Lowe#include <stdlib.h>
260c10c16deSRichard Lowe
261c10c16deSRichard Lowe#define LOCKFILE "/etc/ptmp"
262c10c16deSRichard Lowe\&...
263c10c16deSRichard Loweint pfd;
264c10c16deSRichard LoweFILE *fpfd;
265c10c16deSRichard Lowe\&...
266c10c16deSRichard Loweif ((fpfd = fdopen (pfd, "w")) == NULL) {
267c10c16deSRichard Lowe        close(pfd);
268c10c16deSRichard Lowe        unlink(LOCKFILE);
269c10c16deSRichard Lowe        exit(1);
270c10c16deSRichard Lowe}
271c10c16deSRichard Lowe\&...
2721fa2a664SJoshua M. Clulow.Ed
2731fa2a664SJoshua M. Clulow.Sh ERRORS
2741fa2a664SJoshua M. ClulowThe
2751fa2a664SJoshua M. Clulow.Fn close
2761fa2a664SJoshua M. Clulowfunction will fail if:
2771fa2a664SJoshua M. Clulow.Bl -tag -width Er
2781fa2a664SJoshua M. Clulow.It Er EBADF
2791fa2a664SJoshua M. ClulowThe
2801fa2a664SJoshua M. Clulow.Fa fildes
2811fa2a664SJoshua M. Clulowargument is not a valid file descriptor.
2821fa2a664SJoshua M. Clulow.It Er EINTR
2831fa2a664SJoshua M. ClulowThe
2841fa2a664SJoshua M. Clulow.Fn close
2851fa2a664SJoshua M. Clulowfunction was interrupted by a signal.
2861fa2a664SJoshua M. Clulow.It Er ENOLINK
2871fa2a664SJoshua M. ClulowThe
2881fa2a664SJoshua M. Clulow.Fa fildes
2891fa2a664SJoshua M. Clulowargument is on a remote machine and the link to that machine is no longer
2901fa2a664SJoshua M. Clulowactive.
2911fa2a664SJoshua M. Clulow.It Er ENOSPC
2921fa2a664SJoshua M. ClulowThere was no free space remaining on the device containing the file.
2931fa2a664SJoshua M. Clulow.El
2941fa2a664SJoshua M. Clulow.Pp
2951fa2a664SJoshua M. ClulowThe
2961fa2a664SJoshua M. Clulow.Fn close
2971fa2a664SJoshua M. Clulowfunction may fail if:
2981fa2a664SJoshua M. Clulow.Bl -tag -width Er
2991fa2a664SJoshua M. Clulow.It Er EIO
3001fa2a664SJoshua M. ClulowAn I/O error occurred while reading from or writing to the file system.
3011fa2a664SJoshua M. Clulow.El
3021fa2a664SJoshua M. Clulow.Sh USAGE
3031fa2a664SJoshua M. ClulowAn application that used the
3041fa2a664SJoshua M. Clulow.Xr stdio 3C
3051fa2a664SJoshua M. Clulowfunction
3061fa2a664SJoshua M. Clulow.Xr fopen 3C
3071fa2a664SJoshua M. Clulowto open a file should use the corresponding
3081fa2a664SJoshua M. Clulow.Xr fclose 3C
3091fa2a664SJoshua M. Clulowfunction rather than
3101fa2a664SJoshua M. Clulow.Fn close .
3111fa2a664SJoshua M. Clulow.Sh INTERFACE STABILITY
3121fa2a664SJoshua M. Clulow.Sy Committed
3131fa2a664SJoshua M. Clulow.Sh MT-LEVEL
3141fa2a664SJoshua M. Clulow.Sy Async-Signal-Safe
3151fa2a664SJoshua M. Clulow.Sh SEE ALSO
3161fa2a664SJoshua M. Clulow.Xr creat 2 ,
3171fa2a664SJoshua M. Clulow.Xr dup 2 ,
3181fa2a664SJoshua M. Clulow.Xr exec 2 ,
3191fa2a664SJoshua M. Clulow.Xr fcntl 2 ,
3201fa2a664SJoshua M. Clulow.Xr Intro 2 ,
3211fa2a664SJoshua M. Clulow.Xr ioctl 2 ,
3221fa2a664SJoshua M. Clulow.Xr open 2 ,
3231fa2a664SJoshua M. Clulow.Xr pipe 2 ,
3241fa2a664SJoshua M. Clulow.Xr fattach 3C ,
3251fa2a664SJoshua M. Clulow.Xr fclose 3C ,
3261fa2a664SJoshua M. Clulow.Xr fdetach 3C ,
3271fa2a664SJoshua M. Clulow.Xr fopen 3C ,
3281fa2a664SJoshua M. Clulow.Xr signal 3C ,
3291fa2a664SJoshua M. Clulow.Xr signal.h 3HEAD ,
330*bbf21555SRichard Lowe.Xr streamio 4I ,
331*bbf21555SRichard Lowe.Xr attributes 7 ,
332*bbf21555SRichard Lowe.Xr standards 7
333