Skip to content

Commit 995105d

Browse files
author
Siva Chandra Reddy
committed
[libc] Add the POSIX waitpid function and the BSD wait4 function.
Reviewed By: lntue, michaelrj Differential Revision: https://reviews.llvm.org/D135225
1 parent a3a9b07 commit 995105d

File tree

23 files changed

+390
-3
lines changed

23 files changed

+390
-3
lines changed

libc/config/linux/api.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def SysStatAPI : PublicAPI<"sys/stat.h"> {
272272
}
273273

274274
def SysWaitAPI : PublicAPI<"sys/wait.h"> {
275-
let Types = ["pid_t"];
275+
let Types = ["pid_t", "struct rusage"];
276276
}
277277

278278
def SysSendfileAPI : PublicAPI<"sys/sendfile.h"> {

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ set(TARGET_LIBC_ENTRYPOINTS
133133

134134
# sys/wait.h entrypoints
135135
libc.src.sys.wait.wait
136+
libc.src.sys.wait.wait4
137+
libc.src.sys.wait.waitpid
136138

137139
# unistd.h entrypoints
138140
libc.src.unistd.access

libc/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ add_gen_header(
284284
.llvm_libc_common_h
285285
.llvm-libc-macros.sys_wait_macros
286286
.llvm-libc-types.pid_t
287+
.llvm-libc-types.struct_rusage
287288
)
288289

289290
if(NOT LLVM_LIBC_FULL_BUILD)

libc/include/llvm-libc-macros/linux/sys-wait-macros.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#ifndef __LLVM_LIBC_MACROS_LINUX_SYS_WAIT_MACROS_H
1010
#define __LLVM_LIBC_MACROS_LINUX_SYS_WAIT_MACROS_H
1111

12+
// Wait flags
13+
#define WNOHANG 1 // Do not block
14+
#define WUNTRACED 2 // Report is a child has stopped even if untraced
15+
#define WCONTINUED 8 // Report if a stopped child has been resumed by SIGCONT
16+
1217
// Wait status info macros
1318
#define WTERMSIG(status) (((status)&0x7F))
1419
#define WIFEXITED(status) (WTERMSIG(status) == 0)

libc/include/llvm-libc-types/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@ add_header(pthread_t HDR pthread_t.h DEPENDS .__thread_type)
4343
add_header(pthread_mutexattr_t HDR pthread_mutexattr_t.h)
4444
add_header(pthread_once_t HDR pthread_once_t.h DEPENDS .__futex_word)
4545
add_header(rlim_t HDR rlim_t.h)
46+
add_header(time_t HDR time_t.h)
47+
add_header(suseconds_t HDR suseconds_t.h)
48+
add_header(struct_timeval HDR struct_timeval.h DEPENDS .suseconds_t .time_t)
4649
add_header(struct_rlimit HDR struct_rlimit.h DEPENDS .rlim_t)
50+
add_header(struct_rusage HDR struct_rusage.h DEPENDS .struct_timeval)
4751
add_header(ssize_t HDR ssize_t.h)
4852
add_header(struct_dirent HDR struct_dirent.h DEPENDS .ino_t .off_t)
4953
add_header(union_sigval HDR union_sigval.h)
5054
add_header(siginfo_t HDR siginfo_t.h DEPENDS .union_sigval .pid_t .uid_t)
5155
add_header(sigset_t HDR sigset_t.h DEPENDS libc.include.llvm-libc-macros.signal_macros)
5256
add_header(struct_sigaction HDR struct_sigaction.h DEPENDS .sigset_t .siginfo_t)
53-
add_header(time_t HDR time_t.h)
5457
add_header(struct_timespec HDR struct_timespec.h DEPENDS .time_t)
5558
add_header(
5659
struct_stat
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===-- Definition of type struct rusage ----------------------------------===//
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-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __LLVM_LIBC_TYPES_STRUCT_RUSAGE_H__
10+
#define __LLVM_LIBC_TYPES_STRUCT_RUSAGE_H__
11+
12+
#include <llvm-libc-types/struct_timeval.h>
13+
14+
struct rusage {
15+
struct timeval ru_utime;
16+
struct timeval ru_stime;
17+
#ifdef __linux__
18+
// Following fields are linux extensions as expected by the
19+
// linux syscalls.
20+
long ru_maxrss; // Maximum resident set size
21+
long ru_ixrss; // Integral shared memory size
22+
long ru_idrss; // Integral unshared data size
23+
long ru_isrss; // Integral unshared stack size
24+
long ru_minflt; // Page reclaims
25+
long ru_majflt; // Page faults
26+
long ru_nswap; // Swaps
27+
long ru_inblock; // Block input operations
28+
long ru_oublock; // Block output operations
29+
long ru_msgsnd; // Messages sent
30+
long ru_msgrcv; // Messages received
31+
long ru_nsignals; // Signals received
32+
long ru_nvcsw; // Voluntary context switches
33+
long ru_nivcsw; // Involuntary context switches
34+
#endif
35+
};
36+
37+
#endif // __LLVM_LIBC_TYPES_STRUCT_RUSAGE_H__
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Definition of struct timeval -------------------------------------===//
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-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __LLVM_LIBC_TYPES_TIMEVAL_H__
10+
#define __LLVM_LIBC_TYPES_TIMEVAL_H__
11+
12+
#include <llvm-libc-types/suseconds_t.h>
13+
#include <llvm-libc-types/time_t.h>
14+
15+
struct timeval {
16+
time_t tv_sec; // Seconds
17+
suseconds_t tv_usec; // Micro seconds
18+
};
19+
20+
#endif // __LLVM_LIBC_TYPES_TIMEVAL_H__
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Definition of suseconds_t type ------------------------------------===//
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-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __LLVM_LIBC_TYPES_SUSECONDS_T_H__
10+
#define __LLVM_LIBC_TYPES_SUSECONDS_T_H__
11+
12+
typedef __INT32_TYPE__ suseconds_t;
13+
14+
#endif // __LLVM_LIBC_TYPES_SUSECONDS_T_H__

libc/spec/bsd_ext.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,22 @@ def BsdExtensions : StandardSpec<"BSDExtensions"> {
1818
]
1919
>;
2020

21+
HeaderSpec SysWait = HeaderSpec<
22+
"sys/wait.h",
23+
[], // Macros
24+
[StructRUsage], // Types
25+
[], // Enumerations
26+
[
27+
FunctionSpec<
28+
"wait4",
29+
RetValSpec<PidT>,
30+
[ArgSpec<PidT>, ArgSpec<IntPtr>, ArgSpec<IntType>, ArgSpec<StructRUsagePtr>]
31+
>
32+
]
33+
>;
34+
2135
let Headers = [
2236
String,
37+
SysWait,
2338
];
2439
}

libc/spec/posix.td

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,13 +971,18 @@ def POSIX : StandardSpec<"POSIX"> {
971971
HeaderSpec SysWait = HeaderSpec<
972972
"sys/wait.h",
973973
[], // Macros
974-
[PidT],
974+
[PidT, StructRUsage],
975975
[], // Enumerations
976976
[
977977
FunctionSpec<
978978
"wait",
979979
RetValSpec<PidT>,
980980
[ArgSpec<IntPtr>]
981+
>,
982+
FunctionSpec<
983+
"waitpid",
984+
RetValSpec<PidT>,
985+
[ArgSpec<PidT>, ArgSpec<IntPtr>, ArgSpec<IntType>]
981986
>
982987
]
983988
>;

libc/spec/spec.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ def FILERestrictedPtr : RestrictedPtrType<FILE>;
115115
def PThreadTType : NamedType<"pthread_t">;
116116

117117
def PidT : NamedType<"pid_t">;
118+
def StructRUsage : NamedType<"struct rusage">;
119+
def StructRUsagePtr : PtrType<StructRUsage>;
118120

119121
//added because __assert_fail needs it.
120122
def UnsignedType : NamedType<"unsigned">;

libc/src/sys/wait/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,17 @@ add_entrypoint_object(
88
DEPENDS
99
.${LIBC_TARGET_OS}.wait
1010
)
11+
12+
add_entrypoint_object(
13+
wait4
14+
ALIAS
15+
DEPENDS
16+
.${LIBC_TARGET_OS}.wait4
17+
)
18+
19+
add_entrypoint_object(
20+
waitpid
21+
ALIAS
22+
DEPENDS
23+
.${LIBC_TARGET_OS}.waitpid
24+
)

libc/src/sys/wait/linux/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,31 @@ add_entrypoint_object(
1111
libc.src.__support.OSUtil.osutil
1212
libc.src.errno.errno
1313
)
14+
15+
add_entrypoint_object(
16+
wait4
17+
SRCS
18+
wait4.cpp
19+
HDRS
20+
../wait4.h
21+
DEPENDS
22+
libc.include.errno
23+
libc.include.sys_wait
24+
libc.include.sys_syscall
25+
libc.src.__support.OSUtil.osutil
26+
libc.src.errno.errno
27+
)
28+
29+
add_entrypoint_object(
30+
waitpid
31+
SRCS
32+
waitpid.cpp
33+
HDRS
34+
../waitpid.h
35+
DEPENDS
36+
libc.include.errno
37+
libc.include.sys_wait
38+
libc.include.sys_syscall
39+
libc.src.__support.OSUtil.osutil
40+
libc.src.errno.errno
41+
)

libc/src/sys/wait/linux/wait4.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===-- Linux implementation of wait4 -------------------------------------===//
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-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/sys/wait/wait4.h"
10+
11+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12+
#include "src/__support/common.h"
13+
14+
#include <errno.h>
15+
#include <sys/syscall.h> // For syscall numbers.
16+
#include <sys/wait.h>
17+
18+
namespace __llvm_libc {
19+
20+
LLVM_LIBC_FUNCTION(pid_t, wait4,
21+
(pid_t pid, int *wait_status, int options,
22+
struct rusage *usage)) {
23+
pid = __llvm_libc::syscall_impl(SYS_wait4, pid, wait_status, options, usage);
24+
if (pid < 0) {
25+
errno = -pid;
26+
return -1;
27+
}
28+
return pid;
29+
}
30+
31+
} // namespace __llvm_libc

libc/src/sys/wait/linux/waitpid.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- Linux implementation of waitpid -----------------------------------===//
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-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/sys/wait/waitpid.h"
10+
11+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12+
#include "src/__support/common.h"
13+
14+
#include <errno.h>
15+
#include <sys/syscall.h> // For syscall numbers.
16+
#include <sys/wait.h>
17+
18+
namespace __llvm_libc {
19+
20+
LLVM_LIBC_FUNCTION(pid_t, waitpid, (pid_t pid, int *wait_status, int options)) {
21+
pid = __llvm_libc::syscall_impl(SYS_wait4, pid, wait_status, options, 0);
22+
if (pid < 0) {
23+
errno = -pid;
24+
return -1;
25+
}
26+
return pid;
27+
}
28+
29+
} // namespace __llvm_libc

libc/src/sys/wait/wait4.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for wait4 -------------------------*- 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-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SYS_WAIT_WAIT4_H
10+
#define LLVM_LIBC_SRC_SYS_WAIT_WAIT4_H
11+
12+
#include <sys/wait.h>
13+
14+
namespace __llvm_libc {
15+
16+
pid_t wait4(pid_t pid, int *waitstatus, int options, struct rusage *usage);
17+
18+
} // namespace __llvm_libc
19+
20+
#endif // LLVM_LIBC_SRC_SYS_WAIT_WAIT4_H

libc/src/sys/wait/waitpid.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for waitpid -----------------------*- 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-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SYS_WAIT_WAITPID_H
10+
#define LLVM_LIBC_SRC_SYS_WAIT_WAITPID_H
11+
12+
#include <sys/wait.h>
13+
14+
namespace __llvm_libc {
15+
16+
pid_t waitpid(pid_t pid, int *waitstatus, int options);
17+
18+
} // namespace __llvm_libc
19+
20+
#endif // LLVM_LIBC_SRC_SYS_WAIT_WAITPID_H

libc/test/integration/src/unistd/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ add_integration_test(
1616
libc.include.unistd
1717
libc.src.signal.raise
1818
libc.src.sys.wait.wait
19+
libc.src.sys.wait.wait4
20+
libc.src.sys.wait.waitpid
1921
libc.src.unistd.fork
2022
)

0 commit comments

Comments
 (0)