Add support for C.123.12345 symbols in static_local_symbol.
[ksplice.git] / ksplice-apply.in
blob31ce6405716c1aa9d613eeeeb85a15837e167318
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 my $partial = 0;
27 GetOptions(@common_options,
28 "partial" => \$partial,
29 "debug" => \$debug) or pod2usage(1);
31 pod2usage(1) if($help || scalar(@ARGV) != 1);
33 my $file = abs_path($ARGV[0]);
35 my $tmpdir = tempdir('ksplice-tmp-XXXXXX', TMPDIR => 1, CLEANUP => 1);
36 chdir($tmpdir);
37 my $ksplice = unpack_update($file);
38 chdir($ksplice);
40 my $nounload = runstr("lsmod") =~ m/- $/m;
42 (my $kid = $ksplice) =~s|ksplice[-_]([^-_]+)|$1|;
43 my $update = "ksplice_$kid";
44 if(update_loaded($kid)) {
45 my $stage = get_stage($kid);
46 if ($stage eq "applied") {
47 print STDERR "Ksplice update $kid already applied.\n";
48 exit(0);
50 die "Reversed Ksplice module $update already loaded!" if ($stage eq "reversed");
53 runstr_err(qw(modprobe -q ksplice)) eq "" || die;
54 if (-e "ksplice-$kid.ko") {
55 if (runstr("lsmod") =~ m/^ksplice_$kid\s+/) {
56 die "Ksplice core module ksplice_$kid already loaded.";
58 if (!load_module("ksplice-$kid.ko", "debug=$debug")) {
59 die "Error loading Ksplice core for update $kid";
63 my @helpers = ();
64 foreach my $helper (glob('ksplice-*-helper.ko')) {
65 (my $primary = $helper) =~ s/\-helper\.ko$/.ko/;
66 die unless(-e $primary && -e $helper);
67 (my $target = $primary) =~ s/^ksplice-[^_]+_(.*)\.ko/$1/;
68 if ($target ne 'vmlinux' && runstr("lsmod") !~ m/^${target}\s/m) {
69 if (!$partial) {
70 cleanup_modules();
71 print_error("target_not_loaded");
72 die "Module $target to be patched not loaded";
75 push @helpers, $helper;
77 foreach my $helper (@helpers) {
78 (my $primary = $helper) =~ s/\-helper\.ko$/.ko/;
79 if(!load_module($primary)) {
80 die "Error loading primary module $primary";
82 if(!load_module($helper)) {
83 my ($debugfile, $debugout) = get_debug_output("init_$kid");
84 print STDERR "Error loading helper module $helper\n";
85 (my $module = $primary) =~ s/-/_/g;
86 $module =~ s/\.ko//;
87 cleanup_modules();
88 if(defined $debugfile) {
89 print $debugout;
90 print("Debugging output saved to $debugfile\n");
92 die;
96 set_debug_level($kid, $debug);
97 set_partial($kid, $partial);
98 set_stage($kid, "applied");
99 my $stage = get_stage($kid);
100 if($stage ne 'applied') {
101 my ($debugfile, $debugout) = get_debug_output($kid);
102 my $abort_cause = get_abort_cause($kid);
103 my $conflicts = get_conflicts($kid);
104 cleanup_modules();
105 print STDERR "Error applying Ksplice update $kid:\n";
106 print $debugout if (defined $debugfile);
107 print_error($abort_cause);
108 if ($abort_cause eq 'code_busy') {
109 print $conflicts;
111 print("Debugging output saved to $debugfile\n") if (defined $debugfile);
112 die;
114 mkpath("/var/run/ksplice/updates/$kid");
115 copy("patch", "/var/run/ksplice/updates/$kid/patch") if (-e "patch");
116 copy("description", "/var/run/ksplice/updates/$kid/description") if (-e "description");
117 if (!$nounload) {
118 foreach my $helper (@helpers) {
119 $helper =~ s/-/_/g;
120 $helper =~ s/\.ko$//;
121 (my $primary = $helper) =~ s/_helper$//;
122 runval('rmmod', $helper);
123 runval('rmmod', $primary) if ($partial && refcount($primary) == 0);
126 print "Done!\n";
127 exit(0);
129 my @modules_loaded = qw();
131 sub load_module {
132 my ($module, @params) = @_;
133 if (runval_raw("insmod", $module, @params) != 0) {
134 child_error();
135 return 0;
137 $module =~ s/\.ko//;
138 $module =~ s/-/_/g;
139 push @modules_loaded, $module;
140 return 1;
143 sub refcount {
144 my ($module) = @_;
145 foreach(split(/\n/, runstr("lsmod"))) {
146 if (m/^(\S+)\s+[0-9]+\s+([0-9])+\s/) {
147 return $2 if ($1 eq $module);
150 return -1;
153 sub cleanup_modules {
154 foreach my $module (reverse(@modules_loaded)) {
155 runval("rmmod", $module) if(!$nounload);
159 sub print_error {
160 my ($error) = @_;
161 my %errors = (
162 "no_match" => <<'END',
163 Ksplice has aborted the upgrade because Ksplice has been unable to match the
164 object code produced by your current compiler and assembler against the running
165 kernel's object code. If you provided the exact kernel source to the running
166 kernel, then it appears that your current compiler and/or assembler are
167 behaving differently from the compiler and assembler used to build the running
168 kernel. If possible, please use the exact compiler and assembler that were
169 used to build the running kernel. If you are using exactly the same compiler
170 and assembler, consider reporting a bug to PACKAGE_BUGREPORT.
172 "code_busy" => <<'END',
173 Ksplice has aborted the upgrade because it appears that the code that you are
174 trying to patch is continuously in use by the system. More specifically,
175 Ksplice has been unable to find a moment when one or more of the to-be-patched
176 functions is not on a thread's kernel stack.
178 "bad_system_map" => <<'END',
179 Ksplice has aborted the upgrade because it appears that the System.map file
180 provided to ksplice-create does not match the running kernel.
182 "failed_to_find" => <<'END',
183 Ksplice has aborted the upgrade because it was unable to resolve some of the
184 symbols used in the update.
186 "already_reversed" => <<'END',
187 The Ksplice update that you are attempting to apply has already been applied
188 and reversed. You need to unload the Ksplice modules associated with this
189 update before you can apply this update again.
191 "missing_export" => <<'END',
192 Ksplice has aborted the upgrade because the symbols exported by the kernel
193 did not match Ksplice's expectations.
195 "unexpected_running_task" => <<'END',
196 Ksplice has aborted the upgrade because of an unexpected failure during the
197 kernel stack check. Please consider reporting a bug to PACKAGE_BUGREPORT.
199 "target_not_loaded" => <<'END',
200 Ksplice has aborted the upgrade because one of the modules to be
201 patched by the update was not loaded. If you want to apply this
202 update only to those modules that are loaded, then you should use the
203 --partial option.
205 "out_of_memory" => <<'END',
206 Ksplice has aborted the upgrade because the kernel ran out of memory.
208 "unexpected" => <<'END',
209 Ksplice has aborted because of an unexpected error.
210 Please consider reporting a bug to PACKAGE_BUGREPORT.
212 "UNKNOWN" => <<'END',
213 The Ksplice kernel component has returned an error code that this version of
214 ksplice-apply does not understand.
216 "ok" => <<'END',
217 Ksplice has aborted the upgrade for unknown reasons.
218 Please consider reporting a bug to PACKAGE_BUGREPORT.
221 $error = "UNKNOWN" if (!exists $errors{$error});
222 print STDERR "\n$errors{$error}\n";
225 =head1 NAME
227 ksplice-apply - Apply an on-disk Ksplice update to the running kernel
229 =head1 SYNOPSIS
231 B<ksplice-apply> I<UPDATE_TARBALL>
233 =head1 DESCRIPTION
235 B<ksplice-apply> takes as input a Ksplice update tarball, as generated by
236 L<ksplice-create(8)>, and it applies the update to the running binary kernel.
238 The update tarball used with B<ksplice-apply> must have been generated for the
239 running kernel's version.
241 =head1 OPTIONS
243 =over 8
245 =item B<--debug>
247 Applies the update with debugging output enabled. Recommended only for
248 debugging.
250 =item B<--partial>
252 Applies the update only to those modules which are loaded. Any
253 modules patched by the update that are not loaded are ignored (without
254 this option, Ksplice aborts if any modules patched by the update are
255 not loaded).
257 =back
259 =head1 SEE ALSO
261 L<ksplice-create(8)>, L<ksplice-view(8)>, L<ksplice-undo(8)>
263 =head1 BUGS
265 Please report bugs to <PACKAGE_BUGREPORT>.
267 =head1 COPYRIGHT
269 Copyright (C) 2007-2008 Jeffrey Brian Arnold <jbarnold@mit.edu>
271 Copyright (C) 2008 Anders Kaseorg <andersk@mit.edu>,
272 Tim Abbott <tabbott@mit.edu>
274 This is free software and documentation. You can redistribute and/or modify it
275 under the terms of the GNU General Public License, version 2.
277 =cut