PatchworkOS  da8a090
A non-POSIX operating system.
Loading...
Searching...
No Matches
process.h
Go to the documentation of this file.
1#pragma once
2
4#include <kernel/fs/cwd.h>
7#include <kernel/fs/sysfs.h>
8#include <kernel/mem/space.h>
10#include <kernel/sched/wait.h>
11#include <kernel/sync/futex.h>
12#include <kernel/utils/ref.h>
13
14#include <stdatomic.h>
15
16/**
17 * @brief Processes.
18 * @defgroup kernel_sched_processes Processes
19 * @ingroup kernel_sched
20 *
21 * Processes store the shared resources for threads of execution, for example the address space and open files.
22 *
23 * ## Process Filesystem
24 *
25 * Each process has a directory located at `/proc/[pid]`, which contains various files that can be used to interact with
26 * the process. Additionally, there is a `/proc/self` bound mount point that points to the `/proc/[pid]` directory of
27 * the current process.
28 *
29 * Included below is a list of all entries found in the `/proc/[pid]` directory along with their formats.
30 *
31 * ## prio
32 *
33 * A readable and writable file that contains the scheduling priority of the process.
34 *
35 * Format:
36 *
37 * ```
38 * %llu
39 * ```
40 *
41 * ## cwd
42 *
43 * A readable and writable file that contains the current working directory of the process.
44 *
45 * Format:
46 *
47 * ```
48 * %s
49 * ```
50 *
51 * ## cmdline
52 *
53 * A readable file that contains the command line arguments of the process (argv).
54 *
55 * Format:
56 *
57 * ```
58 * %s\0%s\0...%s\0
59 * ```
60 *
61 * ## note
62 *
63 * A writable file that can be used to send notes to the process. Writing data to this file will enqueue that data as a
64 * note in the note queue of one of the process's threads.
65 *
66 * ## wait
67 *
68 * A readable and pollable file that can be used to wait for the process to exit and retrieve its exit status. Reading
69 * from this file will block until the process has exited.
70 *
71 * Format:
72 *
73 * ```
74 * %lld
75 * ```
76 *
77 * ## perf
78 *
79 * A readable file that contains performance statistics for the process.
80 *
81 * Format:
82 *
83 * ```
84 * user_clocks kernel_clocks start_clocks user_pages thread_count
85 * %llu %llu %llu %llu %llu
86 * ```
87 *
88 * ## ctl
89 *
90 * A writable file that can be used to control certain aspects of the process, such as closing file descriptors.
91 *
92 * Included is a list of all supported commands.
93 *
94 * ### close <fd>
95 *
96 * Closes the specified file descriptor in the process.
97 *
98 * ### close <minfd> <maxfd>
99 *
100 * Closes the range `[minfd, maxfd)` of file descriptors in the process.
101 *
102 * Note that specifying `-1` as `maxfd` will close all file descriptors from `minfd` to the maximum allowed file descriptor.
103 *
104 * ### dup2 <oldfd> <newfd>
105 *
106 * Duplicates the specified old file descriptor to the new file descriptor in the process.
107 *
108 * ### start
109 *
110 * Starts the process if it was previously suspended.
111 *
112 * ### kill
113 *
114 * Sends a kill note to all threads in the process, effectively terminating it.
115 *
116 * ## fd
117 *
118 * @todo Implement the `/proc/[pid]/fd` directory.
119 *
120 * ## env
121 *
122 * A directory that contains the environment variables of the process. Each environment variable is represented as a
123 * readable and writable file whose name is the name of the variable and whose content is the value of the variable.
124 *
125 * To add or modify an environment variable, create or write to a file with the name of the variable. To remove an
126 * environment variable, delete the corresponding file.
127 *
128 * @{
129 */
130
131/**
132 * @brief Represents the threads in a process.
133 * @struct process_threads_t
134 */
141
142/**
143 * @brief Process flags enum.
144 * @enum process_flags_t
145 */
146typedef enum
147{
152
153/**
154 * @brief Process structure.
155 * @struct process_t
156 */
157typedef struct process
158{
174 mount_t* self; ///< The `/proc/self` bind mount.
175 dentry_t* proc; ///< The `/proc/[pid]` directory, also stored in `dentries` for convenience.
176 dentry_t* env; ///< The `/proc/[pid]/env` directory, also stored in `dentries` for convenience.
177 list_t dentries; ///< List of dentries in the `/proc/[pid]/` directory.
178 list_t envVars; ///< List of dentries in the `/proc/[pid]/env/` directory.
180 char* cmdline;
182} process_t;
183
184/**
185 * @brief Allocates and initializes a new process.
186 *
187 * There is no `process_free()`, instead use `UNREF()`, `UNREF_DEFER()` or `process_kill()` to free a process.
188 *
189 * @param priority The priority of the new process.
190 * @return On success, the newly created process. On failure, `NULL` and `errno` is set.
191 */
193
194/**
195 * @brief Kills a process.
196 *
197 * Sends a kill note to all threads in the process and sets its exit status.
198 * Will also close all files opened by the process and deinitialize its `/proc` directory.
199 *
200 * When all threads have exited and all entires in its `/proc` directory have been closed, the process will be freed.
201 *
202 * @param process The process to kill.
203 * @param status The exit status of the process.
204 */
205void process_kill(process_t* process, int32_t status);
206
207/**
208 * @brief Copies the environment variables from one process to another.
209 *
210 * @param dest The destination process, must have an empty environment.
211 * @param src The source process.
212 * @return On success, `0`. On failure, `ERR` and `errno` is set to:
213 * - `EINVAL`: Invalid parameters.
214 * - `EBUSY`: The destination process already has environment variables.
215 */
217
218/**
219 * @brief Sets the command line arguments for a process.
220 *
221 * This value is only used for the `/proc/[pid]/cmdline` file.
222 *
223 * @param process The process to set the cmdline for.
224 * @param argv The array of argument strings.
225 * @param argc The number of arguments.
226 * @return On success, `0`. On failure, `ERR` and `errno` is set to:
227 * - `EINVAL`: Invalid parameters.
228 * - `ENOMEM`: Out of memory.
229 */
230uint64_t process_set_cmdline(process_t* process, char** argv, uint64_t argc);
231
232/**
233 * @brief Checks if a process has a thread with the specified thread ID.
234 *
235 * @param process The process to check.
236 * @param tid The thread ID to look for.
237 * @return `true` if the process has a thread with the specified ID, `false` otherwise.
238 */
239bool process_has_thread(process_t* process, tid_t tid);
240
241/**
242 * @brief Gets the kernel process.
243 *
244 * The kernel process will be initalized lazily on the first call to this function, which should happen during early
245 * boot.
246 *
247 * Will never return `NULL`.
248 *
249 * Will not increment the reference count of the returned process as it should never be freed either way.
250 *
251 * @return The kernel process.
252 */
254
255/**
256 * @brief Initializes the `/proc` directory.
257 */
258void process_procfs_init(void);
259
260/**
261 * @brief Initializes the process reaper.
262 *
263 * The process reaper allows us to delay the freeing of processes, this is useful if, for example, another process
264 * wanted that process's exit status.
265 */
266void process_reaper_init(void);
267
268/** @} */
process_t * process_new(priority_t priority)
Allocates and initializes a new process.
Definition process.c:718
void process_reaper_init(void)
Initializes the process reaper.
Definition process.c:1032
process_t * process_get_kernel(void)
Gets the kernel process.
Definition process.c:943
bool process_has_thread(process_t *process, tid_t tid)
Checks if a process has a thread with the specified thread ID.
Definition process.c:927
void process_procfs_init(void)
Initializes the /proc directory.
Definition process.c:958
uint64_t process_set_cmdline(process_t *process, char **argv, uint64_t argc)
Sets the command line arguments for a process.
Definition process.c:869
uint64_t process_copy_env(process_t *dest, process_t *src)
Copies the environment variables from one process to another.
Definition process.c:817
void process_kill(process_t *process, int32_t status)
Kills a process.
Definition process.c:776
process_flags_t
Process flags enum.
Definition process.h:147
@ PROCESS_NONE
Definition process.h:148
@ PROCESS_DYING
Definition process.h:149
@ PROCESS_SUSPENDED
Definition process.h:150
uint8_t priority_t
Priority type.
Definition proc.h:46
__UINT64_TYPE__ tid_t
Thread Identifier.
Definition tid_t.h:12
__UINT64_TYPE__ pid_t
Process Identifier.
Definition pid_t.h:11
static const path_flag_t flags[]
Definition path.c:42
__INT32_TYPE__ int32_t
Definition stdint.h:14
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__INT64_TYPE__ int64_t
Definition stdint.h:16
Definition cwd.h:18
Directory entry structure.
Definition dentry.h:84
File table structure.
Definition file_table.h:24
Per-process futex context.
Definition futex.h:34
A entry in a doubly linked list.
Definition list.h:36
A doubly linked list.
Definition list.h:49
A simple ticket lock implementation.
Definition lock.h:43
Mount structure.
Definition mount.h:44
Process structure.
Definition process.h:158
mount_t * self
The /proc/self bind mount.
Definition process.h:174
file_table_t fileTable
Definition process.h:166
namespace_t ns
Definition process.h:164
_Atomic(int64_t) status
list_t envVars
List of dentries in the /proc/[pid]/env/ directory.
Definition process.h:178
futex_ctx_t futexCtx
Definition process.h:167
ref_t ref
Definition process.h:159
wait_queue_t suspendQueue
Definition process.h:169
perf_process_ctx_t perf
Definition process.h:168
uint64_t cmdlineSize
Definition process.h:181
_Atomic(process_flags_t) flags
char * cmdline
Definition process.h:180
dentry_t * proc
The /proc/[pid] directory, also stored in dentries for convenience.
Definition process.h:175
space_t space
Definition process.h:163
list_entry_t zombieEntry
Definition process.h:173
pid_t id
Definition process.h:160
list_t dentries
List of dentries in the /proc/[pid]/ directory.
Definition process.h:177
cwd_t cwd
Definition process.h:165
dentry_t * env
The /proc/[pid]/env directory, also stored in dentries for convenience.
Definition process.h:176
process_threads_t threads
Definition process.h:172
lock_t dentriesLock
Definition process.h:179
wait_queue_t dyingQueue
Definition process.h:170
_Atomic(priority_t) priority
Represents the threads in a process.
Definition process.h:136
Reference counting structure.
Definition ref.h:30
Virtual address space structure.
Definition space.h:79
The primitive that threads block on.
Definition wait.h:182