Skip to content

Conversation

michaelrj-google
Copy link
Contributor

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

Simplify utims a bit and add proper error handling to setsid as
described in the standard
@llvmbot llvmbot added the libc label Aug 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 26, 2025

@llvm/pr-subscribers-libc

Author: Michael Jones (michaelrj-google)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/155495.diff

2 Files Affected:

  • (modified) libc/src/sys/time/linux/utimes.cpp (+11-11)
  • (modified) libc/src/unistd/linux/setsid.cpp (+7-1)
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

@michaelrj-google michaelrj-google merged commit b6a0802 into llvm:main Aug 26, 2025
21 checks passed
@michaelrj-google michaelrj-google deleted the libcSmallSyscallCleanups branch August 26, 2025 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants