MentOS  0.8.0
The Mentoring Operating System
Macros | Functions | Variables
process.c File Reference

Process data structures and functions. More...

Macros

#define __DEBUG_HEADER__   "[PROC ]"
 Change header.
 
#define __DEBUG_LEVEL__   LOGLEVEL_NOTICE
 Set log level.
 

Functions

static int __count_args (char **args)
 Counts the number of arguments. More...
 
static int __count_args_bytes (char **args)
 Counts the bytes occupied by the arguments. More...
 
static char ** __push_args_on_stack (uintptr_t *stack, char *args[])
 Pushes the arguments on the stack. More...
 
static int __reset_process (task_struct *task)
 Resets the process. More...
 
static int __has_shebang (vfs_file_t *file)
 Checks if the file starts with a shebang. More...
 
static int __load_executable (const char *path, task_struct *task, uint32_t *entry)
 Replace the current process with a loaded exectuable. More...
 
static task_struct__alloc_task (task_struct *source, task_struct *parent, const char *name)
 Allocates the memory for a task. More...
 
int init_tasking (void)
 Initialize the task management. More...
 
task_structprocess_create_init (const char *path)
 Create and spawn the init process. More...
 
char * sys_getcwd (char *buf, size_t size)
 Get current working directory. More...
 
int sys_chdir (char const *path)
 Changes the working directory. More...
 
int sys_fchdir (int fd)
 Changes the working directory. More...
 
pid_t sys_fork (pt_regs *f)
 Clone the calling process, but without copying the whole address space. The calling process is suspended until the new process exits or is replaced by a call to ‘execve’. More...
 
int sys_execve (pt_regs *f)
 Replaces the current process image with a new process image. More...
 

Variables

static kmem_cache_ttask_struct_cache
 Cache for creating the task structs.
 
static task_structinit_proc
 The task_struct of the init process.
 

Detailed Description

Process data structures and functions.

Function Documentation

◆ __alloc_task()

static task_struct* __alloc_task ( task_struct source,
task_struct parent,
const char *  name 
)
inlinestatic

Allocates the memory for a task.

Parameters
sourcethe source task we use for the copy.
parentthe parent process.
namethe name of the new process.
Returns
pointer to the newly allocated task.

◆ __count_args()

static int __count_args ( char **  args)
inlinestatic

Counts the number of arguments.

Parameters
argsthe array of arguments, it must be NULL terminated.
Returns
the number of arguments.

◆ __count_args_bytes()

static int __count_args_bytes ( char **  args)
inlinestatic

Counts the bytes occupied by the arguments.

Parameters
argsthe array of arguments, it must be NULL terminated.
Returns
the bytes occupied by the arguments.

◆ __has_shebang()

static int __has_shebang ( vfs_file_t file)
static

Checks if the file starts with a shebang.

Parameters
filethe file to check.
Returns
1 if it contains a shebang, 0 otherwise.

◆ __load_executable()

static int __load_executable ( const char *  path,
task_struct task,
uint32_t entry 
)
static

Replace the current process with a loaded exectuable.

Parameters
paththe path to the executable to load.
taskthe task to laod the exectuable.
entry
Returns
-errno or 0 on failure, 1 on success, 2 if a interpreter was loaded

◆ __push_args_on_stack()

static char** __push_args_on_stack ( uintptr_t stack,
char *  args[] 
)
inlinestatic

Pushes the arguments on the stack.

Parameters
stackpointer to the stack location.
argsthe list of arguments.
Returns
the final position of the stack, where the list of pushed arguments is stored.

◆ __reset_process()

static int __reset_process ( task_struct task)
static

Resets the process.

Parameters
taskthe process to reset.
Returns
0 on failure, 1 otherwise.

◆ init_tasking()

int init_tasking ( void  )

Initialize the task management.

Returns
1 success, 0 failure.

◆ process_create_init()

task_struct* process_create_init ( const char *  path)

Create and spawn the init process.

Parameters
pathPath of the init program.
Returns
Pointer to init process.

◆ sys_chdir()

int sys_chdir ( char const *  path)

Changes the working directory.

Parameters
pathThe new working directory.
Returns
On success 0. On error -1, and errno indicates the error.

◆ sys_execve()

int sys_execve ( pt_regs f)

Replaces the current process image with a new process image.

Parameters
fCPU registers whe calling this function.
Returns
0 on success, -1 on error.

◆ sys_fchdir()

int sys_fchdir ( int  fd)

Changes the working directory.

Parameters
fdFile descriptor of the new working directory.
Returns
On success 0. On error -1, and errno indicates the error.

◆ sys_fork()

pid_t sys_fork ( pt_regs f)

Clone the calling process, but without copying the whole address space. The calling process is suspended until the new process exits or is replaced by a call to ‘execve’.

Parameters
fCPU registers whe calling this function.
Returns
Return -1 for errors, 0 to the new process, and the process ID of the new process to the old process.

◆ sys_getcwd()

char* sys_getcwd ( char *  buf,
size_t  size 
)

Get current working directory.

Parameters
bufThe array where the CWD will be copied.
sizeThe size of the array.
Returns
On success, returns the same pointer to buf. On failure, returnr NULL, and errno is set to indicate the error.