Syscalls

List of syscalls :

POSIX-compliant (except use of 64 integer instead of 32bits):

POSIX-non-compliant:

If a syscall is sucessful, it returns a null or positive value. On error a negative error value is returned.

exit

void exit(int64_t code)

Exit process with code code. Zero should be used on success, positive values on errors.

Does not return.

fork

int64_t fork()

Creates an independent copy of the process and attaches it as a child of the original.

Return value:

waitpid

int64_t waitpid(pid_t pid, int64_t *status, int64_t options)

Wait for a child process to exit.

If pid is -1 (aka no_pid), waits on any child. Otherwise, waits on a child with given pid.

If the WNOHANG bit is set in options, returns immediatly even if there is an alive child matching pid.

Return value:

wait

int64_t wait(int64_t *status)

Wrapper for waitpid(no_pid, status, 0)

microsleep

int64_t microsleep(uint64_t ms)

Sleeps for at most ms microseconds.

Return value:

execve

int64_t execve(char const* path, char const* const* argv, char const* const* envp)

Replaces the current process image with another process image, found in path. Passes as arguments argv (NULL-terminated array of NULL-terminated strings). Environment for the process is set to envp (NULL-terminated array of NULL-terminated strings). As a convention, strings in envp follow the variable=value syntax.

Return value:

execv

int64_t execve(char const* path, char const* const* argv)

Wrapper for execve(path, argv, environ), where environ is the current environment.

exec

int64_t exec(char const* path)

Wrapper for execv(path, {path, nullptr}).

sbrk

uintptr_t sbrk(int64_t delta)

Changes heap size by delta.

Return value:

open

int64_t open(const char* path, int64_t flags)

Open the file at path and return the newly created file descriptor.

Flag bits:

Return value:

close

int64_t close(int64_t fd)

Closes file descriptor fd.

Return value:

dup2

int64_t dup2(uint64_t oldfd, uint64_t newfd)

Duplicates file descriptor oldfd into file descriptor newfd.

Closes newfd beforehand if it was open.

Return value:

pipe

int64_t pipe(uint64_t fds[2])

Creates a pipe, read end is stored in fds[pipe_out], write end in fds[pipe_in].

Return value:

flushpipe

int64_t flush_pipe(fd_t fds[2])

Same as pipe, but the created pipe is a FlushPipe.

write

int64_t write(fd_t fd, const char* buf, uint64_t count) int64_t write(fd_t fd, const uint8_t* buf, uint64_t count)

Writes at most count bytes from buf to file descriptor fd.

Return value:

read

int64_t read(fd_t fd, uint8_t* buf, uint64_t count) int64_t read(fd_t fd, char* buf, uint64_t count)

Reads at most count bytes to buf from file descriptor fd.

Return value:

pollin

int64_t pollin(fd_t* fds, uint64_t count)

Edits fds in place to put the file descriptors ready to be read at the begining. Note that the end of fds becomes garbage.

Return value:

chdir

int64_t chdir(const char* path)

Changes current directory to path

Return value:

opendir

int64_t opendir(char const* path = ".")

Prepare directory for readdir.

Return value:

readdir

int64_t readdir(char* buf)

Reads an entry from the last directory opened with opendir and stores its name in buf.

Return value:

raise

int64_t raise(uint64_t sig)

Equivalent to kill(self_pid, sig). where self_pid is the pid of the current process.

kill

int64_t kill(pid_t pid, uint64_t sig)

Send signal sig to process pid.

If sig is not a valid signal, returns err_badsig. Valid signals are 0 to NBSIG (excluded).

Implemented signals are the following :

Return value:

sigaction

int64_t sigaction(uint64_t sig, const sigact* new_action, sigact* oldaction = nullptr)

struct sigact {
  void (*sa_handler)(int) = nullptr;
  uint64_t sa_mask = 0;
  void (*sa_restorer)(void) = &sigreturn;
};

Sets a handler for signal sig.

sa_handler can be a function, SIG_DFL (to set ot default) or SIG_IGN (to ignore signal). During the execution of the handler, the signal mask will temporarily be set to sa_mask. sa_restorer is the address to which the handler will return, most of the time it should not be modified.

Return value:

sigprocmask

int64_t sigprocmask(sig_mask_action how, uint64_t *set, uint64_t *oldset = nullptr)

Sets the signal mask and/or write back the old mask. The signal i will be pending until 1<<i is cleared from the mask.

If set is not null, sets the signal mask :

how can be one of the following :

If oldset is not null, sets *oldset to the old signal mask.

Return value:

sigreturn

void sigreturn()

Used internally. Should not be called directly.

pause

int64_t pause()

Waits for a signal that is not ignored. Returns iff interrupted by a signal caught by a handler.

Return value:

window_get

WindowSize window_get()

struct WindowSize {
  uint32_t w;
  uint32_t h;
};

Opens a window if the process doesn't have one.

Returns the size of the window.

window_refresh

void window_refresh(*WindowData)

struct WindowData {
  uint32_t w;
  uint32_t h;
  uint16_t *data;
};

Changes window buffer.

get_key

int16_t get_key()

Receive a keycode from the Window Manager.

Return value: