https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/240331

This PR move setsid behind the centralized syscall wrappers, following 
libc/docs/dev/syscall_wrapper_refactor.md.


>From 8ce83f58d16dc51898503e62a32463c3d9377408 Mon Sep 17 00:00:00 2001
From: c8ef <[email protected]>
Date: Mon, 9 Nov 2026 18:59:34 +0800
Subject: [PATCH] [libc] Use a syscall wrapper for setsid

This PR move setsid behind the centralized syscall wrappers, following 
libc/docs/dev/syscall_wrapper_refactor.md.
---
 .../linux/syscall_wrappers/CMakeLists.txt     | 13 +++++++
 .../OSUtil/linux/syscall_wrappers/setsid.h    | 34 +++++++++++++++++++
 libc/src/unistd/linux/CMakeLists.txt          |  7 ++--
 libc/src/unistd/linux/setsid.cpp              | 19 ++++++-----
 .../llvm-project-overlay/libc/BUILD.bazel     | 34 +++++++++++++++++++
 5 files changed, 97 insertions(+), 10 deletions(-)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/setsid.h

diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt 
b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index f318c5bedb82a0..39bf7ad0b5e6ee 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -669,6 +669,19 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  setsid
+  HDRS
+    setsid.h
+  DEPENDS
+    libc.hdr.types.pid_t
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   setsockopt
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/setsid.h 
b/libc/src/__support/OSUtil/linux/syscall_wrappers/setsid.h
new file mode 100644
index 00000000000000..a1a717fcdbad12
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/setsid.h
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for setsid.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SETSID_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SETSID_H
+
+#include "hdr/types/pid_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<pid_t> setsid() {
+  return syscall_checked<pid_t>(SYS_setsid);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SETSID_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt 
b/libc/src/unistd/linux/CMakeLists.txt
index 8a028572af79b1..48b5f6a796b836 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -938,8 +938,11 @@ add_entrypoint_object(
     ../setsid.h
   DEPENDS
     libc.hdr.types.pid_t
-    libc.include.sys_syscall
-    libc.src.__support.OSUtil.osutil
+    libc.src.__support.OSUtil.linux.syscall_wrappers.setsid
+    libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
diff --git a/libc/src/unistd/linux/setsid.cpp b/libc/src/unistd/linux/setsid.cpp
index b1a265c9a01dd5..fbdf6f9ee4a826 100644
--- a/libc/src/unistd/linux/setsid.cpp
+++ b/libc/src/unistd/linux/setsid.cpp
@@ -1,30 +1,33 @@
-//===-- Linux implementation of 
setsid-------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
 //
 
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of setsid.
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/unistd/setsid.h"
 
 #include "hdr/types/pid_t.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/setsid.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
 
-#include <sys/syscall.h> // For syscall numbers.
-
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(pid_t, setsid, ()) {
-  pid_t ret = LIBC_NAMESPACE::syscall_impl<pid_t>(SYS_setsid);
-  if (ret < 0) {
-    libc_errno = static_cast<int>(-ret);
+  auto result = linux_syscalls::setsid();
+  if (!result) {
+    libc_errno = result.error();
     return -1;
   }
-  return ret;
+  return result.value();
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel 
b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index b90c54e791d131..4c001a29091221 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4830,6 +4830,22 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_osutil_linux_syscall_wrappers_setsid",
+    hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/setsid.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        ":__support_common",
+        ":__support_error_or",
+        ":__support_macros_config",
+        ":__support_osutil_syscall",
+        ":types_pid_t",
+    ],
+)
+
 libc_support_library(
     name = "__support_osutil_linux_syscall_wrappers_setsockopt",
     hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/setsockopt.h"],
@@ -19124,6 +19140,24 @@ libc_function(
     ],
 )
 
+libc_function(
+    name = "setsid",
+    srcs = ["src/unistd/linux/setsid.cpp"],
+    hdrs = ["src/unistd/setsid.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        ":__support_common",
+        ":__support_libc_errno",
+        ":__support_macros_config",
+        ":__support_osutil_linux_syscall_wrappers_setsid",
+        ":errno",
+        ":types_pid_t",
+    ],
+)
+
 libc_function(
     name = "symlink",
     srcs = ["src/unistd/linux/symlink.cpp"],

_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to