Skip to contentSkip to conversation

tools/bpf: make libbpf _GNU_SOURCE friendly

PATCH Superseded Yonghong Song 2 participants 3 messages Nov 30, 2018

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/bpf: make libbpf _GNU_SOURCE friendly

During porting libbpf to bcc, I got some warnings like below:
...
[ 2%] Building C object src/cc/CMakeFiles/bpf-shared.dir/libbpf/src/libbpf.c.o /home/yhs/work/bcc2/src/cc/libbpf/src/libbpf.c:12:0:
warning: "_GNU_SOURCE" redefined [enabled by default]
#define _GNU_SOURCE
...
[ 3%] Building C object src/cc/CMakeFiles/bpf-shared.dir/libbpf/src/libbpf_errno.c.o /home/yhs/work/bcc2/src/cc/libbpf/src/libbpf_errno.c: In function ‘libbpf_strerror’:
/home/yhs/work/bcc2/src/cc/libbpf/src/libbpf_errno.c:45:7:
warning: assignment makes integer from pointer without a cast [enabled by default]

     ret = strerror_r(err, buf, size);

...

bcc is built with _GNU_SOURCE defined and this caused the above warning. This patch intends to make libpf _GNU_SOURCE friendly by
. define _GNU_SOURCE unless it is not defined
. gnu version strerror_r has different return value than non-gnu version,
so strerror_r return value is handled differently if _GNU_SOURCE is defined.

---
 tools/lib/bpf/libbpf.c       |  2 ++
 tools/lib/bpf/libbpf_errno.c | 10 ++++++++++
 2 files changed, 12 insertions(+)
tools/lib/bpf/libbpf.c +2 -0
index ed4212a4c5f9..59b748ebd15f 100644
@@ -9,7 +9,9 @@
  * Copyright (C) 2017 Nicira, Inc.
  */
 
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE
+#endif
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdarg.h>
tools/lib/bpf/libbpf_errno.c +10 -0
index d83b17f8435c..286e497c50ec 100644
@@ -40,9 +40,19 @@ int libbpf_strerror(int err, char *buf, size_t size)
 	err = err > 0 ? err : -err;
 
 	if (err < __LIBBPF_ERRNO__START) {
+#ifdef _GNU_SOURCE
+		const char *ret_buf;
+#endif
 		int ret;
 
+#ifdef _GNU_SOURCE
+		ret_buf = strerror_r(err, buf, size);
+		if (ret_buf != buf)
+			snprintf(buf, size, "%s", ret_buf);
+		ret = 0;
+#else
 		ret = strerror_r(err, buf, size);
+#endif
 		buf[size - 1] = '\0';
 		return ret;
 	}
Signature · 1 line
2.17.1
#1 lore raw
Quoted 14 lines · Yonghong Song
tools/lib/bpf/libbpf_errno.c +4 -0
index d83b17f8435c..286e497c50ec 100644
@@ -40,9 +40,19 @@ int libbpf_strerror(int err, char *buf, size_t size)
 	err = err > 0 ? err : -err;
 
 	if (err < __LIBBPF_ERRNO__START) {
+#ifdef _GNU_SOURCE
+		const char *ret_buf;
+#endif
 		int ret;
 
+#ifdef _GNU_SOURCE

tools/lib/bpf/libbpf_errno.c int libbpf_strerror(int err, char *buf, size_t size) · line 58

Quoted 10 lines
tools/lib/bpf/libbpf_errno.c +6 -0
+		ret_buf = strerror_r(err, buf, size);
+		if (ret_buf != buf)
+			snprintf(buf, size, "%s", ret_buf);
+		ret = 0;
+#else
 		ret = strerror_r(err, buf, size);
+#endif
 		buf[size - 1] = '\0';
 		return ret;
 	}

That is kinda strange, the whole point for this file was to have non-GNU strerror_r, would doing #undef _GNU_SOURCE at the top not work?

#2 lore raw
Quoted 14 lines · Jakub Kicinski
Quoted 13 lines · Yonghong Song
tools/lib/bpf/libbpf_errno.c +3 -0
index d83b17f8435c..286e497c50ec 100644
@@ -40,9 +40,19 @@ int libbpf_strerror(int err, char *buf, size_t size)
  	err = err > 0 ? err : -err;
 
  	if (err < __LIBBPF_ERRNO__START) {
+#ifdef _GNU_SOURCE
+		const char *ret_buf;
+#endif
  		int ret;
 
Quoted 14 lines

tools/lib/bpf/libbpf_errno.c int libbpf_strerror(int err, char *buf, size_t size) · line 58

Quoted 11 lines
tools/lib/bpf/libbpf_errno.c +7 -0
+#ifdef _GNU_SOURCE
+		ret_buf = strerror_r(err, buf, size);
+		if (ret_buf != buf)
+			snprintf(buf, size, "%s", ret_buf);
+		ret = 0;
+#else
  		ret = strerror_r(err, buf, size);
+#endif
  		buf[size - 1] = '\0';
  		return ret;
  	}

That is kinda strange, the whole point for this file was to have non-GNU strerror_r, would doing #undef _GNU_SOURCE at the top not work?

Thanks for suggestion as I did not the history of this file. Yes, #undef _GNU_SOURCE works. Will send a revision shortly.