Skip to content

Commit f5c5f9f

Browse files
authored
[libc] Implement getitimer and setitimer, add proxy headers for itimerval (#134773)
#133983
1 parent a2c57e1 commit f5c5f9f

File tree

17 files changed

+348
-0
lines changed

17 files changed

+348
-0
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ set(TARGET_LIBC_ENTRYPOINTS
363363
# sys/uio.h entrypoints
364364
libc.src.sys.uio.writev
365365
libc.src.sys.uio.readv
366+
367+
# sys/time.h entrypoints
368+
libc.src.sys.time.setitimer
369+
libc.src.sys.time.getitimer
366370
)
367371

368372
if(LLVM_LIBC_INCLUDE_SCUDO)

libc/config/linux/arm/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ set(TARGET_LIBC_ENTRYPOINTS
185185
# libc.src.sys.epoll.epoll_pwait
186186
# libc.src.sys.epoll.epoll_pwait2
187187

188+
# sys/time.h entrypoints
189+
libc.src.sys.time.setitimer
190+
libc.src.sys.time.getitimer
188191
)
189192

190193
if(LLVM_LIBC_FULL_BUILD)

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ set(TARGET_LIBC_ENTRYPOINTS
368368
# sys/uio.h entrypoints
369369
libc.src.sys.uio.writev
370370
libc.src.sys.uio.readv
371+
372+
# sys/time.h entrypoints
373+
libc.src.sys.time.setitimer
374+
libc.src.sys.time.getitimer
371375
)
372376

373377
if(LLVM_LIBC_INCLUDE_SCUDO)

libc/hdr/types/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ add_proxy_header_library(
190190
libc.include.sys_time
191191
)
192192

193+
add_proxy_header_library(
194+
struct_itimerval
195+
HDRS
196+
struct_itimerval.h
197+
FULL_BUILD_DEPENDS
198+
libc.include.llvm-libc-types.struct_itimerval
199+
libc.include.sys_time
200+
)
201+
193202
add_proxy_header_library(
194203
pid_t
195204
HDRS

libc/hdr/types/struct_itimerval.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Proxy for struct itimerval ----------------------------------------===//
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+
#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_ITIMERVAL_H
9+
#define LLVM_LIBC_HDR_TYPES_STRUCT_ITIMERVAL_H
10+
11+
#ifdef LIBC_FULL_BUILD
12+
13+
#include "include/llvm-libc-types/struct_itimerval.h"
14+
15+
#else
16+
17+
#include <sys/time.h>
18+
19+
#endif // LIBC_FULL_BUILD
20+
21+
#endif // LLVM_LIBC_HDR_TYPES_STRUCT_ITIMERVAL_H

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ add_header(struct_pollfd HDR struct_pollfd.h)
7777
add_header(struct_rlimit HDR struct_rlimit.h DEPENDS .rlim_t)
7878
add_header(struct_sched_param HDR struct_sched_param.h)
7979
add_header(struct_timeval HDR struct_timeval.h DEPENDS .suseconds_t .time_t)
80+
add_header(struct_itimerval HDR struct_itimerval.h DEPENDS .struct_timeval)
8081
add_header(struct_rusage HDR struct_rusage.h DEPENDS .struct_timeval)
8182
add_header(union_sigval HDR union_sigval.h)
8283
add_header(siginfo_t HDR siginfo_t.h DEPENDS .union_sigval .pid_t .uid_t .clock_t)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Definition of struct itimerval ------------------------------------===//
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_ITIMERVAL_H
10+
#define LLVM_LIBC_TYPES_STRUCT_ITIMERVAL_H
11+
12+
#include "struct_timeval.h"
13+
14+
struct itimerval {
15+
struct timeval it_interval; /* Interval for periodic timer */
16+
struct timeval it_value; /* Time until next expiration */
17+
};
18+
19+
#endif // LLVM_LIBC_TYPES_STRUCT_ITIMERVAL_H

libc/include/sys/time.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ standards: Linux
44
macros: []
55
types:
66
- type_name: struct_timeval
7+
- type_name: struct_itimerval
78
enums: []
89
objects: []
910
functions:
@@ -12,3 +13,20 @@ functions:
1213
arguments:
1314
- type: const char*
1415
- type: const struct timeval*
16+
17+
- name: setitimer
18+
standards:
19+
- POSIX
20+
return_type: int
21+
arguments:
22+
- type: int
23+
- type: const struct itimerval *__restrict
24+
- type: struct itimerval *__restrict
25+
26+
- name: getitimer
27+
standards:
28+
- POSIX
29+
return_type: int
30+
arguments:
31+
- type: int
32+
- type: struct itimerval *

libc/src/sys/time/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}.utimes
1010
)
11+
12+
add_entrypoint_object(
13+
setitimer
14+
ALIAS
15+
DEPENDS
16+
.${LIBC_TARGET_OS}.setitimer
17+
)
18+
19+
add_entrypoint_object(
20+
getitimer
21+
ALIAS
22+
DEPENDS
23+
.${LIBC_TARGET_OS}.getitimer
24+
)

