WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] [PATCH V2] xenconsole: add file lock to xenconsole

To: xen-devel@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-devel] [PATCH V2] xenconsole: add file lock to xenconsole
From: Yufang Zhang <yufang521247@xxxxxxxxx>
Date: Fri, 27 May 2011 11:16:42 +0800
Cc: pbonzini@xxxxxxxxxx, Yufang Zhang <yuzhang@xxxxxxxxxx>, Ian.Jackson@xxxxxxxxxxxxx, yufang521247@xxxxxxxxx, lersek@xxxxxxxxxx
Delivery-date: Fri, 27 May 2011 00:23:14 -0700
Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer; bh=1OM1ioQQhOhahVSZqglHfAgrNyofS00NIxcVoroqvrI=; b=xHeD2yTJDameRoGX/lWcpID0EOwNMGvI9rxVlnYQcB/4WO+UVG09fNokjt0YSqxkgs MZWI3L18y39uBBX7wRNhIFs4wqIUiikVK9eSDMS5TKIiPErdxjAPlpakSWiFn6pYAW0B y/EMWVAJwP95OE/dq2YPPjZoAOhV6HEZVq9cU=
Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=fARaibdbGx3FNcEBFokLw/81ita7wg6ATteBDWVbdA+Sz2EsfPea1RWIzycZyQjtvY Oh8QbpVovyUHJgIgjTpfHl/NaFpvQONuuba7PYcx4HAC2jxSG23CC9vJPaPe+rSjKLwK PjaBid3xsBRViqHrufPZ7xVgBfan6vc9L460U=
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
From: Yufang Zhang <yuzhang@xxxxxxxxxx>

This patch add a file lock to xenconsole for each console id, so
that only one console could be attached to a guest at a time.
Otherwise, consoles would get stuck and print strange outputs.

The version2 patch fix bellow problems:
1) fix for indent level and coding style as the surrounding code
2) put the lock files in /var/run/
3) use snprintf instead of sprintf

Considering the lock files are just blank files and they can be
reused, leaving them behind when xenconsole crashes(or killed by
kill -9) is not so bad.

Signed-off-by: Yufang Zhang <yufang521247@xxxxxxxxx>
---
 tools/console/client/main.c |   31 +++++++++++++++++++++++++++++++
 1 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/tools/console/client/main.c b/tools/console/client/main.c
index df3636f..ff1c64e 100644
--- a/tools/console/client/main.c
+++ b/tools/console/client/main.c
@@ -46,6 +46,28 @@
 
 static volatile sig_atomic_t received_signal = 0;
 
+static int console_locked(const char *file)
+{
+       int fd;
+
+       fd = open(file, O_RDWR | O_CREAT, S_IRUSR|S_IWUSR);
+       if (fd == -1) {
+               fprintf(stderr,"can't open lock file: %s\n", file);
+               exit(1);
+       }
+
+       if (lockf(fd, F_TLOCK, 0) == -1) {
+               if ( errno == EACCES || errno == EAGAIN ) {
+                       close(fd);
+                       return (1);
+               }
+               fprintf(stderr,"can't lock file: %s\n", file);
+               exit(1);
+       }
+
+       return (0);
+}
+
 static void sighandler(int signum)
 {
        received_signal = 1;
@@ -280,6 +302,7 @@ int main(int argc, char **argv)
        int spty, xsfd;
        struct xs_handle *xs;
        char *end;
+       char buf[100];
        console_type type = CONSOLE_INVAL;
 
        while((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
@@ -368,6 +391,13 @@ int main(int argc, char **argv)
                exit(EINVAL);
        }
 
+       /* Make sure only one console is attached to guest */
+       snprintf(buf, strlen("/var/run/xenconsole-%d-%d") + 1, 
"/var/run/xenconsole-%d-%d", domid, num);
+       if (console_locked(buf) == 1) {
+               fprintf(stderr, "Another console has already been attached to 
guest\n");
+               exit(EINVAL);
+       }
+
        /* Set a watch on this domain's console pty */
        if (!xs_watch(xs, path, ""))
                err(errno, "Can't set watch for console pty");
@@ -387,6 +417,7 @@ int main(int argc, char **argv)
        console_loop(spty, xs, path);
        restore_term(STDIN_FILENO, &attr);
 
+       unlink(buf);
        free(path);
        free(dom_path);
        return 0;
-- 
1.7.4.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel

<Prev in Thread] Current Thread [Next in Thread>