Compare commits
3 Commits
755242719c
...
656667812a
Author | SHA1 | Date | |
---|---|---|---|
656667812a | |||
a29f7f8df2 | |||
48d4a5910a |
@ -163,24 +163,30 @@ int main()
|
||||
}
|
||||
if (child == 0)
|
||||
{
|
||||
sleep(2);
|
||||
sleep(1);
|
||||
pid_t child = fork();
|
||||
printf("I am the child (PID %ld), my parent is PID %ld!!\n", getpid(), getppid());
|
||||
execv("/bin/sym", NULL);
|
||||
if (child) execv("/bin/sym", NULL);
|
||||
else
|
||||
execv("/bin/init", NULL);
|
||||
perror("execv");
|
||||
return 1;
|
||||
}
|
||||
else { printf("Success!! Got PID %ld\n", child); }
|
||||
|
||||
int status;
|
||||
pid_t result;
|
||||
while ((result = waitpid(child, &status, 0)) == 0) // Child has not yet exited
|
||||
for (;;)
|
||||
{
|
||||
msleep(100);
|
||||
pid_t result;
|
||||
while ((result = wait(&status)) == 0) // No child has exited yet
|
||||
{
|
||||
msleep(100);
|
||||
}
|
||||
if (result < 0)
|
||||
{
|
||||
perror("wait");
|
||||
return 1;
|
||||
}
|
||||
if (WIFEXITED(status)) { printf("Child process %ld exited with code %d\n", result, WEXITSTATUS(status)); }
|
||||
}
|
||||
if (result < 0)
|
||||
{
|
||||
perror("waitpid");
|
||||
return 1;
|
||||
}
|
||||
if (WIFEXITED(status)) { printf("Child process %ld exited with code %d\n", result, WEXITSTATUS(status)); }
|
||||
}
|
||||
|
@ -25,8 +25,6 @@
|
||||
namespace Syscall
|
||||
{
|
||||
void entry(Context* context);
|
||||
|
||||
char* strdup_from_user(const char* user_string);
|
||||
}
|
||||
|
||||
void sys_exit(Context* context, int status);
|
||||
|
29
kernel/include/sys/UserMemory.h
Normal file
29
kernel/include/sys/UserMemory.h
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include "memory/MemoryManager.h"
|
||||
#include "memory/VMM.h"
|
||||
#include "misc/utils.h"
|
||||
|
||||
char* strdup_from_user(const char* user_string);
|
||||
|
||||
template <typename T, unsigned long S = sizeof(T), typename V> T* user_address_to_typed_pointer(V address)
|
||||
{
|
||||
uint64_t phys = VMM::get_physical((uint64_t)address);
|
||||
if (phys == (uint64_t)-1) return nullptr;
|
||||
return (T*)MemoryManager::get_unaligned_mappings((void*)phys, Utilities::get_blocks_from_size(PAGE_SIZE, S),
|
||||
MAP_READ_WRITE);
|
||||
}
|
||||
|
||||
template <typename T, unsigned long S = sizeof(T)> void free_user_typed_pointer(T* ptr)
|
||||
{
|
||||
MemoryManager::release_unaligned_mappings(ptr, Utilities::get_blocks_from_size(PAGE_SIZE, S));
|
||||
}
|
||||
|
||||
template <typename T> T* obtain_user_ref(T* user_ptr)
|
||||
{
|
||||
return user_address_to_typed_pointer<T>(user_ptr);
|
||||
}
|
||||
|
||||
template <typename T> void release_user_ref(T* ptr)
|
||||
{
|
||||
return free_user_typed_pointer(ptr);
|
||||
}
|
@ -34,10 +34,3 @@ void Syscall::entry(Context* context)
|
||||
}
|
||||
VMM::exit_syscall_context();
|
||||
}
|
||||
|
||||
char* Syscall::strdup_from_user(const char* user_string) // FIXME: This function is a little hacky.
|
||||
{
|
||||
uint64_t phys = VMM::get_physical((uint64_t)user_string);
|
||||
if (phys == (uint64_t)-1) { return nullptr; }
|
||||
return strdup((const char*)phys);
|
||||
}
|
10
kernel/src/sys/UserMemory.cpp
Normal file
10
kernel/src/sys/UserMemory.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
#include "sys/UserMemory.h"
|
||||
#include "std/string.h"
|
||||
|
||||
char* strdup_from_user(
|
||||
const char* user_string) // FIXME: This function is a little hacky. Use the obtain_user_ref and similar functions.
|
||||
{
|
||||
uint64_t phys = VMM::get_physical((uint64_t)user_string);
|
||||
if (phys == (uint64_t)-1) { return nullptr; }
|
||||
return strdup((const char*)phys);
|
||||
}
|
@ -10,6 +10,7 @@
|
||||
#include "std/stdlib.h"
|
||||
#include "std/string.h"
|
||||
#include "sys/Syscall.h"
|
||||
#include "sys/UserMemory.h"
|
||||
#include "sys/elf/ELFLoader.h"
|
||||
#include "thread/Scheduler.h"
|
||||
|
||||
@ -57,7 +58,7 @@ void sys_fork(Context* context)
|
||||
|
||||
void sys_exec(Context* context, const char* pathname)
|
||||
{
|
||||
char* kpathname = Syscall::strdup_from_user(pathname);
|
||||
char* kpathname = strdup_from_user(pathname);
|
||||
if (!kpathname)
|
||||
{
|
||||
context->rax = -EFAULT;
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "errno.h"
|
||||
#include "memory/VMM.h"
|
||||
#include "sys/UserMemory.h"
|
||||
#include "thread/Scheduler.h"
|
||||
|
||||
void sys_exit(Context* context, int status)
|
||||
@ -21,23 +22,3 @@ void sys_sleep(Context* context, uint64_t ms)
|
||||
task->state = task->Sleeping;
|
||||
Scheduler::task_yield(context);
|
||||
}
|
||||
|
||||
void sys_waitpid(Context* context, long pid, int* wstatus,
|
||||
int) // FIXME: Use the value in options and block if WNOHANG has not been specified.
|
||||
{
|
||||
Task* child = Scheduler::find_by_pid(pid); // FIXME: Wait for any child process if PID is -1.
|
||||
if (!child)
|
||||
{
|
||||
context->rax = -ESRCH;
|
||||
return;
|
||||
}
|
||||
if (child->state != child->Dying) // FIXME: This should block if WNOHANG has not been specified.
|
||||
{
|
||||
context->rax = 0;
|
||||
return;
|
||||
}
|
||||
uint64_t phys = VMM::get_physical((uint64_t)wstatus);
|
||||
if (phys != (uint64_t)-1) { *(int*)phys = (int)(child->exit_status & 0xff); }
|
||||
child->state = child->Exited;
|
||||
context->rax = (long)child->id;
|
||||
}
|
@ -8,6 +8,7 @@
|
||||
#include "render/TextRenderer.h"
|
||||
#include "std/stdlib.h"
|
||||
#include "sys/Syscall.h"
|
||||
#include "sys/UserMemory.h"
|
||||
#include "thread/Scheduler.h"
|
||||
#include "thread/Task.h"
|
||||
|
||||
@ -145,7 +146,7 @@ void sys_open(Context* context, const char* filename, int flags)
|
||||
return;
|
||||
}
|
||||
|
||||
char* kernel_filename = Syscall::strdup_from_user(filename);
|
||||
char* kernel_filename = strdup_from_user(filename);
|
||||
if (!kernel_filename)
|
||||
{
|
||||
context->rax = -EFAULT;
|
||||
@ -225,7 +226,7 @@ void sys_close(Context* context, int fd)
|
||||
|
||||
void sys_mkdir(Context* context, const char* filename)
|
||||
{
|
||||
char* kfilename = Syscall::strdup_from_user(filename);
|
||||
char* kfilename = strdup_from_user(filename);
|
||||
if (!kfilename)
|
||||
{
|
||||
context->rax = -EFAULT;
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include "panic/Panic.h"
|
||||
#include "std/stdlib.h"
|
||||
#include "std/string.h"
|
||||
#include "sys/UserMemory.h"
|
||||
#include "sys/elf/ELFLoader.h"
|
||||
#include "thread/PIT.h"
|
||||
#include "thread/Task.h"
|
||||
@ -44,6 +45,14 @@ template <typename Callback> void sched_for_each_task(Callback callback)
|
||||
} while (task != base_task);
|
||||
}
|
||||
|
||||
template <typename Callback> void sched_for_each_child(Task* task, Callback callback)
|
||||
{
|
||||
sched_for_each_task([&](Task* child) {
|
||||
if (child->ppid == task->id) { return callback(child); }
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
Task* Scheduler::find_by_pid(uint64_t pid)
|
||||
{
|
||||
Task* result = nullptr;
|
||||
@ -264,6 +273,13 @@ void Scheduler::task_exit(Context* context, int64_t status)
|
||||
else
|
||||
sched_current_task->state = sched_current_task->Dying;
|
||||
sched_current_task->exit_status = status;
|
||||
if (sched_current_task->id != 1)
|
||||
{
|
||||
sched_for_each_child(sched_current_task, [](Task* child) {
|
||||
if (child->state != child->Exited) child->ppid = 1;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
task_yield(context);
|
||||
}
|
||||
|
||||
@ -423,3 +439,50 @@ Task* Scheduler::current_task()
|
||||
{
|
||||
return sched_current_task;
|
||||
}
|
||||
|
||||
void sys_waitpid(Context* context, long pid, int* wstatus,
|
||||
int) // FIXME: Use the value in options and block if WNOHANG has not been specified.
|
||||
{
|
||||
Task* child = nullptr;
|
||||
if (pid == -1)
|
||||
{
|
||||
sched_for_each_child(sched_current_task, [&](Task* task) {
|
||||
if (task->state == task->Dying)
|
||||
{
|
||||
child = task;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
if (!child)
|
||||
{
|
||||
context->rax = 0; // No child has exited, let's return 0.
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
child = Scheduler::find_by_pid(pid);
|
||||
if (!child)
|
||||
{
|
||||
context->rax = -ESRCH;
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (child->state != child->Dying) // FIXME: This should block if WNOHANG has not been specified.
|
||||
{
|
||||
context->rax = 0;
|
||||
return;
|
||||
}
|
||||
if (wstatus)
|
||||
{
|
||||
int* kwstatus = obtain_user_ref(wstatus);
|
||||
if (kwstatus)
|
||||
{
|
||||
*kwstatus = (int)(child->exit_status & 0xff);
|
||||
release_user_ref(kwstatus);
|
||||
}
|
||||
}
|
||||
child->state = child->Exited;
|
||||
context->rax = (long)child->id;
|
||||
}
|
Loading…
Reference in New Issue
Block a user