xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Yufang Zhang <yuzhang@redhat.com>
To: xen-devel@lists.xensource.com
Cc: pbonzini@redhat.com, yuzhang@redhat.com, yufang521247@gmail.com,
	stefano.stabellini@eu.citrix.com
Subject: [PATCH] xenconsole: add file lock to xenconsole
Date: Tue, 24 May 2011 22:13:22 +0800	[thread overview]
Message-ID: <1306246402-12917-1-git-send-email-yuzhang@redhat.com> (raw)

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.

Signed-off-by: Yufang Zhang <yuzhang@redhat.com>
---
 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..d7461a8 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 */
+        sprintf(buf, "/tmp/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

             reply	other threads:[~2011-05-24 14:13 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-24 14:13 Yufang Zhang [this message]
2011-05-24 17:09 ` [PATCH] xenconsole: add file lock to xenconsole Ian Jackson
2011-05-27  3:20   ` Yufang Zhang
2011-05-27  8:09     ` Ian Campbell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1306246402-12917-1-git-send-email-yuzhang@redhat.com \
    --to=yuzhang@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=stefano.stabellini@eu.citrix.com \
    --cc=xen-devel@lists.xensource.com \
    --cc=yufang521247@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).