Add a guest_connected field that ensures only one process
can have a port open at a time.

Signed-off-by: Amit Shah <amit.s...@redhat.com>
---
 drivers/char/virtio_console.c |   17 +++++++++++++++++
 1 files changed, 17 insertions(+), 0 deletions(-)

diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c
index c3498ec..8c9bac8 100644
--- a/drivers/char/virtio_console.c
+++ b/drivers/char/virtio_console.c
@@ -138,6 +138,9 @@ struct virtio_console_port {
 
        /* Is the host device open */
        bool host_connected;
+
+       /* We should allow only one process to open a port */
+       bool guest_connected;
 };
 
 struct virtio_console_struct virtconsole;
@@ -455,6 +458,10 @@ static int virtconsole_release(struct inode *inode, struct 
file *filp)
        send_buf(filp->private_data, (char *)&cpkt, sizeof(cpkt),
                 VIRTIO_CONSOLE_ID_CONTROL, false);
 
+       spin_lock(&port->readbuf_list_lock);
+       port->guest_connected = false;
+       spin_unlock(&port->readbuf_list_lock);
+
        return 0;
 }
 
@@ -474,6 +481,15 @@ static int virtconsole_open(struct inode *inode, struct 
file *filp)
        if (port->hvc)
                return -ENXIO;
 
+       /* Allow only one process to open a particular port at a time */
+       spin_lock(&port->readbuf_list_lock);
+       if (port->guest_connected) {
+               spin_unlock(&port->readbuf_list_lock);
+               return -EMFILE;
+       }
+       port->guest_connected = true;
+       spin_unlock(&port->readbuf_list_lock);
+
        /* Notify host of port being opened */
        cpkt.event = VIRTIO_CONSOLE_PORT_OPEN;
        cpkt.value = 1;
@@ -639,6 +655,7 @@ int init_port_console(struct virtio_console_port *port)
                port->hvc = NULL;
        } else {
                port->vtermno = port->vcon->hvc_vtermno++;
+               port->guest_connected = true;
        }
        return ret;
 }
-- 
1.6.2.5

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/virtualization

Reply via email to