Remove unused variables pos, n from __reverse_patches.
[ksplice.git] / ksplice.pm.in
blobb4df72d0e9fdf92f17f6944edce20848c37a2674
1 package ksplice;
2 use Cwd 'abs_path';
3 use File::Temp qw/:mktemp/;
4 use strict;
5 use warnings;
6 require Exporter;
7 our @ISA = qw(Exporter);
8 our @EXPORT = qw($datadir $libexecdir $verbose $version_str init_tmpdir runcd runstr runval runval_raw runsuc unpack_update check_in_proc);
10 our ($datadir, $libexecdir) = qw(KSPLICE_DATA_DIR KSPLICE_LIBEXEC_DIR);
11 our $version_str = "Ksplice version PACKAGE_VERSION\n";
12 our $verbose = 0;
14 sub init_tmpdir {
15 my $argdir = $ENV{TMPDIR};
16 $argdir = "/tmp" if(!defined $argdir || ! -d $argdir);
17 die if(! -d $argdir);
19 my $tmpdir = mkdtemp("$argdir/ksplice-tmp-XXXXXX");
20 die if(!defined $tmpdir || ! -d $tmpdir);
21 return $tmpdir;
24 sub runcd {
25 my ($dir) = @_;
26 print "chdir($dir)\n" if($verbose);
27 if(!chdir($dir)) {
28 die "Failed during: chdir($dir)\n";
32 sub runval {
33 my (@cmd) = @_;
34 if(runval_raw(@cmd) != 0) {
35 die "Failed during: @cmd\n";
39 sub runval_raw {
40 my (@cmd) = @_;
41 my ($out, $err);
42 print "@cmd\n" if($verbose);
43 if(!$verbose) {
44 open $out, ">&STDOUT" or die "Can't dup STDOUT: $!";
45 open $err, ">&STDERR" or die "Can't dup STDERR: $!";
46 open STDOUT, '>', "/dev/null" or die "Can't hide STDOUT: $!";
47 open STDERR, '>', "/dev/null" or die "Can't hide STDERR: $!";
49 my $val = system(@cmd);
50 if(!$verbose) {
51 open STDOUT, ">&", $out or die "Can't restore STDOUT: $!";
52 open STDERR, ">&", $err or die "Can't restore STDERR: $!";
54 return $val;
57 sub runstr {
58 my @cmd = @_;
59 print "@cmd\n" if($verbose);
60 local $/;
61 open PIPE, '-|', @cmd or die "Can't run @cmd: $!";
62 my $output = <PIPE>;
63 close PIPE or $! == 0 or die "Can't run @cmd: $!";
64 return $output;
67 sub runsuc {
68 my ($cmd, @args) = @_;
69 my $output = runstr("$libexecdir/ksplice-$cmd", @args);
70 if($? != 0) {
71 print "Output: $output\n";
72 die "Failed during: $libexecdir/ksplice-$cmd @args";
74 return $output;
77 sub unpack_update {
78 my ($file) = @_;
79 runval("tar", "zxf", $file);
80 my ($ksplice) = glob('*/');
81 chop($ksplice); # remove the trailing slash
82 return $ksplice;
85 sub check_in_proc {
86 my ($kid) = @_;
88 if(! -e "/proc/1") {
89 print "/proc not mounted?\n";
90 exit(1);
92 if(! -e "/proc/ksplice_$kid") {
93 print "Ksplice id $kid is not present in the kernel\n";
94 exit(1);