-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ksplice.pm.in
347 lines (313 loc) · 7.76 KB
/
Ksplice.pm.in
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
package Ksplice;
use Cwd qw(abs_path getcwd);
use Getopt::Long qw(:config bundling);
use File::Basename;
use File::Copy;
use File::Path;
use File::Spec::Functions qw(tmpdir);
use File::Temp qw(tempfile tempdir);
use Fatal qw(:void copy rename move chdir mkdir rmdir unlink rmtree);
use IPC::Open2;
use IPC::Open3;
use Pod::Usage;
use Text::ParseWords;
use strict;
use warnings;
use Verbose qw(:2 copy rename move utime chdir mkdir mkpath unlink rmtree tempfile tempdir);
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(
Verbose GetOptions pod2usage shellwords
$datadir $libexecdir @common_options $help $raw_errors
child_error runval runval_raw runstr runstr_err runval_in runval_infile runval_outfile
unpack_update
get_stage set_stage set_debug_level set_partial get_abort_cause get_patch update_loaded
get_debug_output get_conflicts get_raw_conflicts get_short_description
read_file write_file
abs_path getcwd basename dirname tmpdir
copy rename move utime chdir mkdir mkpath unlink rmtree tempfile tempdir
print_abort_error print_abort_code
);
our ($datadir, $libexecdir) = qw(KSPLICE_DATA_DIR KSPLICE_LIBEXEC_DIR);
our $help = 0;
our $raw_errors = 0;
our $printed_abort_code;
our @common_options = (
"help|?" => \$help,
"raw-errors" => \$raw_errors,
"version" => sub { print "Ksplice version PACKAGE_VERSION\n"; exit(0); },
"api-version" => sub { print "KSPLICE_API_VERSION\n"; exit(0); },
"verbose|v:+" => \$Verbose::level,
"quiet|q:+" => sub { $Verbose::level -= $_[1]; },
);
$SIG{__DIE__} = sub {
die @_ if $^S || !$raw_errors;
my ($msg) = @_;
if(!$printed_abort_code) {
print STDERR "OTHER\n$msg";
}
exit(-1);
};
sub child_error {
if($raw_errors) {
return ($? != 0);
}
if($? == -1) {
print STDERR "Failed to exec child\n";
} elsif(($? & 127) != 0) {
print STDERR "Child exited with signal ", ($? & 127), ($? & 128) ? " (core dumped)\n" : "\n";
} elsif($? >> 8 != 0) {
print STDERR "Child exited with status ", $? >> 8, "\n";
} else {
return 0;
}
return 1;
}
sub runval {
my (@cmd) = @_;
if(runval_raw(@cmd) != 0) {
child_error();
die "Failed during: @cmd\n";
}
}
sub runval_raw {
my (@cmd) = @_;
my ($out, $err);
print "+ @cmd\n" if($Verbose::level >= 1);
if($raw_errors) {
my $pid = open3(fileno STDIN, ">&STDOUT", ">/dev/null", @cmd);
waitpid($pid, 0);
return $?;
} else {
return system(@cmd);
}
}
sub runstr {
my @cmd = @_;
print "+ @cmd\n" if($Verbose::level >= 1);
local $/;
local (*PIPE);
if($raw_errors) {
open3(fileno STDIN, \*PIPE, ">/dev/null", @cmd);
} else {
open PIPE, '-|', @cmd or die "Can't run @cmd: $!";
}
my $output = <PIPE>;
close PIPE or $! == 0 or die "Can't run @cmd: $!";
return $output;
}
sub runstr_err {
my @cmd = @_;
print "+ @cmd\n" if($Verbose::level >= 1);
local (*ERROR);
my $pid = open3(fileno STDIN, '>&STDOUT', \*ERROR, @cmd);
local $/;
my $error = <ERROR>;
waitpid($pid, 0);
print STDERR $error unless $raw_errors;
return $error;
}
sub runval_in {
my ($in, @cmd) = @_;
print "+ @cmd <<'EOF'\n${in}EOF\n" if($Verbose::level >= 1);
local (*WRITE);
if($raw_errors) {
open3(\*WRITE, ">&STDOUT", ">/dev/null", @cmd);
} else {
open(WRITE, '|-', @cmd) or die "Can't run @cmd: $!";
}
print WRITE $in;
close(WRITE) or $! == 0 or die "Can't run @cmd: $!";
if(child_error()) {
die "Failed during: @cmd";
}
}
sub runval_infile {
my ($infile, @cmd) = @_;
print "+ @cmd < $infile\n" if($Verbose::level >= 1);
local (*INFILE);
open(INFILE, '<', $infile) or die "Can't open $infile: $!";
my $pid;
if($raw_errors) {
$pid = open3('<&INFILE', '>&STDOUT', ">/dev/null", @cmd);
} else {
$pid = open2('>&STDOUT', '<&INFILE', @cmd);
}
waitpid($pid, 0);
if(child_error()) {
die "Failed during: @cmd";
}
}
sub runval_outfile {
my ($outfile, @cmd) = @_;
print "+ @cmd > $outfile\n" if($Verbose::level >= 1);
local (*OUTFILE);
open(OUTFILE, '>', $outfile) or die "Can't open $outfile: $!";
my $pid;
if($raw_errors) {
$pid = open3('</dev/null', '>&OUTFILE', ">/dev/null", @cmd);
} else {
$pid = open2('>&OUTFILE', '</dev/null', @cmd);
}
waitpid($pid, 0);
if(child_error()) {
die "Failed during: @cmd";
}
}
sub unpack_update {
my ($file) = @_;
if (-d $file) {
return $file;
}
my $tmpdir = tempdir('ksplice-tmp-XXXXXX', TMPDIR => 1, CLEANUP => 1);
runval("tar", "-C", $tmpdir, "--force-local", "-zxf", $file);
my ($ksplice) = glob("$tmpdir/*/");
chop($ksplice); # remove the trailing slash
return $ksplice;
}
sub get_sysfs {
my ($kid) = @_;
if(! -d "/sys/module") {
die "/sys not mounted?\n";
}
my $update = "ksplice_$kid";
if (-d "/sys/kernel/ksplice/$kid") {
return "/sys/kernel/ksplice/$kid";
}
if (-d "/sys/module/$update/ksplice") {
return "/sys/module/$update/ksplice";
}
return undef;
}
sub update_loaded {
my ($kid) = @_;
return defined(get_sysfs($kid));
}
sub read_file {
my ($file) = @_;
local (*INPUT, $/);
open(INPUT, "<", $file) or die $!;
return <INPUT>;
}
sub write_file {
my ($file, $string) = @_;
local *INPUT;
open(INPUT, ">", $file) or die $!;
print INPUT $string;
}
sub read_sysfs {
my ($kid, $attr) = @_;
my $sysfs = get_sysfs($kid);
return undef if (!defined($sysfs));
return read_file("$sysfs/$attr");
}
sub write_sysfs {
my ($kid, $attr, $string) = @_;
my $sysfs = get_sysfs($kid);
return undef if (!defined($sysfs));
write_file("$sysfs/$attr", $string);
}
sub get_debug_output {
my ($kid, $debugfs_out) = @_;
my $update = "ksplice_$kid";
if (!$debugfs_out) {
(undef, $debugfs_out) = tempfile('ksplice-debug-XXXXXX', DIR => tmpdir());
}
if (runval_raw("grep", "-qFx", "nodev\tdebugfs", "/proc/filesystems") == 0) {
my $debugfsdir = tempdir('ksplice-debugfs-XXXXXX', TMPDIR => 1);
runval(qw(mount -t debugfs debugfs), $debugfsdir);
if (-e "$debugfsdir/$update") {
copy("$debugfsdir/$update", $debugfs_out);
}
runval(qw(umount), $debugfsdir);
rmdir($debugfsdir);
return $debugfs_out;
} elsif ($? >> 8 == 1) {
return ();
} else {
child_error();
exit(-1);
}
}
sub get_stage {
my ($kid) = @_;
chomp(my $result = read_sysfs($kid, "stage"));
return $result;
}
sub get_abort_cause {
my ($kid) = @_;
chomp(my $result = read_sysfs($kid, "abort_cause"));
return $result;
}
sub get_conflicts {
my ($kid) = @_;
chomp(my $conflicts = read_sysfs($kid, "conflicts"));
my @conflicts = split('\n', $conflicts);
my $out = '';
foreach my $conflict (@conflicts) {
my ($name, $pid, @symbols) = split(' ', $conflict);
next if (!@symbols);
$out .= "Process $name(pid $pid) is using the following symbols changed by update $kid:\n";
foreach my $symbol (@symbols) {
$out .= " $symbol\n";
}
}
return $out;
}
sub get_raw_conflicts {
my ($kid) = @_;
my $conflicts = read_sysfs($kid, "conflicts");
return $conflicts;
}
sub get_patch {
my ($kid) = @_;
my $result = read_file("/var/run/ksplice/updates/$kid/patch");
return $result;
}
sub get_short_description {
my ($kid) = @_;
open(INPUT, "<", "/var/run/ksplice/updates/$kid/description") or return undef;
my $result = <INPUT>;
close(INPUT);
return $result;
}
sub set_stage {
my ($kid, $string) = @_;
write_sysfs($kid, "stage", "$string\n");
}
sub set_debug_level {
my ($kid, $string) = @_;
write_sysfs($kid, "debug", "$string\n");
}
sub set_partial {
my ($kid, $string) = @_;
write_sysfs($kid, "partial", "$string\n");
}
sub print_abort_error {
my ($kid, %errors) = @_;
my $error = get_abort_cause($kid);
print_abort_code($error, %errors);
if ($error eq 'code_busy') {
if($raw_errors) {
print STDERR get_raw_conflicts($kid);
} else {
print STDERR get_conflicts($kid);
}
}
$printed_abort_code = 1;
}
sub print_abort_code {
my ($error, %errors) = @_;
if($raw_errors) {
print STDERR "$error\n";
} else {
$error = "UNKNOWN" if (!exists $errors{$error});
print STDERR "\n$errors{$error}\n";
}
$printed_abort_code = 1;
}
END {
$Verbose::level = 0;
chdir("/");
}
1;