2020-07-30 23:38:15 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<FlatPtr> Process::sys$sched_setparam(int pid, Userspace<const struct sched_param*> 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));
|
2021-12-17 09:12:20 +01:00
|
|
|
auto param = TRY(copy_typed_from_user(user_param));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-12-17 09:12:20 +01:00
|
|
|
if (param.sched_priority < THREAD_PRIORITY_MIN || param.sched_priority > THREAD_PRIORITY_MAX)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2020-09-27 08:53:35 -06:00
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
auto* peer = Thread::current();
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(g_scheduler_lock);
|
2020-08-09 01:08:24 +02:00
|
|
|
if (pid != 0)
|
|
|
|
|
peer = Thread::from_tid(pid);
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
|
if (!peer)
|
2021-03-01 13:49:16 +01:00
|
|
|
return ESRCH;
|
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
|
|
|
|
2021-12-17 09:12:20 +01:00
|
|
|
peer->set_priority((u32)param.sched_priority);
|
2020-07-30 23:38:15 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<FlatPtr> Process::sys$sched_getparam(pid_t pid, Userspace<struct sched_param*> 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));
|
2020-09-27 08:53:35 -06:00
|
|
|
int priority;
|
|
|
|
|
{
|
|
|
|
|
auto* peer = Thread::current();
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(g_scheduler_lock);
|
2020-09-27 08:53:35 -06:00
|
|
|
if (pid != 0) {
|
|
|
|
|
// FIXME: PID/TID BUG
|
|
|
|
|
// The entire process is supposed to be affected.
|
|
|
|
|
peer = Thread::from_tid(pid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!peer)
|
2021-03-01 13:49:16 +01:00
|
|
|
return ESRCH;
|
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
|
|
|
|
2020-08-09 01:08:24 +02:00
|
|
|
struct sched_param param {
|
2020-09-27 08:53:35 -06:00
|
|
|
priority
|
2020-08-09 01:08:24 +02:00
|
|
|
};
|
2021-09-05 17:38:37 +02:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
TRY(copy_to_user(user_param, ¶m));
|
|
|
|
|
return 0;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|