1*d44a6ffeSmatthieu.\" $OpenBSD: file.9,v 1.23 2024/11/09 15:54:14 matthieu Exp $ 256247843Sart.\" 356247843Sart.\" Copyright (c) 2002 Artur Grabowski <art@openbsd.org> 456247843Sart.\" All rights reserved. 556247843Sart.\" 656247843Sart.\" Redistribution and use in source and binary forms, with or without 756247843Sart.\" modification, are permitted provided that the following conditions 856247843Sart.\" are met: 956247843Sart.\" 1. Redistributions of source code must retain the above copyright 1056247843Sart.\" notice, this list of conditions and the following disclaimer. 1156247843Sart.\" 2. The name of the author may not be used to endorse or promote products 1256247843Sart.\" derived from this software without specific prior written permission 1356247843Sart.\" 1456247843Sart.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1556247843Sart.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1656247843Sart.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1756247843Sart.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1856247843Sart.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1956247843Sart.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2056247843Sart.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2156247843Sart.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2256247843Sart.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2356247843Sart.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2456247843Sart.\" 25*d44a6ffeSmatthieu.Dd $Mdocdate: November 9 2024 $ 2633378d91Sjmc.Dt FALLOC 9 2756247843Sart.Os 2856247843Sart.Sh NAME 2933378d91Sjmc.Nm falloc , 3033378d91Sjmc.Nm fdrelease , 3133378d91Sjmc.Nm FREF , 3233378d91Sjmc.Nm FRELE , 3333378d91Sjmc.Nm fd_getfile , 34a747643cSvisa.Nm fd_getfile_mode , 35210860c7Svisa.Nm fd_checkclosed , 3633378d91Sjmc.Nm getsock , 3733378d91Sjmc.Nm getvnode 3856247843Sart.Nd an overview of file descriptor handling 3956247843Sart.Sh SYNOPSIS 40dddd2645Sschwarze.In sys/file.h 41dddd2645Sschwarze.In sys/filedesc.h 4256247843Sart.Ft int 43*d44a6ffeSmatthieu.Fn falloc "struct proc *p" "struct file **resultfp" "int *resultfd" 4456247843Sart.Ft int 4556247843Sart.Fn fdrelease "struct proc *p" "int fd" 4656247843Sart.Ft void 4756247843Sart.Fn FREF "struct file *fp" 4817f4e87fSmillert.Ft int 49e23836ebSguenther.Fn FRELE "struct file *fp" "struct proc *p" 5056247843Sart.Ft struct file * 5156247843Sart.Fn fd_getfile "struct filedesc *fdp" "int fd" 52a747643cSvisa.Ft struct file * 53a747643cSvisa.Fn fd_getfile_mode "struct filedesc *fdp" "int fd" "int mode" 5456247843Sart.Ft int 55210860c7Svisa.Fn fd_checkclosed "struct filedesc *fdp" "int fd" "struct file *fp" 56210860c7Svisa.Ft int 574051c3a3Smpi.Fn getsock "struct proc *p" "int fd" "struct file **fpp" 58dddd2645Sschwarze.In sys/file.h 59dddd2645Sschwarze.In sys/filedesc.h 60dddd2645Sschwarze.In sys/vnode.h 61c0ca7f2aSjmc.Ft int 62f4ce75d2Smpi.Fn getvnode "struct proc *p" "int fd" "struct file **fpp" 6356247843Sart.Sh DESCRIPTION 6456247843SartThese functions provide the interface for the UNIX file descriptors. 6556247843SartFile descriptors can be used to access vnodes (see 6656247843Sart.Xr vnode 9 ) , 6756247843Sartsockets (see 6856247843Sart.Xr socket 2 ) , 6956247843Sartpipes (see 7056247843Sart.Xr pipe 2 ) , 7156247843Sartkqueues (see 7256247843Sart.Xr kqueue 2 ) , 7356247843Sartand various special purpose communication endpoints. 74146ab397Smpech.Pp 7565014aebSguentherA new file and a file descriptor for it are allocated with the function 7665014aebSguenther.Fn falloc . 7765014aebSguentherThe larval file and fd are returned via 7865014aebSguenther.Fa resultfp 7956247843Sartand 8065014aebSguenther.Fa resultfd , 8165014aebSguentherwhich must not be 8265014aebSguenther.Dv NULL . 8365014aebSguenther.Fn falloc 8465014aebSguentherinitializes the new file to have a reference count of two: 8565014aebSguentherone for the reference from the file descriptor table and one 8665014aebSguentherfor the caller to release with 8765014aebSguenther.Fn FRELE 88a6fd7085Sjmcwhen it's done initializing it. 8965014aebSguenther.Pp 9065014aebSguentherA file descriptor is freed with 9165014aebSguenther.Fn fdrelease . 9265014aebSguentherThis releases the reference that it holds to the underlying file; 9365014aebSguentherif that's the last reference then the file will be freed. 9465014aebSguenther.\" with 9565014aebSguenther.\" .Xr closef 9 . 96324c2fc0SvisaThe file descriptor table has to be locked on entry. 97324c2fc0Svisa.Fn fdrelease 98324c2fc0Svisaunlocks the table on return. 9956247843Sart.Pp 10056247843SartThe files are extracted from the file descriptor table using the 10139a43e07Svisafunction 10239a43e07Svisa.Fn fd_getfile . 10356247843Sart.Fn fd_getfile 10456247843Sartperforms all necessary checks to see if the file descriptor number is 10556247843Sartwithin the range of file descriptor table, and if the descriptor is 10656247843Sartvalid. 10739a43e07SvisaIt also increases the descriptor's use count with 10839a43e07Svisa.Fn FREF . 1094051c3a3Smpi.Pp 110a747643cSvisa.Fn fd_getfile_mode 111a747643cSvisais like 112a747643cSvisa.Fn fd_getfile 113a747643cSvisabut also checks if the file has been opened with the given mode. 114a747643cSvisa.Pp 115210860c7Svisa.Fn fd_checkclosed 116210860c7Svisachecks if file descriptor 117210860c7Svisa.Fa fd 118210860c7Svisahas been closed and no longer points to file 119210860c7Svisa.Fa fp . 120210860c7SvisaThe file must have been retrieved from the descriptor previously. 121210860c7Svisa.Pp 1224051c3a3SmpiThe files are extracted from the process context using the 1234051c3a3Smpifunction 1244051c3a3Smpi.Fn getsock 125f4ce75d2Smpiand 126f4ce75d2Smpi.Fn getvnode . 127f4ce75d2SmpiThese functions are special cases that besides doing 1284051c3a3Smpi.Fn fd_getfile 129f4ce75d2Smpialso check if the descriptor is a socket or a vnode respectively, 13039a43e07Svisaand return the proper errno on error. 13156247843Sart.Sh CONCURRENT ACCESS 13218280b74SjmcSince multiple processes can share the same file descriptor table, 13356247843Sartit's important that the file is not freed in one process while some 13456247843Sartother process is still accessing it. 13556247843SartTo solve that problem a special use count is kept with the functions 13656247843Sart.Fn FREF 13756247843Sartand 13856247843Sart.Fn FRELE . 13939a43e07SvisaThe function 14056247843Sart.Fn FREF 14139a43e07Svisaincreases the use count of a file descriptor. 14239a43e07SvisaThe function 14356247843Sart.Fn FRELE 14439a43e07Svisadecreases the use count, and releases the file descriptor if the use count 14539a43e07Svisabecomes zero. 14656247843Sart.Sh CODE REFERENCES 14796adb44aSmpechThe majority of those functions are implemented in 14856247843Sart.Pa sys/kern/kern_descrip.c . 14956247843SartThe function prototypes and the macros are located in 15056247843Sart.Pa sys/sys/file.h 15156247843Sartand 15256247843Sart.Pa sys/sys/filedesc.h . 1534f82885cSjasper.Sh SEE ALSO 1544f82885cSjasper.Xr vnode 9 155