File: needrestart

package info (click to toggle)
needrestart 1.2-7~bpo70+1
  • links: PTS
  • area: main
  • in suites: wheezy-backports
  • size: 524 kB
  • sloc: perl: 1,562; sh: 43; makefile: 25
file content (577 lines) | stat: -rwxr-xr-x 15,155 bytes parent folder | download | duplicates (3)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#!/usr/bin/perl

# needrestart - Restart daemons after library updates.
#
# Authors:
#   Thomas Liske <thomas@fiasko-nw.net>
#
# Copyright Holder:
#   2013 - 2014 (C) Thomas Liske [http://fiasko-nw.net/~thomas/]
#
# License:
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   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 package; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

use Getopt::Std;
use NeedRestart;
use NeedRestart::UI;
use NeedRestart::Interp;
use NeedRestart::Kernel;
use NeedRestart::Utils;

use warnings;
use strict;

$|++;
$Getopt::Std::STANDARD_HELP_VERSION++;

my $LOGPREF = '[main]';
my $is_systemd = -d qq(/run/systemd/system);

sub HELP_MESSAGE {
    print <<USG;
Usage:

  needrestart [-vn] [-c <cfg>] [-r <mode>] [-bkl]

    -v		be more verbose
    -n		set default answer to 'no'
    -c <cfg>	config filename
    -r <mode>	set restart mode
	l	(l)ist only
	i	(i)nteractive restart
	a	(a)utomatically restart
    -b		enable batch mode

  By using the following options only the specified checks are performed:
    -k          check for obsolete kernel
    -l          check for obsolete libraries

    --help      show this help
    --version   show version information

USG
}

