]> git.neil.brown.name Git - freerunner.git/blob - apm/interlock
apm/interlock: make sure /var/lock/suspend/suspend always exists.
[freerunner.git] / apm / interlock
1 #!/bin/sh
2
3 # This script should be run from /etc/apm/event.d/interlock
4 # It uses files in /var/lock/suspend to allow interlock with
5 # daemons that might need to know respond to suspend/resume events.
6 # Said daemon should:
7 #   1/  get a flock(LOCK_SH) on /var/lock/suspend/suspend,
8 #        looping if the file is found, after lock, to have no links
9 #   2/  use dnotify or similar to watch for changes to this file
10 #   3/  when the file has size > 0, prepare for suspend
11 #   3a/  take a flock(LOCK_SH) on /var/lock/suspend/next_suspend
12 #   3b/  releaes the lock on .../suspend
13 #   4/  use dnotify or similar to watch for next_suspend to be renamed to
14 #       suspend.  At that point wake-from-suspend processing can happen.
15
16 mkdir -p /var/lock/suspend
17
18 case $1 in
19
20   start)
21     > /var/lock/suspend/suspend
22   ;;
23   suspend)
24     # prepare for next cycle
25     > /var/lock/suspend/next_suspend
26     {
27        # wakeup daemons that care
28        echo suspending >& 9
29        # wait for those daemons to be ready
30        flock --exclusive 9
31        sleep 1000000000 &
32        echo $! > /var/lock/suspend/.pid
33     } 9>> /var/lock/suspend/suspend
34   ;;
35   resume )
36     > /var/lock/suspend/next_suspend
37     mv /var/lock/suspend/next_suspend /var/lock/suspend/suspend
38     pid=`cat /var/lock/suspend/.pid`
39     rm -f /var/lock/suspend/.pid
40     if [ "$pid" -gt 1 ]; then
41         kill -9 "$pid"
42     fi
43
44   ;;
45 esac