Patrick Williams | da29531 | 2023-12-05 16:48:56 -0600 | [diff] [blame] | 1 | From f629a76e0fba300a9d511614160fee38dd4a5e57 Mon Sep 17 00:00:00 2001 |
| 2 | From: Khem Raj <raj.khem@gmail.com> |
| 3 | Date: Mon, 23 Jan 2023 23:39:46 -0800 |
| 4 | Subject: [PATCH] errno-util: Make STRERROR portable for musl |
| 5 | |
| 6 | Sadly, systemd has decided to use yet another GNU extention in a macro |
| 7 | lets make this such that we can use XSI compliant strerror_r() for |
| 8 | non-glibc hosts |
| 9 | |
| 10 | Upstream-Status: Inappropriate [musl specific] |
| 11 | |
| 12 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 13 | --- |
| 14 | src/basic/errno-util.h | 10 +++++++++- |
| 15 | 1 file changed, 9 insertions(+), 1 deletion(-) |
| 16 | |
| 17 | diff --git a/src/basic/errno-util.h b/src/basic/errno-util.h |
| 18 | index 091f99c590..eb5c1f9961 100644 |
| 19 | --- a/src/basic/errno-util.h |
| 20 | +++ b/src/basic/errno-util.h |
| 21 | @@ -14,8 +14,16 @@ |
| 22 | * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks |
| 23 | * |
| 24 | * Note that we use the GNU variant of strerror_r() here. */ |
| 25 | -#define STRERROR(errnum) strerror_r(abs(errnum), (char[ERRNO_BUF_LEN]){}, ERRNO_BUF_LEN) |
| 26 | +static inline const char * STRERROR(int errnum); |
| 27 | |
| 28 | +static inline const char * STRERROR(int errnum) { |
| 29 | +#ifdef __GLIBC__ |
| 30 | + return strerror_r(abs(errnum), (char[ERRNO_BUF_LEN]){}, ERRNO_BUF_LEN); |
| 31 | +#else |
| 32 | + static __thread char buf[ERRNO_BUF_LEN]; |
| 33 | + return strerror_r(abs(errnum), buf, ERRNO_BUF_LEN) ? "unknown error" : buf; |
| 34 | +#endif |
| 35 | +} |
| 36 | /* A helper to print an error message or message for functions that return 0 on EOF. |
| 37 | * Note that we can't use ({ … }) to define a temporary variable, so errnum is |
| 38 | * evaluated twice. */ |
| 39 | -- |
| 40 | 2.39.2 |
| 41 | |