[libc-commits] [libc] [libc] Implement getitimer and setitimer, add proxy headers for itimerval (PR #134773)

Tsz Chan via libc-commits libc-commits at lists.llvm.org
Wed Apr 9 20:02:02 PDT 2025


https://github.com/tszhin-swe updated https://github.com/llvm/llvm-project/pull/134773

>From 46f5b38dffd7b7e8f1ac3bf4cc1f993e0803d804 Mon Sep 17 00:00:00 2001
From: Tsz Chan <keithcth2001 at gmail.com>
Date: Mon, 7 Apr 2025 20:04:09 -0700
Subject: [PATCH 1/4] [libc] Implement getitimer and setitimer, add proxy
 headers for itimerval

---
 libc/config/linux/aarch64/entrypoints.txt     |  4 ++
 libc/config/linux/arm/entrypoints.txt         |  3 ++
 libc/config/linux/x86_64/entrypoints.txt      |  4 ++
 libc/hdr/types/CMakeLists.txt                 |  9 ++++
 libc/hdr/types/struct_itimerval.h             | 21 ++++++++
 libc/include/llvm-libc-types/CMakeLists.txt   |  1 +
 .../llvm-libc-types/struct_itimerval.h        | 20 ++++++++
 libc/include/sys/time.yaml                    | 20 +++++++-
 libc/src/sys/time/CMakeLists.txt              | 14 ++++++
 libc/src/sys/time/getitimer.h                 | 20 ++++++++
 libc/src/sys/time/linux/CMakeLists.txt        | 27 ++++++++++
 libc/src/sys/time/linux/getitimer.cpp         | 30 ++++++++++++
 libc/src/sys/time/linux/setitimer.cpp         | 31 ++++++++++++
 libc/src/sys/time/setitimer.h                 | 21 ++++++++
 libc/test/src/sys/time/CMakeLists.txt         | 26 ++++++++++
 libc/test/src/sys/time/getitimer_test.cpp     | 32 ++++++++++++
 libc/test/src/sys/time/setitimer_test.cpp     | 49 +++++++++++++++++++
 17 files changed, 331 insertions(+), 1 deletion(-)
 create mode 100644 libc/hdr/types/struct_itimerval.h
 create mode 100644 libc/include/llvm-libc-types/struct_itimerval.h
 create mode 100644 libc/src/sys/time/getitimer.h
 create mode 100644 libc/src/sys/time/linux/getitimer.cpp
 create mode 100644 libc/src/sys/time/linux/setitimer.cpp
 create mode 100644 libc/src/sys/time/setitimer.h
 create mode 100644 libc/test/src/sys/time/getitimer_test.cpp
 create mode 100644 libc/test/src/sys/time/setitimer_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 5f293dc1c3c73..2c621737e380b 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -363,6 +363,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     # sys/uio.h entrypoints
     libc.src.sys.uio.writev
     libc.src.sys.uio.readv
+
+    # sys/time.h entrypoints
+    libc.src.sys.time.setitimer
+    libc.src.sys.time.getitimer
 )
 
 if(LLVM_LIBC_INCLUDE_SCUDO)
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 05e8e9e308168..870a194418f43 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -185,6 +185,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # libc.src.sys.epoll.epoll_pwait
     # libc.src.sys.epoll.epoll_pwait2
 
+    # sys/time.h entrypoints
+    libc.src.sys.time.setitimer
+    libc.src.sys.time.getitimer
 )
 
 if(LLVM_LIBC_FULL_BUILD)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 1ac3a781d5279..73dfeae1a2c94 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -368,6 +368,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     # sys/uio.h entrypoints
     libc.src.sys.uio.writev
     libc.src.sys.uio.readv
+
+    # sys/time.h entrypoints
+    libc.src.sys.time.setitimer
+    libc.src.sys.time.getitimer
 )
 
 if(LLVM_LIBC_INCLUDE_SCUDO)
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index e43c6f4552644..ac9fe40abf516 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -190,6 +190,15 @@ add_proxy_header_library(
     libc.include.sys_time
 )
 
