Split the addend field into the instruction and target addends.
[ksplice.git] / ksplice-apply.in
blobfa25e7f140316566948ad23e27522d098f584033
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") if (-e "patch");
108 copy("description", "/var/run/ksplice/updates/$kid/description") if (-e "description");
109 foreach my $helper (glob('ksplice-*-helper.ko')) {
110 $helper =~ s/-/_/g;
111 $helper =~ s/\.ko$//;
112 runval('rmmod', $helper) if(!$nounload);
114 print "Done!\n";
115 exit(0);
117 my @modules_loaded = qw();
119 sub load_module {
120 my ($module, @params) = @_;
121 if (runval_raw("insmod", $module, @params) != 0) {
122 child_error();
123 return 0;
125 $module =~ s/\.ko//;
126 $module =~ s/-/_/g;
127 push @modules_loaded, $module;
128 return 1;
131 sub cleanup_modules {
132 foreach my $module (reverse(@modules_loaded)) {
133 runval("rmmod", $module) if(!$nounload);
137 sub print_error {
138 my ($error) = @_;
139 my %errors = (
140 "no_match" => <<'END',
141 Ksplice has aborted the upgrade because Ksplice has been unable to match the
142 object code produced by your current compiler and assembler against the running
143 kernel's object code. If you provided the exact kernel source to the running
144 kernel, then it appears that your current compiler and/or assembler are
145 behaving differently from the compiler and assembler used to build the running
146 kernel. If possible, please use the exact compiler and assembler that were
147 used to build the running kernel. If you are using exactly the same compiler
148 and assembler, consider reporting a bug to PACKAGE_BUGREPORT.
150 "code_busy" => <<'END',
151 Ksplice has aborted the upgrade because it appears that the code that you are
152 trying to patch is continuously in use by the system. More specifically,
153 Ksplice has been unable to find a moment when one or more of the to-be-patched
154 functions is not on a thread's kernel stack.
156 "bad_system_map" => <<'END',
157 Ksplice has aborted the upgrade because it appears that the System.map file
158 provided to ksplice-create does not match the running kernel.
160 "failed_to_find" => <<'END',
161 Ksplice has aborted the upgrade because it was unable to resolve some of the
162 symbols used in the update.
164 "already_reversed" => <<'END',
165 The Ksplice update that you are attempting to apply has already been applied
166 and reversed. You need to unload the Ksplice modules associated with this
167 update before you can apply this update again.
169 "missing_export" => <<'END',
170 Ksplice has aborted the upgrade because the symbols exported by the kernel
171 did not match Ksplice's expectations.
173 "unexpected_running_task" => <<'END',
174 Ksplice has aborted the upgrade because of an unexpected failure during the
175 kernel stack check. Please consider reporting a bug to PACKAGE_BUGREPORT.
177 "out_of_memory" => <<'END',
178 Ksplice has aborted the upgrade because the kernel ran out of memory.
180 "unexpected" => <<'END',
181 Ksplice has aborted because of an unexpected error.
182 Please consider reporting a bug to PACKAGE_BUGREPORT.
184 "UNKNOWN" => <<'END',
185 The Ksplice kernel component has returned an error code that this version of
186 ksplice-apply does not understand.
188 "ok" => <<'END',
189 Ksplice has aborted the upgrade for unknown reasons.
190 Please consider reporting a bug to PACKAGE_BUGREPORT.
193 $error = "UNKNOWN" if (!exists $errors{$error});
194 print STDERR "\n$errors{$error}\n";
197 =head1 NAME
199 ksplice-apply - Apply an on-disk Ksplice update to the running kernel
201 =head1 SYNOPSIS
203 B<ksplice-apply> I<UPDATE_TARBALL>
205 =head1 DESCRIPTION
207 B<ksplice-apply> takes as input a Ksplice update tarball, as generated by
208 L<ksplice-create(8)>, and it applies the update to the running binary kernel.
210 The update tarball used with B<ksplice-apply> must have been generated for the
211 running kernel's version.
213 =head1 OPTIONS
215 =over 8
217 =item B<--debug>
219 Applies the update with debugging output enabled. Recommended only for
220 debugging.
222 =back
224 =head1 SEE ALSO
226 L<ksplice-create(8)>, L<ksplice-view(8)>, L<ksplice-undo(8)>
228 =head1 BUGS
230 Please report bugs to <PACKAGE_BUGREPORT>.
232 =head1 COPYRIGHT
234 Copyright (C) 2007-2008 Jeffrey Brian Arnold <jbarnold@mit.edu>
236 Copyright (C) 2008 Anders Kaseorg <andersk@mit.edu>,
237 Tim Abbott <tabbott@mit.edu>
239 This is free software and documentation. You can redistribute and/or modify it
240 under the terms of the GNU General Public License, version 2.
242 =cut