syscalls: fix waitpid's behaviour

This commit is contained in:
Matheus
2022-09-20 16:58:59 -03:00
parent c637995d52
commit 5f9aba07b2
+32 -5
View File
@@ -37,13 +37,20 @@ syscallret syscall_waitpid(pid_t pid, int *status, int options){
proc_t* proc = arch_getcls()->thread->proc; proc_t* proc = arch_getcls()->thread->proc;
event_wait(&proc->childevent, true); bool loop = true;
proc_t* child;
proc_t* sibling;
while(1){
child = proc->child;
sibling = NULL;
// now find the child // now find the child
spinlock_acquire(&proc->lock); spinlock_acquire(&proc->lock);
proc_t* child = proc->child;
proc_t* sibling = NULL;
while(child){ while(child){
if(pid > 0 && child->pid != pid) if(pid > 0 && child->pid != pid)
@@ -59,6 +66,19 @@ syscallret syscall_waitpid(pid_t pid, int *status, int options){
} }
spinlock_release(&proc->lock);
if(loop){
event_wait(&proc->childevent, true);
loop = false;
continue;
}
else break;
}
if(!child){ if(!child){
spinlock_release(&proc->lock); spinlock_release(&proc->lock);
retv.errno = ECHILD; retv.errno = ECHILD;
@@ -71,10 +91,17 @@ syscallret syscall_waitpid(pid_t pid, int *status, int options){
spinlock_release(&sibling->lock); spinlock_release(&sibling->lock);
} }
spinlock_release(&proc->lock);
*status = child->status; // XXX safer way of doing this *status = child->status; // XXX safer way of doing this
// the pointer to the last remaining thread is is proc->threads
thread_t* thread = child->threads;
// TODO free ctx
free(thread->regs);
free(thread->kernelstackbase);
free(thread);
free(child); free(child);
retv.ret = child->pid; retv.ret = child->pid;