diff options
author | Arnaldo Carvalho de Melo <acme@redhat.com> | 2016-07-08 15:21:37 -0300 |
---|---|---|
committer | Steven Rostedt <rostedt@goodmis.org> | 2016-11-17 17:37:16 -0500 |
commit | cb0b0138d04fd8efea7f5a620a29a06d6fa81dc7 (patch) | |
tree | fd58a963027c2b7cdc0c6ab236a828e91205e4f8 | |
parent | 67c0816ea2bc5568e5a7f90183b1b897cade0114 (diff) | |
download | trace-cmd-cb0b0138d04fd8efea7f5a620a29a06d6fa81dc7.tar.gz |
tools lib traceevent: Use str_error_r()
To make it portable to non-glibc systems, that follow the XSI variant
instead of the GNU specific one that gets in place when _GNU_SOURCE is
defined.
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-c1gn8x978qfop65m510wy43o@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
[ Updated to have include/linux/string.h too ]
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | event-parse.c | 8 | ||||
-rw-r--r-- | include/linux/string.h | 6 | ||||
-rw-r--r-- | str_error_r.c | 26 |
4 files changed, 36 insertions, 8 deletions
@@ -226,7 +226,7 @@ export Q VERBOSE TRACECMD_VERSION = $(TC_VERSION).$(TC_PATCHLEVEL).$(TC_EXTRAVERSION) KERNELSHARK_VERSION = $(KS_VERSION).$(KS_PATCHLEVEL).$(KS_EXTRAVERSION) -INCLUDES = -I. -I $(srctree)/../../include $(CONFIG_INCLUDES) +INCLUDES = -I. -I ./include -I $(srctree)/../../include $(CONFIG_INCLUDES) include $(src)/features.mk @@ -339,7 +339,7 @@ TRACE_GRAPH_MAIN_OBJS = trace-graph-main.o $(TRACE_GRAPH_OBJS) $(TRACE_GUI_OBJS) KERNEL_SHARK_OBJS = $(TRACE_VIEW_OBJS) $(TRACE_GRAPH_OBJS) $(TRACE_GUI_OBJS) \ trace-capture.o kernel-shark.o -PEVENT_LIB_OBJS = event-parse.o trace-seq.o parse-filter.o parse-utils.o +PEVENT_LIB_OBJS = event-parse.o trace-seq.o parse-filter.o parse-utils.o str_error_r.o TCMD_LIB_OBJS = $(PEVENT_LIB_OBJS) trace-util.o trace-input.o trace-ftrace.o \ trace-output.o trace-record.o trace-recorder.o \ trace-restore.o trace-usage.o trace-blk-hack.o \ diff --git a/event-parse.c b/event-parse.c index 82400ead..d6ca5225 100644 --- a/event-parse.c +++ b/event-parse.c @@ -31,6 +31,7 @@ #include <errno.h> #include <stdint.h> #include <limits.h> +#include <linux/string.h> #include <netinet/ip6.h> #include "event-parse.h" @@ -6157,12 +6158,7 @@ int pevent_strerror(struct pevent *pevent __maybe_unused, const char *msg; if (errnum >= 0) { - msg = strerror_r(errnum, buf, buflen); - if (msg != buf) { - size_t len = strlen(msg); - memcpy(buf, msg, min(buflen - 1, len)); - *(buf + min(buflen - 1, len)) = '\0'; - } + str_error_r(errnum, buf, buflen); return 0; } diff --git a/include/linux/string.h b/include/linux/string.h new file mode 100644 index 00000000..8052d80f --- /dev/null +++ b/include/linux/string.h @@ -0,0 +1,6 @@ +#ifndef __LINUX_STRING_H_ +#define __LINUX_STRING_H_ + +char *str_error_r(int errnum, char *buf, size_t buflen); + +#endif diff --git a/str_error_r.c b/str_error_r.c new file mode 100644 index 00000000..503ae072 --- /dev/null +++ b/str_error_r.c @@ -0,0 +1,26 @@ +#undef _GNU_SOURCE +#include <string.h> +#include <stdio.h> +#include <linux/string.h> + +/* + * 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. + */ +char *str_error_r(int errnum, char *buf, size_t buflen) +{ + int err = strerror_r(errnum, buf, buflen); + if (err) + snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, %p, %zd)=%d", errnum, buf, buflen, err); + return buf; +} |