Skip to content

Commit

Permalink
Merge pull request #1002 from Dennisbonke/waitid
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennisbonke authored Feb 22, 2024
2 parents 2d0473f + 4c7ed4c commit c62984d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
10 changes: 7 additions & 3 deletions options/posix/generic/sys-wait-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
#include <mlibc/posix-sysdeps.hpp>
#include <mlibc/debug.hpp>

int waitid(idtype_t, id_t, siginfo_t *, int) {
__ensure(!"Not implemented");
__builtin_unreachable();
int waitid(idtype_t idtype, id_t id, siginfo_t *info, int options) {
auto sysdep = MLIBC_CHECK_OR_ENOSYS(mlibc::sys_waitid, -1);
if(int e = sysdep(idtype, id, info, options); e) {
errno = e;
return -1;
}
return 0;
}

pid_t waitpid(pid_t pid, int *status, int flags) {
Expand Down
2 changes: 2 additions & 0 deletions options/posix/include/mlibc/posix-sysdeps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ int sys_vm_unmap(void *pointer, size_t size);
[[gnu::weak]] int sys_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask);
[[gnu::weak]] int sys_setthreadaffinity(pid_t tid, size_t cpusetsize, const cpu_set_t *mask);

[[gnu::weak]] int sys_waitid(idtype_t idtype, id_t id, siginfo_t *info, int options);

} //namespace mlibc

#endif // MLIBC_POSIX_SYSDEPS
7 changes: 7 additions & 0 deletions sysdeps/linux/generic/sysdeps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,13 @@ int sys_semctl(int semid, int semnum, int cmd, void *semun, int *out) {
return 0;
}

int sys_waitid(idtype_t idtype, id_t id, siginfo_t *info, int options) {
auto ret = do_syscall(SYS_waitid, idtype, id, info, options, 0);
if(int e = sc_error(ret); e)
return e;
return sc_int_result<int>(ret);
}

#endif // __MLIBC_POSIX_OPTION

#if __MLIBC_LINUX_OPTION
Expand Down
36 changes: 36 additions & 0 deletions sysdeps/managarm/generic/fork-exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,42 @@ int sys_waitpid(pid_t pid, int *status, int flags, struct rusage *ru, pid_t *ret
return 0;
}

int sys_waitid(idtype_t idtype, id_t id, siginfo_t *info, int options) {
SignalGuard sguard;

managarm::posix::WaitIdRequest<MemoryAllocator> req(getSysdepsAllocator());

req.set_idtype(idtype);
req.set_id(id);
req.set_flags(options);

auto [offer, send_head, recv_resp] =
exchangeMsgsSync(
getPosixLane(),
helix_ng::offer(
helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()),
helix_ng::recvInline()
)
);

HEL_CHECK(offer.error());
HEL_CHECK(send_head.error());
HEL_CHECK(recv_resp.error());

managarm::posix::WaitIdResponse<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) {
return EINVAL;
}
__ensure(resp.error() == managarm::posix::Errors::SUCCESS);
info->si_pid = resp.pid();
info->si_uid = resp.uid();
info->si_signo = SIGCHLD;
info->si_status = resp.sig_status();
info->si_code = resp.sig_code();
return 0;
}

void sys_exit(int status) {
// This implementation is inherently signal-safe.
HEL_CHECK(helSyscall1(kHelCallSuper + posix::superExit, status));
Expand Down
1 change: 1 addition & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ all_test_cases = [
'posix/posix-timer',
'posix/strdupa',
'posix/mkstemp',
'posix/waitid',
'glibc/getopt',
'glibc/ffsl-ffsll',
'glibc/error_message_count',
Expand Down
20 changes: 20 additions & 0 deletions tests/posix/waitid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <assert.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

siginfo_t si;

int main() {
int pid = fork();

if(!pid)
exit(69);

int ret = waitid(P_ALL, 0, &si, WEXITED);
assert(ret == 0);
assert(si.si_pid == pid);
assert(si.si_signo == SIGCHLD);
assert(si.si_code == CLD_EXITED);
}

0 comments on commit c62984d

Please sign in to comment.