From: Mike Yuan Date: Tue, 31 Dec 2024 01:46:28 +0000 (+0100) Subject: errno-list: prefer strerrorname_np() as errno_to_name() provider X-Git-Tag: v258-rc1~1758 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=03ccee19396ae604d9ac1570dddb8b41ec0e7253;p=thirdparty%2Fsystemd.git errno-list: prefer strerrorname_np() as errno_to_name() provider --- diff --git a/TODO b/TODO index cd8be425e79..a7b6a9d847f 100644 --- 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 diff --git a/meson.build b/meson.build index efdf0501b32..6b81203168b 100644 --- a/meson.build +++ b/meson.build @@ -675,6 +675,7 @@ foreach ident : [ ['fsmount', '''#include '''], ['getdents64', '''#include '''], ['pidfd_spawn', '''#include '''], + ['strerrorname_np', '''#include '''], ] have = cc.has_function(ident[0], prefix : ident[1], args : '-D_GNU_SOURCE') diff --git a/src/basic/errno-list.c b/src/basic/errno-list.c index ba0f7c16283..897f6303074 100644 --- a/src/basic/errno-list.c +++ b/src/basic/errno-list.c @@ -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; diff --git a/src/basic/errno-list.h b/src/basic/errno-list.h index 81c193df12e..3c15afdaab6 100644 --- a/src/basic/errno-list.h +++ b/src/basic/errno-list.h @@ -2,14 +2,28 @@ #pragma once #include +#include +#include + /* * 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; } diff --git a/src/test/test-errno-list.c b/src/test/test-errno-list.c index 77f418750cd..032cf3bb11d 100644 --- a/src/test/test-errno-list.c +++ b/src/test/test-errno-list.c @@ -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]);