AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
sys_proc.c File Reference

System calls about processes and threads: exit(), execve(), fork(), waitpid(), threads, IDs, process groups and sessions, memory (sbrk(), mmap(), munmap()), and the machine (uname(), reboot(), sethostname()). More...

#include "syscalls.h"

Go to the source code of this file.

Functions

long sys_exit (int code)
 Implements _exit(): ends the calling process.
long sys_execve (const char *path, char *const argp[], char *const envp[])
 Implements execve(): runs another program in the calling process.
long sys_fork (void)
 Implements fork(): makes a child process, a copy of the caller.
long sys_getpid (void)
 Implements getpid().
long sys_sbrk (ptrdiff_t incr)
 Implements sbrk(): grows or shrinks the heap.
long sys_times (struct aquila_tms *buf)
 Implements times(): the CPU times of the process and of its children that it waited for, in ticks (AQUILA_CLK_TCK a second).
long sys_waitpid (int pid, int *stat_loc, int options)
 Implements waitpid(): waits for a child to end (or stop, or continue, if asked), and reaps it if it ended.
long sys_uname (struct utsname *name)
 Implements uname(): the system's name, release, version and machine, and the host name.
long sys_thread_create (struct aquila_thread_args *__uthread)
 Makes a new thread in the calling process (for pthread_create()), and makes it ready to run.
long sys_thread_exit (void *value_ptr)
 Ends the calling thread (for pthread_exit()).
long sys_thread_join (int tid, void **value_ptr)
 Waits for a thread of the calling process to end, and frees it (for pthread_join()).
long sys_setpgid (pid_t pid, pid_t pgid)
 Implements setpgid(): moves a process (the caller or one of its children) into a process group of its session.
long sys_setuid (uid_t uid)
 Implements setuid(): sets the process's user ID.
long sys_setgid (gid_t gid)
 Implements setgid(): sets the process's group ID.
long sys_getuid (void)
 Implements getuid().
long sys_getgid (void)
 Implements getgid().
long sys_mmap (struct aquila_mmap_args *uargs, void **ret)
 Implements mmap(): maps anonymous memory or a file into the calling process.
long sys_munmap (void *addr, size_t len)
 Implements munmap(): removes a mapping.
long sys_sysconf (int name)
 Implements sysconf(): not supported; the C library answers it.
long sys_getppid (void)
 Implements getppid().
long sys_getpgrp (void)
 Implements getpgrp().