libc/src/sys/time/getitimer.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation header for getitimer -------------------------------===//
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_TIME_GETITIMER_H
10+
#define LLVM_LIBC_SRC_SYS_TIME_GETITIMER_H
11+
12+
#include "hdr/types/struct_itimerval.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
int getitimer(int which, struct itimerval *curr_value);
17+
} // namespace LIBC_NAMESPACE_DECL
18+
19+
#endif // LLVM_LIBC_SRC_SYS_TIME_GETITIMER_H

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,31 @@ add_entrypoint_object(
1414
libc.src.__support.OSUtil.osutil
1515
libc.src.errno.errno
1616
)
17+
18+
add_entrypoint_object(
19+
setitimer
20+
SRCS
21+
setitimer.cpp
22+
HDRS
23+
../setitimer.h
24+
DEPENDS
25+
libc.hdr.types.struct_itimerval
26+
libc.include.sys_syscall
27+
libc.src.__support.OSUtil.osutil
28+
libc.src.__support.common
29+
libc.src.errno.errno
30+
)
31+
32+
add_entrypoint_object(
33+
getitimer
34+
SRCS
35+
getitimer.cpp
36+
HDRS
37+
../getitimer.h
38+
DEPENDS
39+
libc.hdr.types.struct_itimerval
40+
libc.include.sys_syscall
41+
libc.src.__support.OSUtil.osutil
42+
libc.src.__support.common
43+
libc.src.errno.errno
44+
)

libc/src/sys/time/linux/getitimer.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- Implementation file for getitimer ---------------------------------===//
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/time/getitimer.h"
10+
#include "hdr/types/struct_itimerval.h"
11+
#include "src/__support/OSUtil/syscall.h"
12+
#include "src/__support/common.h"
13+
#include "src/errno/libc_errno.h"
14+
#include <sys/syscall.h>
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
LLVM_LIBC_FUNCTION(int, getitimer, (int which, struct itimerval *curr_value)) {
19+
long ret =
20+
LIBC_NAMESPACE::syscall_impl<long>(SYS_getitimer, which, curr_value);
21+
// On failure, return -1 and set errno.
22+
if (ret < 0) {
23+
libc_errno = static_cast<int>(-ret);
24+
return -1;
25+
}
26+
return 0;
27+
}
28+
29+
} // namespace LIBC_NAMESPACE_DECL

libc/src/sys/time/linux/setitimer.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- Implementation file for setitimer ---------------------------------===//
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+
#include "src/sys/time/setitimer.h"
9+
#include "hdr/types/struct_itimerval.h"
10+
#include "src/__support/OSUtil/syscall.h"
11+
#include "src/__support/common.h"
12+
#include "src/errno/libc_errno.h"
13+
#include <sys/syscall.h>
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(int, setitimer,
18+
(int which, const struct itimerval *new_value,
19+
struct itimerval *old_value)) {
20+
long ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_setitimer, which, new_value,
21+
old_value);
22+
// On failure, return -1 and set errno.
23+
if (ret < 0) {
24+
libc_errno = static_cast<int>(-ret);
25+
return -1;
26+
}
27+
return 0;
28+
}
29+
30+
} // namespace LIBC_NAMESPACE_DECL

libc/src/sys/time/setitimer.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for setitimer -------------------------------===//
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_TIME_SETITIMER_H
10+
#define LLVM_LIBC_SRC_SYS_TIME_SETITIMER_H
11+
12+
#include "hdr/types/struct_itimerval.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
int setitimer(int which, const struct itimerval *new_value,
17+
struct itimerval *old_value);
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_SYS_TIME_SETITIMER_H

libc/test/src/sys/time/CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,34 @@ add_libc_unittest(
1919
libc.src.sys.stat.stat
2020
libc.test.UnitTest.ErrnoCheckingTest
2121
)
22+
23+
add_libc_unittest(
24+
setitimer_test
25+
SUITE
26+
libc_sys_time_unittests
27+
SRCS
28+
setitimer_test.cpp
29+
DEPENDS
30+
libc.include.signal
31+
libc.src.sys.time.setitimer
32+
libc.src.signal.sigaction
33+
libc.src.signal.sigemptyset
34+
libc.src.__support.common
35+
libc.src.errno.errno
36+
libc.test.UnitTest.ErrnoCheckingTest
37+
libc.test.UnitTest.ErrnoSetterMatcher
38+
)
39+
40+
add_libc_unittest(
41+
getitimer_test
42+
SUITE
43+
libc_sys_time_unittests
44+
SRCS
45+
getitimer_test.cpp
46+
DEPENDS
47+
libc.src.sys.time.getitimer
48+
libc.src.__support.common
49+
libc.src.errno.errno
50+
libc.test.UnitTest.ErrnoCheckingTest
51+
libc.test.UnitTest.ErrnoSetterMatcher
52+
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===-- Unittests for getitimer -------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4+
// Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "hdr/types/struct_itimerval.h"
11+
#include "src/sys/time/getitimer.h"
12+
#include "test/UnitTest/ErrnoCheckingTest.h"
13+
#include "test/UnitTest/ErrnoSetterMatcher.h"
14+
#include "test/UnitTest/Test.h"
15+
16+
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
17+
using LlvmLibcSysTimeGetitimerTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
18+
19+
TEST_F(LlvmLibcSysTimeGetitimerTest, SmokeTest) {
20+
struct itimerval timer;
21+
timer.it_value.tv_sec = -1;
22+
timer.it_value.tv_usec = -1;
23+
timer.it_interval.tv_sec = -1;
24+
timer.it_interval.tv_usec = -1;
25+
26+
ASSERT_THAT(LIBC_NAMESPACE::getitimer(0, &timer),
27+
returns(EQ(0)).with_errno(EQ(0)));
28+
29+
ASSERT_TRUE(timer.it_value.tv_sec == 0);
30+
ASSERT_TRUE(timer.it_value.tv_usec == 0);
31+
ASSERT_TRUE(timer.it_interval.tv_sec == 0);
32+
ASSERT_TRUE(timer.it_interval.tv_usec == 0);
33+
}
34+
35+
TEST_F(LlvmLibcSysTimeGetitimerTest, InvalidRetTest) {
36+
struct itimerval timer;
37+
38+
// out of range timer type (which)
39+
ASSERT_THAT(LIBC_NAMESPACE::getitimer(99, &timer),
40+
returns(NE(0)).with_errno(NE(0)));
41+
}

0 commit comments

Comments
 (0)