|
AquilaOS documentation
How it works, and its code
|
#include <core/system.h>#include <mm/vm.h>#include <fs/vfs.h>#include <ds/queue.h>#include <sys/waitq.h>#include <sys/thread.h>#include <sys/signal.h>#include <dev/dev.h>Go to the source code of this file.
Data Structures | |
| struct | session |
| A session: its process groups, and its controlling terminal. More... | |
| struct | fd |
| A file descriptor: the open file it refers to (fs/file.c, shared with the descriptors that dup() and fork() copied), and its own flags. More... | |
| struct | pgroup |
| A process group: the processes of a job. More... | |
| struct | proc |
| A process. More... | |
Macros | |
| #define | FD_RESERVED ((struct file *) -1) |
| #define | PROC_EXIT(info, code) |
| #define | PROC_UIO(proc) |
Functions | |
| int | proc_fork (struct thread *thread, struct proc **ref) |
| Makes a child process, a copy of the process of thread. | |
| int | proc_execve (struct thread *thread, const char *fn, char *const argv[], char *const env[]) |
| Runs another program in a thread's process, for execve(). | |
| void | init_execve (struct proc *proc, int argc, char *const argv[], int envc, char *const envp[]) |
| Sets up init, the first process, to start its program: its initial stack with its arguments and environment, before it first runs. | |
| pid_t | proc_pid_alloc (void) |
| Allocates a process ID, from 1 (init's) on. | |
| void | proc_pid_free (int pid) |
| Frees a process ID. | |
| struct proc * | proc_pid_find (pid_t pid) |
| Finds a process by its ID. | |
| int | proc_new (struct proc **ref) |
| Makes a new process structure, with one thread and an empty address space, on the list of every process. | |
| int | session_new (struct proc *proc) |
| Makes a new session, with a new process group, both led by a process and with its ID, and moves the process into it, as setsid() does. | |
| int | proc_setpgid (struct proc *proc, pid_t pgid) |
| Moves a process into a process group of its session, as setpgid() does. | |
| void | pgrp_release (struct pgroup *pgrp) |
| Frees a process group if no process is left in it, and its session if that was the session's last group. | |
| int | pgrp_orphaned (struct pgroup *pgrp, struct proc *gone) |
| Says whether a process group is orphaned (POSIX): none of its members has a parent in another group of the same session, so no one there would continue it once it stops. | |
| void | proc_kill (struct proc *proc) |
| Ends a process: its threads end, its descriptors are closed, its memory is freed, its children go to init, and it becomes a zombie that its parent may wait for. | |
| int | proc_threads_end (struct proc *proc, struct thread *keep, int exiting) |
| Ends the threads of a process, but one. | |
| void | proc_threads_exec (struct proc *proc) |
| Ends every thread of a process but the current one, for execve() (POSIX: they run in the program that is replaced). | |
| int | proc_reap (struct proc *proc) |
| Frees a zombie, once its parent waited for it (or doesn't wait for its children). | |
| int | proc_fd_get (struct proc *proc) |
| Reserves the lowest free descriptor of a process, while a system call sets it up: no other thread takes it, and it doesn't refer to a file yet. | |
| void | proc_fd_release (struct proc *proc, int fd) |
| Frees a reserved descriptor that wasn't set up after all. | |
| void | proc_fd_install (struct proc *proc, int fd, struct file *file, int flags) |
| Makes a reserved descriptor refer to an open file. | |
| struct file * | proc_fd_file (struct proc *proc, int fd) |
| Finds the open file a descriptor refers to. | |
| int | proc_fd_close (struct proc *proc, int fd) |
| Closes a descriptor: it is free again, and its reference to the open file is dropped. | |
| void | proc_dump (struct proc *proc) |
| int | proc_init (struct proc *proc) |
| Gives a new process (init) its ID and its descriptor table. | |
Variables | |
| struct queue * | procs |
| struct proc * | initproc |
| struct queue * | sessions |
| #define FD_RESERVED ((struct file *) -1) |
Definition at line 53 of file proc.h.
Referenced by proc_fd_file(), and proc_fd_get().
| #define PROC_EXIT | ( | info, | |
| code ) |
Definition at line 207 of file proc.h.
Referenced by proc_execve(), signal_exit(), and sys_exit().
| #define PROC_UIO | ( | proc | ) |
Definition at line 210 of file proc.h.
Referenced by at_uio(), binfmt_load(), cwd_set(), exec_script(), socket_unix_bind(), socket_unix_connect(), sys_chdir(), sys_fchmod(), sys_fchown(), sys_mount(), and sys_utimensat().
| void init_execve | ( | struct proc * | proc, |
| int | argc, | ||
| char *const | argv[], | ||
| int | envc, | ||
| char *const | envp[] ) |
Sets up init, the first process, to start its program: its initial stack with its arguments and environment, before it first runs.
The kernel calls it at boot, when no process runs yet.
| proc | init's process, with its program loaded |
| argc | how many arguments there are |
| argv | the arguments, in kernel memory |
| envc | how many environment strings there are |
| envp | the environment strings, in kernel memory |
Definition at line 332 of file execve.c.
References curthread, exec_start(), queue::head, init_execve(), NULL, vm_space::pmap, pmap_switch(), proc::threads, qnode::value, and proc::vm_space.
Referenced by init_execve(), and kmain().
Says whether a process group is orphaned (POSIX): none of its members has a parent in another group of the same session, so no one there would continue it once it stops.
Init doesn't count as such a parent, since it isn't a shell.
| pgrp | the process group |
| gone | a process on its way out, which doesn't count as a member or a parent; or NULL |
Definition at line 227 of file proc.c.
References initproc, proc::parent, proc::pgrp, pgrp_orphaned(), pgroup::procs, queue_for, pgroup::session, and proc::zombie.
Referenced by pgrp_orphaned(), pgrp_orphaned_hangup(), signal_take(), and tty_background().
| void pgrp_release | ( | struct pgroup * | pgrp | ) |
Frees a process group if no process is left in it, and its session if that was the session's last group.
A terminal whose foreground group or session it was loses it.
| pgrp | the process group |
Definition at line 742 of file proc.c.
References queue::count, session::ctty, tty::fg, kfree(), NULL, session::pgps, pgrp_release(), pgroup::procs, pgroup::session, tty::session, and pgroup::session_node.
Referenced by pgrp_release(), proc_reap(), proc_setpgid(), and sys_setsid().
| void proc_dump | ( | struct proc * | proc | ) |
References proc_dump(), and proc_init().
Referenced by proc_dump().
| int proc_execve | ( | struct thread * | thread, |
| const char * | fn, | ||
| char *const | _argp[], | ||
| char *const | _envp[] ) |
Runs another program in a thread's process, for execve().
On success, the thread starts the new program when it returns to user mode.
| thread | the thread calling execve() |
| fn | the program's path |
| _argp | the arguments, in user memory |
| _envp | the environment, in user memory |
Definition at line 358 of file execve.c.
References arch_sleep(), binfmt_load(), queue::count, EXEC_ARG_MAX, exec_script(), exec_script_args(), EXEC_SCRIPT_DEPTH, exec_start(), exec_strings_free(), exec_strings_get(), proc::exit, proc::fds, FDS_COUNT, fd::flags, kfree(), kmalloc(), NULL, thread::owner, proc_execve(), PROC_EXIT, proc_fd_close(), proc_fd_file(), proc_kill(), SIG_DFL, SIG_IGN, SIG_MAX, proc::sigaction, thread::spawned, vm_space::vm_entries, and proc::vm_space.
Referenced by proc_execve(), and sys_execve().
| int proc_fd_close | ( | struct proc * | proc, |
| int | fd ) |
Closes a descriptor: it is free again, and its reference to the open file is dropped.
Dropping the last reference closes the file.
| proc | the process |
| fd | the descriptor |
Definition at line 578 of file proc.c.
References proc::fds, file_put(), proc_fd_close(), and proc_fd_file().
Referenced by proc_execve(), proc_fd_close(), proc_fork(), proc_kill(), sys_accept(), sys_close(), and sys_pipe().
Finds the open file a descriptor refers to.
| proc | the process |
| fd | the descriptor |
Definition at line 560 of file proc.c.
References FD_RESERVED, proc::fds, FDS_COUNT, fd::file, NULL, and proc_fd_file().
Referenced by copy_fds(), fd_file(), fd_peek(), proc_execve(), proc_fd_close(), and proc_fd_file().
| int proc_fd_get | ( | struct proc * | proc | ) |
Reserves the lowest free descriptor of a process, while a system call sets it up: no other thread takes it, and it doesn't refer to a file yet.
proc_fd_install() or proc_fd_release() ends the reservation.
| proc | the process |
Definition at line 514 of file proc.c.
References FD_RESERVED, proc::fds, FDS_COUNT, fd::file, and proc_fd_get().
Referenced by do_openat(), proc_fd_get(), sys_accept(), sys_pipe(), and sys_socket().
Makes a reserved descriptor refer to an open file.
The caller's reference to the file becomes the descriptor's.
| proc | the process |
| fd | the descriptor, reserved by proc_fd_get() |
| file | the open file |
| flags | the descriptor's flags (FD_CLOEXEC) |
Definition at line 547 of file proc.c.
References proc::fds, fd::file, fd::flags, file::flags, and proc_fd_install().
Referenced by do_openat(), proc_fd_install(), sys_accept(), sys_fcntl(), sys_pipe(), and sys_socket().
| void proc_fd_release | ( | struct proc * | proc, |
| int | fd ) |
Frees a reserved descriptor that wasn't set up after all.
| proc | the process |
| fd | the descriptor |
Definition at line 532 of file proc.c.
References proc::fds, FDS_COUNT, and proc_fd_release().
Referenced by do_openat(), proc_fd_release(), sys_accept(), sys_pipe(), and sys_socket().
Makes a child process, a copy of the process of thread.
The child's one thread is a copy of thread, which will return 0 from fork(); it isn't made ready to run, the caller does that.
| thread | the thread calling fork() |
| ref | if not NULL, set to the child |
Definition at line 81 of file fork.c.
References arch_proc_fork(), arch_syscall_return(), copy_fds(), proc::cwd, proc::fds, FDS_COUNT, fork_proc_copy(), queue::head, proc::heap_vm, kfree(), proc::name, qnode::next, NULL, thread::owner, proc::parent, proc::pgrp, proc::pgrp_node, proc::pid, vm_space::pmap, pmap_decref(), proc_fd_close(), proc_fork(), proc_new(), proc_pid_alloc(), proc_pid_free(), pgroup::procs, procs, vnode::ref, thread::spawned, proc::stack_vm, proc::threads, qnode::value, vfs_close(), vm_space::vm_entries, proc::vm_space, vm_space_destroy(), and vm_space_fork().
Referenced by proc_fork(), and sys_fork().
| int proc_init | ( | struct proc * | proc | ) |
Gives a new process (init) its ID and its descriptor table.
| proc | the process |
Definition at line 197 of file proc.c.
References proc::fds, FDS_COUNT, kmalloc(), M_ZERO, proc::pid, proc_init(), proc_pid_alloc(), and proc_pid_free().
Referenced by proc_dump(), proc_init(), and sched_init_spawn().
| void proc_kill | ( | struct proc * | proc | ) |
Ends a process: its threads end, its descriptors are closed, its memory is freed, its children go to init, and it becomes a zombie that its parent may wait for.
If the current thread is the process's, it doesn't return. Ending init restarts the machine.
| proc | the process; its exit status is set already |
Definition at line 375 of file proc.c.
References arch_cur_thread_kill(), arch_interrupts_disable(), arch_reboot(), session::ctty, curthread, proc::cwd, proc::exit, proc::fds, FDS_COUNT, queue::head, initproc, kfree(), kvm_space, session::leader, qnode::next, NULL, panic, proc::parent, proc::pgrp, pgrp_orphaned_hangup(), pgrp_other_same_session(), proc::pid, vm_space::pmap, pmap_decref(), printk(), proc_fd_close(), proc_kill(), proc_threads_end(), proc_zombie_notify(), procs, proc::running, sched_alarm(), pgroup::session, proc::sig_pending, thread_files_release(), proc::threads, tty_hangup(), qnode::value, vfs_close(), proc::vm_space, vm_space_destroy(), and proc::zombie.
Referenced by proc_execve(), proc_kill(), signal_exit(), and sys_exit().
| int proc_new | ( | struct proc ** | ref | ) |
Makes a new process structure, with one thread and an empty address space, on the list of every process.
It has no ID yet.
| ref | if not NULL, set to the new process |
Definition at line 114 of file proc.c.
References kfree(), kmalloc(), M_ZERO, NULL, vm_space::pmap, pmap_create(), pmap_decref(), proc_new(), procs, pmap::ref, proc::running, SIG_DFL, SIG_MAX, proc::sigaction, thread_new(), proc::threads, and proc::vm_space.
Referenced by kmain(), proc_fork(), and proc_new().
| pid_t proc_pid_alloc | ( | void | ) |
Allocates a process ID, from 1 (init's) on.
Definition at line 80 of file proc.c.
References last_pid, bitmap::max_idx, pid_bitmap, pid_is_group(), and proc_pid_alloc().
Referenced by proc_fork(), proc_init(), and proc_pid_alloc().
Finds a process by its ID.
| pid | the ID |
Definition at line 179 of file proc.c.
References NULL, proc::pid, proc_pid_find(), procs, and queue_for.
Referenced by kernel_ctrlaltdel(), proc_alive(), proc_pid_find(), procfs_proc_maps(), procfs_proc_status(), procfs_proc_vmstats(), signal_send(), sys_setpgid(), and vt_signal().
| void proc_pid_free | ( | int | pid | ) |
Frees a process ID.
| pid | the ID |
Definition at line 102 of file proc.c.
References pid_bitmap, and proc_pid_free().
Referenced by proc_fork(), proc_init(), proc_pid_free(), and proc_reap().
| int proc_reap | ( | struct proc * | proc | ) |
Frees a zombie, once its parent waited for it (or doesn't wait for its children).
Its CPU times are added to its parent's children's times.
| proc | the zombie |
Definition at line 484 of file proc.c.
References proc::cstime, proc::cutime, kfree(), proc::name, proc::parent, proc::pgrp, proc::pgrp_node, pgrp_release(), proc::pid, proc_pid_free(), proc_reap(), pgroup::procs, procs, proc::stime, and proc::utime.
Referenced by proc_reap(), proc_zombie_notify(), and sys_waitpid().
Moves a process into a process group of its session, as setpgid() does.
A group can only be joined if it exists and has members; a process can only start a new group with its own ID as the group's ID.
| proc | the process |
| pgid | the group's ID |
Definition at line 667 of file proc.c.
References queue::count, kfree(), kmalloc(), pgroup::leader, M_ZERO, NULL, pgroup::pgid, session::pgps, proc::pgrp, proc::pgrp_node, pgrp_release(), proc::pid, proc_setpgid(), pgroup::procs, queue_for, pgroup::session, and pgroup::session_node.
Referenced by proc_setpgid(), and sys_setpgid().
Ends the threads of a process, but one.
A thread that isn't running is freed. One running on another CPU (in user mode, since this CPU has the kernel lock) is marked killed and made to enter the kernel now, where it frees itself.
| proc | the process |
| keep | the thread to keep, or NULL |
| exiting | whether the process is ending: then the threads running elsewhere are detached from it at once; otherwise they leave its list when they go, and wake up proc->thread_join |
Definition at line 305 of file proc.c.
References queue::head, kfree(), thread::killed, NULL, thread::owner, proc_threads_end(), thread::run_link, sched_thread_cpu(), sched_thread_kick(), sched_timer_cancel(), thread::state, thread_files_release(), thread_kill(), proc::threads, thread::waitq, and ZOMBIE.
Referenced by proc_kill(), proc_threads_end(), and proc_threads_exec().
| void proc_threads_exec | ( | struct proc * | proc | ) |
Ends every thread of a process but the current one, for execve() (POSIX: they run in the program that is replaced).
It waits for the threads running on other CPUs to be gone.
| proc | the process, which must be the current thread's |
Definition at line 358 of file proc.c.
References curthread, proc_threads_end(), proc_threads_exec(), proc::thread_join, and waitq_sleep_unint().
Referenced by binfmt_fmt_load(), and proc_threads_exec().
| int session_new | ( | struct proc * | proc | ) |
Makes a new session, with a new process group, both led by a process and with its ID, and moves the process into it, as setsid() does.
| proc | the process |
Definition at line 596 of file proc.c.
References kfree(), kmalloc(), pgroup::leader, session::leader, M_ZERO, NULL, pgroup::pgid, session::pgps, proc::pgrp, proc::pgrp_node, proc::pid, pgroup::procs, pgroup::session, session_new(), pgroup::session_node, and session::sid.
Referenced by kproc_init(), sched_init_spawn(), session_new(), and sys_setsid().
|
extern |
Definition at line 39 of file proc.c.
Referenced by kmain(), pgrp_orphaned(), and proc_kill().
|
extern |
Definition at line 37 of file proc.c.
Referenced by kproc_init(), proc_fork(), proc_kill(), proc_new(), proc_pid_find(), proc_reap(), procfs_finddir(), procfs_readdir(), signal_send(), and sys_waitpid().
|
extern |
Definition at line 42 of file proc.c.
Referenced by pid_is_group().