Test CONFIG_FUNCTION_TRACER instead of CONFIG_FTRACE.
[ksplice.git] / ksplice-apply.in
blob104b7de9f8ecfb2739b6894f80347fa57a3226d8
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 = $ARGV[0];
39 $debugon = 1 if(defined $debug);
40 $debug = abs_path($debug) if (defined $debug);
42 chdir(unpack_update($file));
44 die "No contents file in $file\n" if (!-e "contents");
45 open(CONTENTS, '<', "contents");
46 my $core;
47 my @changes;
48 my $kid;
49 while (<CONTENTS>) {
50 my ($type, @args) = split(' ', $_);
51 if ($type eq 'core') {
52 die "Multiple core modules in $file!" if (defined $core);
53 $core = {};
54 @$core{qw(module file)} = @args;
55 } elsif ($type eq 'change') {
56 my $change = {};
57 @$change{qw(target new_code new_code_file old_code old_code_file)} = @args;
58 push @changes, $change;
59 my ($ckid) = $change->{'new_code'} =~ m/^ksplice_([^-_]+)_/ or die "Bad new_code $change->{'new_code'}";
60 !$kid or $kid eq $ckid or die "Multiple kids";
61 $kid = $ckid;
64 $kid or die "No kid";
65 close(CONTENTS);
67 die "Update was built using an old version of Ksplice" if (@changes == 0);
69 my $nounload = runstr("lsmod") =~ m/- $/m;
71 my $update = "ksplice_$kid";
72 if(update_loaded($kid)) {
73 my $stage = get_stage($kid);
74 if ($stage eq "applied") {
75 print STDERR "Ksplice update $kid already applied.\n" unless $raw_errors;
76 exit(0);
78 die "Reversed Ksplice module $update already loaded!" if ($stage eq "reversed");
81 runstr_err(qw(modprobe -q ksplice)) eq "" or die("Error loading Ksplice module.\n");
82 if (defined $core) {
83 die "Could not find Ksplice core module $core->{file}\n" if (!-e $core->{file});
84 if (runstr("lsmod") =~ m/^\Q$core->{module}\E\s+/) {
85 die "Ksplice core module $core already loaded.";
87 if (!load_module($core->{file}, "debug=$debugon")) {
88 die "Error loading Ksplice core module $core->{module} for update $kid";
92 foreach my $change (@changes) {
93 die unless (-e $change->{old_code_file} && -e $change->{new_code_file});
94 if ($change->{'target'} ne 'vmlinux' &&
95 runstr("lsmod") !~ m/^\Q$change->{target}\E\s+/m) {
96 if (!$partial) {
97 cleanup_modules();
98 print_abort_code("target_not_loaded", %apply_errors);
99 die "Module $change->{target} to be patched not loaded";
104 foreach my $change (@changes) {
105 if(!load_module($change->{new_code_file})) {
106 die "Error loading new code module $change->{new_code}";
108 if(!load_module($change->{old_code_file})) {
109 if($debugon) {
110 my $debugfile = get_debug_output("init_$kid", $debug);
111 print("Debugging output saved to $debugfile\n") if $debugfile;
113 cleanup_modules();
114 die("Error loading old code module $change->{old_code}\n");
118 mkpath("/var/run/ksplice/updates/$kid");
119 copy("patch", "/var/run/ksplice/updates/$kid/patch") if (-e "patch");
120 copy("description", "/var/run/ksplice/updates/$kid/description") if (-e "description");
122 set_debug_level($kid, $debugon);
123 set_partial($kid, $partial);
124 set_stage($kid, "applied");
125 my $stage = get_stage($kid);
126 if($stage ne 'applied') {
127 print STDERR "Error applying Ksplice update $kid:\n" unless $raw_errors;
128 print_error($kid);
130 if ($debugon) {
131 my $debugfile = get_debug_output($kid, $debug);
132 print("Debugging output saved to $debugfile\n") if $debugfile;
135 rmtree("/var/run/ksplice/updates/$kid") if (-e "/var/run/ksplice/updates/$kid");
136 cleanup_modules();
138 exit(-1);
141 if (!$nounload) {
142 foreach my $change (@changes) {
143 runval('rmmod', $change->{old_code});
144 runval('rmmod', $change->{new_code}) if ($partial && refcount($change->{new_code}) == 0);
148 print "Done!\n";
149 exit(0);
151 my @modules_loaded = qw();
153 sub handler {
154 my ($sig) = @_;
155 die "caught SIG$sig, abort\n" if (!defined($kid));
156 die "caught SIG$sig after finished\n"
157 if (update_loaded($kid) && (get_stage($kid) eq 'applied'));
158 print STDERR "caught SIG$sig, aborting\n";
159 rmtree("/var/run/ksplice/updates/$kid") if (-e "/var/run/ksplice/updates/$kid");
160 cleanup_modules();
161 exit(1);
164 sub load_module {
165 my ($module, @params) = @_;
166 push @modules_loaded, ($module =~ m/^(.*)\.ko$/);
167 if (runval_raw("insmod", $module, @params) != 0) {
168 pop @modules_loaded;
169 child_error();
170 return 0;
172 return 1;
175 sub refcount {
176 my ($module) = @_;
177 $module =~ s/-/_/g;
178 foreach(split(/\n/, runstr("lsmod"))) {
179 if (m/^(\S+)\s+[0-9]+\s+([0-9])+\s/) {
180 return $2 if ($1 eq $module);
183 return -1;
186 sub cleanup_modules {
187 foreach my $module (reverse(@modules_loaded)) {
188 runval_raw("rmmod", $module) if(!$nounload);
192 sub initialize_abort_code_table {
193 %apply_errors = (
194 "no_match" => <<'END',
195 Ksplice has aborted the upgrade because Ksplice has been unable to match the
196 object code produced by your current compiler and assembler against the running
197 kernel's object code. If you provided the exact kernel source to the running
198 kernel, then it appears that your current compiler and/or assembler are
199 behaving differently from the compiler and assembler used to build the running
200 kernel. If possible, please use the exact compiler and assembler that were
201 used to build the running kernel. If you are using exactly the same compiler
202 and assembler, consider reporting a bug to PACKAGE_BUGREPORT.
204 "code_busy" => <<'END',
205 Ksplice has aborted the upgrade because it appears that the code that you are
206 trying to patch is continuously in use by the system. More specifically,
207 Ksplice has been unable to find a moment when one or more of the to-be-patched
208 functions is not on a thread's kernel stack.
210 "bad_system_map" => <<'END',
211 Ksplice has aborted the upgrade because it appears that the System.map file
212 provided to ksplice-create does not match the running kernel.
214 "failed_to_find" => <<'END',
215 Ksplice has aborted the upgrade because it was unable to resolve some of the
216 symbols used in the update.
218 "already_reversed" => <<'END',
219 The Ksplice update that you are attempting to apply has already been applied
220 and reversed. You need to unload the Ksplice modules associated with this
221 update before you can apply this update again.
223 "missing_export" => <<'END',
224 Ksplice has aborted the upgrade because the symbols exported by the kernel
225 did not match Ksplice's expectations.
227 "unexpected_running_task" => <<'END',
228 Ksplice has aborted the upgrade because of an unexpected failure during the
229 kernel stack check. Please consider reporting a bug to PACKAGE_BUGREPORT.
231 "target_not_loaded" => <<'END',
232 Ksplice has aborted the upgrade because one of the modules to be
233 patched by the update was not loaded. If you want to apply this
234 update only to those modules that are loaded, then you should use the
235 --partial option.
237 "out_of_memory" => <<'END',
238 Ksplice has aborted the upgrade because the kernel ran out of memory.
240 "call_failed" => <<'END',
241 Ksplice has aborted the upgrade at the request of a one of the
242 pre-application hooks that were included as part of this Ksplice
243 update. This is likely the result of a bug in the patch used to
244 generate this update.
246 "unexpected" => <<'END',
247 Ksplice has aborted because of an unexpected error.
248 Please consider reporting a bug to PACKAGE_BUGREPORT.
250 "UNKNOWN" => <<'END',
251 The Ksplice kernel component has returned an error code that this version of
252 ksplice-apply does not understand.
254 "ok" => <<'END',
255 Ksplice has aborted the upgrade for unknown reasons.
256 Please consider reporting a bug to PACKAGE_BUGREPORT.
261 sub print_error {
262 my ($kid) = @_;
263 print_abort_error($kid, %apply_errors);
266 =head1 NAME
268 ksplice-apply - Apply an on-disk Ksplice update to the running kernel
270 =head1 SYNOPSIS
272 B<ksplice-apply> [I<OPTIONS>] {I<UPDATE_TARBALL> | I<UPDATE_TREE>}
274 =head1 DESCRIPTION
276 B<ksplice-apply> takes as input a Ksplice update, as generated by
277 L<ksplice-create(8)>, and it applies the update to the running binary kernel.
278 The update may be supplied in the form of a tarball or an unpacked tree.
280 The update is required to have been generated for the running kernel's
281 version.
283 =head1 OPTIONS
285 =over 8
287 =item B<--debug>
289 Applies the update with debugging output enabled. Recommended only for
290 debugging.
292 =item B<--debugfile=>I<filename>
294 Sets the location where debugging output should be saved. Implies --debug.
296 =item B<--partial>
298 Applies the update only to those modules which are loaded. Any
299 modules patched by the update that are not loaded are ignored (without
300 this option, Ksplice aborts if any modules patched by the update are
301 not loaded).
303 =item B<--raw-errors>
305 Print only raw error information designed to be machine-readable on
306 standard error (standard output is still intended to be
307 human-readable). If B<ksplice-apply> fails due to an error from the
308 Ksplice kernel modules, the first line on standard error will be a
309 Ksplice B<abort code> (see the Ksplice source code for documentation
310 on these codes). Further lines will vary depending on the abort code.
311 If B<ksplice-apply> fails for any other reason, it will output the
312 line C<"OTHER\n">, followed by a human-readable failure message, to
313 standard error.
315 =back
317 =head1 SEE ALSO
319 L<ksplice-create(8)>, L<ksplice-view(8)>, L<ksplice-undo(8)>
321 =head1 BUGS
323 Please report bugs to <PACKAGE_BUGREPORT>.
325 =head1 AUTHORS
327 Jeff Arnold, Anders Kaseorg, and Tim Abbott
329 =head1 COPYRIGHT
331 Copyright (C) 2007-2009 Ksplice, Inc.
333 This is free software and documentation. You can redistribute and/or modify it
334 under the terms of the GNU General Public License, version 2.
336 =cut