AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
file.c File Reference

Open files (struct file, in fs/vfs.h): what open(), pipe(), socket() and accept() make, and what descriptors refer to. More...

#include <core/system.h>
#include <core/panic.h>
#include <fs/vfs.h>
#include <sys/proc.h>
#include <sys/thread.h>
#include <sys/sched.h>

Go to the source code of this file.

Functions

struct file * file_new (void)
 Makes a new open file, with one reference.
struct file * file_ref (struct file *file)
 Adds a reference to an open file.
int file_put (struct file *file)
 Drops a reference to an open file.
struct file * file_hold (struct file *file)
 Holds an open file for the current system call, until it returns.
void thread_files_release (struct thread *thread)
 Lets go of the open files a thread's system call held, when the call returns or the thread is killed.

Detailed Description

Open files (struct file, in fs/vfs.h): what open(), pipe(), socket() and accept() make, and what descriptors refer to.

An open file is shared by everything that refers to it: descriptors that dup() and fork() copied share it, with its offset and status flags (what POSIX calls an open file description). It counts its references, from descriptors and from system calls using it. When the last one goes, the file is closed (the filesystem's or the device's close runs once) and freed.

A system call holds the files it uses (file_hold()) until it returns, so that a close() by another thread meanwhile can't free a file under it. The thread keeps the list, so a thread killed in the middle of a call lets go of them too.

Definition in file file.c.

Function Documentation

◆ file_hold()

struct file * file_hold ( struct file * file)

Holds an open file for the current system call, until it returns.

Parameters
filethe file
Returns
file

Definition at line 82 of file file.c.

References curthread, file_hold(), file_ref(), thread::files, thread::files_held, panic, and THREAD_FILES_HELD.

Referenced by fd_file(), and file_hold().

◆ file_new()

struct file * file_new ( void )

Makes a new open file, with one reference.

Returns
the file, or NULL if there was no memory

Definition at line 32 of file file.c.

References file_new(), kmalloc(), M_ZERO, and file::ref.

Referenced by do_openat(), file_new(), sys_accept(), sys_pipe(), and sys_socket().

◆ file_put()

int file_put ( struct file * file)

Drops a reference to an open file.

The last one closes and frees it.

Parameters
filethe file
Returns
0, or what closing the file returned

Definition at line 60 of file file.c.

References file_put(), kfree(), panic, file::ref, and vfs_file_close().

Referenced by file_put(), proc_fd_close(), and thread_files_release().

◆ file_ref()

struct file * file_ref ( struct file * file)

Adds a reference to an open file.

Parameters
filethe file
Returns
file

Definition at line 48 of file file.c.

References file_ref(), and file::ref.

Referenced by copy_fds(), file_hold(), file_ref(), and sys_fcntl().

◆ thread_files_release()

void thread_files_release ( struct thread * thread)

Lets go of the open files a thread's system call held, when the call returns or the thread is killed.

Parameters
threadthe thread

Definition at line 99 of file file.c.

References file_put(), thread::files, thread::files_held, and thread_files_release().

Referenced by proc_kill(), proc_threads_end(), syscall_dispatch(), and thread_files_release().