+add_proxy_header_library(
+  struct_itimerval
+  HDRS
+    struct_itimerval.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.struct_itimerval
+    libc.include.sys_time
+)
+
 add_proxy_header_library(
   pid_t
   HDRS
diff --git a/libc/hdr/types/struct_itimerval.h b/libc/hdr/types/struct_itimerval.h
new file mode 100644
index 0000000000000..b2281675b8023
--- /dev/null
+++ b/libc/hdr/types/struct_itimerval.h
@@ -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
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 9ed39bcd05190..861b983b34219 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -77,6 +77,7 @@ add_header(struct_pollfd HDR struct_pollfd.h)
 add_header(struct_rlimit HDR struct_rlimit.h DEPENDS .rlim_t)
 add_header(struct_sched_param HDR struct_sched_param.h)
 add_header(struct_timeval HDR struct_timeval.h DEPENDS .suseconds_t .time_t)
+add_header(struct_itimerval HDR struct_itimerval.h DEPENDS .struct_timeval)
 add_header(struct_rusage HDR struct_rusage.h DEPENDS .struct_timeval)
 add_header(union_sigval HDR union_sigval.h)
 add_header(siginfo_t HDR siginfo_t.h DEPENDS .union_sigval .pid_t .uid_t .clock_t)
diff --git a/libc/include/llvm-libc-types/struct_itimerval.h b/libc/include/llvm-libc-types/struct_itimerval.h
new file mode 100644
index 0000000000000..9d3026fc2d9d7
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_itimerval.h
@@ -0,0 +1,20 @@
+//===-- 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
diff --git a/libc/include/sys/time.yaml b/libc/include/sys/time.yaml
index 92ab9a467f33a..e24a3eadf4359 100644
--- a/libc/include/sys/time.yaml
+++ b/libc/include/sys/time.yaml
@@ -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: []
diff --git a/libc/src/sys/time/CMakeLists.txt b/libc/src/sys/time/CMakeLists.txt
index f599cddaaeeb3..8ea6752ba87c0 100644
--- a/libc/src/sys/time/CMakeLists.txt
+++ b/libc/src/sys/time/CMakeLists.txt
@@ -8,3 +8,17 @@ add_entrypoint_object(
   DEPENDS
     .${LIBC_TARGET_OS}.utimes
 )
+
+add_entrypoint_object(
+  setitimer
+  ALIAS
+  DEPENDS
+  .${LIBC_TARGET_OS}.setitimer 
+)
+
+add_entrypoint_object(
+  getitimer
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.getitimer 
+)
diff --git a/libc/src/sys/time/getitimer.h b/libc/src/sys/time/getitimer.h
new file mode 100644
index 0000000000000..a10462a30adfc
--- /dev/null
+++ b/libc/src/sys/time/getitimer.h
@@ -0,0 +1,20 @@
+//===-- 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
diff --git a/libc/src/sys/time/linux/CMakeLists.txt b/libc/src/sys/time/linux/CMakeLists.txt
index 506001e5c9fd2..617beb2534e4f 100644
--- a/libc/src/sys/time/linux/CMakeLists.txt
+++ b/libc/src/sys/time/linux/CMakeLists.txt
@@ -12,5 +12,32 @@ add_entrypoint_object(
     libc.include.sys_syscall
     libc.include.fcntl
     libc.src.__support.OSUtil.osutil
+)
+
+add_entrypoint_object(
+  setitimer
+  SRCS
+    setitimer.cpp
+  HDRS
+    ../setitimer.h
+  DEPENDS
+    libc.hdr.types.struct_itimerval
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common 
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  getitimer
+  SRCS
+    getitimer.cpp
+  HDRS
+    ../getitimer.h
+  DEPENDS
+    libc.hdr.types.struct_itimerval
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common 
     libc.src.errno.errno
 )
diff --git a/libc/src/sys/time/linux/getitimer.cpp b/libc/src/sys/time/linux/getitimer.cpp
new file mode 100644
index 0000000000000..de9e4c0f26cbc
--- /dev/null
+++ b/libc/src/sys/time/linux/getitimer.cpp
@@ -0,0 +1,30 @@
+//===-- 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
diff --git a/libc/src/sys/time/linux/setitimer.cpp b/libc/src/sys/time/linux/setitimer.cpp
new file mode 100644
index 0000000000000..3af0358fde139
--- /dev/null
+++ b/libc/src/sys/time/linux/setitimer.cpp
@@ -0,0 +1,31 @@
+//===-- 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
diff --git a/libc/src/sys/time/setitimer.h b/libc/src/sys/time/setitimer.h
new file mode 100644
index 0000000000000..a2a750c2a5649
--- /dev/null
+++ b/libc/src/sys/time/setitimer.h
@@ -0,0 +1,21 @@
+//===-- 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
diff --git a/libc/test/src/sys/time/CMakeLists.txt b/libc/test/src/sys/time/CMakeLists.txt
index 72a65eec00937..a8c53232f10c6 100644
--- a/libc/test/src/sys/time/CMakeLists.txt
+++ b/libc/test/src/sys/time/CMakeLists.txt
@@ -19,3 +19,29 @@ add_libc_unittest(
     libc.src.sys.stat.stat
     libc.test.UnitTest.ErrnoCheckingTest
 )
+
+add_libc_unittest(
+  setitimer_test
+  SUITE
+    libc_sys_time_unittests
+  SRCS
+    setitimer_test.cpp
+  DEPENDS
+    libc.src.sys.time.setitimer
+    libc.src.__support.common
+    libc.src.errno.errno
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
+add_libc_unittest(
+  getitimer_test
+  SUITE
+    libc_sys_time_unittests
+  SRCS
+    getitimer_test.cpp
+  DEPENDS
+    libc.src.sys.time.getitimer
+    libc.src.__support.common
+    libc.src.errno.errno
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
diff --git a/libc/test/src/sys/time/getitimer_test.cpp b/libc/test/src/sys/time/getitimer_test.cpp
new file mode 100644
index 0000000000000..f1ca67a0870d6
--- /dev/null
+++ b/libc/test/src/sys/time/getitimer_test.cpp
@@ -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 "src/sys/time/getitimer.h"
+#include "hdr/types/struct_itimerval.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+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(ITIMER_REAL, &timer),
+              returns(EQ(0)).with_errno(EQ(0)));
+
+  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);
+}
diff --git a/libc/test/src/sys/time/setitimer_test.cpp b/libc/test/src/sys/time/setitimer_test.cpp
new file mode 100644
index 0000000000000..6275bb074c44d
--- /dev/null
+++ b/libc/test/src/sys/time/setitimer_test.cpp
@@ -0,0 +1,49 @@
+// //===-- 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 "src/signal/signal.h"
+#include "hdr/types/struct_itimerval.h"
+#include "src/sys/time/setitimer.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include <atomic>
+#include <signal.h>
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+
+static std::atomic<bool> timer_fired(false);
+
+extern "C" void handle_sigalrm(int) { timer_fired.store(true); }
+
+TEST(LlvmLibcSysTimeSetitimerTest, SmokeTest) {
+  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(ITIMER_REAL, &timer, nullptr),
+              returns(EQ(0)).with_errno(EQ(0)));
+
+  while (true) {
+    if (timer_fired.load())
+      break;
+  }
+
+  ASSERT_TRUE(timer_fired.load());
+}

