From ea0761b13a79ce1dd11805e451626dd67d8b2550 Mon Sep 17 00:00:00 2001 From: Tokunori Ikegami Date: Sun, 30 Mar 2025 19:58:44 +0900 Subject: [PATCH] nvme-print: add nvme_show_perror() variable number arguments Then show_perror print function outputs the message with a va_list. Signed-off-by: Tokunori Ikegami --- nvme-print-json.c | 6 +++--- nvme-print-stdout.c | 9 +++++++-- nvme-print.c | 9 +++++++-- nvme-print.h | 4 ++-- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/nvme-print-json.c b/nvme-print-json.c index b092f437..b7b198eb 100644 --- a/nvme-print-json.c +++ b/nvme-print-json.c @@ -4629,16 +4629,16 @@ static void json_output_message(bool error, const char *msg, va_list ap) obj_print(r); } -static void json_output_perror(const char *msg) +static void json_output_perror(const char *msg, va_list ap) { struct json_object *r = json_create_object(); _cleanup_free_ char *error = NULL; - if (asprintf(&error, "%s: %s", msg, strerror(errno)) < 0) + if (vasprintf(&error, msg, ap) < 0) error = "Could not allocate string"; - obj_add_str(r, "error", error); + obj_add_key(r, "error", "%s: %s", error, strerror(errno)); json_output_object(r); } diff --git a/nvme-print-stdout.c b/nvme-print-stdout.c index e87c605d..23d5ba9c 100644 --- a/nvme-print-stdout.c +++ b/nvme-print-stdout.c @@ -5604,9 +5604,14 @@ static void stdout_message(bool error, const char *msg, va_list ap) fprintf(error ? stderr : stdout, "\n"); } -static void stdout_perror(const char *msg) +static void stdout_perror(const char *msg, va_list ap) { - perror(msg); + _cleanup_free_ char *error = NULL; + + if (vasprintf(&error, msg, ap) < 0) + error = "Could not allocate string"; + + perror(error); } static void stdout_discovery_log(struct nvmf_discovery_log *log, int numrec) diff --git a/nvme-print.c b/nvme-print.c index 8492225e..824062a6 100644 --- a/nvme-print.c +++ b/nvme-print.c @@ -1460,15 +1460,20 @@ void nvme_show_message(bool error, const char *msg, ...) va_end(ap); } -void nvme_show_perror(const char *msg) +void nvme_show_perror(const char *msg, ...) { struct print_ops *ops = nvme_print_ops(NORMAL); + va_list ap; + + va_start(ap, msg); if (nvme_is_output_format_json()) ops = nvme_print_ops(JSON); if (ops && ops->show_perror) - ops->show_perror(msg); + ops->show_perror(msg, ap); + + va_end(ap); } void nvme_show_discovery_log(struct nvmf_discovery_log *log, uint64_t numrec, diff --git a/nvme-print.h b/nvme-print.h index ad5d4c82..ac3f9785 100644 --- a/nvme-print.h +++ b/nvme-print.h @@ -108,7 +108,7 @@ struct print_ops { /* status and error messages */ void (*connect_msg)(nvme_ctrl_t c); void (*show_message)(bool error, const char *msg, va_list ap); - void (*show_perror)(const char *msg); + void (*show_perror)(const char *msg, va_list ap); void (*show_status)(int status); void (*show_error_status)(int status, const char *msg, va_list ap); @@ -326,7 +326,7 @@ const char *nvme_pls_mode_to_string(__u8 mode); void nvme_dev_full_path(nvme_ns_t n, char *path, size_t len); void nvme_generic_full_path(nvme_ns_t n, char *path, size_t len); void nvme_show_message(bool error, const char *msg, ...); -void nvme_show_perror(const char *msg); +void nvme_show_perror(const char *msg, ...); void nvme_show_error_status(int status, const char *msg, ...); void nvme_show_init(void); void nvme_show_finish(void); -- 2.50.1