]> git.neil.brown.name Git - susman.git/commitdiff
Require process requesting immediate suspend to remain active.
authorNeilBrown <neilb@suse.de>
Fri, 16 Mar 2012 01:49:52 +0000 (12:49 +1100)
committerNeilBrown <neilb@suse.de>
Fri, 16 Mar 2012 01:49:52 +0000 (12:49 +1100)
The process that creates 'immediate' must do so while disabling
suspend and must take a lock on the file.
If the process dies, this lock will disappear and auto-suspension
will stop.
This is a good fail-safe for development.

Might be good to make it configurable later.

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

diff --git a/lsusd.c b/lsusd.c
index 5e5dbd9d9417113637a680f8a209a6450c00a5c2..53a7fe3c352de2f1ff0497c1c48873bc2ae67a47 100644 (file)
--- a/lsusd.c
+++ b/lsusd.c
@@ -145,6 +145,31 @@ static void wait_request(int dirfd)
        } while (!found_immediate && !found_request);
 }
 
+static int request_valid()
+{
+       /* check if the request to suspend is still valid.
+        * If the 'immediate' file is not locked, we remove
+        * and ignore it as the requesting process has died
+        */
+       int fd = open("/var/run/suspend/immediate", O_RDWR);
+       if (fd >= 0) {
+               if (flock(fd, LOCK_EX|LOCK_NB) == 0) {
+                       /* we got the lock, so owner must have died */
+                       unlink("/var/run/suspend/immediate");
+                       close(fd);
+               } else {
+                       /* Still valid */
+                       close(fd);
+                       return 1;
+               }
+       }
+       fd = open("/var/run/suspend/request", O_RDONLY);
+       if (fd < 0)
+               return 0;
+       close(fd);
+       return 1;
+}
+
 static void do_suspend(void)
 {
        int fd = open("/sys/power/state", O_RDWR);
@@ -191,6 +216,7 @@ main(int argc, char *argv)
                alert_watchers();
 
                if (flock(disable, LOCK_EX|LOCK_NB) == 0
+                   && request_valid()
                    && set_wakeup_count(count))
                        do_suspend();
                flock(disable, LOCK_UN);