|
ginnOS
The reference: libc, the libraries and the kernel
|
#include "usermode.h"#include "arch/arch.h"#include "arch/x86/cpu/gdt.h"#include "kernel/sync/atomic.h"#include "kernel/sync/futex.h"#include "kernel/memory/pmm.h"#include "kernel/memory/heap.h"#include "kernel/memory/memory_layout.h"#include "kernel/memory/mman.h"#include "kernel/elf/elf_loader.h"#include "kernel/process/process.h"#include "kernel/process/wait_status.h"#include "kernel/usermode/uaccess.h"#include "kernel/signal/signal.h"#include "kernel/vfs/permission.h"#include "kernel/vfs/stat.h"#include "kernel/vfs/vfs.h"#include "kernel/scheduler/scheduler.h"#include "kernel/panic.h"#include "kernel/klog/klog.h"#include "common/stdio.h"#include "common/memory.h"#include "common/string.h"Macros | |
| #define | EXEC_FAILED_EXIT_STATUS 127 |
| #define | SCRIPT_LINE_MAX 128 |
Functions | |
| int | exec_program (const char *path, const char **argv) |
| start a program as a new process. | |
| kerr_t | exec_replace (struct registers *regs, const char *path, const char **argv, const char **envp) |
| replace the current process's program (execve). | |
| void | usermode_exit (int exit_code) |
| called by SYS_exit: end the current thread, and never run again. | |
| void | usermode_exit_group (int exit_code) |
| called by SYS_exit_group (and so by exit()): end every thread of the current process, which then ends with exit_code. | |
| void | usermode_kill (int signal) |
| end the current process (every thread) as killed by a signal: the wait status names the signal (SIGSEGV for a bad memory access). | |
| void | usermode_end_thread (void) |
| end the current thread because its process is ending (exit_group or a fatal signal in another thread), with the process's status. | |
| #define EXEC_FAILED_EXIT_STATUS 127 |
| #define SCRIPT_LINE_MAX 128 |
| int exec_program | ( | const char * | path, |
| const char ** | argv ) |
start a program as a new process.
creates the process, loads the ELF at path into its address space, copies argv, inherits the caller's cwd and its fds without FD_CLOEXEC (when called from a process) and queues it on the scheduler. with no current process (boot), the new process has no parent: PID 1.
| path | absolute path to the ELF executable. |
| argv | null-terminated array of argument strings (may be NULL). |
| kerr_t exec_replace | ( | struct registers * | regs, |
| const char * | path, | ||
| const char ** | argv, | ||
| const char ** | envp ) |
replace the current process's program (execve).
the new image is loaded into a new address space first, so on failure the caller keeps running its old program. on success the old address space is gone, the areas and break are the new program's, its stack holds argv and envp, and the syscall frame returns into its entry point. pid, parent, cwd and fds stay (except FD_CLOEXEC ones, which are closed).
| regs | the caller's syscall frame. a script that starts with "#!interpreter [arg]" runs as "interpreter [arg] path argv[1]...". |
| path | absolute path of the ELF executable or script. |
| argv | NULL-terminated argument strings in kernel memory. |
| envp | NULL-terminated environment strings in kernel memory. |
| void usermode_exit | ( | int | exit_code | ) |
called by SYS_exit: end the current thread, and never run again.
if it is the last thread of its process, the process ends: it becomes a zombie for its parent to reap, with a wait status that says it exited with exit_code. if PID 1 ends, the system halts.
| exit_code | the exit code passed by the user program (low 8 bits). |
| void usermode_exit_group | ( | int | exit_code | ) |
called by SYS_exit_group (and so by exit()): end every thread of the current process, which then ends with exit_code.
| exit_code | the exit code (low 8 bits). |
| void usermode_kill | ( | int | signal | ) |
end the current process (every thread) as killed by a signal: the wait status names the signal (SIGSEGV for a bad memory access).
| signal | the signal number (kernel/signal/signal.h). |
| void usermode_end_thread | ( | void | ) |
end the current thread because its process is ending (exit_group or a fatal signal in another thread), with the process's status.