AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
proc.h
Go to the documentation of this file.
1#ifndef _SYS_PROC_H
2#define _SYS_PROC_H
3
4#include <core/system.h>
5
6struct proc;
7struct pgroup;
8struct session;
9
10#include <mm/vm.h>
11#include <fs/vfs.h>
12#include <ds/queue.h>
13#include <sys/waitq.h>
14#include <sys/thread.h>
15#include <sys/signal.h>
16#include <dev/dev.h>
17
18struct tty;
19
20/*
21 * Processes, their process groups and sessions (sys/proc.c). See
22 * docs/kernel/processes.md.
23 */
24
26struct session {
29
31 struct queue *pgps;
32
34 struct proc *leader;
35
37 struct tty *ctty;
38
40 struct qnode *qnode;
41};
42
47struct fd {
48 struct file *file; /* NULL when it's free, FD_RESERVED while it's being set up */
49 int flags; /* FD_CLOEXEC */
50};
51
52/* A descriptor that's taken, but not set up yet. */
53#define FD_RESERVED ((struct file *) -1)
54
56struct pgroup {
59
62
65
67 struct queue *procs;
68
70 struct proc *leader;
71
72};
73
169
170/* sys/fork.c: copies the process of `thread` */
171int proc_fork(struct thread *thread, struct proc **ref);
172
173/* sys/execve.c: loads a program into the process of `thread` */
174int proc_execve(struct thread *thread, const char *fn, char * const argv[], char * const env[]);
175
176/* Sets up init, the first process, to start its program, at boot. */
177void init_execve(struct proc *proc, int argc, char * const argv[], int envc, char * const envp[]);
178
179/* sys/proc.c: process IDs, making and ending processes, groups and
180 * sessions, file descriptors */
182void proc_pid_free(int pid);
183struct proc *proc_pid_find(pid_t pid);
184
185int proc_new(struct proc **ref);
186int session_new(struct proc *proc);
187int proc_setpgid(struct proc *proc, pid_t pgid);
188void pgrp_release(struct pgroup *pgrp);
189int pgrp_orphaned(struct pgroup *pgrp, struct proc *gone);
190
191void proc_kill(struct proc *proc);
192int proc_threads_end(struct proc *proc, struct thread *keep, int exiting);
193void proc_threads_exec(struct proc *proc);
194int proc_reap(struct proc *proc);
195int proc_fd_get(struct proc *proc);
196void proc_fd_release(struct proc *proc, int fd);
197void proc_fd_install(struct proc *proc, int fd, struct file *file, int flags);
198struct file *proc_fd_file(struct proc *proc, int fd);
199int proc_fd_close(struct proc *proc, int fd);
200void proc_dump(struct proc *proc);
201
202int proc_init(struct proc *proc);
203
204/* An exit status, as waitpid() reports it: `info` (the exit code) in the
205 * second byte, and `code` (the signal that ended the process, or 0) in
206 * the first. */
207#define PROC_EXIT(info, code) ((((info) & 0xff) << 8) | ((code) & 0xff))
208
209/* A process's uio: its current directory and credentials. */
210#define PROC_UIO(proc) ((struct uio){.cwd = (proc)->cwd, .uid = (proc)->uid, .gid = (proc)->gid, .mask = (proc)->mask})
211
212/* All the processes. */
213extern struct queue *procs;
214
215/* init (PID 1), the orphans' parent; only it and the kernel's process have
216 * no parent. And the sessions. */
217extern struct proc *initproc;
218extern struct queue *sessions;
219
220#endif /* ! _SYS_PROC_H */
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 tak...
Definition proc.c:514
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...
Definition proc.c:358
void proc_dump(struct proc *proc)
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.
Definition proc.c:578
void proc_pid_free(int pid)
Frees a process ID.
Definition proc.c:102
int proc_reap(struct proc *proc)
Frees a zombie, once its parent waited for it (or doesn't wait for its children).
Definition proc.c:484
int proc_init(struct proc *proc)
Gives a new process (init) its ID and its descriptor table.
Definition proc.c:197
struct queue * sessions
Definition proc.c:42
int proc_fork(struct thread *thread, struct proc **ref)
Makes a child process, a copy of the process of thread.
Definition fork.c:81
int proc_threads_end(struct proc *proc, struct thread *keep, int exiting)
Ends the threads of a process, but one.
Definition proc.c:305
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().
Definition execve.c:358
struct file * proc_fd_file(struct proc *proc, int fd)
Finds the open file a descriptor refers to.
Definition proc.c:560
pid_t proc_pid_alloc(void)
Allocates a process ID, from 1 (init's) on.
Definition proc.c:80
void proc_fd_release(struct proc *proc, int fd)
Frees a reserved descriptor that wasn't set up after all.
Definition proc.c:532
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 o...
Definition proc.c:227
int proc_new(struct proc **ref)
Makes a new process structure, with one thread and an empty address space, on the list of every proce...
Definition proc.c:114
struct proc * proc_pid_find(pid_t pid)
Finds a process by its ID.
Definition proc.c:179
int session_new(struct proc *proc)
Makes a new session, with a new process group, both led by a process and with its ID,...
Definition proc.c:596
void proc_fd_install(struct proc *proc, int fd, struct file *file, int flags)
Makes a reserved descriptor refer to an open file.
Definition proc.c:547
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 envir...
Definition execve.c:332
int proc_setpgid(struct proc *proc, pid_t pgid)
Moves a process into a process group of its session, as setpgid() does.
Definition proc.c:667
struct queue * procs
Definition proc.c:37
struct proc * initproc
Definition proc.c:39
void proc_kill(struct proc *proc)
Ends a process: its threads end, its descriptors are closed, its memory is freed, its children go to ...
Definition proc.c:375
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 gro...
Definition proc.c:742
#define SIG_MAX
Definition signal.h:16
__SIZE_TYPE__ uintptr_t
Definition stddef.h:12
unsigned long long int uint64_t
Definition stddef.h:23
A file descriptor: the open file it refers to (fs/file.c, shared with the descriptors that dup() and ...
Definition proc.h:47
int flags
Definition proc.h:49
struct file * file
Definition proc.h:48
An open file (POSIX's "open file description", fs/file.c).
Definition vfs.h:157
A link in a list, inside the structure that is listed.
Definition list.h:31
A process group: the processes of a job.
Definition proc.h:56
struct proc * leader
its leader, the process whose PID is its ID
Definition proc.h:70
struct queue * procs
its processes
Definition proc.h:67
struct session * session
its session
Definition proc.h:61
pid_t pgid
the group's ID
Definition proc.h:58
struct qnode * session_node
its node on its session's list
Definition proc.h:64
A process.
Definition proc.h:75
struct waitq stop_queue
its stopped threads sleep here
Definition proc.h:104
pid_t pid
the process's ID
Definition proc.h:77
struct proc * parent
its parent
Definition proc.h:90
struct aquila_sigaction sigaction[SIG_MAX+1]
its signal actions
Definition proc.h:157
struct qnode * pgrp_node
Definition proc.h:81
uint64_t cstime
Definition proc.h:109
uint64_t utime
its CPU time in user mode and in the kernel (ns), and that of the children it waited for
Definition proc.h:108
struct waitq wait_queue
a parent waiting for a child (waitpid()) sleeps here
Definition proc.h:154
int tid_last
the last thread ID given out (threads leave in any order, so their count can't be the next one's ID)
Definition proc.h:140
uint64_t stime
Definition proc.h:108
struct vm_space vm_space
its address space
Definition proc.h:129
struct vm_entry * stack_vm
Definition proc.h:133
struct waitq thread_join
threads waiting in thread_join() sleep here
Definition proc.h:143
int exit
its exit status (PROC_EXIT())
Definition proc.h:160
struct vnode * cwd
its current directory (a reference), or NULL for the root
Definition proc.h:93
uintptr_t heap_start
where its heap starts, and where it ends now (sbrk())
Definition proc.h:122
struct list_node alarm_link
Definition proc.h:115
uint64_t alarm_at
its real time timer (ITIMER_REAL): SIGALRM when arch_rtime_ns() reaches alarm_at, then every alarm_in...
Definition proc.h:114
uint64_t cutime
Definition proc.h:109
int stopped
the signal that stopped it, or 0; and what waitpid() has to report (WAIT_*)
Definition proc.h:100
struct vm_entry * heap_vm
its heap's and its stack's mappings
Definition proc.h:132
gid_t gid
Definition proc.h:119
int running
whether it's running
Definition proc.h:163
uintptr_t heap
Definition proc.h:123
struct fd * fds
its file descriptors (FDS_COUNT of them)
Definition proc.h:87
mode_t mask
its file mode creation mask (umask)
Definition proc.h:96
char * name
its name: the program it runs
Definition proc.h:84
uint64_t alarm_interval
Definition proc.h:114
uid_t uid
its user and group IDs
Definition proc.h:118
struct queue threads
its threads
Definition proc.h:136
sigset_t sig_pending
its pending signals, a bit (1 << sig) each: a signal sent again while it's pending is delivered once ...
Definition proc.h:148
struct pgroup * pgrp
its process group, and its node on the group's list
Definition proc.h:80
uintptr_t entry
its program's entry point
Definition proc.h:126
int wait_event
Definition proc.h:101
int zombie
proc_kill() is done with it, so it can be reaped (that isn't as soon as it stops running: closing its...
Definition proc.h:167
sigset_t sigmask
its blocked signals, a bit (1 << sig) each
Definition proc.h:151
A queue's node, holding one entry.
Definition queue.h:19
A queue.
Definition queue.h:29
A session: its process groups, and its controlling terminal.
Definition proc.h:26
struct queue * pgps
its process groups
Definition proc.h:31
struct qnode * qnode
its node on the list of sessions
Definition proc.h:40
struct tty * ctty
its controlling terminal, or NULL
Definition proc.h:37
struct proc * leader
its leader, the process that made it
Definition proc.h:34
pid_t sid
the session's ID
Definition proc.h:28
A thread.
Definition thread.h:23
A terminal, and its line discipline's state.
Definition tty.h:25
A mapping (map entry): a range of an address space, its permissions, and where its pages come from.
Definition vm.h:66
A file in memory (a vnode): what its inode says, and the kernel's own state.
Definition vfs.h:113
Definition waitq.h:19
int pid_t
Definition types.h:8
unsigned long sigset_t
Definition types.h:27
uint32_t gid_t
Definition types.h:21
uint32_t uid_t
Definition types.h:20
uint32_t mode_t
Definition types.h:15