OpenCloudOS-Kernel/tools/lib/traceevent/tep_strerror.c

54 lines
1.4 KiB
C
Raw Normal View History

tools lib traceevent: Separate out tep_strerror() for strerror_r() issues While working on having PowerTop use libtracevent as a shared object library, Tzvetomir hit "str_error_r not defined". This was added by commit c3cec9e68f12d ("tools lib traceevent: Use str_error_r()") because strerror_r() has two definitions, where one is GNU specific, and the other is XSI complient. The strerror_r() is in a wrapper str_error_r() to keep the code from having to worry about which compiler is being used. The problem is that str_error_r() is external to libtraceevent, and not part of the library. If it is used as a shared object then the tools using it will need to define that function. I do not want that function defined in libtraceevent itself, as it is out of scope for that library. As there's only a single instance of this call, and its in the traceevent library's own tep_strerror() function, we can copy what was done in perf, and create yet another external file that undefs _GNU_SOURCE to use the more portable version of the function. We don't need to worry about the errors that strerror_r() returns. If the buffer isn't big enough, we simply truncate it. Reported-by: Tzvetomir Stoyanov <tstoyanov@vmware.com> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com> Cc: linux trace devel <linux-trace-devel@vger.kernel.org> Link: http://lkml.kernel.org/r/20181005121816.484e654f@gandalf.local.home Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2018-10-06 00:18:16 +08:00
// SPDX-License-Identifier: LGPL-2.1
#undef _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include "event-parse.h"
#undef _PE
#define _PE(code, str) str
static const char * const tep_error_str[] = {
TEP_ERRORS
};
#undef _PE
/*
* The tools so far have been using the strerror_r() GNU variant, that returns
* a string, be it the buffer passed or something else.
*
* But that, besides being tricky in cases where we expect that the function
* using strerror_r() returns the error formatted in a provided buffer (we have
* to check if it returned something else and copy that instead), breaks the
* build on systems not using glibc, like Alpine Linux, where musl libc is
* used.
*
* So, introduce yet another wrapper, str_error_r(), that has the GNU
* interface, but uses the portable XSI variant of strerror_r(), so that users
* rest asured that the provided buffer is used and it is what is returned.
*/
int tep_strerror(struct tep_handle *tep __maybe_unused,
enum tep_errno errnum, char *buf, size_t buflen)
{
const char *msg;
int idx;
if (!buflen)
return 0;
if (errnum >= 0) {
int err = strerror_r(errnum, buf, buflen);
buf[buflen - 1] = 0;
return err;
}
if (errnum <= __TEP_ERRNO__START ||
errnum >= __TEP_ERRNO__END)
return -1;
idx = errnum - __TEP_ERRNO__START - 1;
msg = tep_error_str[idx];
snprintf(buf, buflen, "%s", msg);
return 0;
}