1*f7c14bbaSAndroid Build Coastguard Worker // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2*f7c14bbaSAndroid Build Coastguard Worker #undef _GNU_SOURCE
3*f7c14bbaSAndroid Build Coastguard Worker #include <string.h>
4*f7c14bbaSAndroid Build Coastguard Worker #include <stdio.h>
5*f7c14bbaSAndroid Build Coastguard Worker #include <errno.h>
6*f7c14bbaSAndroid Build Coastguard Worker #include "str_error.h"
7*f7c14bbaSAndroid Build Coastguard Worker
8*f7c14bbaSAndroid Build Coastguard Worker /* make sure libbpf doesn't use kernel-only integer typedefs */
9*f7c14bbaSAndroid Build Coastguard Worker #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
10*f7c14bbaSAndroid Build Coastguard Worker
11*f7c14bbaSAndroid Build Coastguard Worker /*
12*f7c14bbaSAndroid Build Coastguard Worker * Wrapper to allow for building in non-GNU systems such as Alpine Linux's musl
13*f7c14bbaSAndroid Build Coastguard Worker * libc, while checking strerror_r() return to avoid having to check this in
14*f7c14bbaSAndroid Build Coastguard Worker * all places calling it.
15*f7c14bbaSAndroid Build Coastguard Worker */
libbpf_strerror_r(int err,char * dst,int len)16*f7c14bbaSAndroid Build Coastguard Worker char *libbpf_strerror_r(int err, char *dst, int len)
17*f7c14bbaSAndroid Build Coastguard Worker {
18*f7c14bbaSAndroid Build Coastguard Worker int ret = strerror_r(err < 0 ? -err : err, dst, len);
19*f7c14bbaSAndroid Build Coastguard Worker /* on glibc <2.13, ret == -1 and errno is set, if strerror_r() can't
20*f7c14bbaSAndroid Build Coastguard Worker * handle the error, on glibc >=2.13 *positive* (errno-like) error
21*f7c14bbaSAndroid Build Coastguard Worker * code is returned directly
22*f7c14bbaSAndroid Build Coastguard Worker */
23*f7c14bbaSAndroid Build Coastguard Worker if (ret == -1)
24*f7c14bbaSAndroid Build Coastguard Worker ret = errno;
25*f7c14bbaSAndroid Build Coastguard Worker if (ret) {
26*f7c14bbaSAndroid Build Coastguard Worker if (ret == EINVAL)
27*f7c14bbaSAndroid Build Coastguard Worker /* strerror_r() doesn't recognize this specific error */
28*f7c14bbaSAndroid Build Coastguard Worker snprintf(dst, len, "unknown error (%d)", err < 0 ? err : -err);
29*f7c14bbaSAndroid Build Coastguard Worker else
30*f7c14bbaSAndroid Build Coastguard Worker snprintf(dst, len, "ERROR: strerror_r(%d)=%d", err, ret);
31*f7c14bbaSAndroid Build Coastguard Worker }
32*f7c14bbaSAndroid Build Coastguard Worker return dst;
33*f7c14bbaSAndroid Build Coastguard Worker }
34