[libc-commits] [libc] 7fb96fb - [libc] Add implementation of POSIX "uname" function.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Fri Sep 16 14:21:40 PDT 2022


Author: Siva Chandra Reddy
Date: 2022-09-16T21:21:29Z
New Revision: 7fb96fb5d33ee55fa5b65497c6074086f43babd2

URL: https://github.com/llvm/llvm-project/commit/7fb96fb5d33ee55fa5b65497c6074086f43babd2
DIFF: https://github.com/llvm/llvm-project/commit/7fb96fb5d33ee55fa5b65497c6074086f43babd2.diff

LOG: [libc] Add implementation of POSIX "uname" function.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D134065

Added: 
    libc/include/llvm-libc-types/struct_utsname.h
    libc/include/sys/utsname.h.def
    libc/src/sys/utsname/CMakeLists.txt
    libc/src/sys/utsname/linux/CMakeLists.txt
    libc/src/sys/utsname/linux/uname.cpp
    libc/src/sys/utsname/uname.h
    libc/test/src/sys/utsname/CMakeLists.txt
    libc/test/src/sys/utsname/uname_test.cpp

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/api.td
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/CMakeLists.txt
    libc/include/llvm-libc-types/CMakeLists.txt
    libc/spec/posix.td
    libc/src/sys/CMakeLists.txt
    libc/test/src/sys/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 5e2f2f47196d3..9fc201b389a53 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -111,6 +111,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.stat.mkdir
     libc.src.sys.stat.mkdirat
 
+    # sys/utsname.h entrypoints
+    libc.src.sys.utsname.uname
+
     # unistd.h entrypoints
     libc.src.unistd.access
     libc.src.unistd.chdir

diff  --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index 32f18945eaa0c..463f40fe220e9 100644
--- a/libc/config/linux/api.td
+++ b/libc/config/linux/api.td
@@ -282,3 +282,7 @@ def SysStatAPI : PublicAPI<"sys/stat.h"> {
 def SysSendfileAPI : PublicAPI<"sys/sendfile.h"> {
   let Types = ["off_t", "size_t", "ssize_t"];
 }
+
+def SysUtsNameAPI : PublicAPI<"sys/utsname.h"> {
+  let Types = ["struct utsname"];
+}

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 750670e25a80a..8e85b56014d61 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -115,6 +115,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.stat.mkdir
     libc.src.sys.stat.mkdirat
 
+    # sys/utsname.h entrypoints
+    libc.src.sys.utsname.uname
+
     # unistd.h entrypoints
     libc.src.unistd.access
     libc.src.unistd.chdir

diff  --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index fdaf88b0efb29..f9aefb76ac4ae 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -244,6 +244,15 @@ add_gen_header(
     ../config/${LIBC_TARGET_OS}/syscall_numbers.h.inc
 )
 
+add_gen_header(
+  sys_utsname
+  DEF_FILE sys/utsname.h.def
+  GEN_HDR sys/utsname.h
+  DEPENDS
+    .llvm_libc_common_h
+    .llvm-libc-types.struct_utsname
+)
+
 if(NOT LLVM_LIBC_FULL_BUILD)
   # We don't install headers in non-fullbuild mode.
   return()

diff  --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index f1081ddc91acc..79d969d634160 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -37,6 +37,7 @@ add_header(ssize_t HDR ssize_t.h)
 add_header(struct_dirent HDR struct_dirent.h DEPENDS .ino_t .off_t)
 add_header(struct_sigaction HDR struct_sigaction.h)
 add_header(struct_tm HDR struct_tm.h)
+add_header(struct_utsname HDR struct_utsname.h)
 add_header(thrd_start_t HDR thrd_start_t.h)
 add_header(thrd_t HDR thrd_t.h DEPENDS .__thread_type)
 add_header(time_t HDR time_t.h)

diff  --git a/libc/include/llvm-libc-types/struct_utsname.h b/libc/include/llvm-libc-types/struct_utsname.h
new file mode 100644
index 0000000000000..8d41938ab6932
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_utsname.h
@@ -0,0 +1,32 @@
+//===-- Definition of struct utsname --------------------------------------===//
+//
+// 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_UTSNAME_H__
+#define __LLVM_LIBC_TYPES_STRUCT_UTSNAME_H__
+
+#ifdef __unix__
+#define __UTS_NAME_LENGTH 65
+#else
+// Arbitray default. Should be specialized for each platform.
+#define __UTS_NAME_LENGTH 1024
+#endif
+
+struct utsname {
+  char sysname[__UTS_NAME_LENGTH];
+  char nodename[__UTS_NAME_LENGTH];
+  char release[__UTS_NAME_LENGTH];
+  char version[__UTS_NAME_LENGTH];
+  char machine[__UTS_NAME_LENGTH];
+#ifdef __unix__
+  char domainname[__UTS_NAME_LENGTH];
+#endif
+};
+
+#undef __UTS_NAME_LENGTH
+
+#endif // __LLVM_LIBC_TYPES_STRUCT_UTSNAME_H__

diff  --git a/libc/include/sys/utsname.h.def b/libc/include/sys/utsname.h.def
new file mode 100644
index 0000000000000..6d7daeb45f018
--- /dev/null
+++ b/libc/include/sys/utsname.h.def
@@ -0,0 +1,16 @@
+//===-- Linux sys/utsname.h -----------------------------------------------===//
+//
+// 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_SYS_UTSNAME_H
+#define LLVM_LIBC_SYS_UTSNAME_H
+
+#include <__llvm-libc-common.h>
+
+%%public_api()
+
+#endif // LLVM_LIBC_SYS_UTSNAME_H

diff  --git a/libc/spec/posix.td b/libc/spec/posix.td
index 244e88a80d311..1f3c10ebe9b6c 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -522,6 +522,22 @@ def POSIX : StandardSpec<"POSIX"> {
     ]
   >;
 
+  NamedType StructUtsName = NamedType<"struct utsname">;
+  PtrType StructUtsNamePtr = PtrType<StructUtsName>;
+  HeaderSpec SysUtsName = HeaderSpec<
+    "sys/utsname.h",
+    [], // Macros
+    [StructUtsName], // Types
+    [], // Enumerations
+    [
+        FunctionSpec<
+            "uname",
+            RetValSpec<IntType>,
+            [ArgSpec<StructUtsNamePtr>]
+        >,
+    ]
+  >;
+
   HeaderSpec PThread = HeaderSpec<
     "pthread.h",
     [], // Macros
@@ -789,6 +805,7 @@ def POSIX : StandardSpec<"POSIX"> {
     SysMMan,
     SysResource,
     SysStat,
+    SysUtsName,
     UniStd,
     String
   ];

diff  --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index 5239aa84da46f..7e6ab887b3107 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -2,3 +2,4 @@ add_subdirectory(mman)
 add_subdirectory(resource)
 add_subdirectory(sendfile)
 add_subdirectory(stat)
+add_subdirectory(utsname)

diff  --git a/libc/src/sys/utsname/CMakeLists.txt b/libc/src/sys/utsname/CMakeLists.txt
new file mode 100644
index 0000000000000..c72e68e7ed0b1
--- /dev/null
+++ b/libc/src/sys/utsname/CMakeLists.txt
@@ -0,0 +1,10 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+  uname
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.uname
+)

