[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v2] xenconsole: Ensure exclusive access to console using locks



If more than one instance of xenconsole is run against the same DOMID
then each instance will only get some data. This change ensures
exclusive access to the console by obtaining an exclusive lock on
<XEN_LOCK_DIR>/xenconsole.<DOMID>.

The locking strategy used is based on
tools/libxl/libxl_internal.c:libxl__lock_domain_userdata().

Signed-off-by: Martin Lucina <martin@xxxxxxxxxx>
Cc: Ian Jackson <ian.jackson@xxxxxxxxxxxxx>
Cc: Stefano Stabellini <stefano.stabellini@xxxxxxxxxxxxx>
Cc: Ian Campbell <ian.campbell@xxxxxxxxxx>
Cc: Wei Liu <wei.liu2@xxxxxxxxxx>
---
 .gitignore                  |  1 +
 tools/console/Makefile      |  5 ++++-
 tools/console/client/main.c | 53 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index f6ddb00..bccbd12 100644
--- a/.gitignore
+++ b/.gitignore
@@ -100,6 +100,7 @@ tools/blktap2/vhd/vhd-update
 tools/blktap2/vhd/vhd-util
 tools/console/xenconsole
 tools/console/xenconsoled
+tools/console/client/_paths.h
 tools/debugger/gdb/gdb-6.2.1-linux-i386-xen/*
 tools/debugger/gdb/gdb-6.2.1/*
 tools/debugger/gdb/gdb-6.2.1.tar.bz2
diff --git a/tools/console/Makefile b/tools/console/Makefile
index 71f8088..1ce5590 100644
--- a/tools/console/Makefile
+++ b/tools/console/Makefile
@@ -28,9 +28,12 @@ distclean: clean
 xenconsoled: $(patsubst %.c,%.o,$(wildcard daemon/*.c))
        $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS) $(LDLIBS_xenconsoled) 
$(APPEND_LDFLAGS)
 
-xenconsole: $(patsubst %.c,%.o,$(wildcard client/*.c))
+xenconsole: $(patsubst %.c,%.o,$(wildcard client/*.c)) client/_paths.h
        $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS) $(LDLIBS_xenconsole) 
$(APPEND_LDFLAGS)
 
+genpath-target = $(call buildmakevars2header,client/_paths.h)
+$(eval $(genpath-target))
+
 .PHONY: install
 install: $(BIN)
        $(INSTALL_DIR) $(DESTDIR)/$(sbindir)
diff --git a/tools/console/client/main.c b/tools/console/client/main.c
index 753b3aa..eeb6e70 100644
--- a/tools/console/client/main.c
+++ b/tools/console/client/main.c
@@ -18,7 +18,9 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 \*/
 
+#include <sys/file.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <stdio.h>
@@ -40,10 +42,13 @@
 
 #include <xenstore.h>
 #include "xenctrl.h"
+#include "_paths.h"
 
 #define ESCAPE_CHARACTER 0x1d
 
 static volatile sig_atomic_t received_signal = 0;
+static char lockfile[sizeof (XEN_LOCK_DIR "/xenconsole.") + 8] = { 0 };
+static int lockfd = -1;
 
 static void sighandler(int signum)
 {
@@ -267,6 +272,51 @@ static void restore_term_stdin(void)
        restore_term(STDIN_FILENO, &stdin_old_attr);
 }
 
+/* The following locking strategy is based on that from
+ * libxl__domain_userdata_lock(), with the difference that we want to fail if 
we
+ * cannot acquire the lock rather than wait indefinitely.
+ */
+static void console_lock(int domid)
+{
+       struct stat stab, fstab;
+
+       snprintf(lockfile, sizeof lockfile, "%s%d", XEN_LOCK_DIR 
"/xenconsole.", domid);
+
+       while (true) {
+               lockfd = open(lockfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
+               if (lockfd < 0)
+                       err(errno, "Could not open %s", lockfile);
+
+               while (flock(lockfd, LOCK_EX | LOCK_NB)) {
+                       if (errno == EINTR)
+                               continue;
+                       else
+                               err(errno, "Could not lock %s", lockfile);
+               }
+               if (fstat(lockfd, &fstab))
+                       err(errno, "Could not fstat %s", lockfile);
+               if (stat(lockfile, &stab)) {
+                       if (errno != ENOENT)
+                               err(errno, "Could not stat %s", lockfile);
+               } else {
+                       if (stab.st_dev == fstab.st_dev && stab.st_ino == 
fstab.st_ino)
+                               break;
+               }
+
+               close(lockfd);
+       }
+       
+       return;
+}
+
+static void console_unlock(void)
+{
+       if (lockfile[0])
+               unlink(lockfile);
+       if (lockfd != -1)
+               close(lockfd);
+}
+
 int main(int argc, char **argv)
 {
        struct termios attr;
@@ -382,6 +432,9 @@ int main(int argc, char **argv)
                exit(EINVAL);
        }
 
+       console_lock(domid);
+       atexit(console_unlock);
+
        /* Set a watch on this domain's console pty */
        if (!xs_watch(xs, path, ""))
                err(errno, "Can't set watch for console pty");
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.