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
|
|
|
*/
|
|
|
|
|
|
2021-08-22 22:01:04 -06:00
|
|
|
#include <AK/Variant.h>
|
2021-01-25 16:07:10 +01:00
|
|
|
#include <Kernel/Debug.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
ErrorOr<siginfo_t> Process::do_waitid(Variant<Empty, NonnullLockRefPtr<Process>, NonnullLockRefPtr<ProcessGroup>> waitee, int options)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<siginfo_t> result = siginfo_t {};
|
2021-08-22 22:01:04 -06:00
|
|
|
if (Thread::current()->block<Thread::WaitBlocker>({}, options, move(waitee), result).was_interrupted())
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINTR;
|
2021-11-08 00:51:39 +01:00
|
|
|
VERIFY(!result.is_error() || (options & WNOHANG));
|
2020-11-29 16:05:27 -07:00
|
|
|
return result;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$waitid(Userspace<Syscall::SC_waitid_params const*> user_params)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2022-08-17 21:03:04 +01:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_promise(Pledge::proc));
|
2021-09-05 17:51:37 +02:00
|
|
|
auto params = TRY(copy_typed_from_user(user_params));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
Variant<Empty, NonnullLockRefPtr<Process>, NonnullLockRefPtr<ProcessGroup>> waitee;
|
2021-02-12 18:23:28 +01:00
|
|
|
switch (params.idtype) {
|
|
|
|
|
case P_ALL:
|
|
|
|
|
break;
|
2021-08-22 22:01:04 -06:00
|
|
|
case P_PID: {
|
|
|
|
|
auto waitee_process = Process::from_pid(params.id);
|
2021-09-15 15:42:45 +03:00
|
|
|
if (!waitee_process)
|
|
|
|
|
return ECHILD;
|
|
|
|
|
bool waitee_is_child = waitee_process->ppid() == Process::current().pid();
|
|
|
|
|
bool waitee_is_our_tracee = waitee_process->has_tracee_thread(Process::current().pid());
|
|
|
|
|
if (!waitee_is_child && !waitee_is_our_tracee)
|
2021-08-22 22:01:04 -06:00
|
|
|
return ECHILD;
|
|
|
|
|
waitee = waitee_process.release_nonnull();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case P_PGID: {
|
|
|
|
|
auto waitee_group = ProcessGroup::from_pgid(params.id);
|
|
|
|
|
if (!waitee_group) {
|
|
|
|
|
return ECHILD;
|
|
|
|
|
}
|
|
|
|
|
waitee = waitee_group.release_nonnull();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-02-12 18:23:28 +01:00
|
|
|
default:
|
|
|
|
|
return EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(PROCESS_DEBUG, "sys$waitid({}, {}, {}, {})", params.idtype, params.id, params.infop, params.options);
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-09-05 18:42:32 +02:00
|
|
|
auto siginfo = TRY(do_waitid(move(waitee), params.options));
|
2021-11-08 00:51:39 +01:00
|
|
|
TRY(copy_to_user(params.infop, &siginfo));
|
|
|
|
|
return 0;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|