Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c8b5f2c9 authored by Arnaldo Carvalho de Melo's avatar Arnaldo Carvalho de Melo
Browse files

tools: Introduce str_error_r()

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.

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-d4t42fnf48ytlk8rjxs822tf@git.kernel.org


Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent ffe3a28a
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -12,4 +12,6 @@ int strtobool(const char *s, bool *res);
extern size_t strlcpy(char *dest, const char *src, size_t size);
extern size_t strlcpy(char *dest, const char *src, size_t size);
#endif
#endif


char *str_error_r(int errnum, char *buf, size_t buflen);

#endif /* _LINUX_STRING_H_ */
#endif /* _LINUX_STRING_H_ */
+26 −0
Original line number Original line Diff line number Diff line
#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;
}
+1 −0
Original line number Original line Diff line number Diff line
@@ -29,6 +29,7 @@ tools/lib/symbol/kallsyms.c
tools/lib/symbol/kallsyms.h
tools/lib/symbol/kallsyms.h
tools/lib/find_bit.c
tools/lib/find_bit.c
tools/lib/bitmap.c
tools/lib/bitmap.c
tools/lib/str_error_r.c
tools/include/asm/atomic.h
tools/include/asm/atomic.h
tools/include/asm/barrier.h
tools/include/asm/barrier.h
tools/include/asm/bug.h
tools/include/asm/bug.h
+2 −2
Original line number Original line Diff line number Diff line
@@ -111,14 +111,14 @@ static int __test__rdpmc(void)
	if (fd < 0) {
	if (fd < 0) {
		pr_err("Error: sys_perf_event_open() syscall returned "
		pr_err("Error: sys_perf_event_open() syscall returned "
		       "with %d (%s)\n", fd,
		       "with %d (%s)\n", fd,
		       strerror_r(errno, sbuf, sizeof(sbuf)));
		       str_error_r(errno, sbuf, sizeof(sbuf)));
		return -1;
		return -1;
	}
	}


	addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
	addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
	if (addr == (void *)(-1)) {
	if (addr == (void *)(-1)) {
		pr_err("Error: mmap() syscall returned with (%s)\n",
		pr_err("Error: mmap() syscall returned with (%s)\n",
		       strerror_r(errno, sbuf, sizeof(sbuf)));
		       str_error_r(errno, sbuf, sizeof(sbuf)));
		goto out_close;
		goto out_close;
	}
	}


+4 −4
Original line number Original line Diff line number Diff line
@@ -351,7 +351,7 @@ int cmd_buildid_cache(int argc, const char **argv,
						continue;
						continue;
					}
					}
					pr_warning("Couldn't add %s: %s\n",
					pr_warning("Couldn't add %s: %s\n",
						   pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
						   pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
				}
				}


			strlist__delete(list);
			strlist__delete(list);
@@ -369,7 +369,7 @@ int cmd_buildid_cache(int argc, const char **argv,
						continue;
						continue;
					}
					}
					pr_warning("Couldn't remove %s: %s\n",
					pr_warning("Couldn't remove %s: %s\n",
						   pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
						   pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
				}
				}


			strlist__delete(list);
			strlist__delete(list);
@@ -387,7 +387,7 @@ int cmd_buildid_cache(int argc, const char **argv,
						continue;
						continue;
					}
					}
					pr_warning("Couldn't remove %s: %s\n",
					pr_warning("Couldn't remove %s: %s\n",
						   pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
						   pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
				}
				}


			strlist__delete(list);
			strlist__delete(list);
@@ -408,7 +408,7 @@ int cmd_buildid_cache(int argc, const char **argv,
						continue;
						continue;
					}
					}
					pr_warning("Couldn't update %s: %s\n",
					pr_warning("Couldn't update %s: %s\n",
						   pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
						   pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
				}
				}


			strlist__delete(list);
			strlist__delete(list);
Loading