89 lines · c
1//===-- String to integer conversion utils ----------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC_SYS_WAIT_WAIT4IMPL_H10#define LLVM_LIBC_SRC_SYS_WAIT_WAIT4IMPL_H11 12#include "src/__support/OSUtil/syscall.h" // For internal syscall function.13#include "src/__support/common.h"14#include "src/__support/error_or.h"15#include "src/__support/libc_errno.h"16#include "src/__support/macros/config.h"17 18#include <signal.h>19#include <sys/syscall.h> // For syscall numbers.20#include <sys/wait.h>21 22namespace LIBC_NAMESPACE_DECL {23namespace internal {24 25// The implementation of wait here is very minimal. We will add more26// functionality and standard compliance in future.27 28LIBC_INLINE ErrorOr<pid_t> wait4impl(pid_t pid, int *wait_status, int options,29 struct rusage *usage) {30#if SYS_wait431 pid = LIBC_NAMESPACE::syscall_impl<pid_t>(SYS_wait4, pid, wait_status,32 options, usage);33#elif defined(SYS_waitid)34 int idtype = P_PID;35 if (pid == -1) {36 idtype = P_ALL;37 } else if (pid < -1) {38 idtype = P_PGID;39 pid *= -1;40 } else if (pid == 0) {41 idtype = P_PGID;42 }43 44 options |= WEXITED;45 46 siginfo_t info;47 pid = LIBC_NAMESPACE::syscall_impl<pid_t>(SYS_waitid, idtype, pid, &info,48 options, usage);49 if (pid >= 0)50 pid = info.si_pid;51 52 if (wait_status) {53 switch (info.si_code) {54 case CLD_EXITED:55 *wait_status = W_EXITCODE(info.si_status, 0);56 break;57 case CLD_DUMPED:58 *wait_status = info.si_status | WCOREFLAG;59 break;60 case CLD_KILLED:61 *wait_status = info.si_status;62 break;63 case CLD_TRAPPED:64 case CLD_STOPPED:65 *wait_status = W_STOPCODE(info.si_status);66 break;67 case CLD_CONTINUED:68 // Set wait_status to a value that the caller can check via WIFCONTINUED.69 // glibc has a non-POSIX macro definition __W_CONTINUED for this value.70 *wait_status = 0xffff;71 break;72 default:73 *wait_status = 0;74 break;75 }76 }77#else78#error "wait4 and waitid syscalls not available."79#endif80 if (pid < 0)81 return Error(-pid);82 return pid;83}84 85} // namespace internal86} // namespace LIBC_NAMESPACE_DECL87 88#endif // LLVM_LIBC_SRC_SYS_WAIT_WAIT4IMPL_H89