[kernel] Add command line support, fix bugs
authorcoderain <coderain@sdf.org>
Wed, 15 Nov 2017 05:21:43 +0000 (06:21 +0100)
committercoderain <coderain@sdf.org>
Wed, 15 Nov 2017 05:21:43 +0000 (06:21 +0100)
kernel/src/process.c

index 215ac97cfcfd770d3e3dce4ade1d92b5b023d0f8..9aa0b66868da1cd4431e1b6fed753b5992e1a37a 100644 (file)
@@ -25,6 +25,8 @@
 #include <exec/aout.h>
 #include <cpu.h>
 
+dword_t load_elf(handle_t file, process_params_t *parameters, thread_state_t *initial_state);
+
 typedef dword_t (*loader_proc)(handle_t, process_params_t*, thread_state_t*);
 
 static dword_t pid_alloc_bitmap[MAX_PROCESSES / 32];
@@ -32,6 +34,7 @@ static lock_t pid_bitmap_lock = 0;
 
 static loader_proc loaders[] =
 {
+    load_elf,
     load_aout,
     NULL
 };
@@ -68,7 +71,7 @@ void destroy_process(process_t *proc)
 
     acquire_resource_exclusive(&proc->handle_table_res);
 
-    for (i = 0; i < proc->handle_table_size / sizeof(object_t*); i++)
+    for (i = 0; i < proc->handle_table_size / sizeof(proc->handle_table[i]); i++)
     {
         if (proc->handle_table[i].obj)
         {
@@ -181,6 +184,10 @@ void init_user_stack(uintptr_t *stack_pointer, process_params_t *parameters)
 
     uintptr_t stack_top = *stack_pointer;
 
+    *stack_pointer -= (strlen(parameters->command_line) + 1 + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1);
+    strcpy((char*)*stack_pointer, parameters->command_line);
+    parameters->command_line = (char*)*stack_pointer;
+
     *stack_pointer -= (sizeof(process_params_t) + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1);
     *(process_params_t*)*stack_pointer = *parameters;
     uintptr_t parameters_addr = *stack_pointer;
@@ -222,11 +229,24 @@ sysret_t syscall_create_process(const char *path, dword_t flags, process_params_
 
         if (parameters)
         {
-            if (!check_usermode(parameters, sizeof(process_params_t))) return ERR_BADPTR;
+            if (!check_usermode(parameters, sizeof(process_params_t)))
+            {
+                free(safe_path);
+                return ERR_BADPTR;
+            }
 
             EH_TRY safe_params = *parameters;
             EH_CATCH EH_ESCAPE(return ERR_BADPTR);
             EH_DONE;
+
+            char *cmdline = copy_user_string(safe_params.command_line);
+            if (cmdline == NULL)
+            {
+                free(safe_path);
+                return ERR_BADPTR;
+            }
+
+            safe_params.command_line = cmdline;
         }
     }
     else
@@ -407,7 +427,12 @@ cleanup:
         free(proc);
     }
 
-    if (get_previous_mode() == USER_MODE && path != NULL) free(safe_path);
+    if (get_previous_mode() == USER_MODE)
+    {
+        if (path != NULL) free(safe_path);
+        free(safe_params.command_line);
+    }
+
     return ret;
 }