2011-12-11 03:34:10 +04:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
*
|
|
|
|
* Processes
|
2011-12-14 12:37:43 +04:00
|
|
|
*
|
|
|
|
* Internal format format for a process and functions to spawn
|
|
|
|
* new processes and manage the process tree.
|
2011-12-11 03:34:10 +04:00
|
|
|
*/
|
2011-12-08 06:59:08 +04:00
|
|
|
#include <system.h>
|
|
|
|
#include <process.h>
|
|
|
|
#include <tree.h>
|
|
|
|
#include <list.h>
|
2012-09-04 09:35:11 +04:00
|
|
|
#include <logging.h>
|
2013-04-22 04:33:16 +04:00
|
|
|
#include <shm.h>
|
2014-04-06 03:36:17 +04:00
|
|
|
#include <printf.h>
|
2011-12-08 06:59:08 +04:00
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
tree_t * process_tree; /* Parent->Children tree */
|
2013-03-19 08:52:45 +04:00
|
|
|
list_t * process_list; /* Flat storage */
|
2011-12-14 12:37:43 +04:00
|
|
|
list_t * process_queue; /* Ready queue */
|
2012-12-11 08:28:31 +04:00
|
|
|
list_t * sleep_queue;
|
2011-12-09 01:25:48 +04:00
|
|
|
volatile process_t * current_process = NULL;
|
2014-04-05 08:30:14 +04:00
|
|
|
process_t * kernel_idle_task = NULL;
|
2011-12-08 06:59:08 +04:00
|
|
|
|
2014-04-13 05:15:28 +04:00
|
|
|
static uint8_t volatile tree_lock = 0;
|
|
|
|
static uint8_t volatile process_queue_lock = 0;
|
|
|
|
static uint8_t volatile wait_lock_tmp = 0;
|
|
|
|
static uint8_t volatile sleep_lock = 0;
|
2011-12-16 07:08:48 +04:00
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Default process name string */
|
2011-12-08 06:59:08 +04:00
|
|
|
char * default_name = "[unnamed]";
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/*
|
|
|
|
* Initialize the process tree and ready queue.
|
|
|
|
*/
|
2013-06-06 10:10:36 +04:00
|
|
|
void initialize_process_tree(void) {
|
2011-12-08 06:59:08 +04:00
|
|
|
process_tree = tree_create();
|
2013-03-19 08:52:45 +04:00
|
|
|
process_list = list_create();
|
2011-12-08 06:59:08 +04:00
|
|
|
process_queue = list_create();
|
2012-12-11 08:28:31 +04:00
|
|
|
sleep_queue = list_create();
|
2011-12-08 06:59:08 +04:00
|
|
|
}
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/*
|
|
|
|
* Recursively print a process node to the console.
|
|
|
|
*
|
|
|
|
* @param node Node to print.
|
|
|
|
* @param height Current depth in the tree.
|
|
|
|
*/
|
2011-12-08 06:59:08 +04:00
|
|
|
void debug_print_process_tree_node(tree_node_t * node, size_t height) {
|
2011-12-14 12:37:43 +04:00
|
|
|
/* End recursion on a blank entry */
|
2011-12-08 06:59:08 +04:00
|
|
|
if (!node) return;
|
2014-04-06 02:05:24 +04:00
|
|
|
char * tmp = malloc(512);
|
|
|
|
memset(tmp, 0, 512);
|
|
|
|
char * c = tmp;
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Indent output */
|
2014-04-06 02:05:24 +04:00
|
|
|
for (uint32_t i = 0; i < height; ++i) {
|
|
|
|
c += sprintf(c, " ");
|
|
|
|
}
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Get the current process */
|
2011-12-08 06:59:08 +04:00
|
|
|
process_t * proc = (process_t *)node->value;
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Print the process name */
|
2014-04-06 02:05:24 +04:00
|
|
|
c += sprintf(c, "%d.%d %s", proc->group ? proc->group : proc->id, proc->id, proc->name);
|
2011-12-08 06:59:08 +04:00
|
|
|
if (proc->description) {
|
2011-12-14 12:37:43 +04:00
|
|
|
/* And, if it has one, its description */
|
2014-04-06 02:05:24 +04:00
|
|
|
c += sprintf(c, " %s", proc->description);
|
2011-12-08 06:59:08 +04:00
|
|
|
}
|
2011-12-16 07:08:48 +04:00
|
|
|
if (proc->finished) {
|
2014-04-06 02:05:24 +04:00
|
|
|
c += sprintf(c, " [zombie]");
|
2011-12-16 07:08:48 +04:00
|
|
|
}
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Linefeed */
|
2014-04-06 02:05:24 +04:00
|
|
|
debug_print(NOTICE, "%s", tmp);
|
|
|
|
free(tmp);
|
2011-12-08 06:59:08 +04:00
|
|
|
foreach(child, node->children) {
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Recursively print the children */
|
2011-12-08 06:59:08 +04:00
|
|
|
debug_print_process_tree_node(child->value, height + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/*
|
|
|
|
* Print the process tree to the console.
|
|
|
|
*/
|
2013-06-06 10:10:36 +04:00
|
|
|
void debug_print_process_tree(void) {
|
2011-12-08 06:59:08 +04:00
|
|
|
debug_print_process_tree_node(process_tree->root, 0);
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/*
|
|
|
|
* Retreive the next ready process.
|
|
|
|
* XXX: POPs from the ready queue!
|
|
|
|
*
|
|
|
|
* @return A pointer to the next process in the queue.
|
|
|
|
*/
|
2013-06-06 10:10:36 +04:00
|
|
|
process_t * next_ready_process(void) {
|
2014-04-05 08:30:14 +04:00
|
|
|
if (!process_available()) {
|
|
|
|
return kernel_idle_task;
|
|
|
|
}
|
2011-12-09 01:25:48 +04:00
|
|
|
node_t * np = list_dequeue(process_queue);
|
|
|
|
assert(np && "Ready queue is empty.");
|
2011-12-08 06:59:08 +04:00
|
|
|
process_t * next = np->value;
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/*
|
|
|
|
* Reinsert a process into the ready queue.
|
|
|
|
*
|
|
|
|
* @param proc Process to reinsert
|
|
|
|
*/
|
2011-12-08 06:59:08 +04:00
|
|
|
void make_process_ready(process_t * proc) {
|
2014-04-13 05:15:28 +04:00
|
|
|
if (proc->sleep_node.owner != NULL) {
|
2014-04-13 05:25:56 +04:00
|
|
|
if (proc->sleep_node.owner == sleep_queue) {
|
2014-04-13 05:15:28 +04:00
|
|
|
/* XXX can't wake from timed sleep */
|
2014-04-22 06:40:28 +04:00
|
|
|
if (proc->timed_sleep_node) {
|
|
|
|
spin_lock(&sleep_lock);
|
|
|
|
list_delete(sleep_queue, proc->timed_sleep_node);
|
|
|
|
spin_unlock(&sleep_lock);
|
|
|
|
proc->sleep_node.owner = NULL;
|
|
|
|
free(proc->timed_sleep_node->value);
|
|
|
|
}
|
|
|
|
/* Else: I have no idea what happened. */
|
2014-04-13 05:15:28 +04:00
|
|
|
} else {
|
2014-04-27 12:37:33 +04:00
|
|
|
proc->sleep_interrupted = 1;
|
2014-04-13 05:15:28 +04:00
|
|
|
spin_lock(&wait_lock_tmp);
|
|
|
|
list_delete((list_t*)proc->sleep_node.owner, &proc->sleep_node);
|
|
|
|
spin_unlock(&wait_lock_tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_lock(&process_queue_lock);
|
2012-02-21 09:31:00 +04:00
|
|
|
list_append(process_queue, &proc->sched_node);
|
2014-04-13 05:15:28 +04:00
|
|
|
spin_unlock(&process_queue_lock);
|
2011-12-08 06:59:08 +04:00
|
|
|
}
|
|
|
|
|
2011-12-16 03:21:28 +04:00
|
|
|
|
2014-04-27 12:37:33 +04:00
|
|
|
extern void tree_remove_reparent_root(tree_t * tree, tree_node_t * node);
|
2013-03-21 08:24:55 +04:00
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/*
|
|
|
|
* Delete a process from the process tree
|
|
|
|
*
|
|
|
|
* @param proc Process to find and remove.
|
|
|
|
*/
|
2011-12-08 06:59:08 +04:00
|
|
|
void delete_process(process_t * proc) {
|
|
|
|
tree_node_t * entry = proc->tree_entry;
|
2011-12-14 12:37:43 +04:00
|
|
|
|
|
|
|
/* The process must exist in the tree, or the client is at fault */
|
2012-11-29 11:05:19 +04:00
|
|
|
if (!entry) return;
|
2011-12-14 12:37:43 +04:00
|
|
|
|
|
|
|
/* We can not remove the root, which is an error anyway */
|
2011-12-08 06:59:08 +04:00
|
|
|
assert((entry != process_tree->root) && "Attempted to kill init.");
|
2011-12-09 01:25:48 +04:00
|
|
|
|
2014-04-27 12:37:33 +04:00
|
|
|
if (process_tree->root == entry) {
|
|
|
|
/* We are init, don't even bother. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Remove the entry. */
|
2011-12-16 07:08:48 +04:00
|
|
|
spin_lock(&tree_lock);
|
2014-04-27 12:37:33 +04:00
|
|
|
/* Reparent everyone below me to init */
|
|
|
|
tree_remove_reparent_root(process_tree, entry);
|
2013-03-19 08:52:45 +04:00
|
|
|
list_delete(process_list, list_find(process_list, proc));
|
2011-12-16 07:08:48 +04:00
|
|
|
spin_unlock(&tree_lock);
|
2014-04-27 12:37:33 +04:00
|
|
|
|
|
|
|
/* Uh... */
|
|
|
|
free(proc);
|
2011-12-08 06:59:08 +04:00
|
|
|
}
|
|
|
|
|
2014-04-05 08:30:14 +04:00
|
|
|
static void _kidle(void) {
|
|
|
|
while (1) {
|
|
|
|
IRQ_RES;
|
|
|
|
PAUSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Spawn the idle "process".
|
|
|
|
*/
|
|
|
|
process_t * spawn_kidle(void) {
|
|
|
|
process_t * idle = malloc(sizeof(process_t));
|
|
|
|
memset(idle, 0x00, sizeof(process_t));
|
|
|
|
idle->id = -1;
|
|
|
|
idle->name = strdup("[kidle]");
|
|
|
|
idle->is_tasklet = 1;
|
|
|
|
|
|
|
|
idle->image.stack = (uintptr_t)malloc(KERNEL_STACK_SIZE) + KERNEL_STACK_SIZE;
|
2014-04-06 02:05:24 +04:00
|
|
|
idle->thread.eip = (uintptr_t)&_kidle;
|
2014-04-05 08:30:14 +04:00
|
|
|
idle->thread.esp = idle->image.stack;
|
|
|
|
idle->thread.ebp = idle->image.stack;
|
|
|
|
|
|
|
|
idle->started = 1;
|
|
|
|
idle->running = 1;
|
|
|
|
idle->wait_queue = list_create();
|
|
|
|
idle->shm_mappings = list_create();
|
|
|
|
idle->signal_queue = list_create();
|
|
|
|
|
|
|
|
set_process_environment(idle, current_directory);
|
|
|
|
return idle;
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/*
|
|
|
|
* Spawn the initial process.
|
|
|
|
*
|
|
|
|
* @return A pointer to the new initial process entry
|
|
|
|
*/
|
2013-06-06 10:10:36 +04:00
|
|
|
process_t * spawn_init(void) {
|
2011-12-14 12:37:43 +04:00
|
|
|
/* We can only do this once. */
|
2011-12-09 01:25:48 +04:00
|
|
|
assert((!process_tree->root) && "Tried to regenerate init!");
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Allocate space for a new process */
|
2011-12-08 06:59:08 +04:00
|
|
|
process_t * init = malloc(sizeof(process_t));
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Set it as the root process */
|
2011-12-08 06:59:08 +04:00
|
|
|
tree_set_root(process_tree, (void *)init);
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Set its tree entry pointer so we can keep track
|
|
|
|
* of the process' entry in the process tree. */
|
2011-12-08 06:59:08 +04:00
|
|
|
init->tree_entry = process_tree->root;
|
2013-06-07 05:04:13 +04:00
|
|
|
init->id = 1; /* Init is PID 1 */
|
2012-02-16 08:56:16 +04:00
|
|
|
init->group = 0;
|
2012-12-01 06:26:47 +04:00
|
|
|
init->name = strdup("init"); /* Um, duh. */
|
2013-03-19 08:52:45 +04:00
|
|
|
init->cmdline = NULL;
|
2011-12-14 12:37:43 +04:00
|
|
|
init->user = 0; /* UID 0 */
|
2013-04-23 12:14:33 +04:00
|
|
|
init->mask = 022; /* umask */
|
2011-12-14 12:37:43 +04:00
|
|
|
init->group = 0; /* Task group 0 */
|
|
|
|
init->status = 0; /* Run status */
|
2012-02-17 00:31:40 +04:00
|
|
|
init->fds = malloc(sizeof(fd_table_t));
|
2012-03-15 07:03:55 +04:00
|
|
|
init->fds->refs = 1;
|
2014-04-06 02:43:14 +04:00
|
|
|
init->fds->length = 0; /* Initialize the file descriptors */
|
2012-02-17 00:31:40 +04:00
|
|
|
init->fds->capacity = 4;
|
|
|
|
init->fds->entries = malloc(sizeof(fs_node_t *) * init->fds->capacity);
|
2011-12-14 12:37:43 +04:00
|
|
|
|
|
|
|
/* Set the working directory */
|
2011-12-09 01:25:48 +04:00
|
|
|
init->wd_node = clone_fs(fs_root);
|
2012-12-01 06:26:47 +04:00
|
|
|
init->wd_name = strdup("/");
|
2011-12-08 06:59:08 +04:00
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Heap and stack pointers (and actuals) */
|
2011-12-09 01:25:48 +04:00
|
|
|
init->image.entry = 0;
|
|
|
|
init->image.heap = 0;
|
|
|
|
init->image.heap_actual = 0;
|
|
|
|
init->image.stack = initial_esp + 1;
|
|
|
|
init->image.user_stack = 0;
|
|
|
|
init->image.size = 0;
|
2013-04-22 04:33:16 +04:00
|
|
|
init->image.shm_heap = SHM_START; /* Yeah, a bit of a hack. */
|
2014-04-21 00:19:52 +04:00
|
|
|
init->image.lock = 0;
|
2011-12-09 01:25:48 +04:00
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Process is not finished */
|
2011-12-09 01:25:48 +04:00
|
|
|
init->finished = 0;
|
2012-02-09 06:09:28 +04:00
|
|
|
init->started = 1;
|
2013-11-28 07:11:58 +04:00
|
|
|
init->running = 1;
|
2012-02-01 05:27:38 +04:00
|
|
|
init->wait_queue = list_create();
|
2012-02-09 02:00:51 +04:00
|
|
|
init->shm_mappings = list_create();
|
2012-02-08 12:40:44 +04:00
|
|
|
init->signal_queue = list_create();
|
2012-02-21 09:31:00 +04:00
|
|
|
init->signal_kstack = NULL; /* None yet initialized */
|
|
|
|
|
|
|
|
init->sched_node.prev = NULL;
|
|
|
|
init->sched_node.next = NULL;
|
|
|
|
init->sched_node.value = init;
|
2011-12-09 01:25:48 +04:00
|
|
|
|
2012-12-01 06:26:47 +04:00
|
|
|
init->sleep_node.prev = NULL;
|
|
|
|
init->sleep_node.next = NULL;
|
|
|
|
init->sleep_node.value = init;
|
|
|
|
|
2014-04-13 05:25:56 +04:00
|
|
|
init->timed_sleep_node = NULL;
|
|
|
|
|
2013-11-28 07:11:58 +04:00
|
|
|
init->is_tasklet = 0;
|
|
|
|
|
2013-06-07 05:04:13 +04:00
|
|
|
set_process_environment(init, current_directory);
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* What the hey, let's also set the description on this one */
|
2012-12-01 06:26:47 +04:00
|
|
|
init->description = strdup("[init]");
|
2013-06-07 05:04:13 +04:00
|
|
|
list_insert(process_list, (void *)init);
|
|
|
|
|
2011-12-08 06:59:08 +04:00
|
|
|
return init;
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/*
|
|
|
|
* Get the next available PID
|
|
|
|
*
|
|
|
|
* @return A usable PID for a new process.
|
|
|
|
*/
|
2013-06-06 10:10:36 +04:00
|
|
|
pid_t get_next_pid(void) {
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Terribly naïve, I know, but it works for now */
|
2013-06-07 05:04:13 +04:00
|
|
|
static pid_t next = 2;
|
2011-12-08 06:59:08 +04:00
|
|
|
return (next++);
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/*
|
|
|
|
* Disown a process from its parent.
|
|
|
|
*/
|
2011-12-08 06:59:08 +04:00
|
|
|
void process_disown(process_t * proc) {
|
|
|
|
assert(process_tree->root && "No init, has the process tree been initialized?");
|
2011-12-09 01:25:48 +04:00
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Find the process in the tree */
|
2011-12-08 06:59:08 +04:00
|
|
|
tree_node_t * entry = proc->tree_entry;
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Break it of from its current parent */
|
2011-12-16 07:08:48 +04:00
|
|
|
spin_lock(&tree_lock);
|
2011-12-08 06:59:08 +04:00
|
|
|
tree_break_off(process_tree, entry);
|
2011-12-14 12:37:43 +04:00
|
|
|
/* And insert it back elsewhere */
|
2011-12-08 06:59:08 +04:00
|
|
|
tree_node_insert_child_node(process_tree, process_tree->root, entry);
|
2011-12-16 07:08:48 +04:00
|
|
|
spin_unlock(&tree_lock);
|
2011-12-08 06:59:08 +04:00
|
|
|
}
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/*
|
|
|
|
* Spawn a new process.
|
|
|
|
*
|
|
|
|
* @param parent The parent process to spawn the new one off of.
|
|
|
|
* @return A pointer to the new process.
|
|
|
|
*/
|
2011-12-09 01:25:48 +04:00
|
|
|
process_t * spawn_process(volatile process_t * parent) {
|
|
|
|
assert(process_tree->root && "Attempted to spawn a process without init.");
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Allocate a new process */
|
2012-09-04 09:35:11 +04:00
|
|
|
debug_print(INFO," process_t {");
|
2011-12-08 06:59:08 +04:00
|
|
|
process_t * proc = malloc(sizeof(process_t));
|
2012-09-04 09:35:11 +04:00
|
|
|
debug_print(INFO," }");
|
2011-12-14 12:37:43 +04:00
|
|
|
proc->id = get_next_pid(); /* Set its PID */
|
2012-02-16 08:56:16 +04:00
|
|
|
proc->group = proc->id; /* Set the GID */
|
2012-12-01 06:26:47 +04:00
|
|
|
proc->name = strdup(default_name); /* Use the default name */
|
2011-12-14 12:37:43 +04:00
|
|
|
proc->description = NULL; /* No description */
|
2013-03-19 08:52:45 +04:00
|
|
|
proc->cmdline = parent->cmdline;
|
2011-12-09 01:25:48 +04:00
|
|
|
|
2012-01-27 09:11:43 +04:00
|
|
|
/* Copy permissions */
|
|
|
|
proc->user = parent->user;
|
2013-04-23 12:14:33 +04:00
|
|
|
proc->mask = parent->mask;
|
|
|
|
|
|
|
|
/* XXX this is wrong? */
|
2012-01-27 09:11:43 +04:00
|
|
|
proc->group = parent->group;
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Zero out the ESP/EBP/EIP */
|
2011-12-09 01:25:48 +04:00
|
|
|
proc->thread.esp = 0;
|
|
|
|
proc->thread.ebp = 0;
|
|
|
|
proc->thread.eip = 0;
|
2013-04-16 11:03:23 +04:00
|
|
|
proc->thread.fpu_enabled = 0;
|
2011-12-09 01:25:48 +04:00
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Set the process image information from the parent */
|
2011-12-09 01:25:48 +04:00
|
|
|
proc->image.entry = parent->image.entry;
|
|
|
|
proc->image.heap = parent->image.heap;
|
|
|
|
proc->image.heap_actual = parent->image.heap_actual;
|
|
|
|
proc->image.size = parent->image.size;
|
2012-09-04 09:35:11 +04:00
|
|
|
debug_print(INFO," stack {");
|
2012-03-15 07:03:55 +04:00
|
|
|
proc->image.stack = (uintptr_t)malloc(KERNEL_STACK_SIZE) + KERNEL_STACK_SIZE;
|
2012-09-04 09:35:11 +04:00
|
|
|
debug_print(INFO," }");
|
2011-12-14 02:06:45 +04:00
|
|
|
proc->image.user_stack = parent->image.user_stack;
|
2013-04-22 04:33:16 +04:00
|
|
|
proc->image.shm_heap = SHM_START; /* Yeah, a bit of a hack. */
|
2014-04-21 00:19:52 +04:00
|
|
|
proc->image.lock = 0;
|
2011-12-09 01:25:48 +04:00
|
|
|
|
2011-12-16 03:21:28 +04:00
|
|
|
assert(proc->image.stack && "Failed to allocate kernel stack for new process.");
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Clone the file descriptors from the original process */
|
2012-02-17 00:31:40 +04:00
|
|
|
proc->fds = malloc(sizeof(fd_table_t));
|
2012-03-15 07:03:55 +04:00
|
|
|
proc->fds->refs = 1;
|
2012-02-17 00:31:40 +04:00
|
|
|
proc->fds->length = parent->fds->length;
|
|
|
|
proc->fds->capacity = parent->fds->capacity;
|
2012-09-04 09:35:11 +04:00
|
|
|
debug_print(INFO," fds / files {");
|
2012-02-17 00:31:40 +04:00
|
|
|
proc->fds->entries = malloc(sizeof(fs_node_t *) * proc->fds->capacity);
|
|
|
|
assert(proc->fds->entries && "Failed to allocate file descriptor table for new process.");
|
2012-09-04 09:35:11 +04:00
|
|
|
debug_print(INFO," ---");
|
2012-02-17 00:31:40 +04:00
|
|
|
for (uint32_t i = 0; i < parent->fds->length; ++i) {
|
|
|
|
proc->fds->entries[i] = clone_fs(parent->fds->entries[i]);
|
2011-12-09 01:25:48 +04:00
|
|
|
}
|
2012-09-04 09:35:11 +04:00
|
|
|
debug_print(INFO," }");
|
2011-12-09 01:25:48 +04:00
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* As well as the working directory */
|
2011-12-09 01:25:48 +04:00
|
|
|
proc->wd_node = clone_fs(parent->wd_node);
|
2012-12-01 06:26:47 +04:00
|
|
|
proc->wd_name = strdup(parent->wd_name);
|
2011-12-09 01:25:48 +04:00
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Zero out the process status */
|
2011-12-09 01:25:48 +04:00
|
|
|
proc->status = 0;
|
|
|
|
proc->finished = 0;
|
2012-02-09 06:09:28 +04:00
|
|
|
proc->started = 0;
|
2013-11-28 07:11:58 +04:00
|
|
|
proc->running = 0;
|
2012-02-08 12:40:44 +04:00
|
|
|
memset(proc->signals.functions, 0x00, sizeof(uintptr_t) * NUMSIGNALS);
|
2012-02-01 05:27:38 +04:00
|
|
|
proc->wait_queue = list_create();
|
2012-02-06 00:16:59 +04:00
|
|
|
proc->shm_mappings = list_create();
|
2012-02-08 12:40:44 +04:00
|
|
|
proc->signal_queue = list_create();
|
2012-02-21 09:31:00 +04:00
|
|
|
proc->signal_kstack = NULL; /* None yet initialized */
|
|
|
|
|
|
|
|
proc->sched_node.prev = NULL;
|
|
|
|
proc->sched_node.next = NULL;
|
|
|
|
proc->sched_node.value = proc;
|
2011-12-09 01:25:48 +04:00
|
|
|
|
2012-12-01 06:26:47 +04:00
|
|
|
proc->sleep_node.prev = NULL;
|
|
|
|
proc->sleep_node.next = NULL;
|
|
|
|
proc->sleep_node.value = proc;
|
|
|
|
|
2014-04-13 05:25:56 +04:00
|
|
|
proc->timed_sleep_node = NULL;
|
|
|
|
|
2013-11-28 07:11:58 +04:00
|
|
|
proc->is_tasklet = 0;
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Insert the process into the process tree as a child
|
|
|
|
* of the parent process. */
|
2011-12-08 06:59:08 +04:00
|
|
|
tree_node_t * entry = tree_node_create(proc);
|
2011-12-16 03:21:28 +04:00
|
|
|
assert(entry && "Failed to allocate a process tree node for new process.");
|
2011-12-08 06:59:08 +04:00
|
|
|
proc->tree_entry = entry;
|
2011-12-16 07:08:48 +04:00
|
|
|
spin_lock(&tree_lock);
|
2011-12-08 06:59:08 +04:00
|
|
|
tree_node_insert_child_node(process_tree, parent->tree_entry, entry);
|
2013-03-19 08:52:45 +04:00
|
|
|
list_insert(process_list, (void *)proc);
|
2011-12-16 07:08:48 +04:00
|
|
|
spin_unlock(&tree_lock);
|
2011-12-09 01:25:48 +04:00
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/* Return the new process */
|
2011-12-08 06:59:08 +04:00
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t process_compare(void * proc_v, void * pid_v) {
|
|
|
|
pid_t pid = (*(pid_t *)pid_v);
|
|
|
|
process_t * proc = (process_t *)proc_v;
|
|
|
|
|
|
|
|
return (uint8_t)(proc->id == pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
process_t * process_from_pid(pid_t pid) {
|
2014-04-21 00:19:52 +04:00
|
|
|
if (pid < 0) return NULL;
|
2011-12-09 01:25:48 +04:00
|
|
|
|
2011-12-16 07:08:48 +04:00
|
|
|
spin_lock(&tree_lock);
|
2011-12-08 06:59:08 +04:00
|
|
|
tree_node_t * entry = tree_find(process_tree,&pid,process_compare);
|
2011-12-16 07:08:48 +04:00
|
|
|
spin_unlock(&tree_lock);
|
2011-12-08 06:59:08 +04:00
|
|
|
if (entry) {
|
|
|
|
return (process_t *)entry->value;
|
2013-03-21 08:24:55 +04:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-27 12:37:33 +04:00
|
|
|
process_t * process_get_parent(process_t * process) {
|
|
|
|
process_t * result = NULL;
|
2013-03-21 08:24:55 +04:00
|
|
|
spin_lock(&tree_lock);
|
2014-04-27 12:37:33 +04:00
|
|
|
|
|
|
|
tree_node_t * entry = process->tree_entry;
|
|
|
|
|
|
|
|
if (entry->parent) {
|
|
|
|
result = entry->parent->value;
|
|
|
|
}
|
|
|
|
|
2013-03-21 08:24:55 +04:00
|
|
|
spin_unlock(&tree_lock);
|
|
|
|
return result;
|
2011-12-08 06:59:08 +04:00
|
|
|
}
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/*
|
|
|
|
* Wait for children.
|
|
|
|
*
|
|
|
|
* @param process Process doing the waiting.
|
|
|
|
* @param pid PID to wait for
|
|
|
|
* @param status [out] Where to put the status conditions of the waited-for process
|
|
|
|
* @param options Options (unused)
|
|
|
|
* @return A pointer to the process that broke the wait
|
|
|
|
*/
|
2011-12-08 06:59:08 +04:00
|
|
|
process_t * process_wait(process_t * process, pid_t pid, int * status, int options) {
|
|
|
|
/* `options` is ignored */
|
|
|
|
if (pid == -1) {
|
|
|
|
/* wait for any child process */
|
|
|
|
} else if (pid < 0) {
|
|
|
|
/* wait for any porcess whose ->group == processes[abs(pid)]->group */
|
|
|
|
} else if (pid == 0) {
|
|
|
|
/* wait for any process whose ->group == process->group */
|
|
|
|
} else {
|
|
|
|
/* wait for processes[pid] */
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-12-09 01:25:48 +04:00
|
|
|
|
2011-12-12 12:17:14 +04:00
|
|
|
/*
|
|
|
|
* Wake up a sleeping process
|
2011-12-14 12:37:43 +04:00
|
|
|
*
|
|
|
|
* @param process Process to wake up
|
|
|
|
* @param caller Who woke it up
|
|
|
|
* @return Don't know yet, but I think it should return something.
|
2011-12-12 12:17:14 +04:00
|
|
|
*/
|
|
|
|
int process_wake(process_t * process, process_t * caller) {
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/*
|
|
|
|
* Set the directory for a process.
|
|
|
|
*
|
|
|
|
* @param proc Process to set the directory for.
|
|
|
|
* @param directory Directory to set.
|
|
|
|
*/
|
2011-12-09 01:25:48 +04:00
|
|
|
void set_process_environment(process_t * proc, page_directory_t * directory) {
|
|
|
|
assert(proc);
|
|
|
|
assert(directory);
|
|
|
|
|
|
|
|
proc->thread.page_directory = directory;
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/*
|
|
|
|
* Are there any processes available in the queue?
|
|
|
|
* (Queue not empty)
|
|
|
|
*
|
|
|
|
* @return 1 if there are processes available, 0 otherwise
|
|
|
|
*/
|
2013-06-06 10:10:36 +04:00
|
|
|
uint8_t process_available(void) {
|
2011-12-09 01:25:48 +04:00
|
|
|
return (process_queue->head != NULL);
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:37:43 +04:00
|
|
|
/*
|
|
|
|
* Append a file descriptor to a process.
|
|
|
|
*
|
|
|
|
* @param proc Process to append to
|
|
|
|
* @param node The VFS node
|
|
|
|
* @return The actual fd, for use in userspace
|
|
|
|
*/
|
2011-12-09 01:25:48 +04:00
|
|
|
uint32_t process_append_fd(process_t * proc, fs_node_t * node) {
|
2012-02-17 00:31:40 +04:00
|
|
|
if (proc->fds->length == proc->fds->capacity) {
|
|
|
|
proc->fds->capacity *= 2;
|
|
|
|
proc->fds->entries = realloc(proc->fds->entries, sizeof(fs_node_t *) * proc->fds->capacity);
|
2011-12-09 01:25:48 +04:00
|
|
|
}
|
2012-02-17 00:31:40 +04:00
|
|
|
proc->fds->entries[proc->fds->length] = node;
|
|
|
|
proc->fds->length++;
|
|
|
|
return proc->fds->length-1;
|
2011-12-09 01:25:48 +04:00
|
|
|
}
|
2012-01-25 05:06:07 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* dup2() -> Move the file pointed to by `s(ou)rc(e)` into
|
|
|
|
* the slot pointed to be `dest(ination)`.
|
|
|
|
*
|
|
|
|
* @param proc Process to do this for
|
|
|
|
* @param src Source file descriptor
|
|
|
|
* @param dest Destination file descriptor
|
|
|
|
* @return The destination file descriptor, -1 on failure
|
|
|
|
*/
|
|
|
|
uint32_t process_move_fd(process_t * proc, int src, int dest) {
|
2012-02-17 00:31:40 +04:00
|
|
|
if ((size_t)src > proc->fds->length || (size_t)dest > proc->fds->length) {
|
2012-01-25 05:06:07 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#if 0
|
2012-02-17 00:31:40 +04:00
|
|
|
if (proc->fds->entries[dest] != proc->fds->entries[src]) {
|
|
|
|
close_fs(proc->fds->entries[src]);
|
2012-01-25 05:06:07 +04:00
|
|
|
}
|
|
|
|
#endif
|
2012-02-17 00:31:40 +04:00
|
|
|
proc->fds->entries[dest] = proc->fds->entries[src];
|
2012-01-25 05:06:07 +04:00
|
|
|
return dest;
|
|
|
|
}
|
2012-02-01 05:27:38 +04:00
|
|
|
|
|
|
|
int wakeup_queue(list_t * queue) {
|
|
|
|
int awoken_processes = 0;
|
|
|
|
while (queue->length > 0) {
|
2014-04-13 05:15:28 +04:00
|
|
|
spin_lock(&wait_lock_tmp);
|
2012-02-01 05:27:38 +04:00
|
|
|
node_t * node = list_pop(queue);
|
2014-04-13 05:15:28 +04:00
|
|
|
spin_unlock(&wait_lock_tmp);
|
2012-02-08 12:40:44 +04:00
|
|
|
if (!((process_t *)node->value)->finished) {
|
|
|
|
make_process_ready(node->value);
|
|
|
|
}
|
2012-02-01 05:27:38 +04:00
|
|
|
awoken_processes++;
|
|
|
|
}
|
|
|
|
return awoken_processes;
|
|
|
|
}
|
|
|
|
|
2014-04-13 05:15:28 +04:00
|
|
|
|
2012-02-01 05:27:38 +04:00
|
|
|
int sleep_on(list_t * queue) {
|
2014-04-13 13:05:12 +04:00
|
|
|
if (current_process->sleep_node.owner) {
|
2012-12-03 10:16:59 +04:00
|
|
|
/* uh, we can't sleep right now, we're marked as ready */
|
|
|
|
switch_task(0);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-27 12:37:33 +04:00
|
|
|
current_process->sleep_interrupted = 0;
|
2014-04-13 05:15:28 +04:00
|
|
|
spin_lock(&wait_lock_tmp);
|
2012-12-01 06:26:47 +04:00
|
|
|
list_append(queue, (node_t *)¤t_process->sleep_node);
|
2014-04-13 05:15:28 +04:00
|
|
|
spin_unlock(&wait_lock_tmp);
|
2012-02-01 05:27:38 +04:00
|
|
|
switch_task(0);
|
2014-04-27 12:37:33 +04:00
|
|
|
return current_process->sleep_interrupted;
|
2012-02-01 05:27:38 +04:00
|
|
|
}
|
2012-02-08 12:40:44 +04:00
|
|
|
|
2012-12-01 06:26:47 +04:00
|
|
|
int process_is_ready(process_t * proc) {
|
2014-04-13 05:25:56 +04:00
|
|
|
return (proc->sched_node.owner != NULL);
|
2012-02-08 12:40:44 +04:00
|
|
|
}
|
|
|
|
|
2014-04-13 05:15:28 +04:00
|
|
|
|
2012-12-11 08:28:31 +04:00
|
|
|
void wakeup_sleepers(unsigned long seconds, unsigned long subseconds) {
|
2014-04-13 05:15:28 +04:00
|
|
|
spin_lock(&sleep_lock);
|
2012-12-11 08:28:31 +04:00
|
|
|
if (sleep_queue->length) {
|
|
|
|
sleeper_t * proc = ((sleeper_t *)sleep_queue->head->value);
|
|
|
|
while (proc && (proc->end_tick < seconds || (proc->end_tick == seconds && proc->end_subtick <= subseconds))) {
|
|
|
|
process_t * process = proc->process;
|
2014-04-13 05:15:28 +04:00
|
|
|
process->sleep_node.owner = NULL;
|
2014-04-13 05:25:56 +04:00
|
|
|
process->timed_sleep_node = NULL;
|
2012-12-11 08:28:31 +04:00
|
|
|
if (!process_is_ready(process)) {
|
|
|
|
make_process_ready(process);
|
|
|
|
}
|
|
|
|
free(proc);
|
|
|
|
free(list_dequeue(sleep_queue));
|
|
|
|
if (sleep_queue->length) {
|
|
|
|
proc = ((sleeper_t *)sleep_queue->head->value);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-13 05:15:28 +04:00
|
|
|
spin_unlock(&sleep_lock);
|
2012-12-11 08:28:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void sleep_until(process_t * process, unsigned long seconds, unsigned long subseconds) {
|
2014-04-13 13:05:12 +04:00
|
|
|
if (current_process->sleep_node.owner) {
|
2014-04-13 05:25:56 +04:00
|
|
|
/* Can't sleep, sleeping already */
|
2014-04-13 05:15:28 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-04-13 05:25:56 +04:00
|
|
|
process->sleep_node.owner = sleep_queue;
|
2014-04-13 05:15:28 +04:00
|
|
|
spin_lock(&sleep_lock);
|
2012-12-11 08:28:31 +04:00
|
|
|
node_t * before = NULL;
|
|
|
|
foreach(node, sleep_queue) {
|
|
|
|
sleeper_t * candidate = ((sleeper_t *)node->value);
|
|
|
|
if (candidate->end_tick > seconds || (candidate->end_tick == seconds && candidate->end_subtick > subseconds)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
before = node;
|
|
|
|
}
|
|
|
|
sleeper_t * proc = malloc(sizeof(sleeper_t));
|
|
|
|
proc->process = process;
|
|
|
|
proc->end_tick = seconds;
|
|
|
|
proc->end_subtick = subseconds;
|
2014-04-13 05:25:56 +04:00
|
|
|
process->timed_sleep_node = list_insert_after(sleep_queue, before, proc);
|
2014-04-13 05:15:28 +04:00
|
|
|
spin_unlock(&sleep_lock);
|
2012-12-11 08:28:31 +04:00
|
|
|
}
|
2014-04-27 12:37:33 +04:00
|
|
|
|
|
|
|
void reap_process(process_t * proc) {
|
|
|
|
debug_print(INFO, "Reaping process %d; mem before = %d", proc->id, memory_use());
|
|
|
|
list_free(proc->wait_queue);
|
|
|
|
free(proc->wait_queue);
|
|
|
|
list_free(proc->signal_queue);
|
|
|
|
free(proc->signal_queue);
|
|
|
|
free(proc->wd_name);
|
|
|
|
debug_print(INFO, "Releasing shared memory for %d", proc->id);
|
|
|
|
shm_release_all(proc);
|
|
|
|
free(proc->shm_mappings);
|
|
|
|
debug_print(INFO, "Freeing more mems %d", proc->id);
|
|
|
|
free(proc->name);
|
|
|
|
if (proc->signal_kstack) {
|
|
|
|
free(proc->signal_kstack);
|
|
|
|
}
|
|
|
|
debug_print(INFO, "Dec'ing fds for %d", proc->id);
|
|
|
|
proc->fds->refs--;
|
|
|
|
if (proc->fds->refs == 0) {
|
|
|
|
debug_print(INFO, "Reached 0, all dependencies are closed for %d's file descriptors and page directories", proc->id);
|
|
|
|
release_directory(proc->thread.page_directory);
|
|
|
|
debug_print(INFO, "Going to clear out the file descriptors %d", proc->id);
|
|
|
|
for (uint32_t i = 0; i < proc->fds->length; ++i) {
|
|
|
|
if (proc->fds->entries[i]) {
|
|
|
|
//close_fs(proc->fds->entries[i]);
|
|
|
|
//free(proc->fds->entries[i]);
|
|
|
|
}
|
|
|
|
//close_fs(proc->fds->entries[i]);
|
|
|
|
}
|
|
|
|
debug_print(INFO, "... and their storage %d", proc->id);
|
|
|
|
free(proc->fds->entries);
|
|
|
|
free(proc->fds);
|
|
|
|
debug_print(INFO, "... and the kernel stack (hope this ain't us) %d", proc->id);
|
|
|
|
free((void *)(proc->image.stack - KERNEL_STACK_SIZE));
|
|
|
|
}
|
|
|
|
debug_print(INFO, "Reaped process %d; mem after = %d", proc->id, memory_use());
|
|
|
|
|
|
|
|
delete_process(proc);
|
|
|
|
debug_print_process_tree();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wait_candidate(process_t * parent, int pid, int options, process_t * proc) {
|
|
|
|
(void)options; /* there is only one option that affects candidacy, and we don't support it yet */
|
|
|
|
|
|
|
|
if (!proc) return 0;
|
|
|
|
|
|
|
|
if (pid < -1) {
|
|
|
|
if (proc->group == -pid || proc->id == -pid) return 1;
|
|
|
|
} else if (pid == 0) {
|
|
|
|
/* Matches our group ID */
|
|
|
|
if (proc->group == parent->id) return 1;
|
|
|
|
} else if (pid > 0) {
|
|
|
|
/* Specific pid */
|
|
|
|
if (proc->id == pid) return 1;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int waitpid(int pid, int * status, int options) {
|
|
|
|
process_t * proc = (process_t *)current_process;
|
|
|
|
if (proc->group) {
|
|
|
|
proc = process_from_pid(proc->group);
|
|
|
|
}
|
|
|
|
|
2014-05-18 21:27:28 +04:00
|
|
|
debug_print(INFO, "waitpid(%s%d, ..., %d) (from pid=%d.%d)", (pid >= 0) ? "" : "-", (pid >= 0) ? pid : -pid, options, current_process->id, current_process->group);
|
2014-04-27 12:37:33 +04:00
|
|
|
|
|
|
|
do {
|
|
|
|
process_t * candidate = NULL;
|
|
|
|
int has_children = 0;
|
|
|
|
|
|
|
|
/* First, find out if there is anyone to reap */
|
|
|
|
foreach(node, proc->tree_entry->children) {
|
|
|
|
if (!node->value) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
process_t * child = ((tree_node_t *)node->value)->value;
|
|
|
|
|
|
|
|
if (wait_candidate(proc, pid, options, child)) {
|
|
|
|
has_children = 1;
|
|
|
|
if (child->finished) {
|
|
|
|
candidate = child;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_children) {
|
|
|
|
/* No valid children matching this description */
|
2014-05-18 21:27:28 +04:00
|
|
|
debug_print(INFO, "No children matching description.");
|
2014-04-27 12:37:33 +04:00
|
|
|
return -ECHILD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (candidate) {
|
2014-05-18 21:27:28 +04:00
|
|
|
debug_print(INFO, "Candidate found (%x:%d), bailing early.", candidate, candidate->id);
|
2014-04-27 12:37:33 +04:00
|
|
|
if (status) {
|
|
|
|
*status = candidate->status;
|
|
|
|
}
|
|
|
|
int pid = candidate->id;
|
|
|
|
reap_process(candidate);
|
|
|
|
return pid;
|
|
|
|
} else {
|
|
|
|
if (options & 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-18 21:27:28 +04:00
|
|
|
debug_print(INFO, "Sleeping until queue is done.");
|
2014-04-27 12:37:33 +04:00
|
|
|
/* Wait */
|
|
|
|
if (sleep_on(proc->wait_queue) != 0) {
|
2014-05-18 21:27:28 +04:00
|
|
|
debug_print(INFO, "wait() was interrupted");
|
2014-04-27 12:37:33 +04:00
|
|
|
return -EINTR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|