[libc-commits] [libc] [libc][POSIX][unistd] implement getsid (PR #127341)
Zaky Hermawan via libc-commits
libc-commits at lists.llvm.org
Wed Feb 19 09:53:53 PST 2025
https://github.com/ZakyHermawan updated https://github.com/llvm/llvm-project/pull/127341
>From a762588dc959ca13cb3d65446b22984b041faa5f Mon Sep 17 00:00:00 2001
From: ZakyHermawan <zaky.hermawan9615 at gmail.com>
Date: Sun, 16 Feb 2025 01:37:42 +0700
Subject: [PATCH 1/4] feat: implement getsid
Signed-off-by: ZakyHermawan <zaky.hermawan9615 at gmail.com>
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/include/unistd.yaml | 6 +++++
libc/src/unistd/CMakeLists.txt | 7 ++++++
libc/src/unistd/getsid.h | 21 ++++++++++++++++
libc/src/unistd/linux/CMakeLists.txt | 15 ++++++++++++
libc/src/unistd/linux/getsid.cpp | 29 +++++++++++++++++++++++
libc/test/src/unistd/CMakeLists.txt | 10 ++++++++
libc/test/src/unistd/getsid_test.cpp | 16 +++++++++++++
10 files changed, 107 insertions(+)
create mode 100644 libc/src/unistd/getsid.h
create mode 100644 libc/src/unistd/linux/getsid.cpp
create mode 100644 libc/test/src/unistd/getsid_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index efb5e1c7bd698..aac4017a0d845 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -329,6 +329,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.geteuid
libc.src.unistd.getpid
libc.src.unistd.getppid
+ libc.src.unistd.getsid
libc.src.unistd.gettid
libc.src.unistd.getuid
libc.src.unistd.isatty
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index a9ba0c257755b..6b006f0ecca89 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -326,6 +326,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.geteuid
libc.src.unistd.getpid
libc.src.unistd.getppid
+ libc.src.unistd.getsid
libc.src.unistd.gettid
libc.src.unistd.getuid
libc.src.unistd.isatty
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a4f6671a59789..35661004663c9 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -328,6 +328,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.geteuid
libc.src.unistd.getpid
libc.src.unistd.getppid
+ libc.src.unistd.getsid
libc.src.unistd.gettid
libc.src.unistd.getuid
libc.src.unistd.isatty
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index d04d46bd5c002..051e92b006741 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -161,6 +161,12 @@ functions:
return_type: int
arguments:
- type: void
+ - name: getsid
+ standards:
+ - POSIX
+ return_type: pid_t
+ arguments:
+ - type: pid_t
- name: gettid
standards:
- Linux
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index fb563ec4ecfd9..b1a1716aa85c6 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -125,6 +125,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.getppid
)
+add_entrypoint_object(
+ getsid
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.getsid
+)
+
add_entrypoint_object(
geteuid
ALIAS
diff --git a/libc/src/unistd/getsid.h b/libc/src/unistd/getsid.h
new file mode 100644
index 0000000000000..e788b5dc4fba0
--- /dev/null
+++ b/libc/src/unistd/getsid.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for getsid ------------------------*- C++ -*-===//
+//
+// 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_UNISTD_GETSID_H
+#define LLVM_LIBC_SRC_UNISTD_GETSID_H
+
+#include "hdr/types/pid_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+pid_t getsid(pid_t);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_GETSID_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index afdc595d0b26f..9502e2aa60b34 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -235,6 +235,21 @@ add_entrypoint_object(
libc.src.__support.OSUtil.osutil
)
+add_entrypoint_object(
+ getsid
+ SRCS
+ getsid.cpp
+ HDRS
+ ../getsid.h
+ DEPENDS
+ libc.hdr.types.pid_t
+ libc.hdr.fcntl_macros
+ libc.include.unistd
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
getuid
SRCS
diff --git a/libc/src/unistd/linux/getsid.cpp b/libc/src/unistd/linux/getsid.cpp
new file mode 100644
index 0000000000000..5977c5bf10e94
--- /dev/null
+++ b/libc/src/unistd/linux/getsid.cpp
@@ -0,0 +1,29 @@
+//===-- Linux implementation of getsid-------------------------------------===//
+//
+// 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/unistd/getsid.h"
+
+#include "hdr/types/pid_t.h"
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/errno/libc_errno.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(pid_t, getsid, (pid_t pid)) {
+ pid_t ret = LIBC_NAMESPACE::syscall_impl<pid_t>(SYS_getsid, pid);
+ if (ret < 0) {
+ libc_errno = static_cast<int>(-ret);
+ return -1;
+ }
+ return ret;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 665cb367ba4fd..d1f3050e6cccf 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -394,6 +394,16 @@ add_libc_unittest(
libc.src.unistd.getppid
)
+add_libc_unittest(
+ getsid_test
+ SUITE
+ libc_unistd_unittests
+ SRCS
+ getsid_test.cpp
+ DEPENDS
+ libc.src.unistd.getsid
+)
+
add_libc_unittest(
getuid_test
SUITE
diff --git a/libc/test/src/unistd/getsid_test.cpp b/libc/test/src/unistd/getsid_test.cpp
new file mode 100644
index 0000000000000..c0bf777012036
--- /dev/null
+++ b/libc/test/src/unistd/getsid_test.cpp
@@ -0,0 +1,16 @@
+//===-- Unittests for getsid ----------------------------------------------===//
+//
+// 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/unistd/getsid.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcGetPidTest, GetCurrSID) {
+ pid_t sid = LIBC_NAMESPACE::getsid(0);
+ ASSERT_NE(sid, -1);
+ ASSERT_ERRNO_SUCCESS();
+}
>From a9878c1c88d9631c097599bf61301a9c78a71cb9 Mon Sep 17 00:00:00 2001
From: ZakyHermawan <zaky.hermawan9615 at gmail.com>
Date: Sun, 16 Feb 2025 02:20:41 +0700
Subject: [PATCH 2/4] test: fix no member named 'libc_errno' on getsid_test
Signed-off-by: ZakyHermawan <zaky.hermawan9615 at gmail.com>
---
libc/test/src/unistd/getsid_test.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/libc/test/src/unistd/getsid_test.cpp b/libc/test/src/unistd/getsid_test.cpp
index c0bf777012036..6974c302508a7 100644
--- a/libc/test/src/unistd/getsid_test.cpp
+++ b/libc/test/src/unistd/getsid_test.cpp
@@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
+#include "src/errno/libc_errno.h"
#include "src/unistd/getsid.h"
#include "test/UnitTest/Test.h"
>From fa930b0a9aba13026c9ffb9e494a85003c97b8c9 Mon Sep 17 00:00:00 2001
From: ZakyHermawan <zaky.hermawan9615 at gmail.com>
Date: Thu, 20 Feb 2025 00:15:07 +0700
Subject: [PATCH 3/4] test: add test for getsid_test.cpp to check for failing
getsid syscall This commit also do cleanup on
libc/src/unistd/linux/CMakeLists.txt to remove unnecessary fnctl_macros and
unistd from add_entrypoint_object for getsid
Signed-off-by: ZakyHermawan <zaky.hermawan9615 at gmail.com>
---
libc/src/unistd/linux/CMakeLists.txt | 2 --
libc/test/src/unistd/getsid_test.cpp | 4 ++++
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 9502e2aa60b34..368593a3bb7b5 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -243,8 +243,6 @@ add_entrypoint_object(
../getsid.h
DEPENDS
libc.hdr.types.pid_t
- libc.hdr.fcntl_macros
- libc.include.unistd
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
diff --git a/libc/test/src/unistd/getsid_test.cpp b/libc/test/src/unistd/getsid_test.cpp
index 6974c302508a7..c0ad6247d3824 100644
--- a/libc/test/src/unistd/getsid_test.cpp
+++ b/libc/test/src/unistd/getsid_test.cpp
@@ -14,4 +14,8 @@ TEST(LlvmLibcGetPidTest, GetCurrSID) {
pid_t sid = LIBC_NAMESPACE::getsid(0);
ASSERT_NE(sid, -1);
ASSERT_ERRNO_SUCCESS();
+
+ pid_t nonexist_sid = LIBC_NAMESPACE::getsid(999999);
+ ASSERT_EQ(nonexist_sid, -1);
+ ASSERT_ERRNO_FAILURE();
}
>From 53740a0f3e597b9ba664d61fded61753418e37ca Mon Sep 17 00:00:00 2001
From: ZakyHermawan <zaky.hermawan9615 at gmail.com>
Date: Thu, 20 Feb 2025 00:50:36 +0700
Subject: [PATCH 4/4] test: change test for failing getsid to use -1 as the
argument because calling getsid with -1 as the argument will guarantee the
getsid syscall fails
Signed-off-by: ZakyHermawan <zaky.hermawan9615 at gmail.com>
---
libc/test/src/unistd/getsid_test.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/src/unistd/getsid_test.cpp b/libc/test/src/unistd/getsid_test.cpp
index c0ad6247d3824..b3e8d54b14dcb 100644
--- a/libc/test/src/unistd/getsid_test.cpp
+++ b/libc/test/src/unistd/getsid_test.cpp
@@ -15,7 +15,7 @@ TEST(LlvmLibcGetPidTest, GetCurrSID) {
ASSERT_NE(sid, -1);
ASSERT_ERRNO_SUCCESS();
- pid_t nonexist_sid = LIBC_NAMESPACE::getsid(999999);
+ pid_t nonexist_sid = LIBC_NAMESPACE::getsid(-1);
ASSERT_EQ(nonexist_sid, -1);
ASSERT_ERRNO_FAILURE();
}
More information about the libc-commits
mailing list