The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
fd_table.c File Reference
#include "kernel/sync/atomic.h"
#include "fd_table.h"
#include "kernel/errno.h"
#include "kernel/process/process.h"
#include "kernel/memory/heap.h"
#include "arch/arch.h"

Functions

fd_table_t * fd_table_create (void)
 make a table with every descriptor free and one user.
fd_table_t * fd_table_get (fd_table_t *table)
 add a user to a table (a new thread that shares it).
void fd_table_put (fd_table_t *table)
 remove a user from a table.
int fd_install (process_t *proc, file_t *file, uint32_t fd_flags, int min_fd)
 put a description in the lowest free descriptor of a process, at or above min_fd.
fd_entry_t * fd_get (int fd)
 get the entry for a descriptor of the current process.
void fd_release_held (process_t *proc)
 let go of the files fd_get kept open during a syscall (the syscall handler calls this at its end).
int fd_close (int fd)
 close a descriptor of the current process.
void fd_close_entry (fd_entry_t *entry)
 close the description behind an entry and mark the entry free.
void fd_copy_entry (fd_entry_t *dst, const fd_entry_t *src)
 make dst another descriptor for src's description (fork, spawn).

Function Documentation

◆ fd_table_create()

fd_table_t * fd_table_create ( void )

make a table with every descriptor free and one user.

Returns
the table, or NULL if there is no memory.

◆ fd_table_get()

fd_table_t * fd_table_get ( fd_table_t * table)

add a user to a table (a new thread that shares it).

Returns
table.

◆ fd_table_put()

void fd_table_put ( fd_table_t * table)

remove a user from a table.

the last user closes every descriptor (this may sleep) and frees it.

Parameters
tablethe table, or NULL (nothing is done).

◆ fd_install()

int fd_install ( struct process * proc,
file_t * file,
uint32_t fd_flags,
int min_fd )

put a description in the lowest free descriptor of a process, at or above min_fd.

the descriptor takes over the caller's reference.

Parameters
procthe process.
filethe description.
fd_flagsFD_CLOEXEC or 0.
min_fdthe lowest descriptor to use.
Returns
the descriptor, or -EMFILE if none is free (the caller keeps its reference then).

◆ fd_get()

fd_entry_t * fd_get ( int fd)

get the entry for a descriptor of the current process.

when the table is shared with other threads, the file is kept open until the syscall ends (fd_release_held), even if another thread closes fd meanwhile; use entry->file before any sleep, as the entry itself can change.

Parameters
fdthe file descriptor number.
Returns
the entry, or NULL if fd is out of range or not open.

◆ fd_release_held()

void fd_release_held ( struct process * proc)

let go of the files fd_get kept open during a syscall (the syscall handler calls this at its end).

may sleep: a last close can.

Parameters
procthe process.

◆ fd_close()

int fd_close ( int fd)

close a descriptor of the current process.

Parameters
fdthe file descriptor number.
Returns
0, or -EBADF if fd is not open.

◆ fd_close_entry()

void fd_close_entry ( fd_entry_t * entry)

close the description behind an entry and mark the entry free.

works on any process's table, so it can tear down a process that isn't current. may sleep (the last close of a file takes the fs mutex).

Parameters
entrythe entry; a free entry is left as is.

◆ fd_copy_entry()

void fd_copy_entry ( fd_entry_t * dst,
const fd_entry_t * src )

make dst another descriptor for src's description (fork, spawn).

the flags are copied too. dst must be free.

Parameters
dstthe entry to fill.
srcthe entry to duplicate.