brintos

brintos / linux-shallow public Read only

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