tails-htp-notify-user 1.52 KB
Newer Older
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
#!/usr/bin/perl

use strict;
use warnings;
use 5.10.1;

#man{{{

=head1 NAME

tails-htp-notify-user

=head1 VERSION

Version X.XX

=head1 AUTHOR

Tails dev team <tails@boum.org>
See https://tails.boum.org/.

=cut

#}}}

use Data::Dumper;
use Desktop::Notify;
use English '-no_match_vars';
29
use Locale::TextDomain 'tails';
30
use POSIX;
31
32

### initialization
33

34
35
my $htp_done_file    = '/run/htpdate/done';
my $htp_success_file = '/run/htpdate/success';
36
37
38
39
40
my $htp_log_file     = '/var/log/htpdate.log';
my $debug;

### subroutines

Tails developers's avatar
Tails developers committed
41
sub debug { say STDERR $_[0] if $debug; }
42
43
44
45
46
47
48
49
50

### main

exit 0 if -e $htp_success_file;

my $notify = Desktop::Notify->new()
    or die "Failed creating Desktop::Notify object.";
debug('$notify:' . "\n" . Dumper($notify));

51
# Wait until htpdate is done
52
until ( -e $htp_done_file ) {
53
54
55
56
    sleep 1;
}

# in case htpdate failed, notify the user with the corresponding logs
57
unless (-e $htp_success_file) {
58
59
60
61
    open(my $htp_log, '<', $htp_log_file)
        or die "Can not open file '$htp_log_file': $OS_ERROR";
    my $last_log;
    while (<$htp_log>) {
62
        if ($_ =~ /^Running htpdate\./) {
63
64
65
66
67
            $last_log = '';
            next;
        }
        $last_log .= $_;
    }
68
    my $failure_summary      = __("Failed to synchronize the clock!");
69
70
71
72
73
74
    my $failure_body         = $last_log;
    my $failure_notification = $notify->create(summary => $failure_summary,
                                               body    => $failure_body,
                                               timeout => 0);
    $failure_notification->show();
}