| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# needrestart-session - check for processes need to be restarted in user sessions |
| 4 |
# |
| 5 |
# Authors: |
| 6 |
# Thomas Liske <[email protected]> |
| 7 |
# |
| 8 |
# Copyright Holder: |
| 9 |
# 2014 - 2015 (C) Thomas Liske [http://fiasko-nw.net/~thomas/] |
| 10 |
# |
| 11 |
# License: |
| 12 |
# This program is free software; you can redistribute it and/or modify |
| 13 |
# it under the terms of the GNU General Public License as published by |
| 14 |
# the Free Software Foundation; either version 2 of the License, or |
| 15 |
# (at your option) any later version. |
| 16 |
# |
| 17 |
# This program is distributed in the hope that it will be useful, |
| 18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
# GNU General Public License for more details. |
| 21 |
# |
| 22 |
# You should have received a copy of the GNU General Public License |
| 23 |
# along with this package; if not, write to the Free Software |
| 24 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
# |
| 26 |
|
| 27 |
use Net::DBus; |
| 28 |
use Net::DBus::Service; |
| 29 |
use Net::DBus::Reactor; |
| 30 |
use Sys::Syslog qw(:standard :macros); |
| 31 |
use POSIX ":sys_wait_h"; |
| 32 |
use NeedRestart; |
| 33 |
|
| 34 |
use warnings; |
| 35 |
use strict; |
| 36 |
|
| 37 |
$0 = q(needrestart-dbus-session); |
| 38 |
|
| 39 |
BEGIN { |
| 40 |
openlog(q(needrestart-dbus-session), 'pid', LOG_USER); |
| 41 |
} |
| 42 |
|
| 43 |
END { |
| 44 |
syslog(LOG_INFO, q(terminated)); |
| 45 |
} |
| 46 |
|
| 47 |
sub WARN_handler { |
| 48 |
my $signal = shift; |
| 49 |
syslog(LOG_WARNING, q(%s), $signal); |
| 50 |
} |
| 51 |
|
| 52 |
sub DIE_handler { |
| 53 |
my $signal = shift; |
| 54 |
syslog(LOG_ERR, q(%s), $signal); |
| 55 |
exit; |
| 56 |
} |
| 57 |
|
| 58 |
$SIG{__WARN__} = q(WARN_handler); |
| 59 |
$SIG{__DIE__} = q(DIE_handler); |
| 60 |
|
| 61 |
syslog(LOG_INFO, q(%s %s launched), $0, $NeedRestart::VERSION); |
| 62 |
|
| 63 |
my $bus = Net::DBus->system(); |
| 64 |
|
| 65 |
# Net::DBUS seems to be buggy in service activation, trigger |
| 66 |
# service activation manually. |
| 67 |
$bus->get_bus_object->StartServiceByName(q(net.ibh.NeedRestart.System), 0); |
| 68 |
|
| 69 |
my $service = $bus->get_service(q(net.ibh.NeedRestart.System)); |
| 70 |
my $object = $service->get_object(q(/net/ibh/NeedRestart/System), |
| 71 |
q(net.ibh.NeedRestart.System)); |
| 72 |
|
| 73 |
my $nx11pid; |
| 74 |
$SIG{CHLD} = sub { |
| 75 |
while( (my $pid = waitpid(-1, WNOHANG)) > 0 ) { |
| 76 |
$nx11pid = undef if($nx11pid == $pid); |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
sub hNotifySessions { |
| 81 |
if(defined($nx11pid)) { |
| 82 |
syslog(LOG_INFO, q(received NotifySession signal but needrestart-session is already alive)); |
| 83 |
kill(q(USR1), $nx11pid); |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
syslog(LOG_INFO, q(received NotifySession signal)); |
| 88 |
|
| 89 |
$nx11pid = fork(); |
| 90 |
unless(defined($nx11pid)) { |
| 91 |
syslog(LOG_ERR, q(Unable to fork!)); |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
if($nx11pid == 0) { |
| 96 |
exec(qw(needrestart-session -n)); |
| 97 |
die(q(Unable to exec '/usr/bin/needrestart-session -n'!)); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
my $sig = $object->connect_to_signal(q(NotifySessions), \&hNotifySessions); |
| 102 |
|
| 103 |
my $reactor = Net::DBus::Reactor->main(); |
| 104 |
|
| 105 |
syslog(LOG_INFO, q(entering event loop...)); |
| 106 |
$reactor->run(); |
| 107 |
syslog(LOG_INFO, q(leaving event loop)); |