long sys_getrandom (void *buf, size_t len, unsigned flags)
 Implements getrandom() (and the C library's getentropy()): fills a buffer with random bytes.
long sys_thread_self (void)
 Returns the calling thread's ID, for pthread_self().
long sys_reboot (unsigned cmd)
 Implements reboot(): restarts, halts or powers off the machine, or sets what Ctrl+Alt+Del does.
long sys_setsid (void)
 Implements setsid(): makes the calling process the leader of a new session and process group.
long sys_sethostname (const char *name, size_t len)
 Implements sethostname().

Variables

static char nodename [sizeof(((struct utsname *) 0) ->nodename)] = UTSNAME_NODENAME

Detailed Description

System calls about processes and threads: exit(), execve(), fork(), waitpid(), threads, IDs, process groups and sessions, memory (sbrk(), mmap(), munmap()), and the machine (uname(), reboot(), sethostname()).

Definition in file sys_proc.c.

Function Documentation

◆ sys_execve()

long sys_execve ( const char * path,
char *const argp[],
char *const envp[] )

Implements execve(): runs another program in the calling process.

Parameters
paththe program's path
argpits arguments, a NULL-terminated array
envpits environment, a NULL-terminated array
Returns
a negative errno on failure (-ENOENT, -EACCES, -ENOEXEC, -E2BIG, -EFAULT, -ENOMEM); on success it doesn't return, the new program starts

Definition at line 42 of file sys_proc.c.

References curthread, kfree(), LOG_DEBUG, proc_execve(), sched_thread_spawn(), sys_execve(), and upath_get().

Referenced by sys_execve().

◆ sys_exit()

long sys_exit ( int code)

Implements _exit(): ends the calling process.

It doesn't return.

Parameters
codethe exit status, which the parent's wait() reports
Returns
nothing: it doesn't return

Definition at line 16 of file sys_proc.c.

References arch_sleep(), curproc, proc::exit, LOG_DEBUG, PROC_EXIT, proc_kill(), and sys_exit().

Referenced by sys_exit(), and sys_thread_exit().

◆ sys_fork()

long sys_fork ( void )

Implements fork(): makes a child process, a copy of the caller.

Returns
the child's process ID in the parent (the child gets 0), or a negative errno: -ENOMEM, -EAGAIN

Definition at line 76 of file sys_proc.c.

References curthread, queue::head, LOG_DEBUG, NULL, proc::pid, proc_fork(), sched_thread_ready(), sys_fork(), proc::threads, and qnode::value.

Referenced by sys_fork().

◆ sys_getgid()

long sys_getgid ( void )

Implements getgid().

Returns
the calling process's group ID

Definition at line 498 of file sys_proc.c.

References curproc, LOG_DEBUG, and sys_getgid().

Referenced by sys_getgid().

◆ sys_getpgrp()

long sys_getpgrp ( void )

Implements getpgrp().

Returns
the calling process's process group ID

Definition at line 683 of file sys_proc.c.

References curproc, LOG_DEBUG, and sys_getpgrp().

Referenced by sys_getpgrp().

◆ sys_getpid()

long sys_getpid ( void )

Implements getpid().

Returns
the calling process's ID

Definition at line 100 of file sys_proc.c.

References curproc, LOG_DEBUG, and sys_getpid().

Referenced by sys_getpid().

◆ sys_getppid()

long sys_getppid ( void )

Implements getppid().

Returns
the parent's process ID, or 0 if there is none

Definition at line 670 of file sys_proc.c.

References curproc, LOG_DEBUG, and sys_getppid().

Referenced by sys_getppid().

◆ sys_getrandom()

long sys_getrandom ( void * buf,
size_t len,
unsigned flags )

Implements getrandom() (and the C library's getentropy()): fills a buffer with random bytes.

Parameters
bufthe buffer
lenits size
flagsnot used
Returns
how many bytes were written, or -EFAULT

Definition at line 698 of file sys_proc.c.

References memset(), MIN, random_get(), sys_getrandom(), and uaccess_check().

Referenced by sys_getrandom().

◆ sys_getuid()

long sys_getuid ( void )

Implements getuid().

Returns
the calling process's user ID

Definition at line 487 of file sys_proc.c.

References curproc, LOG_DEBUG, and sys_getuid().

Referenced by sys_getuid().

◆ sys_mmap()

long sys_mmap ( struct aquila_mmap_args * uargs,
void ** ret )

Implements mmap(): maps anonymous memory or a file into the calling process.

Pages are brought in by page faults, when first touched.

Parameters
uargsthe arguments, in user memory: the address, length, protection, flags, descriptor and offset
retwhere to put the mapping's address
Returns
0, or a negative errno: -EBADF, -EINVAL, -ENODEV, -ENOMEM, -EFAULT

Definition at line 514 of file sys_proc.c.

References vm_entry::base, curproc, fd_file(), FILE_SOCKET, file::flags, vm_entry::flags, kfree(), LOG_DEBUG, NULL, vm_entry::off, PAGE_MASK, PAGE_ROUND, vm_entry::qnode, vm_entry::size, sys_mmap(), uaccess_check(), vfs_map(), vm_entry_new(), vm_entry::vm_object, vm_object_decref(), vm_object_incref(), vm_object_vnode(), VM_SHARED, vm_space_insert(), VM_UR, VM_UW, and VM_UX.

Referenced by sys_mmap().

◆ sys_munmap()

long sys_munmap ( void * addr,
size_t len )

Implements munmap(): removes a mapping.

Only a whole mapping can be removed, not a part of one; the heap and the stack can't be.

Parameters
addrthe mapping's address
lenits length
Returns
0, or -EINVAL if no mapping has that address and length

Definition at line 622 of file sys_proc.c.

References vm_entry::base, curproc, kfree(), LOG_DEBUG, PAGE_ROUND, vm_entry::qnode, queue_for, vm_entry::size, sys_munmap(), vm_space::vm_entries, vm_entry_destroy(), and vm_unmap_full().

Referenced by sys_munmap().

◆ sys_reboot()

long sys_reboot ( unsigned cmd)

Implements reboot(): restarts, halts or powers off the machine, or sets what Ctrl+Alt+Del does.

Only root may.

Parameters
cmdthe command (RB_*)
Returns
0 for the Ctrl+Alt+Del commands, -EPERM, or -EINVAL; the others don't return

Definition at line 740 of file sys_proc.c.

References curproc, kernel_reboot(), LOG_DEBUG, and sys_reboot().

Referenced by sys_reboot().

◆ sys_sbrk()

long sys_sbrk ( ptrdiff_t incr)

Implements sbrk(): grows or shrinks the heap.

The heap may grow up to the next mapping (or the stack).

Parameters
incrhow many bytes to add (or take off, if negative)
Returns
the old end of the heap, or (uintptr_t) -1 if the heap can't change that much

Definition at line 114 of file sys_proc.c.

References vm_entry::base, curproc, LOG_DEBUG, qnode::next, PAGE_ROUND, sys_sbrk(), USER_STACK_BASE, and qnode::value.

Referenced by sys_sbrk().

◆ sys_setgid()

long sys_setgid ( gid_t gid)

Implements setgid(): sets the process's group ID.

Root may set any ID, other users only their own.

Parameters
gidthe group ID
Returns
0, or -EPERM

Definition at line 470 of file sys_proc.c.

References curproc, LOG_DEBUG, and sys_setgid().

Referenced by sys_setgid().

◆ sys_sethostname()

long sys_sethostname ( const char * name,
size_t len )

Implements sethostname().

Only root may.

Parameters
namethe host name (not NUL-terminated)
lenits length
Returns
0, or a negative errno: -EPERM, -EINVAL if it's too long, -EFAULT

Definition at line 793 of file sys_proc.c.

References curproc, LOG_DEBUG, memcpy(), nodename, and sys_sethostname().

Referenced by sys_sethostname().

◆ sys_setpgid()

long sys_setpgid ( pid_t pid,
pid_t pgid )

Implements setpgid(): moves a process (the caller or one of its children) into a process group of its session.

Parameters
pidthe process, or 0 for the caller
pgidthe group's ID, or 0 for the process's own ID (a new group)
Returns
0, or a negative errno: -EINVAL, -ESRCH, -EPERM, -ENOMEM

Definition at line 418 of file sys_proc.c.

References curproc, session::leader, LOG_DEBUG, proc::parent, proc::pgrp, proc::pid, proc_pid_find(), proc_setpgid(), pgroup::session, and sys_setpgid().

Referenced by sys_setpgid().

◆ sys_setsid()

long sys_setsid ( void )

Implements setsid(): makes the calling process the leader of a new session and process group.

Returns
the new session's ID (the caller's process ID), or a negative errno: -EPERM for a process group leader, -ENOMEM

Definition at line 758 of file sys_proc.c.

References curproc, LOG_DEBUG, pgroup::pgid, proc::pgrp, proc::pgrp_node, pgrp_release(), proc::pid, pgroup::procs, session_new(), and sys_setsid().

Referenced by sys_setsid().

◆ sys_setuid()

long sys_setuid ( uid_t uid)

Implements setuid(): sets the process's user ID.

There are no separate effective or saved IDs: root may set any ID, other users only their own.

Parameters
uidthe user ID
Returns
0, or -EPERM

Definition at line 451 of file sys_proc.c.

References curproc, LOG_DEBUG, and sys_setuid().

Referenced by sys_setuid().

◆ sys_sysconf()

long sys_sysconf ( int name)

Implements sysconf(): not supported; the C library answers it.

Parameters
namethe variable
Returns
-ENOSYS

Definition at line 659 of file sys_proc.c.

References LOG_DEBUG, and sys_sysconf().

Referenced by sys_sysconf().

◆ sys_thread_create()

long sys_thread_create ( struct aquila_thread_args * __uthread)

Makes a new thread in the calling process (for pthread_create()), and makes it ready to run.

Parameters
__uthreadthe arguments, in user memory: the thread's stack, the C library's start, the thread's function and its argument
Returns
the new thread's ID, or a negative errno: -EFAULT, -ENOMEM

Definition at line 286 of file sys_proc.c.

References curthread, LOG_DEBUG, NULL, sched_thread_ready(), thread::stack, sys_thread_create(), thread_create(), thread::tid, and uaccess_check().

Referenced by sys_thread_create().

◆ sys_thread_exit()

long sys_thread_exit ( void * value_ptr)

Ends the calling thread (for pthread_exit()).

It stays as a zombie until another thread joins it. The last live thread ending ends the process. It doesn't return.

Parameters
value_ptrthe value that thread_join() returns
Returns
nothing: it doesn't return

Definition at line 325 of file sys_proc.c.

References arch_cur_thread_kill(), curproc, curthread, LOG_DEBUG, thread::owner, queue_for, thread::state, sys_exit(), sys_thread_exit(), proc::thread_join, proc::threads, waitq_wakeup(), and ZOMBIE.

Referenced by sys_thread_exit().

◆ sys_thread_join()

long sys_thread_join ( int tid,
void ** value_ptr )

Waits for a thread of the calling process to end, and frees it (for pthread_join()).

Parameters
tidthe thread's ID
value_ptrwhere to put the value it ended with, or NULL
Returns
the thread's ID, or a negative errno: -ESRCH (no such thread, or joined already), -EDEADLK (the caller itself), -EINTR, -EFAULT

Definition at line 367 of file sys_proc.c.

References curproc, curthread, ERESTARTSYS, thread::exit_value, kfree(), LOG_DEBUG, NULL, thread::owner, queue_for, thread::state, sys_thread_join(), proc::threads, thread::tid, waitq_sleep(), and ZOMBIE.

Referenced by sys_thread_join().

◆ sys_thread_self()

long sys_thread_self ( void )

Returns the calling thread's ID, for pthread_self().

Returns
the thread's ID

Definition at line 727 of file sys_proc.c.

References curthread, and sys_thread_self().

Referenced by sys_thread_self().

◆ sys_times()

long sys_times ( struct aquila_tms * buf)

Implements times(): the CPU times of the process and of its children that it waited for, in ticks (AQUILA_CLK_TCK a second).

Parameters
bufwhere to put them
Returns
the real time since boot, in ticks, or -EFAULT

Definition at line 148 of file sys_proc.c.

References arch_rtime_ns(), curproc, LOG_DEBUG, sys_times(), and tick.

Referenced by sys_times().

◆ sys_uname()

long sys_uname ( struct utsname * name)

Implements uname(): the system's name, release, version and machine, and the host name.

Parameters
namewhere to put them
Returns
0, or -EFAULT

Definition at line 259 of file sys_proc.c.

References LOG_DEBUG, memcpy(), memset(), MIN, nodename, sys_uname(), UTSNAME_MACHINE, UTSNAME_RELEASE, UTSNAME_SYSNAME, and UTSNAME_VERSION.

Referenced by sys_uname().

◆ sys_waitpid()

long sys_waitpid ( int pid,
int * stat_loc,
int options )

Implements waitpid(): waits for a child to end (or stop, or continue, if asked), and reaps it if it ended.

Parameters
pidwhich child: that one if > 0, any if -1, any in the caller's process group if 0, or any in process group -pid if < -1
stat_locwhere to put its status, or NULL
optionsWNOHANG, WUNTRACED, WCONTINUED
Returns
the child's process ID, 0 if WNOHANG and none is ready, or a negative errno: -ECHILD, -EINTR, -EFAULT

Definition at line 181 of file sys_proc.c.

References curproc, ERESTARTSYS, proc::exit, LOG_DEBUG, options(), proc::parent, pgroup::pgid, proc::pgrp, proc::pid, proc_reap(), procs, queue_for, proc::stopped, sys_waitpid(), WAIT_CONTINUED, proc::wait_event, WAIT_STOPPED, waitq_sleep(), and proc::zombie.

Referenced by sys_waitpid().

Variable Documentation

◆ nodename

char nodename[sizeof(((struct utsname *) 0) ->nodename)] = UTSNAME_NODENAME
static

Definition at line 250 of file sys_proc.c.

Referenced by sys_sethostname(), and sys_uname().