Never inline init_symbol_arrays, to avoid conflicting stack garbage.
[ksplice.git] / ksplice-apply.in
blob2974a8d562fa255e4fdf8fe96163caf3a87189e3
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 lib 'KSPLICE_DATA_DIR';
22 use Ksplice;
24 my $debug;
25 my $debugon = 0;
26 my $partial = 0;
27 GetOptions(@common_options,
28 "partial" => \$partial,
29 "debug" => \$debugon,
30 "debugfile=s" => \$debug) or pod2usage(1);
32 pod2usage(1) if($help || scalar(@ARGV) != 1);
34 my $file = abs_path($ARGV[0]);
35 $debugon = 1 if(defined $debug);
36 $debug = abs_path($debug) if (defined $debug);
38 my $tmpdir = tempdir('ksplice-tmp-XXXXXX', TMPDIR => 1, CLEANUP => 1);
39 chdir($tmpdir);
40 my $ksplice = unpack_update($file);
41 chdir($ksplice);
43 die "No contents file in $file\n" if (!-e "contents");
44 open(CONTENTS, '<', "contents");
45 my $core;
46 while (<CONTENTS>) {
47 my @line = split(' ', $_);
48 if ($line[0] eq 'core') {
49 die "Multiple core modules in $file!" if (defined $core);
50 $core = {
51 module => $line[1],
52 file => $line[2],
56 close(CONTENTS);
58 my $nounload = runstr("lsmod") =~ m/- $/m;
60 (my $kid = $ksplice) =~s|ksplice[-_]([^-_]+)|$1|;
61 my $update = "ksplice_$kid";
62 if(update_loaded($kid)) {
63 my $stage = get_stage($kid);
64 if ($stage eq "applied") {
65 print STDERR "Ksplice update $kid already applied.\n";
66 exit(0);
68 die "Reversed Ksplice module $update already loaded!" if ($stage eq "reversed");
71 runstr_err(qw(modprobe -q ksplice)) eq "" || die;
72 if (defined $core) {
73 die "Could not find Ksplice core module $core->{file}\n" if (!-e $core->{file});
74 if (runstr("lsmod") =~ m/^\Q$core->{module}\E\s+/) {
75 die "Ksplice core module $core already loaded.";
77 if (!load_module($core->{file}, "debug=$debugon")) {
78 die "Error loading Ksplice core module $core->{module} for update $kid";
82 my @old_code_mod_files = ();
83 foreach my $old_code_mod_file (glob('ksplice-*-old.ko')) {
84 (my $new_code_mod_file = $old_code_mod_file) =~ s/-old\.ko$/-new.ko/;
85 die unless(-e $new_code_mod_file && -e $old_code_mod_file);
86 (my $target = $new_code_mod_file) =~ s/^ksplice-[^_]+_(.*)-new\.ko/$1/;
87 if ($target ne 'vmlinux' && runstr("lsmod") !~ m/^${target}\s/m) {
88 if (!$partial) {
89 cleanup_modules();
90 print_error("target_not_loaded");
91 die "Module $target to be patched not loaded";
94 push @old_code_mod_files, $old_code_mod_file;
96 foreach my $old_code_mod_file (@old_code_mod_files) {
97 (my $new_code_mod_file = $old_code_mod_file) =~ s/-old\.ko$/-new.ko/;
98 if(!load_module($new_code_mod_file)) {
99 die "Error loading new code module $new_code_mod_file";
101 if(!load_module($old_code_mod_file)) {
102 my $debugfile = get_debug_output("init_$kid", $debug);
103 print STDERR "Error loading old code module $old_code_mod_file\n";
104 cleanup_modules();
105 if($debugon && defined $debugfile) {
106 print("Debugging output saved to $debugfile\n");
108 die;
112 set_debug_level($kid, $debugon);
113 set_partial($kid, $partial);
114 set_stage($kid, "applied");
115 my $stage = get_stage($kid);
116 if($stage ne 'applied') {
117 my $debugfile = get_debug_output($kid, $debug);
118 my $abort_cause = get_abort_cause($kid);
119 my $conflicts = get_conflicts($kid);
120 cleanup_modules();
121 print STDERR "Error applying Ksplice update $kid:\n";
122 print_error($abort_cause);
123 if ($abort_cause eq 'code_busy') {
124 print $conflicts;
126 if ($debugon && defined $debugfile) {
127 print("Debugging output saved to $debugfile\n");
129 die;
131 mkpath("/var/run/ksplice/updates/$kid");
132 copy("patch", "/var/run/ksplice/updates/$kid/patch") if (-e "patch");
133 copy("description", "/var/run/ksplice/updates/$kid/description") if (-e "description");
134 if (!$nounload) {
135 foreach my $old_code_mod_file (@old_code_mod_files) {
136 (my $old_code_mod = $old_code_mod_file) =~ s/\.ko$//;
137 (my $new_code_mod = $old_code_mod) =~ s/-old$/-new/;
138 runval('rmmod', $old_code_mod);
139 runval('rmmod', $new_code_mod) if ($partial && refcount($new_code_mod) == 0);
142 print "Done!\n";
143 exit(0);
145 my @modules_loaded = qw();
147 sub load_module {
148 my ($module, @params) = @_;
149 if (runval_raw("insmod", $module, @params) != 0) {
150 child_error();
151 return 0;
153 $module =~ s/\.ko//;
154 push @modules_loaded, $module;
155 return 1;
158 sub refcount {
159 my ($module) = @_;
160 $module =~ s/-/_/g;
161 foreach(split(/\n/, runstr("lsmod"))) {
162 if (m/^(\S+)\s+[0-9]+\s+([0-9])+\s/) {
163 return $2 if ($1 eq $module);
166 return -1;
169 sub cleanup_modules {
170 foreach my $module (reverse(@modules_loaded)) {
171 runval("rmmod", $module) if(!$nounload);
175 sub print_error {
176 my ($error) = @_;
177 my %errors = (
178 "no_match" => <<'END',
179 Ksplice has aborted the upgrade because Ksplice has been unable to match the
180 object code produced by your current compiler and assembler against the running
181 kernel's object code. If you provided the exact kernel source to the running
182 kernel, then it appears that your current compiler and/or assembler are
183 behaving differently from the compiler and assembler used to build the running
184 kernel. If possible, please use the exact compiler and assembler that were
185 used to build the running kernel. If you are using exactly the same compiler
186 and assembler, consider reporting a bug to PACKAGE_BUGREPORT.
188 "code_busy" => <<'END',
189 Ksplice has aborted the upgrade because it appears that the code that you are
190 trying to patch is continuously in use by the system. More specifically,
191 Ksplice has been unable to find a moment when one or more of the to-be-patched
192 functions is not on a thread's kernel stack.
194 "bad_system_map" => <<'END',
195 Ksplice has aborted the upgrade because it appears that the System.map file
196 provided to ksplice-create does not match the running kernel.
198 "failed_to_find" => <<'END',
199 Ksplice has aborted the upgrade because it was unable to resolve some of the
200 symbols used in the update.
202 "already_reversed" => <<'END',
203 The Ksplice update that you are attempting to apply has already been applied
204 and reversed. You need to unload the Ksplice modules associated with this
205 update before you can apply this update again.
207 "missing_export" => <<'END',
208 Ksplice has aborted the upgrade because the symbols exported by the kernel
209 did not match Ksplice's expectations.
211 "unexpected_running_task" => <<'END',
212 Ksplice has aborted the upgrade because of an unexpected failure during the
213 kernel stack check. Please consider reporting a bug to PACKAGE_BUGREPORT.
215 "target_not_loaded" => <<'END',
216 Ksplice has aborted the upgrade because one of the modules to be
217 patched by the update was not loaded. If you want to apply this
218 update only to those modules that are loaded, then you should use the
219 --partial option.
221 "out_of_memory" => <<'END',
222 Ksplice has aborted the upgrade because the kernel ran out of memory.
224 "call_failed" => <<'END',
225 Ksplice has aborted the upgrade at the request of a one of the
226 pre-application hooks that were included as part of this Ksplice
227 update. This is likely the result of a bug in the patch used to
228 generate this update.
230 "unexpected" => <<'END',
231 Ksplice has aborted because of an unexpected error.
232 Please consider reporting a bug to PACKAGE_BUGREPORT.
234 "UNKNOWN" => <<'END',
235 The Ksplice kernel component has returned an error code that this version of
236 ksplice-apply does not understand.
238 "ok" => <<'END',
239 Ksplice has aborted the upgrade for unknown reasons.
240 Please consider reporting a bug to PACKAGE_BUGREPORT.
243 $error = "UNKNOWN" if (!exists $errors{$error});
244 print STDERR "\n$errors{$error}\n";
247 =head1 NAME
249 ksplice-apply - Apply an on-disk Ksplice update to the running kernel
251 =head1 SYNOPSIS
253 B<ksplice-apply> [I<OPTIONS>] I<UPDATE_TARBALL>
255 =head1 DESCRIPTION
257 B<ksplice-apply> takes as input a Ksplice update tarball, as generated by
258 L<ksplice-create(8)>, and it applies the update to the running binary kernel.
260 The update tarball used with B<ksplice-apply> must have been generated for the
261 running kernel's version.
263 =head1 OPTIONS
265 =over 8
267 =item B<--debug>
269 Applies the update with debugging output enabled. Recommended only for
270 debugging.
272 =item B<--debugfile=>I<filename>
274 Sets the location where debugging output should be saved. Implies --debug.
276 =item B<--partial>
278 Applies the update only to those modules which are loaded. Any
279 modules patched by the update that are not loaded are ignored (without
280 this option, Ksplice aborts if any modules patched by the update are
281 not loaded).
283 =back
285 =head1 SEE ALSO
287 L<ksplice-create(8)>, L<ksplice-view(8)>, L<ksplice-undo(8)>
289 =head1 BUGS
291 Please report bugs to <PACKAGE_BUGREPORT>.
293 =head1 AUTHORS
295 Jeff Arnold, Anders Kaseorg, and Tim Abbott
297 =head1 COPYRIGHT
299 Copyright (C) 2007-2009 Ksplice, Inc.
301 This is free software and documentation. You can redistribute and/or modify it
302 under the terms of the GNU General Public License, version 2.
304 =cut