Debian Codegraph @

needrestart-x11

raw ◀ ▶
2 Debian patches touch this file
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 warnings;
28 use strict;
29
30 use constant {
31 NRX11_ICO_FILE => q(/usr/share/needrestart-session/needrestart.xpm),
32 };
33
34 use NeedRestart::Utils;
35 use Wx qw(wxDefaultPosition wxDefaultSize wxPD_ELAPSED_TIME wxLC_REPORT wxALL wxTOP wxBOTTOM wxLIST_AUTOSIZE wxHORIZONTAL wxALIGN_CENTER wxEXPAND wxID_CLOSE wxID_REFRESH wxUSER_ATTENTION_INFO wxST_ELLIPSIZE_END wxBITMAP_TYPE_XPM);
36 use Wx::Event qw(EVT_BUTTON EVT_LIST_ITEM_ACTIVATED EVT_CLOSE EVT_TIMER);
37 use List::Util qw(max);
38 use Proc::ProcessTable;
39 use Cwd qw(realpath);
40
41 # Re-exec on SIGUSR1 - used by needrestart-dbus-session to trigger a
42 # refresh of needrestart-session
43 $SIG{USR1} = sub {
44 exec($0, @ARGV);
45 };
46
47 # Don't show any progressbar nor 'Nothing found...' message if there
48 # was a parameter supplied (as needrestart-dbus-session does).
49 my $runsilent = scalar @ARGV;
50
51 # get current session
52 my $csession;
53 if(open(HCGROUP, qq(/proc/$$/cgroup))) {
54 map {
55 chomp;
56 my ($id, $type, $value) = split(/:/);
57 $csession = $2 if($type eq q(name=systemd) && $value =~ m@/user-(\d+).slice/(session-\d+).scope@);
58 } <HCGROUP>;
59 close(HCGROUP);
60 }
61 my @plist;
62
63 # probe needrestart's PolicyKit integration
64 my $haspk = `pkaction --action-id net.fiasko-nw.needrestart 2> /dev/null`;
65
66 # prepare GUI stuff
67 my $app = Wx::SimpleApp->new;
68 $app->SetAppName('needrestart-session');
69
70 # while running in Wx's mainloop perl won't get any signals, therefore
71 # we just run a timer event every 5 seconds to make signal handling
72 # deterministic
73 my $timer = Wx::Timer->new( $app );
74 $timer->Start( 5000 );
75 EVT_TIMER($app, -1, sub { 1; });
76
77 my $frame = Wx::Frame->new(undef, -1, 'needrestart-session');
78 $frame->SetIcon(Wx::Icon->new(NRX11_ICO_FILE, wxBITMAP_TYPE_XPM))
79 if(-r NRX11_ICO_FILE);
80
81 my $sApps = Wx::StaticText->new($frame, -1, 'Some of your processes need to be restarted. You might need to re-login.', wxDefaultPosition, wxDefaultSize, wxST_ELLIPSIZE_END);
82 my $lApps = Wx::ListCtrl->new($frame, -1, wxDefaultPosition, [640, 480], wxLC_REPORT);
83 $lApps->InsertColumn(0, "Application");
84 $lApps->InsertColumn(1, "Session Type");
85 $lApps->InsertColumn(2, "Session Id");
86 $lApps->InsertColumn(3, "PIDs");
87
88 &RefreshAppsList;
89
90 $lApps->SetColumnWidth(0, wxLIST_AUTOSIZE);
91 $lApps->SetColumnWidth(1, wxLIST_AUTOSIZE);
92 $lApps->SetColumnWidth(2, wxLIST_AUTOSIZE);
93 $lApps->SetColumnWidth(3, wxLIST_AUTOSIZE);
94
95 my $bsButtons = Wx::BoxSizer->new(wxHORIZONTAL);
96 my $bRoot = Wx::Button->new($frame, -1, '&System Services');
97 $bRoot->Enable($haspk);
98 my $bRecheck = Wx::Button->new($frame, wxID_REFRESH);
99 my $bClose = Wx::Button->new($frame, wxID_CLOSE);
100
101 my $sDClick = Wx::StaticText->new($frame, -1, '*) Double click to bring application window to front.');
102
103 my $fgs = Wx::FlexGridSizer->new(6, 1, 0, 0);
104 $fgs->Add($sApps, 0, wxEXPAND | wxTOP | wxBOTTOM, 4);
105 $fgs->Add($lApps, 0, wxEXPAND);
106 $fgs->Add($sDClick, 0, wxEXPAND | wxTOP | wxBOTTOM, 4);
107 $bsButtons->Add($bRoot, 0, wxALL, 4);
108 $bsButtons->Add($bRecheck, 0, wxALL, 4);
109 $bsButtons->Add($bClose, 0, wxALL, 4);
110 $fgs->Add($bsButtons, 0, wxALIGN_CENTER);
111
112 $fgs->AddGrowableCol(0);
113 $fgs->AddGrowableRow(1);
114
115 $frame->SetSizerAndFit($fgs);
116 $frame->SetAutoLayout(1);
117 $frame->Centre();
118
119 # bind event handlers
120 EVT_BUTTON($frame, $bRoot, \&OnRunAsRoot);
121 EVT_BUTTON($frame, $bRecheck, \&OnRecheck);
122 EVT_BUTTON($frame, $bClose, \&OnClose);
123 EVT_CLOSE($frame, \&OnClose);
124 EVT_LIST_ITEM_ACTIVATED($frame, $lApps, \&OnAppsDClick);
125
126 # show main window and enter event loop
127 $frame->Show;
128 $app->MainLoop;
129
130 sub RefreshAppsList {
131 my $frProgress = ($runsilent ? undef : Wx::ProgressDialog->new("", "", 4, undef, wxPD_ELAPSED_TIME));
132
133 # get user processes required to be restarted
134 my %fnames;
•135 my $fh = nr_fork_pipe(0, qw(/usr/sbin/needrestart -b));
136 $frProgress->SetTitle(q(Scanning processes...)) if(defined($frProgress));
137 while(<$fh>) {
138 chomp;
139
140 next unless(/^NEEDRESTART-PID: (.+)=([\d,]+)$/);
141
142 @{ $fnames{$1} } = split(/,/, $2);
143 if(defined($frProgress)) {
144 $frProgress->Update(1, $1);
145 $frProgress->Pulse;
146 }
147 }
148 close($fh);
149
150 # terminate if no orphan process is found
151 if(scalar keys %fnames == 0) {
152 $frProgress->Destroy if(defined($frProgress));
153
154 unless($runsilent) {
155 my $msg = Wx::MessageDialog->new($frame, "None of your processes need to be restarted.", "Nothing found...");
156 $msg->ShowModal;
157 $msg->Destroy;
158 }
159 exit;
160 }
161
162 # get list of pid => window mapping
163 $frProgress->SetTitle(q(Scanning X11 windows...)) if(defined($frProgress));
164 my %ptable = map {$_->pid => $_} @{ new Proc::ProcessTable(enable_ttys => 1)->table };
165 my %windows;
166 my %wtitles;
167 $fh = nr_fork_pipe(0, qw(wmctrl -l -p));
168 while(<$fh>) {
169 chomp;
170
171 next unless(/^(0x[\da-f]+) +-?\d+ +(\d+) +(.+)/);
172
173 $windows{$2} = $1;
174 $wtitles{$2} = $3;
175 if(defined($frProgress)) {
176 $frProgress->Update(2, $ptable{$2}->{fname});
177 $frProgress->Pulse;
178 }
179 }
180 close($fh);
181
182 # build process list from `needrestart` and `wmctrl` outputs
183 $frProgress->SetTitle(q(Updating list...)) if(defined($frProgress));
184 my %plist;
185 foreach my $fname (keys %fnames) {
186 foreach my $pid (@{$fnames{$fname}}) {
187 my $session = '?';
188 my $type = '';
189 if(exists($ptable{$pid})) {
190 if($ptable{$pid}->{ttydev} ne '') {
191 $session = realpath( $ptable{$pid}->{ttydev} );
192 }
193 elsif(open(HCGROUP, qq(/proc/$pid/cgroup))) {
194 map {
195 chomp;
196 my ($id, $type, $value) = split(/:/);
197 $session = $2 if($type eq q(name=systemd) && $value =~ m@/user-(\d+).slice/(session-\d+).scope@);
198 } <HCGROUP>;
199 close(HCGROUP);
200 }
201
202 if(defined($csession) && $session ne '?') {
203 $type = ($session eq $csession ? 'current' : 'foreign');
204 }
205 }
206
207 if(grep { $_ == $pid } keys %windows) {
208 push(@{ $plist{$fname}->{plist} }, {
209 fname => $fname,
210 PIDs => [$pid],
211 sessid => $session,
212 sessty => $type,
213 winid => $windows{$pid},
214 winti => $wtitles{$pid},
215 });
216 }
217 elsif(exists($plist{$fname}->{sessions}->{$session})) {
218 push(@{ $plist{$fname}->{sessions}->{$session}->{PIDs} }, $pid);
219 }
220 else {
221 $plist{$fname}->{sessions}->{$session} = {
222 fname => $fname,
223 PIDs => [$pid],
224 sessid => $session,
225 sessty => $type,
226 };
227 }
228 }
229
230 if(defined($frProgress)) {
231 $frProgress->Update(3, $fname);
232 $frProgress->Pulse;
233 }
234 }
235
236 @plist = (
237 (map { (exists($plist{$_}->{plist}) ? @{ $plist{$_}->{plist} } : ()); } keys %plist),
238 (map {
239 # sort PIDs ascending
240 foreach my $sess (keys %{ $plist{$_}->{sessions} }) {
241 $plist{$_}->{sessions}->{$sess}->{PIDs} = [sort {$a <=> $b} @{ $plist{$_}->{sessions}->{$sess}->{PIDs} }];
242 }
243 (values %{ $plist{$_}->{sessions} });
244 } keys %plist),
245 );
246
247 # sort processes by session, fname and pid
248 @plist = sort {
249 $a->{sessty} cmp $b->{sessty} ||
250 $a->{sessid} cmp $b->{sessid} ||
251 $a->{fname} cmp $b->{fname} ||
252 $a->{PIDs}->[0] <=> $b->{PIDs}->[0]
253 } @plist;
254
255 # (re)fill app list control
256 $lApps->DeleteAllItems;
257 my $pos = 0;
258 foreach my $data (@plist) {
259 $lApps->InsertStringItem($pos, $data->{fname} . (exists($data->{winti}) ? '*' : ''));
260 $lApps->SetItem($pos, 1, $data->{sessty});
261 $lApps->SetItem($pos, 2, $data->{sessid});
262 $lApps->SetItem($pos++, 3, join(', ', @{ $data->{PIDs} }));
263 }
264
265 $frProgress->Destroy if(defined($frProgress));
266
267 # Don't keep silence if user hits 'Refresh'.
268 $runsilent++;
269 }
270
271 # make selected window active
272 sub OnAppsDClick {
273 my ($frame, $event) = @_;
274
275 system(qw(wmctrl -i -a), $plist[$event->GetIndex]->{winid}) if(exists($plist[$event->GetIndex]->{winid}));
276 }
277
278 sub OnRunAsRoot {
279 my %frontends = (
280 kde => [qw(QtCore4 QtGui4)],
281 );
282 my $desktop = lc($ENV{XDG_CURRENT_DESKTOP});
283 my $frontend = q(gnome);
284
285 # use desktop specific frontend if available
286 if(defined($desktop) && exists($frontends{$desktop})) {
287 my $e = map { eval "require $_;"; if($@) {(1);} else {()}; } @{$frontends{$desktop}};
288 $frontend = $desktop unless($e);
289 }
290
291 # run needrestart as root using PolicyKit and overwritten debconf frontend
292 my $pid = fork();
293 unless($pid) {
294 exec(qw(pkexec /usr/sbin/needrestart -f), $frontend);
295 die;
296 }
297 }
298
299 sub OnRecheck {
300 RefreshAppsList;
301 }
302
303 sub OnClose {
304 $app->ExitMainLoop;
305 }