#include <types.h>
#include <lib.h>
#include <kern/errno.h>
#include <kern/wait.h>
#include <proc.h>
#include <copyinout.h>
#include <file.h>
#include <filedesc.h>
#include <thread.h>
#include <current.h>
#include <syscall.h>
int
___waitpid( int pid, int *retval, int options ) {
struct proc *p = NULL;
int err;
KASSERT( curthread != NULL );
KASSERT( curthread->td_proc != NULL );
if( options != 0 && options != WNOHANG )
return EINVAL;
err = proc_get( pid, &p );
if( err )
return err;
if( p->p_proc != curthread->td_proc ) {
PROC_UNLOCK( p );
return ECHILD;
}
if( !p->p_is_dead && (options == WNOHANG) ) {
PROC_UNLOCK( p );
*retval = 0;
return 0;
}
PROC_UNLOCK( p );
P( p->p_sem );
PROC_LOCK( p );
KASSERT( p->p_is_dead );
*retval = _MKWAIT_EXIT(p->p_retval);
PROC_UNLOCK( p );
proc_destroy( p );
return 0;
}
int
sys_waitpid( int pid, userptr_t uret, int options, int *retval ) {
int kstatus;
int err;
err = ___waitpid( pid, &kstatus, options );
if( err )
return err;
err = copyout( &kstatus, uret, sizeof( int ) );
if( err )
return err;
*retval = pid;
return 0;
}