The manual
ginnOS
The reference: libc, the libraries and the kernel
Toggle main menu visibility
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
13
struct
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
*/
27
typedef
struct
28
{
29
file_t
*
file
;
// the open-file description, or NULL if the slot is free
30
uint32_t
flags
;
// FD_CLOEXEC
31
}
fd_entry_t
;
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
*/
37
typedef
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
41
}
fd_table_t
;
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
*/
47
fd_table_t
*
fd_table_create
(
void
);
48
49
/**
50
* add a user to a table (a new thread that shares it).
51
* @return table.
52
*/
53
fd_table_t
*
fd_table_get
(
fd_table_t
*table);
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
*/
60
void
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
*/
72
int
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
*/
82
fd_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
*/
89
void
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
*/
96
int
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
*/
104
void
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
*/
112
void
fd_copy_entry
(
fd_entry_t
*dst,
const
fd_entry_t
*src);
stdint.h
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).
Definition
fd_table.c:19
fd_install
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
fd_close_entry
void fd_close_entry(fd_entry_t *entry)
close the description behind an entry and mark the entry free.
Definition
fd_table.c:95
fd_close
int fd_close(int fd)
close a descriptor of the current process.
Definition
fd_table.c:84
fd_get
fd_entry_t * fd_get(int fd)
get the entry for a descriptor of the current process.
Definition
fd_table.c:56
fd_table_put
void fd_table_put(fd_table_t *table)
remove a user from a table.
Definition
fd_table.c:25
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).
Definition
fd_table.c:105
fd_release_held
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_create
fd_table_t * fd_table_create(void)
make a table with every descriptor free and one user.
Definition
fd_table.c:9
FD_MAX
#define FD_MAX
Definition
fd_table.h:16
fd_table_t
struct fd_table fd_table_t
the descriptors of a process.
file.h
file_t
struct file file_t
Definition
file.h:38
uint32_t
unsigned int uint32_t
Definition
fb.h:17
fd_entry_t
a single file descriptor entry.
Definition
fd_table.h:28
fd_entry_t::flags
uint32_t flags
Definition
fd_table.h:30
fd_entry_t::file
file_t * file
Definition
fd_table.h:29
fd_table
the descriptors of a process.
Definition
fd_table.h:38
fd_table::fd
fd_entry_t fd[FD_MAX]
Definition
fd_table.h:40
fd_table::users
volatile uint32_t users
Definition
fd_table.h:39
file
an open-file description.
Definition
file.h:64
process
process control block.
Definition
process.h:69
src
kernel
syscall
fd_table.h
Generated by
1.18.0