>From 02031fbb1a2608598fd70d70d6d837de5f30d58f Mon Sep 17 00:00:00 2001
From: Tsz Chan <keithcth2001 at gmail.com>
Date: Mon, 7 Apr 2025 20:51:26 -0700
Subject: [PATCH 2/4] [libc] Fix header clang formatting

---
 libc/include/llvm-libc-types/struct_itimerval.h |  3 +--
 libc/src/sys/time/getitimer.h                   |  3 +--
 libc/src/sys/time/linux/getitimer.cpp           |  3 +--
 libc/src/sys/time/linux/setitimer.cpp           |  3 +--
 libc/src/sys/time/setitimer.h                   |  3 +--
 libc/test/src/sys/time/getitimer_test.cpp       | 15 +++++++--------
 libc/test/src/sys/time/setitimer_test.cpp       | 15 +++++++--------
 7 files changed, 19 insertions(+), 26 deletions(-)

diff --git a/libc/include/llvm-libc-types/struct_itimerval.h b/libc/include/llvm-libc-types/struct_itimerval.h
index 9d3026fc2d9d7..e23f1e6076dfe 100644
--- a/libc/include/llvm-libc-types/struct_itimerval.h
+++ b/libc/include/llvm-libc-types/struct_itimerval.h
@@ -1,5 +1,4 @@
-//===-- Definition of struct itimerval
-//-------------------------------------===//
+//===-- 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.
diff --git a/libc/src/sys/time/getitimer.h b/libc/src/sys/time/getitimer.h
index a10462a30adfc..e19dfd13522ca 100644
--- a/libc/src/sys/time/getitimer.h
+++ b/libc/src/sys/time/getitimer.h
@@ -1,5 +1,4 @@
-//===-- Implementation header for getitimer
-//-----------------------------------===//
+//===-- 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.
diff --git a/libc/src/sys/time/linux/getitimer.cpp b/libc/src/sys/time/linux/getitimer.cpp
index de9e4c0f26cbc..bbdbaa57dfd30 100644
--- a/libc/src/sys/time/linux/getitimer.cpp
+++ b/libc/src/sys/time/linux/getitimer.cpp
@@ -1,5 +1,4 @@
-//===-- Implementation file for getitimer
-//------------------------------------===//
+//===-- 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.
diff --git a/libc/src/sys/time/linux/setitimer.cpp b/libc/src/sys/time/linux/setitimer.cpp
index 3af0358fde139..b50356004701d 100644
--- a/libc/src/sys/time/linux/setitimer.cpp
+++ b/libc/src/sys/time/linux/setitimer.cpp
@@ -1,5 +1,4 @@
-//===-- Implementation file for setitimer
-//------------------------------------===//
+//===-- 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.
diff --git a/libc/src/sys/time/setitimer.h b/libc/src/sys/time/setitimer.h
index a2a750c2a5649..9daf8a71e8615 100644
--- a/libc/src/sys/time/setitimer.h
+++ b/libc/src/sys/time/setitimer.h
@@ -1,5 +1,4 @@
-//===-- Implementation header for setitimer
-//-----------------------------------===//
+//===-- 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.
diff --git a/libc/test/src/sys/time/getitimer_test.cpp b/libc/test/src/sys/time/getitimer_test.cpp
index f1ca67a0870d6..7d9d0395b6949 100644
--- a/libc/test/src/sys/time/getitimer_test.cpp
+++ b/libc/test/src/sys/time/getitimer_test.cpp
@@ -1,12 +1,11 @@
-// //===-- Unittests for getitimer
-// -------------------------------------------===//
-// //
-// // Part of the LLVM Project, under the Apache License v2.0 with LLVM
+//===-- 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
-// //
-// //===----------------------------------------------------------------------===//
+// 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"
diff --git a/libc/test/src/sys/time/setitimer_test.cpp b/libc/test/src/sys/time/setitimer_test.cpp
index 6275bb074c44d..49d4b3f3882ce 100644
--- a/libc/test/src/sys/time/setitimer_test.cpp
+++ b/libc/test/src/sys/time/setitimer_test.cpp
@@ -1,12 +1,11 @@
-// //===-- Unittests for setitimer
-// -------------------------------------------===//
-// //
-// // Part of the LLVM Project, under the Apache License v2.0 with LLVM
+//===-- 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
-// //
-// //===----------------------------------------------------------------------===//
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
 
 #include "src/signal/signal.h"
 #include "hdr/types/struct_itimerval.h"

