Skip to contentSkip to conversation

Handle closed stdin/stdin from file.

PATCH New Rusty Russell 1 participant 1 message Sep 19, 2003

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Handle closed stdin/stdin from file.

vl chews 100% of cpu in a polling loop when fd 0 is a file or closed. Fix below.

Against 0.4.3, but you get the idea.

vl.c +9 -4
diff -ur qemu-0.4.3/vl.c qemu-0.4.3-umlswitch/vl.c
@@ -2542,11 +2546,12 @@
 
 void main_loop(void *opaque)
 {
-    struct pollfd ufds[2], *pf, *serial_ufd, *net_ufd, *gdb_ufd;
-    int ret, n, timeout;
+    struct pollfd ufds[3], *pf, *serial_ufd, *net_ufd, *gdb_ufd;
+    int ret, n, timeout, serial_ok;
     uint8_t ch;
     CPUState *env = global_env;
 
+    serial_ok = 1;
     for(;;) {
 
         ret = cpu_x86_exec(env);
@@ -2559,12 +2564,12 @@
         /* poll any events */
         serial_ufd = NULL;
         pf = ufds;
-        if (!(serial_ports[0].lsr & UART_LSR_DR)) {
+        if (serial_ok && !(serial_ports[0].lsr & UART_LSR_DR)) {
             serial_ufd = pf;
             pf->fd = 0;
             pf->events = POLLIN;
@@ -2588,7 +2593,11 @@
                 n = read(0, &ch, 1);
                 if (n == 1) {
                     serial_received_byte(&serial_ports[0], ch);
-                }
+                } else {
+		    /* Closed, stop polling. */
+		    serial_ok = 0;
+		    fprintf(stderr, "Ignoring closed serial port\n");
+		}
             }
             if (net_ufd && (net_ufd->revents & POLLIN)) {
                 uint8_t buf[MAX_ETH_FRAME_SIZE];

--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.