sub VERSION_MESSAGE {
    print <<LIC;

needrestart $NeedRestart::VERSION - Restart daemons after library updates.

Authors:
  Thomas Liske <thomas\@fiasko-nw.net>

Copyright Holder:
  2013 - 2014 (C) Thomas Liske [http://fiasko-nw.net/~thomas/]

Upstream:
  https://github.com/liske/needrestart

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

LIC
#/
}

our %nrconf = (
    verbose => 0,
    hook_d => '/etc/needrestart/hook.d',
    restart => 'i',
    defno => 0,
    blacklist => [],
    blacklist_rc => [],
    override_rc => [],
    interpscan => 1,
    kernelhints => 1,
);

# backup ARGV (required for Debconf)
my @argv = @ARGV;

our $opt_c = '/etc/needrestart/needrestart.conf';
our $opt_v;
our $opt_r;
our $opt_n;
our $opt_b;
our $opt_k;
our $opt_l;
unless(getopts('c:vr:nbkl')) {
    HELP_MESSAGE;
    exit 1;
}

# restore ARGV
@ARGV = @argv;

die "ERROR: Could not read config file '$opt_c'!\n" unless(-r $opt_c || $opt_b);

# slurp config file
eval `cat "$opt_c"`;
die "$@\n" if($@);

# fallback to stdio on verbose mode
$nrconf{verbose}++ if($opt_v);
$nrconf{ui} = qq(NeedRestart::UI::stdio) if($nrconf{verbose});

die "Hook directory '$nrconf{hook_d}' is invalid!\n" unless(-d $nrconf{hook_d} || $opt_b);
$opt_r = $nrconf{restart} unless(defined($opt_r));
die "ERROR: Unknown restart option '$opt_r'!\n" unless($opt_r =~ /^(l|i|a)$/);

$nrconf{defno}++ if($opt_n);

warn "WARNING: This program should be run as root!\n" if($< != 0);

# get current runlevel, fallback to '2'
my $runlevel = `who -r` || '';
chomp($runlevel);
$runlevel = 2 unless($runlevel =~ s/^.+run-level (\S)\s.+$/$1/);

# get UI
my $ui = ($opt_b ? NeedRestart::UI->new(1) : needrestart_ui($nrconf{verbose}, $nrconf{ui}));
die "Error: no UI class available!\n" unless(defined($ui));

# enable/disable checks
unless(defined($opt_k) || defined($opt_l)) {
    $opt_k = $opt_l = 1;
}

sub parse_lsbinit($) {
    my $rc = '/etc/init.d/'.shift;
    my %lsb;

    open(HLSB, '<', $rc) || die "Can't open $rc: $!\n";
    my $found;
    while(my $line = <HLSB>) {
	unless($found) {
	    $found++ if($line =~ /^### BEGIN INIT INFO/);
	    next;
	}
	elsif($line =~ /^### END INIT INFO/) {
	    last;
	}

	chomp($line);
	$lsb{lc($1)} = $2 if($line =~ /^# ([^:]+):\s+(.+)$/);
    }

    unless($found) {
	print STDERR "WARNING: $rc has no LSB tags!\n" unless(%lsb);
	return undef;
    }

    # pid file heuristic
    $found = 0;
    my %pidfiles;
    while(my $line = <HLSB>) {
	if($line =~ m@(\S*/run/[^/]+.pid)@ && -r $1) {
	    $pidfiles{$1}++;
	    $found++;
	}
    }
    $lsb{pidfiles} = [keys %pidfiles] if($found);
    close(HLSB);

    return %lsb;
}

print STDERR "$LOGPREF detected systemd\n" if($nrconf{verbose} && $is_systemd);

my @systemd_restart;
sub restart_cmd($) {
    my $rc = shift;

    if($rc =~ /.+\.service$/) {
	push(@systemd_restart, $rc);
	();
    }
    elsif($rc eq q(systemd manager)) {
	(qw(systemctl daemon-reexec));
    }
    elsif($rc eq q(sysv init)) {
	(qw(telinit u));
    }
    else {
	(q(service), $rc, q(restart));
    }
}

my %restart;
if(defined($opt_l)) {
    my @ign_pids=($$, getppid());

    # inspect only pids
    my $ptable = nr_ptable();
    $ui->progress_prep(scalar keys %$ptable, 'Scanning processes...');
    my %stage2;
    for my $pid (sort {$a <=> $b} keys %$ptable) {
	$ui->progress_step;

	# skip myself
	next if(grep {$pid == $_} @ign_pids);

	my $restart = 0;
	my $exe = nr_readlink($pid);

	# ignore kernel threads
	next unless(defined($exe));

	# orphaned binary
	$restart++ if (defined($exe) && $exe =~ s/ \(deleted\)$//);  # Linux
	$restart++ if (defined($exe) && $exe =~ s/^\(deleted\)//);   # Linux VServer
	print STDERR "$LOGPREF #$pid uses obsolete binary $exe\n" if($restart && $nrconf{verbose});

	# ignore blacklisted binaries
	next if(grep { $exe =~ /$_/; } @{$nrconf{blacklist}});

	# read file mappings (Linux 2.0+)
	unless($restart) {
	    open(HMAP, '<', "/proc/$pid/maps") || next;
	    while(<HMAP>) {
		chomp;
		my ($maddr, $mperm, $moffset, $mdev, $minode, $path) = split(/\s+/);

		# skip special handles and non-executable mappings
		next unless(defined($path) && $minode != 0 && $path ne '' && $mperm =~ /x/);

		# skip special device paths
		next if($path =~ m@^/(SYSV00000000$|drm$|dev/)@);

		# skip aio(7) mapping
		next if($path =~ m@^/\[aio\]@);

		# check for non-existing libs
		unless(-e $path) {
		    unless($path =~ m@^/tmp/@) {
			print STDERR "$LOGPREF #$pid uses non-existing $path\n" if($nrconf{verbose});
			$restart++;
			last;
		    }
		}

		# get on-disk info
		my ($sdev, $sinode) = stat($path);
		last unless(defined($sinode));
		my @sdevs = (
		    # glibc gnu_dev_* definition from sysmacros.h
		    sprintf("%02x:%02x", (($sdev >> 8) & 0xfff) | (($sdev >> 32) & ~0xfff), (($sdev & 0xff) | (($sdev >> 12) & ~0xff))),
		    # Traditional definition of major(3) and minor(3)
		    sprintf("%02x:%02x", $sdev >> 8, $sdev & 0xff),
		    # kFreeBSD: /proc/<pid>/maps does not contain device IDs
		    qq(00:00)
		    );

		# compare maps content vs. on-disk
		unless($minode eq $sinode && ((grep {$mdev eq $_} @sdevs) ||
					      # BTRFS breaks device ID mapping completely...
					      # ignoring unnamed device IDs for now
					      $mdev =~ /^00:/)) {
		    print STDERR "$LOGPREF #$pid uses obsolete $path\n" if($nrconf{verbose});
		    $restart++;
		    last;
		}
	    }
	    close(HMAP);
	}

	unless($restart || !$nrconf{interpscan}) {
	    $restart++ if(needrestart_interp_check($nrconf{verbose}, $pid, $exe));
	}

	# restart needed?
	next unless($restart);

	# find parent process
	my $ppid = $ptable->{$pid}->{ppid};
	if($ppid != $pid && $ppid > 1) {
	    print STDERR "$LOGPREF #$pid is a child of #$ppid\n" if($nrconf{verbose});

	    unless(exists($stage2{$ppid})) {
		my $pexe = nr_readlink($ppid);
		# ignore kernel threads
		next unless(defined($pexe));

		$stage2{$ppid} = $pexe;
	    }
	}
	else {
	    print STDERR "$LOGPREF #$pid is not a child\n" if($nrconf{verbose});
	    $stage2{$pid} = $exe;
	}
    }
    $ui->progress_fin;

    if(scalar keys %stage2) {
	$ui->progress_prep(scalar keys %stage2, 'Scanning candidates...');
	foreach my $pid (sort {$a <=> $b} keys %stage2) {
	    $ui->progress_step;

	    # skip myself
	    next if(grep {$pid == $_} @ign_pids);

	    my $exe = nr_readlink($pid);
	    $exe =~ s/ \(deleted\)$//;  # Linux
	    $exe =~ s/^\(deleted\)//;   # Linux VServer
	    print STDERR "$LOGPREF #$pid exe => $exe\n" if($nrconf{verbose});

	    # try to find interpreter source file
	    ($exe) = (needrestart_interp_source($nrconf{verbose}, $pid, $exe), $exe);

	    # ignore blacklisted binaries
	    next if(grep { $exe =~ /$_/; } @{$nrconf{blacklist}});

	    if($is_systemd) {
		# systemd manager
		if($pid == 1 && $exe =~ m@^/lib/systemd/systemd@) {
		    print STDERR "$LOGPREF #$pid is systemd manager\n" if($nrconf{verbose});
		    $restart{q(systemd manager)}++;
		    next;
		}

		# get unit name from /proc/<pid>/cgroup
		if(open(HCGROUP, qq(/proc/$pid/cgroup))) {
		    my ($rc) = map {
			chomp;
			my ($id, $type, $value) = split(/:/);
			if($type ne q(name=systemd)) {
			    ();
			}
			else {
			    if($value =~ m@/([^/]+\.service)$@) {
				($1);
			    }
			    elsif($value = m@/user-(\d+).slice/session-(\d+).scope@) {
				print STDERR "$LOGPREF #$pid is ignored; part of user session: uid=$1 sess=$2\n" if($nrconf{verbose});
				next;
			    }
			    else {
				print STDERR "$LOGPREF #$pid unexpected cgroup '$value'\n" if($nrconf{verbose});
				();
			    }
			}
		    } <HCGROUP>;
		    close(HCGROUP);

		    if($rc) {
			print STDERR "$LOGPREF #$pid is $rc\n" if($nrconf{verbose});
			$restart{$rc}++;
			next;
		    }
		}

		# did not get the unit name, yet - try systemctl status
		print STDERR "$LOGPREF /proc/#$pid/cgroup: $! - trying systemctl status\n" if($nrconf{verbose} && $!);
		my $systemctl = nr_fork_pipe($nrconf{verbose}, qq(systemctl), qq(-n), qq(0), qq(--full), qq(status), $pid);
		my $ret = <$systemctl>;
		close($systemctl);

		if(defined($ret) && $ret =~ /([^.\s]+\.service) /) {
		    my $s = $2;
		    print STDERR "$LOGPREF #$pid is $s\n" if($nrconf{verbose});
		    $restart{$s}++;
		    $s =~ s/\.service$//;
		    delete($restart{$s});
		    next;
		}
	    }
	    else {
		# sysv init
		if($pid == 1 && $exe =~ m@^/sbin/init@) {
		    print STDERR "$LOGPREF #$pid is sysv init\n" if($nrconf{verbose});
		    $restart{q(sysv init)}++;
		    next;
		}
	    }

	    my $pkg;
	    foreach my $hook (sort <$nrconf{hook_d}/*>) {
		print STDERR "$LOGPREF #$pid running $hook\n" if($nrconf{verbose});

		my $found = 0;
		my $prun = nr_fork_pipe($nrconf{verbose}, $hook, ($nrconf{verbose} ? qw(-v) : ()), $exe);
		my @nopids;
		while(<$prun>) {
		    chomp;
		    my @v = split(/\|/);

		    if($v[0] eq 'PACKAGE' && $v[1]) {
			$pkg = $v[1];
			print STDERR "$LOGPREF #$pid package: $v[1]\n" if($nrconf{verbose});
			next;
		    }

		    if($v[0] eq 'RC') {
			my %lsb = parse_lsbinit($v[1]);

			unless(%lsb && exists($lsb{'default-start'})) {
			    # If the script has no LSB tags we consider to call it later - they
			    # are broken anyway.
			    print STDERR "$LOGPREF no LSB headers found at $v[1]\n" if($nrconf{verbose});
			    push(@nopids, $v[1]);
			}
			# In the run-levels S and 1 no daemons are being started (normaly).
			# We don't call any rc.d script not started in the current run-level.
			elsif($lsb{'default-start'} =~ /$runlevel/) {
			    # If a pidfile has been found, try to look for the daemon and ignore
			    # any forked/detached childs (just a heuristic due Debian Bug#721810).
			    if(exists($lsb{pidfiles})) {
				foreach my $pidfile (@{ $lsb{pidfiles} }) {
				    open(HPID, '<', "$pidfile") || next;
				    my $p = <HPID>;
				    close(HPID);

				    if(int($p) == $pid) {
					print STDERR "$LOGPREF #$pid has been started by $v[1] - triggering\n" if($nrconf{verbose});
					$restart{$v[1]}++;
					$found++;
					last;
				    }
				}
			    }
			    else {
				print STDERR "$LOGPREF no pidfile reference found at $v[1]\n" if($nrconf{verbose});
				push(@nopids, $v[1]);
			    }
			}
			else {
			    print STDERR "$LOGPREF #$pid rc.d script $v[1] should not start in the current run-level($runlevel)\n" if($nrconf{verbose});
			}
		    }
		}

		# No perfect hit - call any rc scripts instead.
		if(!$found && $#nopids > -1) {
		    foreach my $rc (@nopids) {
			if($is_systemd && exists($restart{"$rc.service"})) {
			    print STDERR "$LOGPREF #$pid rc.d script $rc seems to be superseeded by $rc.service\n" if($nrconf{verbose});
			}
			else {
			    $restart{$rc}++;
			}
		    }
		    $found++;
		}

		last if($found);
	    }
	}
	$ui->progress_fin;
    }
}

# Apply rc/service blacklist
foreach my $rc (keys %restart) {
    next unless(scalar grep { $rc =~ /$_/; } @{$nrconf{blacklist_rc}});

    print STDERR "$LOGPREF $rc is blacklisted -> ignored\n" if($nrconf{verbose});
    delete($restart{$rc});
}

print "NEEDRESTART-VER: $NeedRestart::VERSION\n" if($opt_b);

if(defined($opt_k)) {
    my ($kresult, %kvars) = ($nrconf{kernelhints} || $opt_b ? nr_kernel_check($nrconf{verbose}, $ui) : ());

    if(defined($kresult)) {
	if($opt_b) {
	    print "NEEDRESTART-KCUR: $kvars{KVERSION}\n";
	    print "NEEDRESTART-KEXP: $kvars{EVERSION}\n" if(defined($kvars{EVERSION}));
	    print "NEEDRESTART-KSTA: $kresult\n";
	}
	else {
	    if($kresult == NRK_NOUPGRADE) {
		$ui->notice('Running kernel seems to be up-to-date.');
	    }
	    elsif($kresult == NRK_ABIUPGRADE) {
		$ui->announce_abi(%kvars);
	    }
	    elsif($kresult == NRK_VERUPGRADE) {
		$ui->announce_ver(%kvars);
	    }
	    else {
		$ui->notice('Failed to retrieve available kernel versions.');
	    }
	}
    }
}

if(defined($opt_l)) {
    unless(scalar %restart) {
	$ui->notice('No services need to be restarted.') unless($opt_b);
    }
    else {
	if($opt_b || $opt_r ne 'i') {
	    $ui->notice('Services to be restarted:');
	    
	    foreach my $rc (sort { lc($a) cmp lc($b) } keys %restart) {
		if($opt_b) {
		    print "NEEDRESTART-SVC: $rc\n";
		    next;
		}

		# don't restart greylisted services...
		my $restart = !$nrconf{defno};
		foreach my $re (keys %{$nrconf{override_rc}}) {
		    next unless($rc =~ /$re/);

		    $restart = $nrconf{override_rc}->{$re};
		    last;
		}
		# ...but complain about them
		unless($restart) {
		    $ui->notice("Skipping $rc...");
		    next;
		}

		my @cmd = restart_cmd($rc);
		next unless($#cmd > -1);

		if($opt_r eq 'a') {
		    system(@cmd);
		}
		else {
		    $ui->notice(join(' ', @cmd));
		}
	    }
	
	    unless($opt_b || $#systemd_restart == -1) {
		my @cmd = (qq(systemctl), qq(restart), @systemd_restart);
		if($opt_r eq 'a') {
		    $ui->notice('Restarting services using systemd...');
		    system(@cmd);
		}
		else {
		    $ui->notice(join(' ', @cmd));
		}
	    }
	}
	else {
	    my $o = 0;

	    $ui->query_pkgs('Services to be restarted:', $nrconf{defno}, \%restart, $nrconf{override_rc},
			    sub {
				my @cmd = restart_cmd(shift);
				system(@cmd) if($#cmd > -1);
			    });

	    if($#systemd_restart > -1) {
		$ui->notice('Restarting services using systemd...');
		system(qq(systemctl), qq(restart), @systemd_restart);
	    }
	}
    }
}