>From ce4c93f306c53853cf9bc1adf71314cc65b0a796 Mon Sep 17 00:00:00 2001
From: Tsz Chan <keithcth2001 at gmail.com>
Date: Mon, 7 Apr 2025 21:19:32 -0700
Subject: [PATCH 3/4] [libc] Fix test includes

---
 libc/test/src/sys/time/CMakeLists.txt     |  1 +
 libc/test/src/sys/time/getitimer_test.cpp |  5 +++--
 libc/test/src/sys/time/setitimer_test.cpp | 14 ++++++--------
 3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/libc/test/src/sys/time/CMakeLists.txt b/libc/test/src/sys/time/CMakeLists.txt
index a8c53232f10c6..057100f1bc2ca 100644
--- a/libc/test/src/sys/time/CMakeLists.txt
+++ b/libc/test/src/sys/time/CMakeLists.txt
@@ -31,6 +31,7 @@ add_libc_unittest(
     libc.src.__support.common
     libc.src.errno.errno
     libc.test.UnitTest.ErrnoSetterMatcher
+    libc.include.signal
 )
 
 add_libc_unittest(
diff --git a/libc/test/src/sys/time/getitimer_test.cpp b/libc/test/src/sys/time/getitimer_test.cpp
index 7d9d0395b6949..0a75ec681c65e 100644
--- a/libc/test/src/sys/time/getitimer_test.cpp
+++ b/libc/test/src/sys/time/getitimer_test.cpp
@@ -7,10 +7,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/sys/time/getitimer.h"
 #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>
 
 using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
 
@@ -21,7 +22,7 @@ TEST(LlvmLibcSysTimeGetitimerTest, SmokeTest) {
   timer.it_interval.tv_sec = -1;
   timer.it_interval.tv_usec = -1;
 
-  ASSERT_THAT(LIBC_NAMESPACE::getitimer(ITIMER_REAL, &timer),
+  ASSERT_THAT(LIBC_NAMESPACE::getitimer(0, &timer),
               returns(EQ(0)).with_errno(EQ(0)));
 
   ASSERT_TRUE(timer.it_value.tv_sec == 0);
diff --git a/libc/test/src/sys/time/setitimer_test.cpp b/libc/test/src/sys/time/setitimer_test.cpp
index 49d4b3f3882ce..de9a6351b8f95 100644
--- a/libc/test/src/sys/time/setitimer_test.cpp
+++ b/libc/test/src/sys/time/setitimer_test.cpp
@@ -7,23 +7,21 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/signal/signal.h"
 #include "hdr/types/struct_itimerval.h"
 #include "src/sys/time/setitimer.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <atomic>
 #include <signal.h>
 
 using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
 
-static std::atomic<bool> timer_fired(false);
+static bool timer_fired(false);
 
-extern "C" void handle_sigalrm(int) { timer_fired.store(true); }
+extern "C" void handle_sigalrm(int) { timer_fired = true; }
 
 TEST(LlvmLibcSysTimeSetitimerTest, SmokeTest) {
-  errno = 0;
+  LIBC_NAMESPACE::libc_errno = 0;
   struct sigaction sa;
   sa.sa_handler = handle_sigalrm;
   sigemptyset(&sa.sa_mask);
@@ -36,13 +34,13 @@ TEST(LlvmLibcSysTimeSetitimerTest, SmokeTest) {
   timer.it_interval.tv_sec = 0;
   timer.it_interval.tv_usec = 0; // One-shot timer
 
-  ASSERT_THAT(LIBC_NAMESPACE::setitimer(ITIMER_REAL, &timer, nullptr),
+  ASSERT_THAT(LIBC_NAMESPACE::setitimer(0, &timer, nullptr),
               returns(EQ(0)).with_errno(EQ(0)));
 
   while (true) {
-    if (timer_fired.load())
+    if (timer_fired)
       break;
   }
 
-  ASSERT_TRUE(timer_fired.load());
+  ASSERT_TRUE(timer_fired);
 }

>From 2be20b3cb1749c4ac11ea53e8b4d2557c8d7d404 Mon Sep 17 00:00:00 2001
From: Tsz Chan <keithcth2001 at gmail.com>
Date: Wed, 9 Apr 2025 19:59:10 -0700
Subject: [PATCH 4/4] [libc] Add invalid tests and use ErrnoCheckingTest

---
 libc/include/sys/time.yaml                |  2 +-
 libc/src/sys/time/linux/CMakeLists.txt    |  1 +
 libc/test/src/sys/time/CMakeLists.txt     |  4 +++-
 libc/test/src/sys/time/getitimer_test.cpp | 13 +++++++++++--
 libc/test/src/sys/time/setitimer_test.cpp | 15 ++++++++++++---
 5 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/libc/include/sys/time.yaml b/libc/include/sys/time.yaml
index e24a3eadf4359..d0a67a9248108 100644
--- a/libc/include/sys/time.yaml
+++ b/libc/include/sys/time.yaml
@@ -6,6 +6,7 @@ types:
   - type_name: struct_timeval
   - type_name: struct_itimerval
 enums: []
+objects: []
 functions:
   - name: utimes
     return_type: int
@@ -29,4 +30,3 @@ functions:
     arguments:
       - type: int
       - type: struct itimerval *
-objects: []
diff --git a/libc/src/sys/time/linux/CMakeLists.txt b/libc/src/sys/time/linux/CMakeLists.txt
index 617beb2534e4f..7a4dd68eeac2c 100644
--- a/libc/src/sys/time/linux/CMakeLists.txt
+++ b/libc/src/sys/time/linux/CMakeLists.txt
@@ -12,6 +12,7 @@ add_entrypoint_object(
     libc.include.sys_syscall
     libc.include.fcntl
     libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
diff --git a/libc/test/src/sys/time/CMakeLists.txt b/libc/test/src/sys/time/CMakeLists.txt
index 057100f1bc2ca..4c59a2e8eb482 100644
--- a/libc/test/src/sys/time/CMakeLists.txt
+++ b/libc/test/src/sys/time/CMakeLists.txt
@@ -27,11 +27,12 @@ add_libc_unittest(
   SRCS
     setitimer_test.cpp
   DEPENDS
+    libc.include.signal
     libc.src.sys.time.setitimer
     libc.src.__support.common
     libc.src.errno.errno
+    libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
-    libc.include.signal
 )
 
 add_libc_unittest(
@@ -44,5 +45,6 @@ add_libc_unittest(
     libc.src.sys.time.getitimer
     libc.src.__support.common
     libc.src.errno.errno
+    libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
 )
diff --git a/libc/test/src/sys/time/getitimer_test.cpp b/libc/test/src/sys/time/getitimer_test.cpp
index 0a75ec681c65e..c1d6f72701627 100644
--- a/libc/test/src/sys/time/getitimer_test.cpp
+++ b/libc/test/src/sys/time/getitimer_test.cpp
@@ -9,13 +9,14 @@
 
 #include "hdr/types/struct_itimerval.h"
 #include "src/sys/time/getitimer.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
-#include <sys/time.h>
 
 using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcSysTimeGetitimerTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
-TEST(LlvmLibcSysTimeGetitimerTest, SmokeTest) {
+TEST_F(LlvmLibcSysTimeGetitimerTest, SmokeTest) {
   struct itimerval timer;
   timer.it_value.tv_sec = -1;
   timer.it_value.tv_usec = -1;
@@ -30,3 +31,11 @@ TEST(LlvmLibcSysTimeGetitimerTest, SmokeTest) {
   ASSERT_TRUE(timer.it_interval.tv_sec == 0);
   ASSERT_TRUE(timer.it_interval.tv_usec == 0);
 }
+
+TEST_F(LlvmLibcSysTimeGetitimerTest, InvalidRetTest) {
+  struct itimerval timer;
+
+  // out of range timer type (which)
+  ASSERT_THAT(LIBC_NAMESPACE::getitimer(99, &timer),
+              returns(NE(0)).with_errno(NE(0)));
+}
diff --git a/libc/test/src/sys/time/setitimer_test.cpp b/libc/test/src/sys/time/setitimer_test.cpp
index de9a6351b8f95..2d1bcd0f167cc 100644
--- a/libc/test/src/sys/time/setitimer_test.cpp
+++ b/libc/test/src/sys/time/setitimer_test.cpp
@@ -8,19 +8,20 @@
 //===----------------------------------------------------------------------===//
 
 #include "hdr/types/struct_itimerval.h"
+#include "hdr/types/struct_sigaction.h"
 #include "src/sys/time/setitimer.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <signal.h>
-
 using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcSysTimeSetitimerTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
 static bool timer_fired(false);
 
 extern "C" void handle_sigalrm(int) { timer_fired = true; }
 
-TEST(LlvmLibcSysTimeSetitimerTest, SmokeTest) {
+TEST_F(LlvmLibcSysTimeSetitimerTest, SmokeTest) {
   LIBC_NAMESPACE::libc_errno = 0;
   struct sigaction sa;
   sa.sa_handler = handle_sigalrm;
@@ -44,3 +45,11 @@ TEST(LlvmLibcSysTimeSetitimerTest, SmokeTest) {
 
   ASSERT_TRUE(timer_fired);
 }
+
+TEST_F(LlvmLibcSysTimeSetitimerTest, InvalidRetTest) {
+  struct itimerval timer;
+
+  // out of range timer type (which)
+  ASSERT_THAT(LIBC_NAMESPACE::setitimer(99, &timer, nullptr),
+              returns(NE(0)).with_errno(NE(0)));
+}



More information about the libc-commits mailing list