diff mbox series

[2/3] Support: Add linux-support.h

Message ID 20240712223147.1809816-3-hjl.tools@gmail.com
State New
Headers show
Series linux: Update the mremap C implementation [BZ #31968] | expand

Commit Message

H.J. Lu July 12, 2024, 10:31 p.m. UTC
Add linux-support.h with

/* Return an unsigned int from version, patch level and sub level of
   Linux kernel.  */
extern unsigned int make_linux_kernel_version (unsigned int __version,
					       unsigned int __patch,
					       unsigned int __sub);

/* Similar to make_linux_kernel_version, but version, patch level and
   sub level are retrieved from the uname system call.  */
extern unsigned int get_linux_kernel_version (void);

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 sysdeps/unix/sysv/linux/Makefile              |  8 +++
 .../unix/sysv/linux/linux-kernel-version.c    | 62 +++++++++++++++++++
 sysdeps/unix/sysv/linux/linux-support.h       | 38 ++++++++++++
 3 files changed, 108 insertions(+)
 create mode 100644 sysdeps/unix/sysv/linux/linux-kernel-version.c
 create mode 100644 sysdeps/unix/sysv/linux/linux-support.h
diff mbox series

Patch

diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 097b5a26fc..7a1798f2f1 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -661,3 +661,11 @@  tests-internal += \
   tst-rseq-nptl \
   # tests-internal
 endif
+
+ifeq ($(subdir),support)
+linux-support-routines += \
+  linux-kernel-version \
+# linux-support-routines
+libsupport-routines += $(linux-support-routines)
+libsupport-static-only-routines += $(linux-support-routines)
+endif
diff --git a/sysdeps/unix/sysv/linux/linux-kernel-version.c b/sysdeps/unix/sysv/linux/linux-kernel-version.c
new file mode 100644
index 0000000000..2968c528b6
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/linux-kernel-version.c
@@ -0,0 +1,62 @@ 
+/* Initialize CPU feature data.  AArch64 version.
+   This file is part of the GNU C Library.
+   Copyright (C) 2017-2024 Free Software Foundation, Inc.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sys/utsname.h>
+#include <support/check.h>
+#include <linux-support.h>
+
+unsigned int
+make_linux_kernel_version (unsigned int version, unsigned int patch,
+			   unsigned int sub)
+{
+  TEST_VERIFY_EXIT (version <= 0xff);
+  TEST_VERIFY_EXIT (patch <= 0xff);
+  TEST_VERIFY_EXIT (sub <= 0xffff);
+  /* Return an unsigned int with VVPPSSSS.  */
+  return (version << 24) | (patch << 16) | sub;
+}
+
+unsigned int
+get_linux_kernel_version (void)
+{
+  struct utsname buf;
+  const char *p = &buf.release[0];
+  unsigned int version;
+  unsigned int patch;
+  unsigned int sub;
+
+  TEST_VERIFY_EXIT (uname (&buf) == 0);
+
+  /* Get kernel version.  */
+  for (version = 0; *p >= '0' && *p <= '9'; p++)
+    version = version * 10 + *p - '0';
+  TEST_VERIFY_EXIT (*p == '.');
+  p++;
+
+  /* Get kernel patch level.  */
+  for (patch = 0; *p >= '0' && *p <= '9'; p++)
+    patch = patch * 10 + *p - '0';
+  TEST_VERIFY_EXIT (*p == '.');
+  p++;
+
+  /* Get kernel sub level.  */
+  for (sub = 0; *p >= '0' && *p <= '9'; p++)
+    sub = sub * 10 + *p - '0';
+
+  return make_linux_kernel_version (version, patch, sub);
+}
diff --git a/sysdeps/unix/sysv/linux/linux-support.h b/sysdeps/unix/sysv/linux/linux-support.h
new file mode 100644
index 0000000000..a7c03040b1
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/linux-support.h
@@ -0,0 +1,38 @@ 
+/* Linux-specific support functions.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef LINUX_SUPPORT_H
+#define LINUX_SUPPORT_H
+
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
+/* Return an unsigned int from version, patch level and sub level of
+   Linux kernel.  */
+extern unsigned int make_linux_kernel_version (unsigned int __version,
+					       unsigned int __patch,
+					       unsigned int __sub);
+
+/* Similar to make_linux_kernel_version, but version, patch level and
+   sub level are retrieved from the uname system call.  */
+extern unsigned int get_linux_kernel_version (void);
+
+__END_DECLS
+
+#endif /* LINUX_SUPPORT_H */