Add extremely verbose debugging output back to ksplice-create.
[ksplice.git] / ksplice.pm.in
blob0e2de2c22264232885d38cdd239c55db89c37cfc
1 package ksplice;
2 use Cwd 'abs_path';
3 use File::Temp qw/:mktemp/;
4 use strict;
5 use warnings;
6 use verbose;
7 require Exporter;
8 our @ISA = qw(Exporter);
9 our @EXPORT = qw($datadir $libexecdir $version_str init_tmpdir runcd runstr runval runval_raw runsuc unpack_update get_debugfs);
11 our ($datadir, $libexecdir) = qw(KSPLICE_DATA_DIR KSPLICE_LIBEXEC_DIR);
12 our $version_str = "Ksplice version PACKAGE_VERSION\n";
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::level);
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::level);
43 if(!$verbose::level) {
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::level) {
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::level);
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 get_debugfs {
86 my ($modname, $debugfs_out) = @_;
87 my $debug;
88 my $debugfsdir = init_tmpdir();
89 runval_raw(qw(mount -t debugfs debugfs), $debugfsdir);
90 if (-e "$debugfsdir/$modname") {
91 system(qw(cp -a), "$debugfsdir/$modname", "$debugfs_out");
92 $debug = runstr("cat", "$debugfsdir/$modname");
93 runval_raw(qw(rmdir), $debugfsdir);
95 runval_raw(qw(umount), $debugfsdir);
96 return $debug;