Make die_perror() more similar to die() by allowing the user to specify a format string and arguments. Signed-off-by: Alexandru Elisei --- include/kvm/util.h | 2 +- util/util.c | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/include/kvm/util.h b/include/kvm/util.h index ac8515f8c46b..a6dba1147d68 100644 --- a/include/kvm/util.h +++ b/include/kvm/util.h @@ -41,7 +41,7 @@ #define MAP_ANON_NORESERVE (MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE) extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); -extern void die_perror(const char *s) NORETURN; +extern void die_perror(const char *fmt, ...) NORETURN __attribute__((format (printf, 1, 2))); extern void pr_err(const char *err, ...) __attribute__((format (printf, 1, 2))); extern void pr_warning(const char *err, ...) __attribute__((format (printf, 1, 2))); extern void pr_info(const char *err, ...) __attribute__((format (printf, 1, 2))); diff --git a/util/util.c b/util/util.c index fc732200f2dc..0f116e0ac459 100644 --- a/util/util.c +++ b/util/util.c @@ -1,6 +1,7 @@ /* * Taken from perf which in turn take it from GIT */ +#include #include "kvm/util.h" @@ -98,11 +99,23 @@ void __pr_debug(const char *debug, ...) va_end(params); } -void die_perror(const char *s) +void die_perror(const char *fmt, ...) { + char buf[1024]; + va_list params; int e = errno; + int ret; + + va_start(params, fmt); + ret = vsnprintf(buf, sizeof(buf), fmt, params); + if (ret < 0) { + e = errno; + strncpy(buf, "vsnprintf", ARRAY_SIZE(buf) - 1); + } + va_end(params); - perror(s); + errno = e; + perror(buf); exit(e); } -- 2.53.0