1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 02:17:35 +00:00

Kernel: Make all syscall functions return KResultOr<T>

This makes it a lot easier to return errors since we no longer have to
worry about negating EFOO errors and can just return them flat.
This commit is contained in:
Andreas Kling 2021-03-01 13:49:16 +01:00
parent 9af1e1a3bf
commit ac71775de5
70 changed files with 747 additions and 742 deletions

View file

@ -34,31 +34,31 @@
namespace Kernel {
int Process::sys$create_thread(void* (*entry)(void*), Userspace<const Syscall::SC_create_thread_params*> user_params)
KResultOr<int> Process::sys$create_thread(void* (*entry)(void*), Userspace<const Syscall::SC_create_thread_params*> user_params)
{
REQUIRE_PROMISE(thread);
Syscall::SC_create_thread_params params;
if (!copy_from_user(&params, user_params))
return -EFAULT;
return EFAULT;
unsigned detach_state = params.m_detach_state;
int schedule_priority = params.m_schedule_priority;
unsigned stack_size = params.m_stack_size;
if (Checked<FlatPtr>::addition_would_overflow((FlatPtr)params.m_stack_location, stack_size))
return -EOVERFLOW;
return EOVERFLOW;
auto user_stack_address = (u8*)params.m_stack_location + stack_size;
if (!MM.validate_user_stack(*this, VirtualAddress(user_stack_address - 4)))
return -EFAULT;
return EFAULT;
// FIXME: return EAGAIN if Thread::all_threads().size() is greater than PTHREAD_THREADS_MAX
int requested_thread_priority = schedule_priority;
if (requested_thread_priority < THREAD_PRIORITY_MIN || requested_thread_priority > THREAD_PRIORITY_MAX)
return -EINVAL;
return EINVAL;
bool is_thread_joinable = (0 == detach_state);
@ -108,31 +108,31 @@ void Process::sys$exit_thread(Userspace<void*> exit_value)
VERIFY_NOT_REACHED();
}
int Process::sys$detach_thread(pid_t tid)
KResultOr<int> Process::sys$detach_thread(pid_t tid)
{
REQUIRE_PROMISE(thread);
auto thread = Thread::from_tid(tid);
if (!thread || thread->pid() != pid())
return -ESRCH;
return ESRCH;
if (!thread->is_joinable())
return -EINVAL;
return EINVAL;
thread->detach();
return 0;
}
int Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
KResultOr<int> Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
{
REQUIRE_PROMISE(thread);
auto thread = Thread::from_tid(tid);
if (!thread || thread->pid() != pid())
return -ESRCH;
return ESRCH;
auto current_thread = Thread::current();
if (thread == current_thread)
return -EDEADLK;
return EDEADLK;
void* joinee_exit_value = nullptr;
@ -151,50 +151,50 @@ int Process::sys$join_thread(pid_t tid, Userspace<void**> exit_value)
}
if (exit_value && !copy_to_user(exit_value, &joinee_exit_value))
return -EFAULT;
return EFAULT;
return 0;
}
int Process::sys$set_thread_name(pid_t tid, Userspace<const char*> user_name, size_t user_name_length)
KResultOr<int> Process::sys$set_thread_name(pid_t tid, Userspace<const char*> user_name, size_t user_name_length)
{
REQUIRE_PROMISE(stdio);
auto name = copy_string_from_user(user_name, user_name_length);
if (name.is_null())
return -EFAULT;
return EFAULT;
const size_t max_thread_name_size = 64;
if (name.length() > max_thread_name_size)
return -EINVAL;
return EINVAL;
auto thread = Thread::from_tid(tid);
if (!thread || thread->pid() != pid())
return -ESRCH;
return ESRCH;
thread->set_name(move(name));
return 0;
}
int Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size)
KResultOr<int> Process::sys$get_thread_name(pid_t tid, Userspace<char*> buffer, size_t buffer_size)
{
REQUIRE_PROMISE(thread);
if (buffer_size == 0)
return -EINVAL;
return EINVAL;
auto thread = Thread::from_tid(tid);
if (!thread || thread->pid() != pid())
return -ESRCH;
return ESRCH;
// We must make a temporary copy here to avoid a race with sys$set_thread_name
auto thread_name = thread->name();
if (thread_name.length() + 1 > (size_t)buffer_size)
return -ENAMETOOLONG;
return ENAMETOOLONG;
if (!copy_to_user(buffer, thread_name.characters(), thread_name.length() + 1))
return -EFAULT;
return EFAULT;
return 0;
}
int Process::sys$gettid()
KResultOr<int> Process::sys$gettid()
{
REQUIRE_PROMISE(stdio);
return Thread::current()->tid().value();