]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
errno-list: prefer strerrorname_np() as errno_to_name() provider
authorMike Yuan <me@yhndnzj.com>
Tue, 31 Dec 2024 01:46:28 +0000 (02:46 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 2 Jan 2025 11:01:53 +0000 (12:01 +0100)
TODO
meson.build
src/basic/errno-list.c
src/basic/errno-list.h
src/test/test-errno-list.c

diff --git a/TODO b/TODO
index cd8be425e79fc7d638af0799615e8c716b40beb6..a7b6a9d847f16a4a5c2c43c1fa8039111bd9e484 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1571,7 +1571,7 @@ Features:
 * add a new flag to chase() that stops chasing once the first missing
   component is found and then allows the caller to create the rest.
 
-* make use of new glibc 2.32 APIs sigabbrev_np() and strerrorname_np().
+* make use of new glibc 2.32 APIs sigabbrev_np().
 
 * if /usr/bin/swapoff fails due to OOM, log a friendly explanatory message about it
 
index efdf0501b32d9937078be0530fd3bd3db1f42add..6b81203168b3d740c4a8a1e3d6090771585d0fce 100644 (file)
@@ -675,6 +675,7 @@ foreach ident : [
         ['fsmount',           '''#include <sys/mount.h>'''],
         ['getdents64',        '''#include <dirent.h>'''],
         ['pidfd_spawn',       '''#include <spawn.h>'''],
+        ['strerrorname_np',   '''#include <string.h>'''],
 ]
 
         have = cc.has_function(ident[0], prefix : ident[1], args : '-D_GNU_SOURCE')
index ba0f7c162839b81d4dc120d656aea69d0dacabea..897f63030749774ea109908498288c6bf9365222 100644 (file)
@@ -10,6 +10,8 @@ static const struct errno_name* lookup_errno(register const char *str,
                                              register GPERF_LEN_TYPE len);
 
 #include "errno-from-name.h"
+
+#if !HAVE_STRERRORNAME_NP
 #include "errno-to-name.h"
 
 const char* errno_to_name(int id) {
@@ -22,6 +24,7 @@ const char* errno_to_name(int id) {
 
         return errno_names[id];
 }
+#endif
 
 int errno_from_name(const char *name) {
         const struct errno_name *sc;
index 81c193df12e6f9ef5a9c46a6b290178dbdf45bfd..3c15afdaab68d5cce29b9f9974f53d1eb25ccc1d 100644 (file)
@@ -2,14 +2,28 @@
 #pragma once
 
 #include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+
 /*
  * MAX_ERRNO is defined as 4095 in linux/err.h
  * We use the same value here.
  */
 #define ERRNO_MAX 4095
 
+#if HAVE_STRERRORNAME_NP
+static inline const char* errno_to_name(int id) {
+        if (id == 0) /* To stay in line with our own impl */
+                return NULL;
+
+        return strerrorname_np(abs(id));
+}
+#else
 const char* errno_to_name(int id);
+#endif
+
 int errno_from_name(const char *name);
+
 static inline bool errno_is_valid(int n) {
         return n > 0 && n <= ERRNO_MAX;
 }
index 77f418750cd4de15203245a72b9d7d909b60842e..032cf3bb11d0f03656959155e70008390f757d90 100644 (file)
@@ -9,6 +9,9 @@
 #include "tests.h"
 
 TEST(errno_list) {
+        ASSERT_NULL(errno_names[0]);
+        ASSERT_NULL(errno_to_name(0));
+
         for (size_t i = 0; i < ELEMENTSOF(errno_names); i++) {
                 if (errno_names[i]) {
                         ASSERT_STREQ(errno_to_name(i), errno_names[i]);