]> xenbits.xen.org Git - xenclient/ioemu-pq.git/commitdiff
switcher: Make the reconnect periodically when disconnected.
authorJean Guyader <jean.guyader@eu.citrix.com>
Mon, 9 Nov 2009 17:04:32 +0000 (17:04 +0000)
committerJean Guyader <jean.guyader@eu.citrix.com>
Mon, 9 Nov 2009 17:04:32 +0000 (17:04 +0000)
master/switcher

index cdadd98c67be291671c535cb831d6de9b9587409..7802cae70e4cfcd9b8f11d134180e71649bcd649 100644 (file)
@@ -571,10 +571,10 @@ index 7883718..0b6214c 100644
  #endif /*QEMU_XEN_H*/
 diff --git a/switcher.c b/switcher.c
 new file mode 100644
-index 0000000..f352d08
+index 0000000..f494229
 --- /dev/null
 +++ b/switcher.c
-@@ -0,0 +1,263 @@
+@@ -0,0 +1,271 @@
 +/*
 + * QEMU dom0_driver
 + *
@@ -624,11 +624,12 @@ index 0000000..f352d08
 +
 +#define DOM0_INPUT_SOCKET       "/tmp/input.socket"
 +
-+static int                      s;
++static int                      switcher_connected = 0;
 +static struct sockaddr_un       remote;
-+static int                      slot;
++static QEMUTimer                *switcher_timer = NULL;
++static int                      switcher_socket = -1;
 +
-+static int switcher_init_socket(void);
++static void switcher_recv(void *opaque);
 +
 +static void switcher_key_inject (int code, uint32_t keycode)
 +{
@@ -760,7 +761,49 @@ index 0000000..f352d08
 +    va_end(arg);
 +
 +    fprintf(stderr, "send: %s\n", buff);
-+    send(s, buff, strlen(buff), 0);
++    send(switcher_socket, buff, strlen(buff), 0);
++}
++
++static void switcher_connect(void *opaque)
++{
++    struct stat st;
++
++    opaque = opaque;
++
++    if (switcher_connected)
++        goto out;
++
++    close(switcher_socket);
++    fprintf(stderr, "switcher: try to connect\n");
++    if ((switcher_socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
++    {
++        fprintf(stderr, "switcher: socket %s\n", strerror(errno));
++        goto out;
++    }
++
++    memset(&remote, 0, sizeof (remote));
++    remote.sun_family = AF_UNIX;
++    strcpy(remote.sun_path, DOM0_INPUT_SOCKET);
++    if (connect(switcher_socket, (struct sockaddr *)&remote, SUN_LEN(&remote)) == -1)
++    {
++        fprintf(stderr, "switcher: connect %s, %s\n", strerror(errno), remote.sun_path);
++        goto out;
++    }
++
++    switcher_send("%c%d", DOM0_INPUT_DOMID, domid);
++    if (vga_passthrough)
++        switcher_send("%c%d", DOM0_INPUT_OPT, 1);
++
++    qemu_set_fd_handler(switcher_socket, switcher_recv, NULL, NULL);
++    switcher_connected = 1;
++out:
++    if (!switcher_timer)
++    {
++        fprintf(stderr, "switcher: new timer\n");
++        switcher_timer = qemu_new_timer(rt_clock, switcher_connect, NULL);
++    }
++    fprintf(stderr, "switcher: Setup the timer\n");
++    qemu_mod_timer(switcher_timer, qemu_get_clock(rt_clock) + 1000);
 +}
 +
 +static void switcher_recv(void *opaque)
@@ -773,17 +816,10 @@ index 0000000..f352d08
 +    int                 ret;
 +
 +    memmove(buff, p, left_over);
-+    if ((read_sz = recv(s, buff + left_over, 128, 0)) <= 0)
++    if ((read_sz = recv(switcher_socket, buff + left_over, 128, 0)) <= 0)
 +    {
-+        do
-+        {
-+            fprintf(stderr, "Trying to reconnect ...\n");
-+            if (switcher_init_socket())
-+                return;
-+            ret = errno;
-+            sleep(1);
-+        }
-+        while (ret == EAGAIN);
++        switcher_connected = 0;
++        return;
 +    }
 +    read_sz += left_over;
 +    left_over = 0;
@@ -802,41 +838,13 @@ index 0000000..f352d08
 +    }
 +}
 +
-+static int switcher_init_socket(void)
-+{
-+    struct stat st;
-+
-+    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
-+    {
-+        fprintf(stderr, "switcher: socket %s\n", strerror(errno));
-+        errno = EAGAIN;
-+        return 0;
-+    }
-+
-+    memset(&remote, 0, sizeof (remote));
-+    remote.sun_family = AF_UNIX;
-+    strcpy(remote.sun_path, DOM0_INPUT_SOCKET);
-+    if (connect(s, (struct sockaddr *)&remote, SUN_LEN(&remote)) == -1)
-+    {
-+        fprintf(stderr, "switcher: connect %s, %s\n", strerror(errno), remote.sun_path);
-+        errno = EAGAIN;
-+        return 0;
-+    }
-+
-+    switcher_send("%c%d", DOM0_INPUT_DOMID, domid);
-+    if (vga_passthrough)
-+        switcher_send("%c%d", DOM0_INPUT_OPT, 1);
-+
-+    qemu_set_fd_handler(s, switcher_recv, NULL, NULL);
-+    return 1;
-+}
-+
 +void switcher_init(const char *str_slot)
 +{
++    int slot;
 +    slot = strtol(str_slot, NULL, 10);
 +    fprintf(stderr, "switcher_init: slot %d\n", slot);
 +    xenstore_dom_write(domid, "switcher/slot", str_slot);
-+    switcher_init_socket();
++    switcher_connect(NULL);
 +}
 diff --git a/vl.c b/vl.c
 index a4b8bd8..6350384 100644