[libc-commits] [libc] [libc][NFC] Clean up utimes and setsid (PR #155495)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Tue Aug 26 13:53:46 PDT 2025


https://github.com/michaelrj-google created https://github.com/llvm/llvm-project/pull/155495

Simplify utims a bit and add proper error handling to setsid as
described in the standard


>From 1c2f3e82ed1b0d033daabdb50b785972972d11c7 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Tue, 26 Aug 2025 20:10:40 +0000
Subject: [PATCH] [libc][NFC] Clean up utimes and setsid

Simplify utims a bit and add proper error handling to setsid as
described in the standard
---
 libc/src/sys/time/linux/utimes.cpp | 22 +++++++++++-----------
 libc/src/unistd/linux/setsid.cpp   |  8 +++++++-
 2 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/libc/src/sys/time/linux/utimes.cpp b/libc/src/sys/time/linux/utimes.cpp
index 9c00ce9909f2f..e740190bc8198 100644
--- a/libc/src/sys/time/linux/utimes.cpp
+++ b/libc/src/sys/time/linux/utimes.cpp
@@ -21,24 +21,21 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-#ifdef SYS_utimes
-constexpr auto UTIMES_SYSCALL_ID = SYS_utimes;
-#elif defined(SYS_utimensat)
-constexpr auto UTIMES_SYSCALL_ID = SYS_utimensat;
-#elif defined(SYS_utimensat_time64)
-constexpr auto UTIMES_SYSCALL_ID = SYS_utimensat_time64;
-#else
-#error "utimes, utimensat, utimensat_time64,  syscalls not available."
-#endif
-
 LLVM_LIBC_FUNCTION(int, utimes,
                    (const char *path, const struct timeval times[2])) {
   int ret;
 
 #ifdef SYS_utimes
   // No need to define a timespec struct, use the syscall directly.
-  ret = LIBC_NAMESPACE::syscall_impl<int>(UTIMES_SYSCALL_ID, path, times);
+  ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_utimes, path, times);
 #elif defined(SYS_utimensat) || defined(SYS_utimensat_time64)
+
+#if defined(SYS_utimensat)
+  constexpr auto UTIMES_SYSCALL_ID = SYS_utimensat;
+#elif defined(SYS_utimensat_time64)
+  constexpr auto UTIMES_SYSCALL_ID = SYS_utimensat_time64;
+#endif
+
   // the utimensat syscall requires a timespec struct, not timeval.
   struct timespec ts[2];
   struct timespec *ts_ptr = nullptr; // default value if times is nullptr
@@ -74,6 +71,9 @@ LLVM_LIBC_FUNCTION(int, utimes,
   // flags=0 means don't follow symlinks (like utimes)
   ret = LIBC_NAMESPACE::syscall_impl<int>(UTIMES_SYSCALL_ID, AT_FDCWD, path,
                                           ts_ptr, 0);
+
+#else
+#error "utimes, utimensat, utimensat_time64,  syscalls not available."
 #endif // SYS_utimensat
 
   if (ret < 0) {
diff --git a/libc/src/unistd/linux/setsid.cpp b/libc/src/unistd/linux/setsid.cpp
index df4629bb326cc..b1a265c9a01dd 100644
--- a/libc/src/unistd/linux/setsid.cpp
+++ b/libc/src/unistd/linux/setsid.cpp
@@ -11,6 +11,7 @@
 #include "hdr/types/pid_t.h"
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
 
 #include <sys/syscall.h> // For syscall numbers.
@@ -18,7 +19,12 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(pid_t, setsid, ()) {
-  return LIBC_NAMESPACE::syscall_impl<pid_t>(SYS_setsid);
+  pid_t ret = LIBC_NAMESPACE::syscall_impl<pid_t>(SYS_setsid);
+  if (ret < 0) {
+    libc_errno = static_cast<int>(-ret);
+    return -1;
+  }
+  return ret;
 }
 
 } // namespace LIBC_NAMESPACE_DECL



More information about the libc-commits mailing list