--raw-errors: Preserve non-abort-code error output.
[ksplice.git] / ksplice-apply.in
blob223798c193862ee0107a3e23b7a33bdd21e70b18
1 #!/usr/bin/perl
3 # Copyright (C) 2007-2009 Ksplice, Inc.
4 # Authors: Jeff Arnold, Anders Kaseorg, Tim Abbott
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License, version 2.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
17 # 02110-1301, USA.
19 use strict;
20 use warnings;
21 use sigtrap 'handler', \&handler, qw(normal-signals error-signals);
22 use lib 'KSPLICE_DATA_DIR';
23 use Ksplice;
25 our %apply_errors;
26 initialize_abort_code_table();
28 my $debug;
29 my $debugon = 0;
30 my $partial = 0;
31 GetOptions(@common_options,
32 "partial" => \$partial,
33 "debug" => \$debugon,
34 "debugfile=s" => \$debug) or pod2usage(1);
36 pod2usage(1) if($help || scalar(@ARGV) != 1);
38 my $file = abs_path($ARGV[0]);
39 $debugon = 1 if(defined $debug);
40 $debug = abs_path($debug) if (defined $debug);
42 my $tmpdir = tempdir('ksplice-tmp-XXXXXX', TMPDIR => 1, CLEANUP => 1);
43 chdir($tmpdir);
44 my $ksplice = unpack_update($file);
45 chdir($ksplice);
47 die "No contents file in $file\n" if (!-e "contents");
48 open(CONTENTS, '<', "contents");
49 my $core;
50 my @changes;
51 while (<CONTENTS>) {
52 my ($type, @args) = split(' ', $_);
53 if ($type eq 'core') {
54 die "Multiple core modules in $file!" if (defined $core);
55 $core = {};
56 @$core{qw(module file)} = @args;
57 } elsif ($type eq 'change') {
58 my $change = {};
59 @$change{qw(target new_code new_code_file old_code old_code_file)} = @args;
60 push @changes, $change;
63 close(CONTENTS);
65 die "Update was built using an old version of Ksplice" if (@changes == 0);
67 my $nounload = runstr("lsmod") =~ m/- $/m;
69 (my $kid = $ksplice) =~s|ksplice[-_]([^-_]+)|$1|;
70 my $update = "ksplice_$kid";
71 if(update_loaded($kid)) {
72 my $stage = get_stage($kid);
73 if ($stage eq "applied") {
74 print STDERR "Ksplice update $kid already applied.\n" unless $raw_errors;
75 exit(0);
77 die "Reversed Ksplice module $update already loaded!" if ($stage eq "reversed");
80 runstr_err(qw(modprobe -q ksplice)) eq "" or die("Error loading Ksplice module.\n");
81 if (defined $core) {
82 die "Could not find Ksplice core module $core->{file}\n" if (!-e $core->{file});
83 if (runstr("lsmod") =~ m/^\Q$core->{module}\E\s+/) {
84 die "Ksplice core module $core already loaded.";
86 if (!load_module($core->{file}, "debug=$debugon")) {
87 die "Error loading Ksplice core module $core->{module} for update $kid";
91 foreach my $change (@changes) {
92 die unless (-e $change->{old_code_file} && -e $change->{new_code_file});
93 if ($change->{'target'} ne 'vmlinux' &&
94 runstr("lsmod") !~ m/^\Q$change->{target}\E\s+/m) {
95 if (!$partial) {
96 cleanup_modules();
97 print_abort_code("target_not_loaded", %apply_errors);
98 die "Module $change->{target} to be patched not loaded";
103 foreach my $change (@changes) {
104 if(!load_module($change->{new_code_file})) {
105 die "Error loading new code module $change->{new_code}";
107 if(!load_module($change->{old_code_file})) {
108 my $debugfile = get_debug_output("init_$kid", $debug);
109 cleanup_modules();
110 if($debugon && defined $debugfile) {
111 print("Debugging output saved to $debugfile\n");
113 die("Error loading old code module $change->{old_code}\n");
117 mkpath("/var/run/ksplice/updates/$kid");
118 copy("patch", "/var/run/ksplice/updates/$kid/patch") if (-e "patch");
119 copy("description", "/var/run/ksplice/updates/$kid/description") if (-e "description");
121 set_debug_level($kid, $debugon);
122 set_partial($kid, $partial);
123 set_stage($kid, "applied");
124 my $stage = get_stage($kid);
125 if($stage ne 'applied') {
126 my $debugfile = get_debug_output($kid, $debug);
127 print STDERR "Error applying Ksplice update $kid:\n" unless $raw_errors;
128 print_error($kid);
130 rmtree("/var/run/ksplice/updates/$kid") if (-e "/var/run/ksplice/updates/$kid");
131 cleanup_modules();
132 if ($debugon && defined $debugfile) {
133 print("Debugging output saved to $debugfile\n");
135 exit(-1);
138 if (!$nounload) {
139 foreach my $change (@changes) {
140 runval('rmmod', $change->{old_code});
141 runval('rmmod', $change->{new_code}) if ($partial && refcount($change->{new_code}) == 0);
145 print "Done!\n";
146 exit(0);
148 my @modules_loaded = qw();
150 sub handler {
151 my ($sig) = @_;
152 die "caught SIG$sig, abort\n" if (!defined($kid));
153 die "caught SIG$sig after finished\n"
154 if (update_loaded($kid) && (get_stage($kid) eq 'applied'));
155 print STDERR "caught SIG$sig, aborting\n";
156 rmtree("/var/run/ksplice/updates/$kid") if (-e "/var/run/ksplice/updates/$kid");
157 cleanup_modules();
158 exit(1);
161 sub load_module {
162 my ($module, @params) = @_;
163 push @modules_loaded, ($module =~ m/^(.*)\.ko$/);
164 if (runval_raw("insmod", $module, @params) != 0) {
165 pop @modules_loaded;
166 child_error();
167 return 0;
169 return 1;
172 sub refcount {
173 my ($module) = @_;
174 $module =~ s/-/_/g;
175 foreach(split(/\n/, runstr("lsmod"))) {
176 if (m/^(\S+)\s+[0-9]+\s+([0-9])+\s/) {
177 return $2 if ($1 eq $module);
180 return -1;
183 sub cleanup_modules {
184 foreach my $module (reverse(@modules_loaded)) {
185 runval_raw("rmmod", $module) if(!$nounload);
189 sub initialize_abort_code_table {
190 %apply_errors = (
191 "no_match" => <<'END',
192 Ksplice has aborted the upgrade because Ksplice has been unable to match the
193 object code produced by your current compiler and assembler against the running
194 kernel's object code. If you provided the exact kernel source to the running
195 kernel, then it appears that your current compiler and/or assembler are
196 behaving differently from the compiler and assembler used to build the running
197 kernel. If possible, please use the exact compiler and assembler that were
198 used to build the running kernel. If you are using exactly the same compiler
199 and assembler, consider reporting a bug to PACKAGE_BUGREPORT.
201 "code_busy" => <<'END',
202 Ksplice has aborted the upgrade because it appears that the code that you are
203 trying to patch is continuously in use by the system. More specifically,
204 Ksplice has been unable to find a moment when one or more of the to-be-patched
205 functions is not on a thread's kernel stack.
207 "bad_system_map" => <<'END',
208 Ksplice has aborted the upgrade because it appears that the System.map file
209 provided to ksplice-create does not match the running kernel.
211 "failed_to_find" => <<'END',
212 Ksplice has aborted the upgrade because it was unable to resolve some of the
213 symbols used in the update.
215 "already_reversed" => <<'END',
216 The Ksplice update that you are attempting to apply has already been applied
217 and reversed. You need to unload the Ksplice modules associated with this
218 update before you can apply this update again.
220 "missing_export" => <<'END',
221 Ksplice has aborted the upgrade because the symbols exported by the kernel
222 did not match Ksplice's expectations.
224 "unexpected_running_task" => <<'END',
225 Ksplice has aborted the upgrade because of an unexpected failure during the
226 kernel stack check. Please consider reporting a bug to PACKAGE_BUGREPORT.
228 "target_not_loaded" => <<'END',
229 Ksplice has aborted the upgrade because one of the modules to be
230 patched by the update was not loaded. If you want to apply this
231 update only to those modules that are loaded, then you should use the
232 --partial option.
234 "out_of_memory" => <<'END',
235 Ksplice has aborted the upgrade because the kernel ran out of memory.
237 "call_failed" => <<'END',
238 Ksplice has aborted the upgrade at the request of a one of the
239 pre-application hooks that were included as part of this Ksplice
240 update. This is likely the result of a bug in the patch used to
241 generate this update.
243 "unexpected" => <<'END',
244 Ksplice has aborted because of an unexpected error.
245 Please consider reporting a bug to PACKAGE_BUGREPORT.
247 "UNKNOWN" => <<'END',
248 The Ksplice kernel component has returned an error code that this version of
249 ksplice-apply does not understand.
251 "ok" => <<'END',
252 Ksplice has aborted the upgrade for unknown reasons.
253 Please consider reporting a bug to PACKAGE_BUGREPORT.
258 sub print_error {
259 my ($kid) = @_;
260 print_abort_error($kid, %apply_errors);
263 =head1 NAME
265 ksplice-apply - Apply an on-disk Ksplice update to the running kernel
267 =head1 SYNOPSIS
269 B<ksplice-apply> [I<OPTIONS>] I<UPDATE_TARBALL>
271 =head1 DESCRIPTION
273 B<ksplice-apply> takes as input a Ksplice update tarball, as generated by
274 L<ksplice-create(8)>, and it applies the update to the running binary kernel.
276 The update tarball used with B<ksplice-apply> must have been generated for the
277 running kernel's version.
279 =head1 OPTIONS
281 =over 8
283 =item B<--debug>
285 Applies the update with debugging output enabled. Recommended only for
286 debugging.
288 =item B<--debugfile=>I<filename>
290 Sets the location where debugging output should be saved. Implies --debug.
292 =item B<--partial>
294 Applies the update only to those modules which are loaded. Any
295 modules patched by the update that are not loaded are ignored (without
296 this option, Ksplice aborts if any modules patched by the update are
297 not loaded).
299 =item B<--raw-errors>
301 Print only raw error information designed to be machine-readable on
302 standard error (standard output is still intended to be
303 human-readable). If B<ksplice-apply> fails due to an error from the
304 Ksplice kernel modules, the first line on standard error will be a
305 Ksplice B<abort code> (see the Ksplice source code for documentation
306 on these codes). Further lines will vary depending on the abort code.
307 If B<ksplice-apply> fails for any other reason, it will output the
308 line C<"OTHER\n">, followed by a human-readable failure message, to
309 standard error.
311 =back
313 =head1 SEE ALSO
315 L<ksplice-create(8)>, L<ksplice-view(8)>, L<ksplice-undo(8)>
317 =head1 BUGS
319 Please report bugs to <PACKAGE_BUGREPORT>.
321 =head1 AUTHORS
323 Jeff Arnold, Anders Kaseorg, and Tim Abbott
325 =head1 COPYRIGHT
327 Copyright (C) 2007-2009 Ksplice, Inc.
329 This is free software and documentation. You can redistribute and/or modify it
330 under the terms of the GNU General Public License, version 2.
332 =cut