Skip to contentSkip to conversation

Make the monitor a bit more program-friendly

PATCH In review Anthony Liguori 2 participants 5 messages Dec 16, 2006 – Dec 17, 2006

lore
Share & tools
lore
#0 lore raw

cover Make the monitor a bit more program-friendly

This is a short patch series to make the monitor a bit more friendly to external programs.

This series is independent of my previous series but may apply with fuzz if the other series isn't applied.

Regards,

Anthony Liguori

#1 lore raw

PATCH 1/2 Escape filenames in monitor

info block is impossible to parse reliably because there is no escaping done on the filename. A really unfortunately name like "Ugly backing_file=foo" would result in:

hda: type=hd removable=0 file=Ugly backing_file=foo ro=0 drv=qcow

Which is ambiguous when compared to a file named "Ugly" with a backing file of "foo". This patch will escape filenames so that this case will be printed as:

hda: type=hd removable=0 file=Ugly\ backing_file=foo ro=0 drv=qcow

Which is now parsable.

Regards,

Anthony Liguori

block.c +6 -3
diff -r 7d5869c61e0d block.c
@@ -868,9 +868,12 @@ void bdrv_info(void)
             term_printf(" locked=%d", bs->locked);
         }
         if (bs->drv) {
-            term_printf(" file=%s", bs->filename);
-            if (bs->backing_file[0] != '\0')
-                term_printf(" backing_file=%s", bs->backing_file);
+            term_printf(" file=");
+	    term_print_filename(bs->filename);
+            if (bs->backing_file[0] != '\0') {
+                term_printf(" backing_file=");
+		term_print_filename(bs->backing_file);
+	    }
             term_printf(" ro=%d", bs->read_only);
             term_printf(" drv=%s", bs->drv->format_name);
             if (bs->encrypted)
monitor.c +26 -0
diff -r 7d5869c61e0d monitor.c
@@ -104,6 +104,32 @@ void term_printf(const char *fmt, ...)
     va_start(ap, fmt);
     term_vprintf(fmt, ap);
     va_end(ap);
+}
+
+void term_print_filename(const char *filename)
+{
+    int i;
+
+    for (i = 0; filename[i]; i++) {
+	switch (filename[i]) {
+	case ' ':
+	case '"':
+	    term_printf("\\%c", filename[i]);
+	    break;
+	case '\t':
+	    term_printf("\\t");
+	    break;
+	case '\r':
+	    term_printf("\\r");
+	    break;
+	case '\n':
+	    term_printf("\\n");
+	    break;
+	default:
+	    term_printf("%c", filename[i]);
+	    break;
+	}
+    }
 }
 
 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
qemu-img.c +5 -0
diff -r 7d5869c61e0d qemu-img.c
@@ -111,6 +111,11 @@ void term_printf(const char *fmt, ...)
     va_start(ap, fmt);
     vprintf(fmt, ap);
     va_end(ap);
+}
+
+void term_print_filename(const char *filename)
+{
+    term_printf(filename);
 }
 
 void __attribute__((noreturn)) error(const char *fmt, ...) 
vl.h +1 -0
diff -r 7d5869c61e0d vl.h
@@ -1318,6 +1318,7 @@ void term_puts(const char *str);
 void term_puts(const char *str);
 void term_vprintf(const char *fmt, va_list ap);
 void term_printf(const char *fmt, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
+void term_print_filename(const char *filename);
 void term_flush(void);
 void term_print_help(void);
 void monitor_readline(const char *prompt, int is_password,
#4 lore raw

PATCH 2/2 Reinitialize monitor upon reconnect

In reply to Anthony Liguori

If you expose the monitor on a character device that support reconnecting, it becomes difficult to deterministically execute commands. For instance, one person may connect and send a partial command in which case reconnecting may result in your command not being what you expect.

Furthermore, not knowing what to expect when you connect initially is annoying. There may be a prompt, there may be nothing, or there may be a banner and prompt.

This patch adds a new event that's signaled whenever a character device supporting reconnecting reconnects. For the monitor, this event causes the monitor to be reset.

This means no matter when you connect to the monitor (or reconnect), you'll always have deterministic behavior (the banner is printed with a new prompt).

Please note, if you don't use the nowait option, you'll see the banner twice the first time you connect. I'm not sure I really see the value of not always doing nowait (particularly since there is -S). Perhaps someone can shed light on what the use case is?

Regards,

Anthony Liguori

monitor.c +11 -0
diff -r 3ce6d719bfc0 monitor.c
@@ -2388,6 +2388,16 @@ static void term_read(void *opaque, cons
         readline_handle_byte(buf[i]);
 }
 
+static void term_event(void *opaque, int event)
+{
+    if (event != CHR_EVENT_RESET)
+	return;
+
+    term_printf("QEMU %s monitor - type 'help' for more information\n",
+		QEMU_VERSION);
+    readline_reset();
+}
+
 static void monitor_start_input(void);
 
 static void monitor_handle_command1(void *opaque, const char *cmdline)
@@ -2410,6 +2420,7 @@ void monitor_init(CharDriverState *hd, i
     }
     qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
     monitor_start_input();
+    qemu_chr_add_event_handler(hd, term_event);
 }
 
 /* XXX: use threads ? */
readline.c +5 -0
diff -r 3ce6d719bfc0 readline.c
@@ -415,6 +415,11 @@ void readline_start(const char *prompt, 
     term_show_prompt();
 }
 
+void readline_reset(void)
+{
+    term_show_prompt();
+}
+
 const char *readline_get_history(unsigned int index)
 {
     if (index >= TERM_MAX_CMDS)
vl.c +2 -0
diff -r 3ce6d719bfc0 vl.c
@@ -2399,6 +2399,8 @@ static void tcp_chr_connect(void *opaque
     s->connected = 1;
     qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
                          tcp_chr_read, NULL, chr);
+    if (chr->chr_event)
+	    chr->chr_event(chr, CHR_EVENT_RESET);
 }
 
 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
vl.h +2 -1
diff -r 3ce6d719bfc0 vl.h
@@ -240,7 +240,7 @@ void qemu_del_wait_object(HANDLE handle,
 
 #define CHR_EVENT_BREAK 0 /* serial break char */
 #define CHR_EVENT_FOCUS 1 /* focus to this terminal (modal input needed) */
-
+#define CHR_EVENT_RESET 2 /* new connection established */
 
 
 #define CHR_IOCTL_SERIAL_SET_PARAMS   1
@@ -1334,6 +1334,7 @@ const char *readline_get_history(unsigne
 const char *readline_get_history(unsigned int index);
 void readline_start(const char *prompt, int is_password,
                     ReadLineFunc *readline_func, void *opaque);
+void readline_reset(void);
 
 void kqemu_record_dump(void);
 
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] Escape filenames in monitor

In reply to Anthony Liguori
Quoted 2 lines · Anthony Liguori

info block is impossible to parse reliably because there is no escaping done on the filename.

Don't you also need to convert \ to \\ ? Else any \ which was in the original string will confuse the parser of the escaped output.

J

#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] Escape filenames in monitor

Quoted 9 lines · Julian Seward
Quoted 3 lines · Anthony Liguori

info block is impossible to parse reliably because there is no escaping done on the filename.

Don't you also need to convert \ to \\ ? Else any \ which was in the original string will confuse the parser of the escaped output.

Yup, you are correct. Thanks!

Regards,

Anthony Liguori

Quoted 8 lines

J

_______________________________________________
Qemu-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/qemu-devel