-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc] Implement getitimer and setitimer, add proxy headers for itimerval #134773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
46f5b38
02031fb
ce4c93f
6b7ec28
9892c21
2b707c9
81719c8
850a729
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| //===-- Proxy for struct itimerval ----------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| #ifndef LLVM_LIBC_HDR_TYPES_STRUCT_ITIMERVAL_H | ||
| #define LLVM_LIBC_HDR_TYPES_STRUCT_ITIMERVAL_H | ||
|
|
||
| #ifdef LIBC_FULL_BUILD | ||
|
|
||
| #include "include/llvm-libc-types/struct_itimerval.h" | ||
|
|
||
| #else | ||
|
|
||
| #include <sys/time.h> | ||
|
|
||
| #endif // LIBC_FULL_BUILD | ||
|
|
||
| #endif // LLVM_LIBC_HDR_TYPES_STRUCT_ITIMERVAL_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| //===-- Definition of struct itimerval ------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_LIBC_TYPES_STRUCT_ITIMERVAL_H | ||
| #define LLVM_LIBC_TYPES_STRUCT_ITIMERVAL_H | ||
|
|
||
| #include "struct_timeval.h" | ||
|
|
||
| struct itimerval { | ||
| struct timeval it_interval; /* Interval for periodic timer */ | ||
| struct timeval it_value; /* Time until next expiration */ | ||
| }; | ||
|
|
||
| #endif // LLVM_LIBC_TYPES_STRUCT_ITIMERVAL_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,29 @@ standards: Linux | |
| macros: [] | ||
| types: | ||
| - type_name: struct_timeval | ||
| - type_name: struct_itimerval | ||
| enums: [] | ||
| objects: [] | ||
| functions: | ||
| - name: utimes | ||
| return_type: int | ||
| arguments: | ||
| - type: const char* | ||
| - type: const struct timeval* | ||
|
|
||
| - name: setitimer | ||
| standards: | ||
| - POSIX | ||
| return_type: int | ||
| arguments: | ||
| - type: int | ||
| - type: const struct itimerval * | ||
| - type: struct itimerval * | ||
|
|
||
| - name: getitimer | ||
| standards: | ||
| - POSIX | ||
| return_type: int | ||
| arguments: | ||
| - type: int | ||
| - type: struct itimerval * | ||
| objects: [] | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| //===-- Implementation header for getitimer -------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_LIBC_SRC_SYS_TIME_GETITIMER_H | ||
| #define LLVM_LIBC_SRC_SYS_TIME_GETITIMER_H | ||
|
|
||
| #include "hdr/types/struct_itimerval.h" | ||
| #include "src/__support/macros/config.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
| int getitimer(int which, struct itimerval *curr_value); | ||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_SYS_TIME_GETITIMER_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| //===-- Implementation file for getitimer ---------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "src/sys/time/getitimer.h" | ||
| #include "hdr/types/struct_itimerval.h" | ||
| #include "src/__support/OSUtil/syscall.h" | ||
| #include "src/__support/common.h" | ||
| #include "src/errno/libc_errno.h" | ||
| #include <sys/syscall.h> | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(int, getitimer, (int which, struct itimerval *curr_value)) { | ||
| long ret = | ||
| LIBC_NAMESPACE::syscall_impl<long>(SYS_getitimer, which, curr_value); | ||
| // On failure, return -1 and set errno. | ||
| if (ret < 0) { | ||
| libc_errno = static_cast<int>(-ret); | ||
| return -1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| //===-- Implementation file for setitimer ---------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| #include "src/sys/time/setitimer.h" | ||
| #include "hdr/types/struct_itimerval.h" | ||
| #include "src/__support/OSUtil/syscall.h" | ||
| #include "src/__support/common.h" | ||
| #include "src/errno/libc_errno.h" | ||
| #include <sys/syscall.h> | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(int, setitimer, | ||
| (int which, const struct itimerval *new_value, | ||
| struct itimerval *old_value)) { | ||
| long ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_setitimer, which, new_value, | ||
| old_value); | ||
| // On failure, return -1 and set errno. | ||
| if (ret < 0) { | ||
| libc_errno = static_cast<int>(-ret); | ||
| return -1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| //===-- Implementation header for setitimer -------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_LIBC_SRC_SYS_TIME_SETITIMER_H | ||
| #define LLVM_LIBC_SRC_SYS_TIME_SETITIMER_H | ||
|
|
||
| #include "hdr/types/struct_itimerval.h" | ||
| #include "src/__support/macros/config.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
| int setitimer(int which, const struct itimerval *new_value, | ||
| struct itimerval *old_value); | ||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_SYS_TIME_SETITIMER_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| //===-- Unittests for getitimer -------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM | ||
| // Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "hdr/types/struct_itimerval.h" | ||
| #include "src/sys/time/getitimer.h" | ||
| #include "test/UnitTest/ErrnoSetterMatcher.h" | ||
| #include "test/UnitTest/Test.h" | ||
| #include <sys/time.h> | ||
SchrodingerZhu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; | ||
|
|
||
| TEST(LlvmLibcSysTimeGetitimerTest, SmokeTest) { | ||
|
||
| struct itimerval timer; | ||
| timer.it_value.tv_sec = -1; | ||
| timer.it_value.tv_usec = -1; | ||
| timer.it_interval.tv_sec = -1; | ||
| timer.it_interval.tv_usec = -1; | ||
|
|
||
| ASSERT_THAT(LIBC_NAMESPACE::getitimer(0, &timer), | ||
| returns(EQ(0)).with_errno(EQ(0))); | ||
|
Comment on lines
+26
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ideally there would be a test where the function succeeds and one where it fails. Could you add a test where the function gives an error?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
|
|
||
| ASSERT_TRUE(timer.it_value.tv_sec == 0); | ||
| ASSERT_TRUE(timer.it_value.tv_usec == 0); | ||
| ASSERT_TRUE(timer.it_interval.tv_sec == 0); | ||
| ASSERT_TRUE(timer.it_interval.tv_usec == 0); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| //===-- Unittests for setitimer -------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM | ||
| // Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "hdr/types/struct_itimerval.h" | ||
| #include "src/sys/time/setitimer.h" | ||
| #include "test/UnitTest/ErrnoSetterMatcher.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| #include <signal.h> | ||
SchrodingerZhu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; | ||
|
|
||
| static bool timer_fired(false); | ||
|
|
||
| extern "C" void handle_sigalrm(int) { timer_fired = true; } | ||
|
|
||
| TEST(LlvmLibcSysTimeSetitimerTest, SmokeTest) { | ||
| LIBC_NAMESPACE::libc_errno = 0; | ||
| struct sigaction sa; | ||
| sa.sa_handler = handle_sigalrm; | ||
| sigemptyset(&sa.sa_mask); | ||
| sa.sa_flags = 0; | ||
| sigaction(SIGALRM, &sa, nullptr); | ||
|
|
||
| struct itimerval timer; | ||
| timer.it_value.tv_sec = 0; | ||
| timer.it_value.tv_usec = 200000; | ||
| timer.it_interval.tv_sec = 0; | ||
| timer.it_interval.tv_usec = 0; // One-shot timer | ||
|
|
||
| ASSERT_THAT(LIBC_NAMESPACE::setitimer(0, &timer, nullptr), | ||
| returns(EQ(0)).with_errno(EQ(0))); | ||
|
|
||
| while (true) { | ||
| if (timer_fired) | ||
| break; | ||
| } | ||
|
|
||
| ASSERT_TRUE(timer_fired); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be restrict
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, should be good to land?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're good to land. Please wait for the presubmits to finish, though if the buildkite is taking a long time you can skip that one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have merge access, could you merge it for me? Thanks