]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
api: Use GNU strerror_r when available
authorKhem Raj <raj.khem@gmail.com>
Fri, 13 Jan 2023 20:36:35 +0000 (13:36 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 13 Jan 2023 20:36:39 +0000 (13:36 -0700)
GNU strerror_r is only available in glibc, musl impelents the XSI
version which is slightly different, therefore check if GNU version is
available before using it, otherwise use the XSI compliant version.

Signed-off-by: Khem Raj <raj.khem@gmail.com>
Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
TJH: Minor formatting change so that the line doesn't exceed 100 chars
(cherry picked from commit 942ef655237b90909edf53eafd121842cdc07ce1)

configure.ac
src/api.c

index f667474ddcf9a9efa2dcd00984fbec2100f8f8e6..1ea331c38b98d6d4583694583bba83228cf95e0a 100644 (file)
@@ -207,6 +207,11 @@ AC_FUNC_REALLOC
 AC_FUNC_STAT
 AC_CHECK_FUNCS([getmntent hasmntopt memset mkdir rmdir strdup])
 
+orig_CFLAGS="$CFLAGS"
+CFLAGS="$CFLAGS -D_GNU_SOURCE"
+AC_FUNC_STRERROR_R
+CFLAGS="$orig_CFLAGS"
+
 AC_SEARCH_LIBS(
        [fts_open],
        [fts],
index 484b46ab43561793ef3fcd48a30ddf6f83a3ed8f..cea3efe1d0011c4100cc41838943de60977fff67 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -4672,9 +4672,14 @@ const char *cgroup_strerror(int code)
 {
        int idx = code % ECGROUPNOTCOMPILED;
 
-       if (code == ECGOTHER)
+       if (code == ECGOTHER) {
+#ifdef STRERROR_R_CHAR_P
                return strerror_r(cgroup_get_last_errno(), errtext, MAXLEN);
-
+#else
+               return strerror_r(cgroup_get_last_errno(), errtext, sizeof (errtext)) ?
+                       "unknown error" : errtext;
+#endif
+       }
        if (idx >= sizeof(cgroup_strerror_codes)/sizeof(cgroup_strerror_codes[0]))
                return "Invalid error code";