The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
fd_table.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * per-process file descriptor tables. a descriptor is a pointer to an
5 * open-file description (kernel/vfs/file.h) plus its own flags; several
6 * descriptors (in one process after dup, or in several after fork) can
7 * point to the same description.
8 */
9
10#include "common/stdint.h"
11#include "kernel/vfs/file.h"
12
13struct process;
14
15// maximum number of file descriptors per process.
16#define FD_MAX 64
17
18// number of standard I/O descriptors (stdin, stdout, stderr).
19#define FD_STDIO_COUNT 3
20
21// descriptor flags (fcntl F_GETFD / F_SETFD)
22#define FD_CLOEXEC 1 // closed by execve
23
24/**
25 * a single file descriptor entry.
26 */
27typedef struct
28{
29 file_t *file; // the open-file description, or NULL if the slot is free
30 uint32_t flags; // FD_CLOEXEC
32
33/**
34 * the descriptors of a process. the threads of a process use one table
35 * together; it is freed when the last of them lets it go.
36 */
37typedef struct fd_table
38{
39 volatile uint32_t users; // the processes (threads) that use it (atomic)
40 fd_entry_t fd[FD_MAX]; // indexed by descriptor number
42
43/**
44 * make a table with every descriptor free and one user.
45 * @return the table, or NULL if there is no memory.
46 */
48
49/**
50 * add a user to a table (a new thread that shares it).
51 * @return table.
52 */
54
55/**
56 * remove a user from a table. the last user closes every descriptor (this
57 * may sleep) and frees it.
58 * @param table the table, or NULL (nothing is done).
59 */
60void fd_table_put(fd_table_t *table);
61
62/**
63 * put a description in the lowest free descriptor of a process, at or
64 * above min_fd. the descriptor takes over the caller's reference.
65 * @param proc the process.
66 * @param file the description.
67 * @param fd_flags FD_CLOEXEC or 0.
68 * @param min_fd the lowest descriptor to use.
69 * @return the descriptor, or -EMFILE if none is free (the caller keeps its
70 * reference then).
71 */
72int fd_install(struct process *proc, file_t *file, uint32_t fd_flags, int min_fd);
73
74/**
75 * get the entry for a descriptor of the current process. when the table
76 * is shared with other threads, the file is kept open until the syscall
77 * ends (fd_release_held), even if another thread closes fd meanwhile;
78 * use entry->file before any sleep, as the entry itself can change.
79 * @param fd the file descriptor number.
80 * @return the entry, or NULL if fd is out of range or not open.
81 */
82fd_entry_t *fd_get(int fd);
83
84/**
85 * let go of the files fd_get kept open during a syscall (the syscall
86 * handler calls this at its end). may sleep: a last close can.
87 * @param proc the process.
88 */
89void fd_release_held(struct process *proc);
90
91/**
92 * close a descriptor of the current process.
93 * @param fd the file descriptor number.
94 * @return 0, or -EBADF if fd is not open.
95 */
96int fd_close(int fd);
97
98/**
99 * close the description behind an entry and mark the entry free. works on
100 * any process's table, so it can tear down a process that isn't current.
101 * may sleep (the last close of a file takes the fs mutex).
102 * @param entry the entry; a free entry is left as is.
103 */
104void fd_close_entry(fd_entry_t *entry);
105
106/**
107 * make dst another descriptor for src's description (fork, spawn). the
108 * flags are copied too. dst must be free.
109 * @param dst the entry to fill.
110 * @param src the entry to duplicate.
111 */
112void fd_copy_entry(fd_entry_t *dst, const fd_entry_t *src);
fd_table_t * fd_table_get(fd_table_t *table)
add a user to a table (a new thread that shares it).
Definition fd_table.c:19
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.
Definition fd_table.c:42
void fd_close_entry(fd_entry_t *entry)
close the description behind an entry and mark the entry free.
Definition fd_table.c:95
int fd_close(int fd)
close a descriptor of the current process.
Definition fd_table.c:84
fd_entry_t * fd_get(int fd)
get the entry for a descriptor of the current process.
Definition fd_table.c:56
void fd_table_put(fd_table_t *table)
remove a user from a table.
Definition fd_table.c:25
void fd_copy_entry(fd_entry_t *dst, const fd_entry_t *src)
make dst another descriptor for src's description (fork, spawn).
Definition fd_table.c:105
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).
Definition fd_table.c:76
fd_table_t * fd_table_create(void)
make a table with every descriptor free and one user.
Definition fd_table.c:9
#define FD_MAX
Definition fd_table.h:16
struct fd_table fd_table_t
the descriptors of a process.
struct file file_t
Definition file.h:38
unsigned int uint32_t
Definition fb.h:17
a single file descriptor entry.
Definition fd_table.h:28
uint32_t flags
Definition fd_table.h:30
file_t * file
Definition fd_table.h:29
the descriptors of a process.
Definition fd_table.h:38
fd_entry_t fd[FD_MAX]
Definition fd_table.h:40
volatile uint32_t users
Definition fd_table.h:39
an open-file description.
Definition file.h:64
process control block.
Definition process.h:69