Compare commits
2 Commits
355dd6c32b
...
b6fb5f3dfe
Author | SHA1 | Date | |
---|---|---|---|
b6fb5f3dfe | |||
41c7e3780d |
@ -3,6 +3,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static char** split_command_into_argv(const char* cmd)
|
||||
@ -94,7 +95,6 @@ int main()
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Don't have sched_yield() or waitpid() yet...
|
||||
sleep(1);
|
||||
if (waitpid(child, NULL, 0) < 0) perror("waitpid");
|
||||
}
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ set(SOURCES
|
||||
src/sys/id.cpp
|
||||
src/sys/mkdir.cpp
|
||||
src/sys/mknod.cpp
|
||||
src/sys/waitpid.cpp
|
||||
src/fs/VFS.cpp
|
||||
src/fs/tmpfs/FileSystem.cpp
|
||||
src/fs/devices/DeviceRegistry.cpp
|
||||
|
@ -1,7 +1,15 @@
|
||||
#include "sys/Syscall.h"
|
||||
#include "thread/Scheduler.h"
|
||||
|
||||
Result<u64> sys_exit(Registers*, SyscallArgs)
|
||||
Result<u64> sys_exit(Registers*, SyscallArgs args)
|
||||
{
|
||||
kernel_exit();
|
||||
u8 status = (u8)args[0];
|
||||
|
||||
Thread* current = Scheduler::current();
|
||||
|
||||
current->status = status;
|
||||
current->state = ThreadState::Exited;
|
||||
|
||||
kernel_yield();
|
||||
unreachable();
|
||||
}
|
||||
|
28
kernel/src/sys/waitpid.cpp
Normal file
28
kernel/src/sys/waitpid.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include "memory/MemoryManager.h"
|
||||
#include "sys/Syscall.h"
|
||||
#include "thread/Scheduler.h"
|
||||
#include <bits/waitpid.h>
|
||||
|
||||
Result<u64> sys_waitpid(Registers*, SyscallArgs args)
|
||||
{
|
||||
pid_t pid = (pid_t)args[0];
|
||||
int* status_ptr = (int*)args[1];
|
||||
int options = (int)args[2];
|
||||
|
||||
Thread* thread = TRY(Result<Thread*>::from_option(Scheduler::find_by_pid(pid), ESRCH));
|
||||
|
||||
while (thread->state != ThreadState::Exited)
|
||||
{
|
||||
if (options & WNOHANG) return err(EAGAIN);
|
||||
kernel_sleep(10);
|
||||
}
|
||||
|
||||
int status = (int)thread->status;
|
||||
|
||||
thread->state = ThreadState::Dying;
|
||||
|
||||
if (status_ptr)
|
||||
if (!MemoryManager::copy_to_user_typed(status_ptr, &status)) return err(EFAULT);
|
||||
|
||||
return (u64)pid;
|
||||
}
|
@ -253,6 +253,17 @@ namespace Scheduler
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Option<Thread*> find_by_pid(pid_t pid)
|
||||
{
|
||||
Option<Thread*> result;
|
||||
|
||||
g_threads.for_each([&](Thread* thread) {
|
||||
if (thread->id == (u64)pid && thread->state != ThreadState::Dying) result = thread;
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
void kernel_sleep(u64 ms)
|
||||
|
@ -26,6 +26,8 @@ namespace Scheduler
|
||||
void invoke(Registers* regs);
|
||||
|
||||
LinkedList<Thread> check_for_dying_threads();
|
||||
|
||||
Option<Thread*> find_by_pid(pid_t pid);
|
||||
}
|
||||
|
||||
extern "C" void kernel_yield();
|
||||
|
@ -19,6 +19,7 @@ enum class ThreadState
|
||||
Idle,
|
||||
Runnable,
|
||||
Sleeping,
|
||||
Exited,
|
||||
Dying
|
||||
};
|
||||
|
||||
@ -64,6 +65,8 @@ struct Thread : public LinkedListNode<Thread>
|
||||
|
||||
bool is_kernel { true };
|
||||
|
||||
u8 status { 0 };
|
||||
|
||||
PageDirectory* directory;
|
||||
|
||||
bool is_idle()
|
||||
|
@ -15,6 +15,7 @@ set(SOURCES
|
||||
src/init.cpp
|
||||
src/sys/stat.cpp
|
||||
src/sys/mman.cpp
|
||||
src/sys/wait.cpp
|
||||
)
|
||||
|
||||
if(${LUNA_ARCH} STREQUAL "x86_64")
|
||||
|
8
libc/include/bits/waitpid.h
Normal file
8
libc/include/bits/waitpid.h
Normal file
@ -0,0 +1,8 @@
|
||||
/* bits/waitpid.h: Constants for waitpid(). */
|
||||
|
||||
#ifndef _BITS_WAITPID_H
|
||||
#define _BITS_WAITPID_H
|
||||
|
||||
#define WNOHANG 1
|
||||
|
||||
#endif
|
@ -95,10 +95,10 @@ extern "C"
|
||||
void srand(int);
|
||||
|
||||
/* Exit the program normally, performing any registered cleanup actions. */
|
||||
__noreturn void exit(int);
|
||||
__noreturn void exit(int status);
|
||||
|
||||
/* Exit the program abnormally, without performing any registered cleanup actions. */
|
||||
__noreturn void _Exit(int);
|
||||
__noreturn void _Exit(int status);
|
||||
|
||||
int system(const char*);
|
||||
|
||||
|
21
libc/include/sys/wait.h
Normal file
21
libc/include/sys/wait.h
Normal file
@ -0,0 +1,21 @@
|
||||
/* sys/wait.h: Functions for waiting. */
|
||||
|
||||
#ifndef _SYS_WAIT_H
|
||||
#define _SYS_WAIT_H
|
||||
|
||||
#include <bits/waitpid.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/* Wait for a child process to exit. */
|
||||
pid_t waitpid(pid_t pid, int* status, int options);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -94,13 +94,13 @@ extern "C"
|
||||
|
||||
__noreturn void abort()
|
||||
{
|
||||
syscall(SYS_exit);
|
||||
syscall(SYS_exit, 255);
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
__noreturn void _Exit(int)
|
||||
__noreturn void _Exit(int status)
|
||||
{
|
||||
syscall(SYS_exit);
|
||||
syscall(SYS_exit, status);
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
|
13
libc/src/sys/wait.cpp
Normal file
13
libc/src/sys/wait.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include <bits/errno-return.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
pid_t waitpid(pid_t pid, int* status, int options)
|
||||
{
|
||||
long rc = syscall(SYS_waitpid, pid, status, options);
|
||||
__errno_return(rc, pid_t);
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
#define enumerate_syscalls(_e) \
|
||||
_e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(open) _e(close) _e(read) _e(getpid) _e(write) \
|
||||
_e(lseek) _e(mkdir) _e(exec) _e(mknod) _e(fork)
|
||||
_e(lseek) _e(mkdir) _e(exec) _e(mknod) _e(fork) _e(waitpid)
|
||||
|
||||
enum Syscalls
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user