diff  --git a/libc/src/sys/utsname/linux/CMakeLists.txt b/libc/src/sys/utsname/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..3e3ccc2ea29fd
--- /dev/null
+++ b/libc/src/sys/utsname/linux/CMakeLists.txt
@@ -0,0 +1,13 @@
+add_entrypoint_object(
+  uname
+  SRCS
+    uname.cpp
+  HDRS
+    ../uname.h
+  DEPENDS
+    libc.include.sys_utsname
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
+

diff  --git a/libc/src/sys/utsname/linux/uname.cpp b/libc/src/sys/utsname/linux/uname.cpp
new file mode 100644
index 0000000000000..42bdf2544913b
--- /dev/null
+++ b/libc/src/sys/utsname/linux/uname.cpp
@@ -0,0 +1,29 @@
+//===-- Linux implementation of uname -------------------------------------===//
+//
+// 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/utsname/uname.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+
+#include <errno.h>
+#include <sys/syscall.h> // For syscall numbers.
+#include <sys/utsname.h>
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, uname, (struct utsname * name)) {
+  long ret = __llvm_libc::syscall(SYS_uname, name);
+
+  if (ret >= 0)
+    return 1;
+  errno = -ret;
+  return -1;
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/sys/utsname/uname.h b/libc/src/sys/utsname/uname.h
new file mode 100644
index 0000000000000..80c748aefe5c0
--- /dev/null
+++ b/libc/src/sys/utsname/uname.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for uname -------------------------*- 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_SYS_UTSNAME_UNAME_H
+#define LLVM_LIBC_SRC_SYS_UTSNAME_UNAME_H
+
+#include <sys/utsname.h>
+
+namespace __llvm_libc {
+
+int uname(struct utsname *name);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_SYS_UTSNAME_UNAME_H

diff  --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt
index 5239aa84da46f..7e6ab887b3107 100644
--- a/libc/test/src/sys/CMakeLists.txt
+++ b/libc/test/src/sys/CMakeLists.txt
@@ -2,3 +2,4 @@ add_subdirectory(mman)
 add_subdirectory(resource)
 add_subdirectory(sendfile)
 add_subdirectory(stat)
+add_subdirectory(utsname)

diff  --git a/libc/test/src/sys/utsname/CMakeLists.txt b/libc/test/src/sys/utsname/CMakeLists.txt
new file mode 100644
index 0000000000000..a459820c86ea1
--- /dev/null
+++ b/libc/test/src/sys/utsname/CMakeLists.txt
@@ -0,0 +1,15 @@
+add_libc_testsuite(libc_sys_utsname_unittests)
+
+add_libc_unittest(
+  uname_test
+  SUITE
+    libc_sys_utsname_unittests
+  SRCS
+    uname_test.cpp
+  DEPENDS
+    libc.include.errno
+    libc.include.sys_utsname
+    libc.src.__support.common
+    libc.src.sys.utsname.uname
+    libc.test.errno_setter_matcher
+)

diff  --git a/libc/test/src/sys/utsname/uname_test.cpp b/libc/test/src/sys/utsname/uname_test.cpp
new file mode 100644
index 0000000000000..1a1e4a77dae00
--- /dev/null
+++ b/libc/test/src/sys/utsname/uname_test.cpp
@@ -0,0 +1,26 @@
+//===-- Unittests for uname -----------------------------------------------===//
+//
+// 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/__support/CPP/string_view.h"
+#include "src/__support/architectures.h"
+#include "src/sys/utsname/uname.h"
+#include "test/ErrnoSetterMatcher.h"
+#include "utils/UnitTest/Test.h"
+
+#include <errno.h>
+#include <sys/utsname.h>
+
+TEST(LlvmLibcUnameTest, GetMachineName) {
+  struct utsname names;
+  ASSERT_GE(__llvm_libc::uname(&names), 0);
+#ifdef LLVM_LIBC_ARCH_X86_64
+  ASSERT_STREQ(names.machine, "x86_64");
+#elif defined(LLVM_LIBC_ARCH_AARCH64)
+  ASSERT_STREQ(names.machine, "aarch64");
+#endif
+}


        


More information about the libc-commits mailing list