From nobody Mon Feb 9 10:27:41 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=1560444404; cv=none; d=zoho.com; s=zohoarc; b=U6XlvwLWszkhbukN88KL7bd2s2+embRTurIERBvsCeWKgaF7owUkOOiLuaLq/jbNEXHEI2a5sn6K9YhOeh61OpUhFxSAmkX+nSUwsIU46nRhJz60j0AbyYpa23dkbWNOIqjnStJIkUZrGoHn0pMUH1ZqOGDCa0iVgCKfa4MZQCE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1560444404; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=yKmkQGDMY6avSeUFD0KM6rS7pbGrkI3BMevOAYxfzPg=; b=FovSSjhmI0txsz9ZscpJKtVqhAvL8t44E6/D66KXMsgLuogupZ8m5bInqv+CRl9lfdeTfRHutFkZDsgpQ+AAuAxvQQfG1aGPaldSnaJsJQdYvZl5gPE51EgJwvxsj5vMs1uEyyVx0FCY2jrT+1RyCxaojX2Hj9lKvPRWtx5qx8g= 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 1560444404956689.4893431126895; Thu, 13 Jun 2019 09:46:44 -0700 (PDT) Received: from localhost ([::1]:41738 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hbSs7-0001Ia-S7 for importer@patchew.org; Thu, 13 Jun 2019 12:46:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59970) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hbRks-0000UU-8O for qemu-devel@nongnu.org; Thu, 13 Jun 2019 11:35:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hbRko-0003QV-VV for qemu-devel@nongnu.org; Thu, 13 Jun 2019 11:35:10 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45378) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hbRkd-0003B9-A1; Thu, 13 Jun 2019 11:34:58 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1A0B530C0DEA; Thu, 13 Jun 2019 15:34:36 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-116-246.ams2.redhat.com [10.36.116.246]) by smtp.corp.redhat.com (Postfix) with ESMTP id 852EE60C78; Thu, 13 Jun 2019 15:34:33 +0000 (UTC) From: Kevin Wolf To: qemu-devel@nongnu.org Date: Thu, 13 Jun 2019 17:33:54 +0200 Message-Id: <20190613153405.24769-5-kwolf@redhat.com> In-Reply-To: <20190613153405.24769-1-kwolf@redhat.com> References: <20190613153405.24769-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Thu, 13 Jun 2019 15:34:46 +0000 (UTC) Content-Transfer-Encoding: quoted-printable 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 04/15] monitor: Create MonitorHMP with readline state X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, berrange@redhat.com, qemu-block@nongnu.org, armbru@redhat.com, dgilbert@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The ReadLineState in Monitor is only used for HMP monitors. Create MonitorHMP and move it there. Signed-off-by: Kevin Wolf Reviewed-by: Dr. David Alan Gilbert Reviewed-by: Markus Armbruster --- include/monitor/monitor.h | 5 +- hmp.c | 4 +- monitor.c | 129 +++++++++++++++++++++----------------- 3 files changed, 79 insertions(+), 59 deletions(-) diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h index 06cfcd8f36..efdea83bb3 100644 --- a/include/monitor/monitor.h +++ b/include/monitor/monitor.h @@ -6,6 +6,7 @@ #include "qemu/readline.h" =20 extern __thread Monitor *cur_mon; +typedef struct MonitorHMP MonitorHMP; =20 /* flags for monitor_init */ /* 0x01 unused */ @@ -34,8 +35,8 @@ void monitor_flush(Monitor *mon); int monitor_set_cpu(int cpu_index); int monitor_get_cpu_index(void); =20 -void monitor_read_command(Monitor *mon, int show_prompt); -int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, +void monitor_read_command(MonitorHMP *mon, int show_prompt); +int monitor_read_password(MonitorHMP *mon, ReadLineFunc *readline_func, void *opaque); =20 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_i= d, diff --git a/hmp.c b/hmp.c index be5e345c6f..99414cd39c 100644 --- a/hmp.c +++ b/hmp.c @@ -1943,6 +1943,8 @@ static void hmp_change_read_arg(void *opaque, const c= har *password, =20 void hmp_change(Monitor *mon, const QDict *qdict) { + /* FIXME Make MonitorHMP public and use container_of */ + MonitorHMP *hmp_mon =3D (MonitorHMP *) mon; const char *device =3D qdict_get_str(qdict, "device"); const char *target =3D qdict_get_str(qdict, "target"); const char *arg =3D qdict_get_try_str(qdict, "arg"); @@ -1960,7 +1962,7 @@ void hmp_change(Monitor *mon, const QDict *qdict) if (strcmp(target, "passwd") =3D=3D 0 || strcmp(target, "password") =3D=3D 0) { if (!arg) { - monitor_read_password(mon, hmp_change_read_arg, NULL); + monitor_read_password(hmp_mon, hmp_change_read_arg, NULL); return; } } diff --git a/monitor.c b/monitor.c index 855a147723..572449f6db 100644 --- a/monitor.c +++ b/monitor.c @@ -192,14 +192,6 @@ struct Monitor { bool skip_flush; bool use_io_thread; =20 - /* - * State used only in the thread "owning" the monitor. - * If @use_io_thread, this is @mon_iothread. - * Else, it's the main thread. - * These members can be safely accessed without locks. - */ - ReadLineState *rs; - gchar *mon_cpu_path; mon_cmd_t *cmd_table; QTAILQ_ENTRY(Monitor) entry; @@ -220,6 +212,18 @@ struct Monitor { int mux_out; }; =20 +struct MonitorHMP { + Monitor common; + /* + * State used only in the thread "owning" the monitor. + * If @use_io_thread, this is @mon_iothread. (This does not actually h= appen + * in the current state of the code.) + * Else, it's the main thread. + * These members can be safely accessed without locks. + */ + ReadLineState *rs; +}; + typedef struct { Monitor common; JSONMessageParser parser; @@ -326,7 +330,7 @@ bool monitor_cur_is_qmp(void) return cur_mon && monitor_is_qmp(cur_mon); } =20 -void monitor_read_command(Monitor *mon, int show_prompt) +void monitor_read_command(MonitorHMP *mon, int show_prompt) { if (!mon->rs) return; @@ -336,7 +340,7 @@ void monitor_read_command(Monitor *mon, int show_prompt) readline_show_prompt(mon->rs); } =20 -int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, +int monitor_read_password(MonitorHMP *mon, ReadLineFunc *readline_func, void *opaque) { if (mon->rs) { @@ -344,7 +348,8 @@ int monitor_read_password(Monitor *mon, ReadLineFunc *r= eadline_func, /* prompt is printed on return from the command handler */ return 0; } else { - monitor_printf(mon, "terminal does not support password prompting\= n"); + monitor_printf(&mon->common, + "terminal does not support password prompting\n"); return -ENOTTY; } } @@ -705,7 +710,7 @@ static void monitor_qapi_event_init(void) qapi_event_throttle_equal); } =20 -static void handle_hmp_command(Monitor *mon, const char *cmdline); +static void handle_hmp_command(MonitorHMP *mon, const char *cmdline); =20 static void monitor_iothread_init(void); =20 @@ -715,7 +720,6 @@ static void monitor_data_init(Monitor *mon, int flags, = bool skip_flush, if (use_io_thread && !mon_iothread) { monitor_iothread_init(); } - memset(mon, 0, sizeof(Monitor)); qemu_mutex_init(&mon->mon_lock); mon->outbuf =3D qstring_new(); /* Use *mon_cmds by default. */ @@ -740,8 +744,10 @@ static void monitor_data_destroy(Monitor *mon) if (monitor_is_qmp(mon)) { MonitorQMP *qmp_mon =3D container_of(mon, MonitorQMP, common); monitor_data_destroy_qmp(qmp_mon); + } else { + MonitorHMP *hmp_mon =3D container_of(mon, MonitorHMP, common); + readline_free(hmp_mon->rs); } - readline_free(mon->rs); qobject_unref(mon->outbuf); qemu_mutex_destroy(&mon->mon_lock); } @@ -750,12 +756,13 @@ char *qmp_human_monitor_command(const char *command_l= ine, bool has_cpu_index, int64_t cpu_index, Error **errp) { char *output =3D NULL; - Monitor *old_mon, hmp; + Monitor *old_mon; + MonitorHMP hmp =3D {}; =20 - monitor_data_init(&hmp, 0, true, false); + monitor_data_init(&hmp.common, 0, true, false); =20 old_mon =3D cur_mon; - cur_mon =3D &hmp; + cur_mon =3D &hmp.common; =20 if (has_cpu_index) { int ret =3D monitor_set_cpu(cpu_index); @@ -770,16 +777,16 @@ char *qmp_human_monitor_command(const char *command_l= ine, bool has_cpu_index, handle_hmp_command(&hmp, command_line); cur_mon =3D old_mon; =20 - qemu_mutex_lock(&hmp.mon_lock); - if (qstring_get_length(hmp.outbuf) > 0) { - output =3D g_strdup(qstring_get_str(hmp.outbuf)); + qemu_mutex_lock(&hmp.common.mon_lock); + if (qstring_get_length(hmp.common.outbuf) > 0) { + output =3D g_strdup(qstring_get_str(hmp.common.outbuf)); } else { output =3D g_strdup(""); } - qemu_mutex_unlock(&hmp.mon_lock); + qemu_mutex_unlock(&hmp.common.mon_lock); =20 out: - monitor_data_destroy(&hmp); + monitor_data_destroy(&hmp.common); return output; } =20 @@ -1349,16 +1356,19 @@ static void hmp_info_sync_profile(Monitor *mon, con= st QDict *qdict) =20 static void hmp_info_history(Monitor *mon, const QDict *qdict) { + MonitorHMP *hmp_mon =3D container_of(mon, MonitorHMP, common); int i; const char *str; =20 - if (!mon->rs) + if (!hmp_mon->rs) { return; + } i =3D 0; for(;;) { - str =3D readline_get_history(mon->rs, i); - if (!str) + str =3D readline_get_history(hmp_mon->rs, i); + if (!str) { break; + } monitor_printf(mon, "%d: '%s'\n", i, str); i++; } @@ -3056,11 +3066,12 @@ static const mon_cmd_t *search_dispatch_table(const= mon_cmd_t *disp_table, * Do not assume the return value points into @table! It doesn't when * the command is found in a sub-command table. */ -static const mon_cmd_t *monitor_parse_command(Monitor *mon, +static const mon_cmd_t *monitor_parse_command(MonitorHMP *hmp_mon, const char *cmdp_start, const char **cmdp, mon_cmd_t *table) { + Monitor *mon =3D &hmp_mon->common; const char *p; const mon_cmd_t *cmd; char cmdname[256]; @@ -3091,7 +3102,7 @@ static const mon_cmd_t *monitor_parse_command(Monitor= *mon, *cmdp =3D p; /* search sub command */ if (cmd->sub_table !=3D NULL && *p !=3D '\0') { - return monitor_parse_command(mon, cmdp_start, cmdp, cmd->sub_table= ); + return monitor_parse_command(hmp_mon, cmdp_start, cmdp, cmd->sub_t= able); } =20 return cmd; @@ -3468,7 +3479,7 @@ fail: return NULL; } =20 -static void handle_hmp_command(Monitor *mon, const char *cmdline) +static void handle_hmp_command(MonitorHMP *mon, const char *cmdline) { QDict *qdict; const mon_cmd_t *cmd; @@ -3476,26 +3487,26 @@ static void handle_hmp_command(Monitor *mon, const = char *cmdline) =20 trace_handle_hmp_command(mon, cmdline); =20 - cmd =3D monitor_parse_command(mon, cmdline, &cmdline, mon->cmd_table); + cmd =3D monitor_parse_command(mon, cmdline, &cmdline, mon->common.cmd_= table); if (!cmd) { return; } =20 - qdict =3D monitor_parse_arguments(mon, &cmdline, cmd); + qdict =3D monitor_parse_arguments(&mon->common, &cmdline, cmd); if (!qdict) { while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) { cmdline--; } - monitor_printf(mon, "Try \"help %.*s\" for more information\n", + monitor_printf(&mon->common, "Try \"help %.*s\" for more informati= on\n", (int)(cmdline - cmd_start), cmd_start); return; } =20 - cmd->cmd(mon, qdict); + cmd->cmd(&mon->common, qdict); qobject_unref(qdict); } =20 -static void cmd_completion(Monitor *mon, const char *name, const char *lis= t) +static void cmd_completion(MonitorHMP *mon, const char *name, const char *= list) { const char *p, *pstart; char cmd[128]; @@ -3519,7 +3530,7 @@ static void cmd_completion(Monitor *mon, const char *= name, const char *list) } } =20 -static void file_completion(Monitor *mon, const char *input) +static void file_completion(MonitorHMP *mon, const char *input) { DIR *ffs; struct dirent *d; @@ -4008,7 +4019,7 @@ void loadvm_completion(ReadLineState *rs, int nb_args= , const char *str) } } =20 -static void monitor_find_completion_by_table(Monitor *mon, +static void monitor_find_completion_by_table(MonitorHMP *mon, const mon_cmd_t *cmd_table, char **args, int nb_args) @@ -4103,7 +4114,7 @@ static void monitor_find_completion_by_table(Monitor = *mon, static void monitor_find_completion(void *opaque, const char *cmdline) { - Monitor *mon =3D opaque; + MonitorHMP *mon =3D opaque; char *args[MAX_ARGS]; int nb_args, len; =20 @@ -4123,7 +4134,7 @@ static void monitor_find_completion(void *opaque, } =20 /* 2. auto complete according to args */ - monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args); + monitor_find_completion_by_table(mon, mon->common.cmd_table, args, nb_= args); =20 cleanup: free_cmdline_args(args, nb_args); @@ -4334,19 +4345,21 @@ static void monitor_qmp_read(void *opaque, const ui= nt8_t *buf, int size) =20 static void monitor_read(void *opaque, const uint8_t *buf, int size) { + MonitorHMP *mon; Monitor *old_mon =3D cur_mon; int i; =20 cur_mon =3D opaque; + mon =3D container_of(cur_mon, MonitorHMP, common); =20 - if (cur_mon->rs) { + if (mon->rs) { for (i =3D 0; i < size; i++) - readline_handle_byte(cur_mon->rs, buf[i]); + readline_handle_byte(mon->rs, buf[i]); } else { if (size =3D=3D 0 || buf[size - 1] !=3D 0) monitor_printf(cur_mon, "corrupted command\n"); else - handle_hmp_command(cur_mon, (char *)buf); + handle_hmp_command(mon, (char *)buf); } =20 cur_mon =3D old_mon; @@ -4355,11 +4368,11 @@ static void monitor_read(void *opaque, const uint8_= t *buf, int size) static void monitor_command_cb(void *opaque, const char *cmdline, void *readline_opaque) { - Monitor *mon =3D opaque; + MonitorHMP *mon =3D opaque; =20 - monitor_suspend(mon); + monitor_suspend(&mon->common); handle_hmp_command(mon, cmdline); - monitor_resume(mon); + monitor_resume(&mon->common); } =20 int monitor_suspend(Monitor *mon) @@ -4405,8 +4418,9 @@ void monitor_resume(Monitor *mon) } =20 if (!monitor_is_qmp(mon)) { - assert(mon->rs); - readline_show_prompt(mon->rs); + MonitorHMP *hmp_mon =3D container_of(mon, MonitorHMP, common); + assert(hmp_mon->rs); + readline_show_prompt(hmp_mon->rs); } =20 aio_bh_schedule_oneshot(ctx, monitor_accept_input, mon); @@ -4468,6 +4482,7 @@ static void monitor_qmp_event(void *opaque, int event) static void monitor_event(void *opaque, int event) { Monitor *mon =3D opaque; + MonitorHMP *hmp_mon =3D container_of(mon, MonitorHMP, common); =20 switch (event) { case CHR_EVENT_MUX_IN: @@ -4475,7 +4490,7 @@ static void monitor_event(void *opaque, int event) mon->mux_out =3D 0; qemu_mutex_unlock(&mon->mon_lock); if (mon->reset_seen) { - readline_restart(mon->rs); + readline_restart(hmp_mon->rs); monitor_resume(mon); monitor_flush(mon); } else { @@ -4502,8 +4517,8 @@ static void monitor_event(void *opaque, int event) monitor_printf(mon, "QEMU %s monitor - type 'help' for more " "information\n", QEMU_VERSION); if (!mon->mux_out) { - readline_restart(mon->rs); - readline_show_prompt(mon->rs); + readline_restart(hmp_mon->rs); + readline_show_prompt(hmp_mon->rs); } mon->reset_seen =3D 1; mon_refcount++; @@ -4564,15 +4579,17 @@ void monitor_init_globals(void) static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque, const char *fmt, ..= .) { + MonitorHMP *mon =3D opaque; va_list ap; va_start(ap, fmt); - monitor_vprintf(opaque, fmt, ap); + monitor_vprintf(&mon->common, fmt, ap); va_end(ap); } =20 static void monitor_readline_flush(void *opaque) { - monitor_flush(opaque); + MonitorHMP *mon =3D opaque; + monitor_flush(&mon->common); } =20 /* @@ -4673,11 +4690,11 @@ static void monitor_init_qmp(Chardev *chr, int flag= s) =20 static void monitor_init_hmp(Chardev *chr, int flags) { - Monitor *mon =3D g_malloc(sizeof(*mon)); + MonitorHMP *mon =3D g_malloc0(sizeof(*mon)); bool use_readline =3D flags & MONITOR_USE_READLINE; =20 - monitor_data_init(mon, flags, false, false); - qemu_chr_fe_init(&mon->chr, chr, &error_abort); + monitor_data_init(&mon->common, flags, false, false); + qemu_chr_fe_init(&mon->common.chr, chr, &error_abort); =20 if (use_readline) { mon->rs =3D readline_init(monitor_readline_printf, @@ -4687,9 +4704,9 @@ static void monitor_init_hmp(Chardev *chr, int flags) monitor_read_command(mon, 0); } =20 - qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read, - monitor_event, NULL, mon, NULL, true); - monitor_list_append(mon); + qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, monitor_r= ead, + monitor_event, NULL, &mon->common, NULL, true= ); + monitor_list_append(&mon->common); } =20 void monitor_init(Chardev *chr, int flags) --=20 2.20.1