summaryrefslogtreecommitdiffstats
path: root/bin/watch-lock
blob: 8545efc9e97b186c0e53c2b9174780134c83f79a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env perl
use strict;
use warnings;
use 5.020;

use POSIX ":sys_wait_h";

$|++;

my $pid = open my $fh, '-|', (
    'dbus-monitor',
    '--system',
    'type=signal,interface=org.freedesktop.login1.Session'
) or die "couldn't spawn dbus-monitor: $!";
$SIG{CHLD} = sub {
    while ((my $child = waitpid -1, WNOHANG) > 0) {
        if ($child == $pid) {
            undef $pid;
        }
    }
    exit unless $pid;
};

sub cleanup {
    kill KILL => $pid if $pid;
    undef $pid;
}

$SIG{TERM} = $SIG{INT} = sub { cleanup; exit; };
END { cleanup }

while (<$fh>) {
    if (/\bmember=Lock\b/) {
        system("on-lock") && say "on-lock failed";
        say "LOCK";
    }
}