Avoid scanning through all relocations in write_reloc.
[ksplice.git] / ksplice-apply.in
blobd9837f0b665d509edf01d685ab2154323524332b
1 #!/usr/bin/perl
3 # Copyright (C) 2007-2008 Jeffrey Brian Arnold <jbarnold@mit.edu>
4 # Copyright (C) 2008 Anders Kaseorg <andersk@mit.edu>,
5 # Tim Abbott <tabbott@mit.edu>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License, version 2.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
18 # 02110-1301, USA.
20 use strict;
21 use warnings;
22 use lib 'KSPLICE_DATA_DIR';
23 use Ksplice;
25 my $debug = 0;
26 GetOptions(@common_options,
27 "debug" => \$debug) or pod2usage(1);
29 pod2usage(1) if($help || scalar(@ARGV) != 1);
31 my $file = abs_path($ARGV[0]);
33 my $tmpdir = tempdir('ksplice-tmp-XXXXXX', TMPDIR => 1, CLEANUP => 1);
34 chdir($tmpdir);
35 my $ksplice = unpack_update($file);
36 chdir($ksplice);
38 my $nounload = runstr("lsmod") =~ m/- $/m;
40 (my $kid = $ksplice) =~s|ksplice[-_]([^-_]+)|$1|;
41 my $update = "ksplice_$kid";
42 if(update_loaded($kid)) {
43 my $stage = get_stage($kid);
44 if ($stage eq "applied") {
45 print STDERR "Ksplice update $kid already applied.\n";
46 exit(0);
48 die "Reversed Ksplice module $update already loaded!" if ($stage eq "reversed");
51 runstr_err(qw(modprobe -q ksplice)) eq "" || die;
52 if (-e "ksplice-$kid.ko") {
53 if (runstr("lsmod") =~ m/^ksplice_$kid\s+/) {
54 die "Ksplice core module ksplice_$kid already loaded.";
56 if (!load_module("ksplice-$kid.ko", "debug=$debug")) {
57 die "Error loading Ksplice core for update $kid";
61 foreach my $helper (glob('ksplice-*-helper.ko')) {
62 (my $primary = $helper) =~ s/\-helper\.ko$/.ko/;
63 die unless(-e $primary && -e $helper);
64 (my $target = $primary) =~ s/^ksplice-[^_]+_(.*)\.ko/$1/;
65 if ($target ne 'vmlinux' && runstr("lsmod") !~ m/^${target}\s/m) {
66 cleanup_modules();
67 die "Module $target to be patched not loaded";
70 foreach my $helper (glob('ksplice-*-helper.ko')) {
71 (my $primary = $helper) =~ s/\-helper\.ko$/.ko/;
72 if(!load_module($primary)) {
73 die "Error loading primary module $primary";
75 if(!load_module($helper)) {
76 my ($debugfile, $debugout) = get_debug_output("init_$kid");
77 print STDERR "Error loading helper module $helper\n";
78 (my $module = $primary) =~ s/-/_/g;
79 $module =~ s/\.ko//;
80 cleanup_modules();
81 if(defined $debugfile) {
82 print $debugout;
83 print("Debugging output saved to $debugfile\n");
85 die;
89 set_debug_level($kid, $debug);
90 set_stage($kid, "applied");
91 my $stage = get_stage($kid);
92 if($stage ne 'applied') {
93 my ($debugfile, $debugout) = get_debug_output($kid);
94 my $abort_cause = get_abort_cause($kid);
95 my $conflicts = get_conflicts($kid);
96 cleanup_modules();
97 print STDERR "Error applying Ksplice update $kid:\n";
98 print $debugout if (defined $debugfile);
99 print_error($abort_cause);
100 if ($abort_cause eq 'code_busy') {
101 print $conflicts;
103 print("Debugging output saved to $debugfile\n") if (defined $debugfile);
104 die;
106 mkpath("/var/run/ksplice/updates/$kid");
107 copy("patch", "/var/run/ksplice/updates/$kid/patch");
108 foreach my $helper (glob('ksplice-*-helper.ko')) {
109 $helper =~ s/-/_/g;
110 $helper =~ s/\.ko$//;
111 runval('rmmod', $helper) if(!$nounload);
113 print "Done!\n";
114 exit(0);
116 my @modules_loaded = qw();
118 sub load_module {
119 my ($module, @params) = @_;
120 if (runval_raw("insmod", $module, @params) != 0) {
121 child_error();
122 return 0;
124 $module =~ s/\.ko//;
125 $module =~ s/-/_/g;
126 push @modules_loaded, $module;
127 return 1;
130 sub cleanup_modules {
131 foreach my $module (reverse(@modules_loaded)) {
132 runval("rmmod", $module) if(!$nounload);
136 sub print_error {
137 my ($error) = @_;
138 my %errors = (
139 "no_match" => <<'END',
140 Ksplice has aborted the upgrade because Ksplice has been unable to match the
141 object code produced by your current compiler and assembler against the running
142 kernel's object code. If you provided the exact kernel source to the running
143 kernel, then it appears that your current compiler and/or assembler are
144 behaving differently from the compiler and assembler used to build the running
145 kernel. If possible, please use the exact compiler and assembler that were
146 used to build the running kernel. If you are using exactly the same compiler
147 and assembler, consider reporting a bug to PACKAGE_BUGREPORT.
149 "code_busy" => <<'END',
150 Ksplice has aborted the upgrade because it appears that the code that you are
151 trying to patch is continuously in use by the system. More specifically,
152 Ksplice has been unable to find a moment when one or more of the to-be-patched
153 functions is not on a thread's kernel stack.
155 "bad_system_map" => <<'END',
156 Ksplice has aborted the upgrade because it appears that the System.map file
157 provided to ksplice-create does not match the running kernel.
159 "failed_to_find" => <<'END',
160 Ksplice has aborted the upgrade because it was unable to resolve some of the
161 symbols used in the update.
163 "already_reversed" => <<'END',
164 The Ksplice update that you are attempting to apply has already been applied
165 and reversed. You need to unload the Ksplice modules associated with this
166 update before you can apply this update again.
168 "missing_export" => <<'END',
169 Ksplice has aborted the upgrade because the symbols exported by the kernel
170 did not match Ksplice's expectations.
172 "unexpected_running_task" => <<'END',
173 Ksplice has aborted the upgrade because of an unexpected failure during the
174 kernel stack check. Please consider reporting a bug to PACKAGE_BUGREPORT.
176 "out_of_memory" => <<'END',
177 Ksplice has aborted the upgrade because the kernel ran out of memory.
179 "unexpected" => <<'END',
180 Ksplice has aborted because of an unexpected error.
181 Please consider reporting a bug to PACKAGE_BUGREPORT.
183 "UNKNOWN" => <<'END',
184 The Ksplice kernel component has returned an error code that this version of
185 ksplice-apply does not understand.
187 "ok" => <<'END',
188 Ksplice has aborted the upgrade for unknown reasons.
189 Please consider reporting a bug to PACKAGE_BUGREPORT.
192 $error = "UNKNOWN" if (!exists $errors{$error});
193 print STDERR "\n$errors{$error}\n";
196 =head1 NAME
198 ksplice-apply - Apply an on-disk Ksplice update to the running kernel
200 =head1 SYNOPSIS
202 B<ksplice-apply> I<UPDATE_TARBALL>
204 =head1 DESCRIPTION
206 B<ksplice-apply> takes as input a Ksplice update tarball, as generated by
207 L<ksplice-create(8)>, and it applies the update to the running binary kernel.
209 The update tarball used with B<ksplice-apply> must have been generated for the
210 running kernel's version.
212 =head1 OPTIONS
214 =over 8
216 =item B<--debug>
218 Applies the update with debugging output enabled. Recommended only for
219 debugging.
221 =back
223 =head1 SEE ALSO
225 L<ksplice-create(8)>, L<ksplice-view(8)>, L<ksplice-undo(8)>
227 =head1 BUGS
229 Please report bugs to <PACKAGE_BUGREPORT>.
231 =head1 COPYRIGHT
233 Copyright (C) 2007-2008 Jeffrey Brian Arnold <jbarnold@mit.edu>
235 Copyright (C) 2008 Anders Kaseorg <andersk@mit.edu>,
236 Tim Abbott <tabbott@mit.edu>
238 This is free software and documentation. You can redistribute and/or modify it
239 under the terms of the GNU General Public License, version 2.
241 =cut