CLOSE(2)
β¨ CLOSE(2) β¨
okay bestie, we're in the System Calls Manual and this one is lowkey the most underrated baddie in the whole syscall roster π
it's close from the Standard C Library (libc, -lc) and yes, it's exactly what it sounds like.
πΈ NAME πΈ
close β delete a descriptor ποΈβ¨
π LIBRARY π
Standard C Library (libc, -lc)
π¦ SYNOPSIS π¦
#include <unistd.h>
int
close(int fd);
π DESCRIPTION π
so the close() system call deletes a descriptor from the per-process object reference table. that's the whole vibe. and if that was the last reference to the underlying object? the object gets deactivated. gone. bye. π
like, for example:
- on the last close of a file, the current seek pointer associated with the file is lost
- on the last close of a
socket(2), associated naming information and queued data are discarded - on the last close of a file holding an advisory lock, the lock is released (see further
flock(2))
but hold on, plot twist π«’ β the semantics of System V and IEEE Std 1003.1-1988 (βPOSIX.1β) dictate that all fcntl(2) advisory record locks associated with a file for a given process are removed when any file descriptor for that file is closed by that process. not just the last one. any. periodt.
when a process exits, all associated file descriptors are freed, no cap. but since there's a limit on active descriptors per process, the close() system call is genuinely useful when a large quantity of file descriptors are being handled. resource management queen π
when a process forks (see fork(2)), all descriptors for the new child process reference the same objects as they did in the parent before the fork. sibling energy fr fr π―
if a new process is then to be run using execve(2), the process would normally inherit these descriptors. most of the descriptors can be rearranged with dup2(2) or deleted with close() before the execve(2) is attempted, but if some of these descriptors will still be needed if the execve fails, it is necessary to arrange for them to be closed if the execve succeeds.
for this reason, the call:
fcntl(d, F_SETFD, FD_CLOEXEC)
is provided, which arranges that a descriptor will be closed after a successful execve. and the call:
fcntl(d, F_SETFD, 0)
restores the default, which is to not close the descriptor. iconic little toggle tbh β
π RETURN VALUES π
the close() function returns the value 0 if successful π otherwise the value -1 is returned and the global variable errno is set to indicate the error. classic.
π₯ ERRORS π₯
the close() system call will fail if:
[EBADF]β the fd argument is not an active descriptor.[EINTR]β an interrupt was received.[ENOSPC]β the underlying object did not fit, cached data was lost.[ECONNRESET]β the underlying object was a stream socket that was shut down by the peer before all pending data was delivered.
β οΈ IMPORTANT BESTIE: in case of any error except EBADF, the supplied file descriptor is deallocated and therefore is no longer valid. do not go touching it afterwards, she's gone π
π SEE ALSO π
accept(2), closefrom(2), execve(2), fcntl(2), flock(2), open(2), pipe(2), socket(2), socketpair(2)
π STANDARDS π
the close() system call is expected to conform to ISO/IEC 9945-1:1990 (βPOSIX.1β). slay.
β HISTORY β
the close() function appeared in Version 1 AT&T UNIX. an absolute OG, we stan a legend π«Ά
and that's the whole page bestie π this came to you from Debian, dated December 1, 2017 β go forth and close your descriptors responsibly, mwah πβ¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms