]> git.neil.brown.name Git - susman.git/commitdiff
request_suspend: change check for suspend-is-disabled.
authorNeilBrown <neilb@suse.de>
Thu, 15 Mar 2012 20:40:00 +0000 (07:40 +1100)
committerNeilBrown <neilb@suse.de>
Thu, 15 Mar 2012 20:40:00 +0000 (07:40 +1100)
Both request_suspend and lsusd try to abort the request if suspend is
disabled.  However as they use the same mechanism they can trip over
each other and produce the wrong result.

So leave the primary checking to lsusd.  It will remove  the 'request'
file if needed, so request_suspend will notice.  It can then check
if a suspend actually happened by examining the 'watching' file.

Also: change request_suspend to exit(2) on strange error, and exit(1)
only if suspend was blocked.

Signed-off-by: NeilBrown <neilb@suse.de>
request_suspend.c

index 3a60341522cd45ae0751da64369ed6f45097481b..c1141ee5a950aebf7d6ca9bb2d1891d224786c20 100644 (file)
@@ -34,12 +34,13 @@ main(int argc, char *argv[])
        int dirfd = open("/var/run/suspend", O_RDONLY);
        int fd_request = open("/var/run/suspend/request",
                              O_RDWR|O_CREAT, 0640);
+       int fd_watching = open("/var/run/suspend/watching", O_RDONLY);
        if (fd_request < 0)
-               exit(1);
+               exit(2);
 
 
        if (dirfd < 0)
-               exit(1);
+               exit(2);
        /* Wait for unlink */
        while (1) {
                int fd;
@@ -54,16 +55,22 @@ main(int argc, char *argv[])
                fcntl(dirfd, F_NOTIFY, DN_DELETE);
 
                if (fstat(fd_request, &stat) != 0
-                   || stat.st_nlink == 0)
-                       exit(0);
-               fd = open("/var/run/suspend/disabled", O_RDONLY);
-               if (fd >= 0) {
-                       if (flock(fd, LOCK_EX|LOCK_NB) != 0) {
-                               /* Someone is blocking suspend */
-                               unlink("/var/run/suspend/request");
+                   || stat.st_nlink == 0) {
+                       struct stat s1, s2;
+                       int fd_watching2 = open("/var/run/suspend/watching",
+                                               O_RDONLY);
+                       if (fd_watching < 0 ||
+                           fd_watching2 < 0 ||
+                           fstat(fd_watching, &s1) < 0 ||
+                           fstat(fd_watching2, &s2) < 0)
+                               /* something strange */
+                               exit(2);
+                       if (s1.st_ino == s2.st_ino)
+                               /* Didn't suspend - someone must be
+                                * blocking suspend
+                                */
                                exit(1);
-                       }
-                       close(fd);
+                       exit(0);
                }
 
                sigsuspend(&oldset);