|
ginnOS
The reference: libc, the libraries and the kernel
|
#include "common/stdint.h"#include "kernel/memory/page.h"#include "kernel/memory/vma.h"#include "kernel/memory/user_space.h"#include "arch/arch.h"#include "kernel/syscall/fd_table.h"#include "kernel/sync/wait_queue.h"#include "kernel/signal/signal.h"Go to the source code of this file.
Data Structures | |
| struct | fs_context |
| where paths start, and the mode bits new files lose. More... | |
| struct | process |
| process control block. More... | |
| struct | process_info_t |
| a copy of what procfs shows about a process. More... | |
Macros | |
| #define | PROCESS_MAX 64 |
| #define | PID_NONE 0 |
| #define | PID_INIT 1 |
| #define | KERNEL_STACK_SIZE (2 * PAGE_SIZE) |
| #define | PATH_MAX 256 |
| #define | PROCESS_NAME_MAX 32 |
Typedefs | |
| typedef struct fs_context | fs_context_t |
| where paths start, and the mode bits new files lose. | |
| typedef struct process | process_t |
| process control block. | |
Enumerations | |
| enum | process_state_t { PROC_STATE_UNUSED = 0 , PROC_STATE_NEW , PROC_STATE_RUNNING , PROC_STATE_READY , PROC_STATE_BLOCKED , PROC_STATE_ZOMBIE , PROC_STATE_STOPPED } |
| process states. More... | |
| enum | wait_report_t { WAIT_REPORT_NONE = 0 , WAIT_REPORT_STOPPED , WAIT_REPORT_CONTINUED } |
| a change of a child that waitpid has not reported yet. More... | |
Functions | |
| fs_context_t * | fs_context_get (fs_context_t *fs) |
| add a user to an fs context (a new thread that shares it). | |
| void | fs_context_put (fs_context_t *fs) |
| remove a user from an fs context; the last user frees it. | |
| void | process_init (void) |
| initialize the process subsystem. | |
| process_t * | process_create (void) |
| create a new process and return its PCB. | |
| process_t * | process_fork (const struct registers *regs) |
| fork the current process. | |
| process_t * | process_clone_thread (const struct registers *regs, uintptr_t stack) |
| make a thread of the current process: a new task in its group that shares its memory, descriptors, fs context and handlers. | |
| void | process_signal_threads (uint32_t tgid, process_t *except, int sig) |
| send a signal to the threads of a group (signal_send to each). | |
| void | process_take_leader (process_t *proc) |
| make a thread the leader of its group in place of the old leader, which has ended (a zombie) and is the only other member: the thread takes the leader's pid and its place for the parent, and the old slot is reaped by the kernel (execve from a thread that is not the leader). | |
| void | process_sync_threads (process_t *proc) |
| give every other thread of a process's group its user and group ids, its process group and its session (they belong to the process). | |
| process_t * | process_pick_thread (uint32_t tgid, uint32_t mask) |
| choose the thread of a group that takes a signal sent to the process: the leader if it does not block mask, else a thread that does not, else any of them (the leader first). | |
| process_t * | process_current (void) |
| get the currently running process. | |
| void | process_set_current (process_t *proc) |
| set the currently running process. | |
| void | process_destroy (process_t *proc) |
| destroy a process and free its slot in the process table. | |
| void | process_release_resources (process_t *proc) |
| release what a process owns apart from its kernel stack and table slot: close its fds, free its argv copy and destroy its address space (after switching away from it, when it is the current process). | |
| void | process_orphan_children (process_t *parent) |
| detach the children of an exiting group (call when its last thread ends; parent is that thread): zombie children are destroyed now (nobody wants their status), running ones get init (PID 1) as their parent, which reaps them. | |
| void | process_note_orphan_zombie (void) |
| record that an orphan (no parent) has become a zombie and must be reaped by the kernel. | |
| void | process_reap_orphans (void) |
| destroy orphan zombies. | |
| process_t * | process_find_waitable_child (process_t *parent, int32_t pid, uint32_t options, bool *has_child) |
| find a child of a process that has something for waitpid: it exited (a zombie), or, with WUNTRACED / WCONTINUED in options, it stopped or continued and that was not reported yet. | |
| uint32_t | process_list_group (uint32_t pgid, uint32_t *pids, uint32_t max) |
| list the pids of a process group. | |
| bool | process_group_exists (uint32_t pgid, uint32_t sid) |
| true if some live process is in the group and the session. | |
| void | process_for_each (void(*fn)(process_t *proc, void *ctx), void *ctx) |
| call fn for every process in the table, with the table locked and interrupts masked: fn must not sleep. | |
| uint32_t | process_list_pids (uint32_t *pids, uint32_t max) |
| list the pids in use. | |
| bool | process_info (uint32_t pid, process_info_t *info) |
| copy what procfs shows about a process, safely against its exit. | |
| void | process_set_name (process_t *proc, const char *path) |
| set a process's name from a program path (its last component). | |
| uint64_t | process_idle_ticks (void) |
| the timer ticks that the cores spent in their idle loops since boot, added over all cores. | |
| void | process_account_tick (bool user) |
| charge one timer tick to the current process (timer interrupt). | |
| void | process_copy_credentials (process_t *child, const process_t *parent) |
| give a new process the user and group ids and umask of another. | |
| process_t * | process_get (uint32_t pid) |
| look up a process by PID. | |
| #define PROCESS_MAX 64 |
| #define PID_NONE 0 |
| #define PID_INIT 1 |
| #define KERNEL_STACK_SIZE (2 * PAGE_SIZE) |
| #define PATH_MAX 256 |
| #define PROCESS_NAME_MAX 32 |
| typedef struct fs_context fs_context_t |
where paths start, and the mode bits new files lose.
the threads of a process use one together; it is freed when the last lets it go.
| enum process_state_t |
| enum wait_report_t |
| fs_context_t * fs_context_get | ( | fs_context_t * | fs | ) |
add a user to an fs context (a new thread that shares it).
| void fs_context_put | ( | fs_context_t * | fs | ) |
remove a user from an fs context; the last user frees it.
| fs | the context, or NULL (nothing is done). |
| void process_init | ( | void | ) |
initialize the process subsystem.
must be called once during kernel startup.
| process_t * process_create | ( | void | ) |
create a new process and return its PCB.
allocates a PID and initializes the fd table with stdin/stdout/stderr. the process starts in PROC_STATE_NEW; scheduler_ready() makes it runnable.
fork the current process.
the child is a copy of the caller: the same memory (shared copy-on-write), areas, fds, cwd and break. it is made runnable at once and resumes from the same syscall, which returns 0 in the child.
| regs | the caller's saved user registers (its syscall frame). |
make a thread of the current process: a new task in its group that shares its memory, descriptors, fs context and handlers.
it resumes from the same syscall, which returns 0 there, with stack as its user stack pointer. the caller makes it runnable.
| regs | the caller's saved user registers (its syscall frame). |
| stack | the thread's user stack pointer. |
send a signal to the threads of a group (signal_send to each).
| tgid | the group. |
| except | a thread that is left out, or NULL. |
| sig | the signal. |
| void process_take_leader | ( | process_t * | proc | ) |
make a thread the leader of its group in place of the old leader, which has ended (a zombie) and is the only other member: the thread takes the leader's pid and its place for the parent, and the old slot is reaped by the kernel (execve from a thread that is not the leader).
| proc | the thread (the current one). |
| void process_sync_threads | ( | process_t * | proc | ) |
give every other thread of a process's group its user and group ids, its process group and its session (they belong to the process).
| proc | the thread whose values the others take. |
choose the thread of a group that takes a signal sent to the process: the leader if it does not block mask, else a thread that does not, else any of them (the leader first).
| tgid | the group. |
| mask | the signal's bit. |
| process_t * process_current | ( | void | ) |
get the currently running process.
| void process_set_current | ( | process_t * | proc | ) |
set the currently running process.
| proc | the process to mark as current. |
| void process_destroy | ( | process_t * | proc | ) |
destroy a process and free its slot in the process table.
closes any open file descriptors.
| proc | the process to destroy. |
| void process_release_resources | ( | process_t * | proc | ) |
release what a process owns apart from its kernel stack and table slot: close its fds, free its argv copy and destroy its address space (after switching away from it, when it is the current process).
run by an exiting process in its own context, since closing a file may sleep. safe to call more than once.
| proc | the process. |
| void process_orphan_children | ( | process_t * | parent | ) |
detach the children of an exiting group (call when its last thread ends; parent is that thread): zombie children are destroyed now (nobody wants their status), running ones get init (PID 1) as their parent, which reaps them.
without init they lose their parent (PID_NONE) and the kernel reaps them when they exit.
| parent | the exiting process. |
| void process_note_orphan_zombie | ( | void | ) |
record that an orphan (no parent) has become a zombie and must be reaped by the kernel.
the reap waits until the zombie has left its core.
| void process_reap_orphans | ( | void | ) |
destroy orphan zombies.
called by the scheduler right after a context switch, on the next process's stack, so a dead process's own stack is never freed while in use. runs with interrupts masked.
| process_t * process_find_waitable_child | ( | process_t * | parent, |
| int32_t | pid, | ||
| uint32_t | options, | ||
| bool * | has_child ) |
find a child of a process that has something for waitpid: it exited (a zombie), or, with WUNTRACED / WCONTINUED in options, it stopped or continued and that was not reported yet.
a zombie that it returns is marked reaped, so no other waitpid takes it. call it with the signal state lock held (signal_state_lock), which guards the reports.
| parent | the parent. |
| pid | a child's pid, or -1 for any child. |
| options | waitpid options (wait_status.h). |
| has_child | set to true if parent has at least one matching child. |
list the pids of a process group.
| pgid | the group. |
| pids | receives up to max pids. |
| max | capacity of pids. |
true if some live process is in the group and the session.
| void process_for_each | ( | void(* | fn )(process_t *proc, void *ctx), |
| void * | ctx ) |
call fn for every process in the table, with the table locked and interrupts masked: fn must not sleep.
| fn | the function. |
| ctx | passed to fn. |
list the pids in use.
| pids | receives up to max pids, in table order. |
| max | capacity of pids. |
| bool process_info | ( | uint32_t | pid, |
| process_info_t * | info ) |
copy what procfs shows about a process, safely against its exit.
| pid | the process. |
| info | receives the copy. |
| void process_set_name | ( | process_t * | proc, |
| const char * | path ) |
set a process's name from a program path (its last component).
| proc | the process. |
| path | the program's path. |
| uint64_t process_idle_ticks | ( | void | ) |
the timer ticks that the cores spent in their idle loops since boot, added over all cores.
| void process_account_tick | ( | bool | user | ) |
charge one timer tick to the current process (timer interrupt).
| user | true if user code was running. |
give a new process the user and group ids and umask of another.