summaryrefslogtreecommitdiffstats
path: root/migrate-mdvonline-applet.pl
blob: ca6663984dcbd45b87fbb2ec4297bd1c7e6a9d7c (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/perl
################################################################################
# migrate-mdvonline-applet.pl                                                  # 
#                                                                              #
# Copyright (C) 2006 Mandriva                                                  #
#                                                                              #
# Thierry Vignaud <tvignaud at mandriva dot com>                               #
#                                                                              #
# This program is free software; you can redistribute it and/or modify         #
# it under the terms of the GNU General Public License Version 2 as            #
# published by the Free Software Foundation.                                   #
#                                                                              #
# This program is distributed in the hope that it will be useful,              #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with this program; if not, write to the Free Software                  #
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.   #
################################################################################

use lib qw(/usr/lib/libDrakX);
use standalone; # for explanations
use MDK::Common;

my $run_file = '/var/run/mdkapplet';

my $mode = $ARGV[0];

if (!-e $run_file) {
    # create the stamp file is needed:
    open(my $_tmp, '>>', $run_file);
} else {
    # exit if we're asked to restart the applets twice in less than 30 seconds
    # (eg the trigger script is run by both removed and newly installed packages,
    # which can lead to applet crash on SIGHUP because of race condition):
    my $mtime = (stat($run_file))[9];
    log::explanations("not restarting the applet (too many restart in a while)");
    exit(0) if time() - $mtime < 30;
}

if ($mode eq 'new') {
    system('killall', '-HUP', 'mdkapplet');
} else {
    #my @lines = `ps -o '$p $u %c'`;
    my @lines = `ps -eo pid,user,cmd`;
    
    # we do not live process ps output in order not to account both old applets and newly started ones:
    foreach (@lines) {
        my ($pid, $user, $cmd) = /^\s*(\d+)\s*(\S*)\s*(.*)$/;
        # do not match su running mdkapplet:
        next if $cmd !~ /perl.*mdkapplet/;
        my ($DISPLAY) = grep { /^DISPLAY=/ } split('\0', cat_("/proc/$pid/environ"));
        $DISPLAY =~ s/^DISPLAY=//;
        log::explanations(qq(killing "$cmd" (pid=$pid)));
        kill 15, $pid;
        my $pid2 = fork();
        if (defined $pid2) {
            local $ENV{DISPLAY} = $DISPLAY;
            !$pid2 and do { exec('su', $user, '-c', 'mdkapplet --auto-update') or do { require POSIX; POSIX::_exit() } };
            log::explanations("restarting applet (pid=$pid2)");
        } else {
            log::explanations(qq(failed to fork Mandriva Online applet for user "$user"));
        }
    }
}

my $atime = time();
utime(($atime) x 2, $run_file);