2020-07-30 23:38:15 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-07-24 16:00:51 +02:00
|
|
|
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
|
2020-07-30 23:38:15 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 23:38:15 +02:00
|
|
|
*/
|
|
|
|
|
2022-07-24 16:00:51 +02:00
|
|
|
#include <Kernel/API/Syscall.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
#include <Kernel/Process.h>
|
2022-01-29 13:08:37 +01:00
|
|
|
#include <Kernel/Scheduler.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<FlatPtr> Process::sys$yield()
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-07-18 11:37:07 -07:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2021-08-10 21:20:45 +02:00
|
|
|
Thread::current()->yield_without_releasing_big_lock();
|
2020-07-30 23:38:15 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-24 16:00:51 +02:00
|
|
|
ErrorOr<NonnullRefPtr<Thread>> Process::get_thread_from_pid_or_tid(pid_t pid_or_tid, Syscall::SchedulerParametersMode mode)
|
|
|
|
{
|
|
|
|
VERIFY(g_scheduler_lock.is_locked_by_current_processor());
|
|
|
|
Thread* peer;
|
|
|
|
switch (mode) {
|
|
|
|
case Syscall::SchedulerParametersMode::Thread: {
|
|
|
|
peer = Thread::current();
|
|
|
|
if (pid_or_tid != 0)
|
|
|
|
peer = Thread::from_tid(pid_or_tid);
|
|
|
|
|
2022-07-24 18:01:57 +02:00
|
|
|
// Only superuser can access other processes' threads.
|
|
|
|
if (!credentials()->is_superuser() && peer && &peer->process() != this)
|
|
|
|
return EPERM;
|
|
|
|
|
2022-07-24 16:00:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Syscall::SchedulerParametersMode::Process: {
|
|
|
|
auto* searched_process = this;
|
|
|
|
if (pid_or_tid != 0)
|
2022-11-02 22:26:02 +02:00
|
|
|
searched_process = Process::from_pid_in_same_jail(pid_or_tid);
|
2022-07-24 16:00:51 +02:00
|
|
|
|
|
|
|
if (searched_process == nullptr)
|
|
|
|
return ESRCH;
|
|
|
|
auto pid = searched_process->pid().value();
|
|
|
|
// Main thread has tid == pid
|
|
|
|
this->thread_list().for_each([&](auto& thread) {
|
|
|
|
if (thread.tid().value() == pid)
|
|
|
|
peer = &thread;
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
if (!peer)
|
|
|
|
return ESRCH;
|
|
|
|
return NonnullRefPtr<Thread> { *peer };
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<FlatPtr> Process::sys$scheduler_set_parameters(Userspace<Syscall::SC_scheduler_parameters_params const*> user_param)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2022-08-17 21:03:04 +01:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_promise(Pledge::proc));
|
2022-07-24 16:00:51 +02:00
|
|
|
auto parameters = TRY(copy_typed_from_user(user_param));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2022-07-24 16:00:51 +02:00
|
|
|
if (parameters.parameters.sched_priority < THREAD_PRIORITY_MIN || parameters.parameters.sched_priority > THREAD_PRIORITY_MAX)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2020-09-27 08:53:35 -06:00
|
|
|
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(g_scheduler_lock);
|
2022-07-24 16:00:51 +02:00
|
|
|
auto peer = TRY(get_thread_from_pid_or_tid(parameters.pid_or_tid, parameters.mode));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2022-08-20 18:21:01 -04:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
auto peer_credentials = peer->process().credentials();
|
|
|
|
if (!credentials->is_superuser() && credentials->euid() != peer_credentials->uid() && credentials->uid() != peer_credentials->uid())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EPERM;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2022-07-24 16:00:51 +02:00
|
|
|
peer->set_priority((u32)parameters.parameters.sched_priority);
|
2022-07-24 18:05:44 +02:00
|
|
|
// POSIX says that process scheduling parameters have precedence over thread scheduling parameters.
|
|
|
|
// We don't track them separately, so overwrite the thread scheduling settings manually for now.
|
|
|
|
if (parameters.mode == Syscall::SchedulerParametersMode::Process) {
|
|
|
|
peer->process().for_each_thread([&](auto& thread) {
|
|
|
|
thread.set_priority((u32)parameters.parameters.sched_priority);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-24 16:00:51 +02:00
|
|
|
ErrorOr<FlatPtr> Process::sys$scheduler_get_parameters(Userspace<Syscall::SC_scheduler_parameters_params*> user_param)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2022-08-17 21:03:04 +01:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_promise(Pledge::proc));
|
2022-07-24 16:00:51 +02:00
|
|
|
|
|
|
|
Syscall::SC_scheduler_parameters_params parameters;
|
|
|
|
TRY(copy_from_user(¶meters, user_param));
|
|
|
|
|
2020-09-27 08:53:35 -06:00
|
|
|
int priority;
|
|
|
|
{
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(g_scheduler_lock);
|
2022-07-24 16:00:51 +02:00
|
|
|
auto peer = TRY(get_thread_from_pid_or_tid(parameters.pid_or_tid, parameters.mode));
|
2020-09-27 08:53:35 -06:00
|
|
|
|
2022-08-20 18:21:01 -04:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
auto peer_credentials = peer->process().credentials();
|
|
|
|
if (!credentials->is_superuser() && credentials->euid() != peer_credentials->uid() && credentials->uid() != peer_credentials->uid())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EPERM;
|
2020-09-27 08:53:35 -06:00
|
|
|
|
|
|
|
priority = (int)peer->priority();
|
2020-08-09 01:08:24 +02:00
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2022-07-24 16:00:51 +02:00
|
|
|
parameters.parameters.sched_priority = priority;
|
2021-09-05 17:38:37 +02:00
|
|
|
|
2022-07-24 16:00:51 +02:00
|
|
|
TRY(copy_to_user(user_param, ¶meters));
|
2021-11-08 00:51:39 +01:00
|
|
|
return 0;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|