|
@@ -0,0 +1,83 @@
|
|
1
|
+#!/bin/bash
|
|
2
|
+
|
|
3
|
+ffoo import pretty
|
|
4
|
+ffoo import recon
|
|
5
|
+
|
|
6
|
+__zleep_user_part() {
|
|
7
|
+ saturnin iam undocking
|
|
8
|
+ slock &
|
|
9
|
+}
|
|
10
|
+
|
|
11
|
+__zleep_try_umount() {
|
|
12
|
+ local mp=$1
|
|
13
|
+ mountpoint -q $mp || return 0
|
|
14
|
+ umount $mp || return 1
|
|
15
|
+ mount | grep $mp && return 1
|
|
16
|
+ return 0
|
|
17
|
+}
|
|
18
|
+
|
|
19
|
+__zleep_try_umount_all() {
|
|
20
|
+ local mp
|
|
21
|
+ for mp in $(saturnin conf -p iam.mounting.point);
|
|
22
|
+ do
|
|
23
|
+ __zleep_try_umount $mp || return 1
|
|
24
|
+ done
|
|
25
|
+ return 0
|
|
26
|
+}
|
|
27
|
+
|
|
28
|
+__zleep_sudo_part() {
|
|
29
|
+ wait_until -d 5 -t 30 __zleep_try_umount_all || die "unable to unmount drives"
|
|
30
|
+ pm-suspend
|
|
31
|
+}
|
|
32
|
+
|
|
33
|
+__zleep_sudo_part_started() {
|
|
34
|
+ # i.e. password prompt went ok
|
|
35
|
+ test -e $ZLEEP_ON_ROOT
|
|
36
|
+ rv=$?
|
|
37
|
+ debug -v rv
|
|
38
|
+ return $rv
|
|
39
|
+}
|
|
40
|
+
|
|
41
|
+__zleep_user_part_done() {
|
|
42
|
+ ! test -e $ZLEEP_ON_USER
|
|
43
|
+ rv=$?
|
|
44
|
+ debug -v rv
|
|
45
|
+ return $rv
|
|
46
|
+}
|
|
47
|
+
|
|
48
|
+zleep() {
|
|
49
|
+ #
|
|
50
|
+ # Put the host on sleep
|
|
51
|
+ #
|
|
52
|
+ # We need to
|
|
53
|
+ #
|
|
54
|
+ # 1. authenticate
|
|
55
|
+ # 2. start user's part in the background -- mainly slock
|
|
56
|
+ # 3. start root's part -- put system on sleep (unmount, suspend)
|
|
57
|
+ #
|
|
58
|
+ # This is solved by forking with sudo, and waiting for user part to indicate
|
|
59
|
+ # it has finished. That way, we can ensure that suspend happens *after* the
|
|
60
|
+ # screen has been locked, and at the same time we leave authentication part
|
|
61
|
+ # on sudo.
|
|
62
|
+ #
|
|
63
|
+ if test -z "$ZLEEP_ON_USER"; ## we are parent (running as normal user)
|
|
64
|
+ then
|
|
65
|
+ ZLEEP_ON_USER=$(mktemp)
|
|
66
|
+ ZLEEP_ON_ROOT=$(mktemp -u)
|
|
67
|
+ debug -v ZLEEP_ON_USER ZLEEP_ON_ROOT
|
|
68
|
+ sudo \
|
|
69
|
+ ZLEEP_ON_ROOT=$ZLEEP_ON_ROOT \
|
|
70
|
+ ZLEEP_ON_USER=$ZLEEP_ON_USER \
|
|
71
|
+ saturnin iam zleeping &
|
|
72
|
+ wait_until -t 60 __zleep_sudo_part_started # i.e. password prompt done
|
|
73
|
+ __zleep_user_part
|
|
74
|
+ rm -f $ZLEEP_ON_USER
|
|
75
|
+ else ## we are child (running as root)
|
|
76
|
+ touch $ZLEEP_ON_ROOT
|
|
77
|
+ test $(id -u) -eq 0 || die "must be root"
|
|
78
|
+ wait_until __zleep_user_part_done || die "timeout waiting for parent"
|
|
79
|
+ __zleep_sudo_part
|
|
80
|
+ rm -f $ZLEEP_ON_ROOT
|
|
81
|
+ return $?
|
|
82
|
+ fi
|
|
83
|
+}
|