File suspend_evtchn_lock.patch of Package xen.import4534

Improve suspend eventchn lock, use flock instead of exclusive lock
file to avoid that sometimes the lock file is not removed cleanly 
and affacts later getting lock.
http://lists.xensource.com/archives/html/xen-devel/2010-11/msg01559.html 

Signed-off-by cyliu@novell.com

Index: xen-4.0.1-testing/tools/libxc/xc_suspend.c
===================================================================
--- xen-4.0.1-testing.orig/tools/libxc/xc_suspend.c
+++ xen-4.0.1-testing/tools/libxc/xc_suspend.c
@@ -6,25 +6,25 @@
 
 #include "xc_private.h"
 #include "xenguest.h"
+#include <sys/file.h>
+#include <sys/fcntl.h>
 
 #define SUSPEND_LOCK_FILE "/var/lib/xen/suspend_evtchn_lock.d"
 static int lock_suspend_event(void)
 {
     int fd, rc;
     mode_t mask;
-    char buf[128];
 
     mask = umask(022);
-    fd = open(SUSPEND_LOCK_FILE, O_CREAT | O_EXCL | O_RDWR, 0666);
+    fd = open(SUSPEND_LOCK_FILE, O_CREAT | O_RDWR, 0666);
     if (fd < 0)
     {
         ERROR("Can't create lock file for suspend event channel\n");
         return -EINVAL;
     }
     umask(mask);
-    snprintf(buf, sizeof(buf), "%10ld", (long)getpid());
+    rc = flock(fd, LOCK_EX | LOCK_NB);
 
-    rc = write_exact(fd, buf, strlen(buf));
     close(fd);
 
     return rc;
@@ -32,30 +32,21 @@ static int lock_suspend_event(void)
 
 static int unlock_suspend_event(void)
 {
-    int fd, pid, n;
-    char buf[128];
+    int fd, rc;
 
     fd = open(SUSPEND_LOCK_FILE, O_RDWR);
 
     if (fd < 0)
         return -EINVAL;
 
-    n = read(fd, buf, 127);
+    rc = flock(fd, LOCK_UN | LOCK_NB);
 
     close(fd);
 
-    if (n > 0)
-    {
-        sscanf(buf, "%d", &pid);
-        /* We are the owner, so we can simply delete the file */
-        if (pid == getpid())
-        {
-            unlink(SUSPEND_LOCK_FILE);
-            return 0;
-        }
-    }
+    if(!rc)
+	unlink(SUSPEND_LOCK_FILE);
 
-    return -EPERM;
+    return rc;
 }
 
 int xc_await_suspend(int xce, int suspend_evtchn)
@@ -110,8 +101,7 @@ int xc_suspend_evtchn_init(int xc, int x
     return suspend_evtchn;
 
 cleanup:
-    if (suspend_evtchn != -1)
-        xc_suspend_evtchn_release(xce, suspend_evtchn);
+    xc_suspend_evtchn_release(xce, suspend_evtchn);
 
     return -1;
 }
openSUSE Build Service is sponsored by