2020-07-31 00:38:15 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-31 00:38:15 +03:00
|
|
|
*/
|
|
|
|
|
2021-08-23 07:01:04 +03:00
|
|
|
#include <AK/Variant.h>
|
2021-01-25 18:07:10 +03:00
|
|
|
#include <Kernel/Debug.h>
|
2023-02-24 20:45:37 +03:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2023-04-02 22:57:12 +03:00
|
|
|
ErrorOr<siginfo_t> Process::do_waitid(Variant<Empty, NonnullRefPtr<Process>, NonnullRefPtr<ProcessGroup>> waitee, int options)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<siginfo_t> result = siginfo_t {};
|
2021-08-23 07:01:04 +03:00
|
|
|
if (Thread::current()->block<Thread::WaitBlocker>({}, options, move(waitee), result).was_interrupted())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINTR;
|
2021-11-08 02:51:39 +03:00
|
|
|
VERIFY(!result.is_error() || (options & WNOHANG));
|
2020-11-30 02:05:27 +03:00
|
|
|
return result;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$waitid(Userspace<Syscall::SC_waitid_params const*> user_params)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-17 23:03:04 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::proc));
|
2021-09-05 18:51:37 +03:00
|
|
|
auto params = TRY(copy_typed_from_user(user_params));
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2023-04-02 22:57:12 +03:00
|
|
|
Variant<Empty, NonnullRefPtr<Process>, NonnullRefPtr<ProcessGroup>> waitee;
|
2021-02-12 20:23:28 +03:00
|
|
|
switch (params.idtype) {
|
|
|
|
case P_ALL:
|
|
|
|
break;
|
2021-08-23 07:01:04 +03:00
|
|
|
case P_PID: {
|
2022-11-02 23:26:02 +03:00
|
|
|
auto waitee_process = Process::from_pid_in_same_jail(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-23 07:01:04 +03: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 20:23:28 +03:00
|
|
|
default:
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(PROCESS_DEBUG, "sys$waitid({}, {}, {}, {})", params.idtype, params.id, params.infop, params.options);
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-09-05 19:42:32 +03:00
|
|
|
auto siginfo = TRY(do_waitid(move(waitee), params.options));
|
2021-11-08 02:51:39 +03:00
|
|
|
TRY(copy_to_user(params.infop, &siginfo));
|
|
|
|
return 0;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|