From nobody Mon Feb 9 13:22:34 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1555528716; cv=none; d=zoho.com; s=zohoarc; b=nmkR/x2QS4QjXVKudLYiIf4mA4o28apWPBnq0NSDNb1tv753Tk/zt73e0DYfmEVfv5e86j6w8RHAkwiEDIhonBk/7KyIxXmKtfaYnMK5xNzDlrPlgMCFHeJ1SJQWoMSqIz2FEZXubQW9Z6PrAbCUMqC1DhsXJfiiR+obo+O6jy4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1555528716; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=i231vbDfF4cW6cyKRCvt937S6BOq7jK1cR5sDofvHjA=; b=SbRqD1LxS00EVDKQwGu+agMpj4Rg1qPUs3UX1bU9st9y7rbOOjgXs6frxs/VsAYGbY0PeW0R9GjLmy7qtig8Y/Rl46T/sUtyt2Vl57sZOTqCV+ViHBOtfeQec8LZsXH7AxVxIszpDoazptR/wmLvTSdD6L2plLbW/GyDF2Y/U0U= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 155552871669371.00708228456642; Wed, 17 Apr 2019 12:18:36 -0700 (PDT) Received: from localhost ([127.0.0.1]:58280 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hGq4n-0008Di-HP for importer@patchew.org; Wed, 17 Apr 2019 15:18:33 -0400 Received: from eggs.gnu.org ([209.51.188.92]:60307) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hGptQ-0007el-FL for qemu-devel@nongnu.org; Wed, 17 Apr 2019 15:06:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hGptO-0003a6-Lw for qemu-devel@nongnu.org; Wed, 17 Apr 2019 15:06:48 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48472) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hGptO-0003Ym-BF for qemu-devel@nongnu.org; Wed, 17 Apr 2019 15:06:46 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8D610306C3F8 for ; Wed, 17 Apr 2019 19:06:45 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-116.ams2.redhat.com [10.36.116.116]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3069860148; Wed, 17 Apr 2019 19:06:45 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 236AA11329F5; Wed, 17 Apr 2019 21:06:42 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Wed, 17 Apr 2019 21:06:36 +0200 Message-Id: <20190417190641.26814-11-armbru@redhat.com> In-Reply-To: <20190417190641.26814-1-armbru@redhat.com> References: <20190417190641.26814-1-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Wed, 17 Apr 2019 19:06:45 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 10/15] monitor error: Make printf()-like functions return a value X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Dr . David Alan Gilbert" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" printf() & friends return the number of characters written on success, negative value on error. monitor_printf(), monitor_vfprintf(), monitor_vprintf(), error_printf(), error_printf_unless_qmp(), error_vprintf(), and error_vprintf_unless_qmp() return void. Some of them carry a TODO comment asking for int instead. Improve them to return int like printf() does. This makes our use of monitor_printf() as fprintf_function slightly less dirty: the function cast no longer adds a return value that isn't there. It still changes a parameter's pointer type. That will be addressed in a future commit. monitor_vfprintf() always returns zero. Improve it to return the proper value. Cc: Dr. David Alan Gilbert Signed-off-by: Markus Armbruster Reviewed-by: Dr. David Alan Gilbert --- include/monitor/monitor.h | 8 ++--- include/qemu/error-report.h | 8 ++--- monitor.c | 61 ++++++++++++++++++++----------------- stubs/error-printf.c | 13 +++++--- util/qemu-error.c | 12 +++++--- 5 files changed, 57 insertions(+), 45 deletions(-) diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h index c1b40a9cac..e4c3717454 100644 --- a/include/monitor/monitor.h +++ b/include/monitor/monitor.h @@ -28,9 +28,9 @@ void monitor_resume(Monitor *mon); int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp); int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp); =20 -void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) +int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0); -void monitor_printf(Monitor *mon, const char *fmt, ...) GCC_FMT_ATTR(2, 3); +int monitor_printf(Monitor *mon, const char *fmt, ...) GCC_FMT_ATTR(2, 3); int monitor_fprintf(FILE *stream, const char *fmt, ...) GCC_FMT_ATTR(2, 3); void monitor_flush(Monitor *mon); int monitor_set_cpu(int cpu_index); @@ -48,7 +48,7 @@ int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd= ); void monitor_fdset_dup_fd_remove(int dup_fd); int monitor_fdset_dup_fd_find(int dup_fd); =20 -void monitor_vfprintf(FILE *stream, - const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0); +int monitor_vfprintf(FILE *stream, + const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0); =20 #endif /* MONITOR_H */ diff --git a/include/qemu/error-report.h b/include/qemu/error-report.h index ce43c02314..00d069b20f 100644 --- a/include/qemu/error-report.h +++ b/include/qemu/error-report.h @@ -30,10 +30,10 @@ void loc_set_none(void); void loc_set_cmdline(char **argv, int idx, int cnt); void loc_set_file(const char *fname, int lno); =20 -void error_vprintf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0); -void error_printf(const char *fmt, ...) GCC_FMT_ATTR(1, 2); -void error_vprintf_unless_qmp(const char *fmt, va_list ap) GCC_FMT_ATTR(1,= 0); -void error_printf_unless_qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2); +int error_vprintf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0); +int error_printf(const char *fmt, ...) GCC_FMT_ATTR(1, 2); +int error_vprintf_unless_qmp(const char *fmt, va_list ap) GCC_FMT_ATTR(1, = 0); +int error_printf_unless_qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2); =20 void error_vreport(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0); void warn_vreport(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0); diff --git a/monitor.c b/monitor.c index 4807bbe811..7b4a78d798 100644 --- a/monitor.c +++ b/monitor.c @@ -430,15 +430,14 @@ void monitor_flush(Monitor *mon) } =20 /* flush at every end of line */ -static void monitor_puts(Monitor *mon, const char *str) +static int monitor_puts(Monitor *mon, const char *str) { + int i; char c; =20 qemu_mutex_lock(&mon->mon_lock); - for(;;) { - c =3D *str++; - if (c =3D=3D '\0') - break; + for (i =3D 0; str[i]; i++) { + c =3D str[i]; if (c =3D=3D '\n') { qstring_append_chr(mon->outbuf, '\r'); } @@ -448,39 +447,48 @@ static void monitor_puts(Monitor *mon, const char *st= r) } } qemu_mutex_unlock(&mon->mon_lock); + + return i; } =20 -void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) +int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) { char *buf; + int n; =20 if (!mon) - return; + return -1; =20 if (monitor_is_qmp(mon)) { - return; + return -1; } =20 buf =3D g_strdup_vprintf(fmt, ap); - monitor_puts(mon, buf); + n =3D monitor_puts(mon, buf); g_free(buf); + return n; } =20 -void monitor_printf(Monitor *mon, const char *fmt, ...) +int monitor_printf(Monitor *mon, const char *fmt, ...) { + int ret; + va_list ap; va_start(ap, fmt); - monitor_vprintf(mon, fmt, ap); + ret =3D monitor_vprintf(mon, fmt, ap); va_end(ap); + return ret; } =20 int monitor_fprintf(FILE *stream, const char *fmt, ...) { + int ret; + va_list ap; va_start(ap, fmt); - monitor_vprintf((Monitor *)stream, fmt, ap); + ret =3D monitor_vprintf((Monitor *)stream, fmt, ap); va_end(ap); - return 0; + return ret; } =20 static void qmp_send_response(Monitor *mon, const QDict *rsp) @@ -4535,35 +4543,32 @@ static void monitor_readline_flush(void *opaque) =20 /* * Print to current monitor if we have one, else to stream. - * TODO should return int, so callers can calculate width, but that - * requires surgery to monitor_vprintf(). Left for another day. */ -void monitor_vfprintf(FILE *stream, const char *fmt, va_list ap) +int monitor_vfprintf(FILE *stream, const char *fmt, va_list ap) { if (cur_mon && !monitor_cur_is_qmp()) { - monitor_vprintf(cur_mon, fmt, ap); - } else { - vfprintf(stream, fmt, ap); + return monitor_vprintf(cur_mon, fmt, ap); } + return vfprintf(stream, fmt, ap); } =20 /* * Print to current monitor if we have one, else to stderr. - * TODO should return int, so callers can calculate width, but that - * requires surgery to monitor_vprintf(). Left for another day. */ -void error_vprintf(const char *fmt, va_list ap) +int error_vprintf(const char *fmt, va_list ap) { - monitor_vfprintf(stderr, fmt, ap); + return monitor_vfprintf(stderr, fmt, ap); } =20 -void error_vprintf_unless_qmp(const char *fmt, va_list ap) +int error_vprintf_unless_qmp(const char *fmt, va_list ap) { - if (cur_mon && !monitor_cur_is_qmp()) { - monitor_vprintf(cur_mon, fmt, ap); - } else if (!cur_mon) { - vfprintf(stderr, fmt, ap); + if (!cur_mon) { + return vfprintf(stderr, fmt, ap); } + if (!monitor_cur_is_qmp()) { + return monitor_vprintf(cur_mon, fmt, ap); + } + return -1; } =20 static void monitor_list_append(Monitor *mon) diff --git a/stubs/error-printf.c b/stubs/error-printf.c index 99c6406668..1f9d3b3714 100644 --- a/stubs/error-printf.c +++ b/stubs/error-printf.c @@ -2,19 +2,22 @@ #include "qemu-common.h" #include "qemu/error-report.h" =20 -void error_vprintf(const char *fmt, va_list ap) +int error_vprintf(const char *fmt, va_list ap) { + int ret; + if (g_test_initialized() && !g_test_subprocess() && getenv("QTEST_SILENT_ERRORS")) { char *msg =3D g_strdup_vprintf(fmt, ap); g_test_message("%s", msg); + ret =3D strlen(msg); g_free(msg); - } else { - vfprintf(stderr, fmt, ap); + return ret; } + return vfprintf(stderr, fmt, ap); } =20 -void error_vprintf_unless_qmp(const char *fmt, va_list ap) +int error_vprintf_unless_qmp(const char *fmt, va_list ap) { - error_vprintf(fmt, ap); + return error_vprintf(fmt, ap); } diff --git a/util/qemu-error.c b/util/qemu-error.c index d08139d9ac..f373f3b3b0 100644 --- a/util/qemu-error.c +++ b/util/qemu-error.c @@ -24,22 +24,26 @@ typedef enum { REPORT_TYPE_INFO, } report_type; =20 -void error_printf(const char *fmt, ...) +int error_printf(const char *fmt, ...) { va_list ap; + int ret; =20 va_start(ap, fmt); - error_vprintf(fmt, ap); + ret =3D error_vprintf(fmt, ap); va_end(ap); + return ret; } =20 -void error_printf_unless_qmp(const char *fmt, ...) +int error_printf_unless_qmp(const char *fmt, ...) { va_list ap; + int ret; =20 va_start(ap, fmt); - error_vprintf_unless_qmp(fmt, ap); + ret =3D error_vprintf_unless_qmp(fmt, ap); va_end(ap); + return ret; } =20 static Location std_loc =3D { --=20 2.17.2