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