Make modinst into its own KSPLICE_MODE.
[ksplice.git] / ksplice-apply.in
blob19f7982ea23ae36bfb2ce1bb305f509a3edf921b
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 die "No contents file in $file\n" if (!-e "contents");
41 open(CONTENTS, '<', "contents");
42 my $core;
43 while (<CONTENTS>) {
44 my @line = split(' ', $_);
45 if ($line[0] eq 'core') {
46 die "Multiple core modules in $file!" if (defined $core);
47 $core = {
48 module => $line[1],
49 file => $line[2],
53 close(CONTENTS);
55 my $nounload = runstr("lsmod") =~ m/- $/m;
57 (my $kid = $ksplice) =~s|ksplice[-_]([^-_]+)|$1|;
58 my $update = "ksplice_$kid";
59 if(update_loaded($kid)) {
60 my $stage = get_stage($kid);
61 if ($stage eq "applied") {
62 print STDERR "Ksplice update $kid already applied.\n";
63 exit(0);
65 die "Reversed Ksplice module $update already loaded!" if ($stage eq "reversed");
68 runstr_err(qw(modprobe -q ksplice)) eq "" || die;
69 if (defined $core) {
70 die "Could not find Ksplice core module $core->{file}\n" if (!-e $core->{file});
71 if (runstr("lsmod") =~ m/^\Q$core->{module}\E\s+/) {
72 die "Ksplice core module $core already loaded.";
74 if (!load_module($core->{file}, "debug=$debug")) {
75 die "Error loading Ksplice core module $core->{module} for update $kid";
79 my @helpers = ();
80 foreach my $helper (glob('ksplice-*-helper.ko')) {
81 (my $primary = $helper) =~ s/\-helper\.ko$/.ko/;
82 die unless(-e $primary && -e $helper);
83 (my $target = $primary) =~ s/^ksplice-[^_]+_(.*)\.ko/$1/;
84 if ($target ne 'vmlinux' && runstr("lsmod") !~ m/^${target}\s/m) {
85 if (!$partial) {
86 cleanup_modules();
87 print_error("target_not_loaded");
88 die "Module $target to be patched not loaded";
91 push @helpers, $helper;
93 foreach my $helper (@helpers) {
94 (my $primary = $helper) =~ s/\-helper\.ko$/.ko/;
95 if(!load_module($primary)) {
96 die "Error loading primary module $primary";
98 if(!load_module($helper)) {
99 my ($debugfile, $debugout) = get_debug_output("init_$kid");
100 print STDERR "Error loading helper module $helper\n";
101 (my $module = $primary) =~ s/-/_/g;
102 $module =~ s/\.ko//;
103 cleanup_modules();
104 if($debug && defined $debugfile) {
105 print $debugout;
106 print("Debugging output saved to $debugfile\n");
108 die;
112 set_debug_level($kid, $debug);
113 set_partial($kid, $partial);
114 set_stage($kid, "applied");
115 my $stage = get_stage($kid);
116 if($stage ne 'applied') {
117 my ($debugfile, $debugout) = get_debug_output($kid);
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 $debugout if (defined $debugfile);
123 print_error($abort_cause);
124 if ($abort_cause eq 'code_busy') {
125 print $conflicts;
127 if ($debug && defined $debugfile) {
128 print("Debugging output saved to $debugfile\n");
130 die;
132 mkpath("/var/run/ksplice/updates/$kid");
133 copy("patch", "/var/run/ksplice/updates/$kid/patch") if (-e "patch");
134 copy("description", "/var/run/ksplice/updates/$kid/description") if (-e "description");
135 if (!$nounload) {
136 foreach my $helper (@helpers) {
137 $helper =~ s/-/_/g;
138 $helper =~ s/\.ko$//;
139 (my $primary = $helper) =~ s/_helper$//;
140 runval('rmmod', $helper);
141 runval('rmmod', $primary) if ($partial && refcount($primary) == 0);
144 print "Done!\n";
145 exit(0);
147 my @modules_loaded = qw();
149 sub load_module {
150 my ($module, @params) = @_;
151 if (runval_raw("insmod", $module, @params) != 0) {
152 child_error();
153 return 0;
155 $module =~ s/\.ko//;
156 $module =~ s/-/_/g;
157 push @modules_loaded, $module;
158 return 1;
161 sub refcount {
162 my ($module) = @_;
163 foreach(split(/\n/, runstr("lsmod"))) {
164 if (m/^(\S+)\s+[0-9]+\s+([0-9])+\s/) {
165 return $2 if ($1 eq $module);
168 return -1;
171 sub cleanup_modules {
172 foreach my $module (reverse(@modules_loaded)) {
173 runval("rmmod", $module) if(!$nounload);
177 sub print_error {
178 my ($error) = @_;
179 my %errors = (
180 "no_match" => <<'END',
181 Ksplice has aborted the upgrade because Ksplice has been unable to match the
182 object code produced by your current compiler and assembler against the running
183 kernel's object code. If you provided the exact kernel source to the running
184 kernel, then it appears that your current compiler and/or assembler are
185 behaving differently from the compiler and assembler used to build the running
186 kernel. If possible, please use the exact compiler and assembler that were
187 used to build the running kernel. If you are using exactly the same compiler
188 and assembler, consider reporting a bug to PACKAGE_BUGREPORT.
190 "code_busy" => <<'END',
191 Ksplice has aborted the upgrade because it appears that the code that you are
192 trying to patch is continuously in use by the system. More specifically,
193 Ksplice has been unable to find a moment when one or more of the to-be-patched
194 functions is not on a thread's kernel stack.
196 "bad_system_map" => <<'END',
197 Ksplice has aborted the upgrade because it appears that the System.map file
198 provided to ksplice-create does not match the running kernel.
200 "failed_to_find" => <<'END',
201 Ksplice has aborted the upgrade because it was unable to resolve some of the
202 symbols used in the update.
204 "already_reversed" => <<'END',
205 The Ksplice update that you are attempting to apply has already been applied
206 and reversed. You need to unload the Ksplice modules associated with this
207 update before you can apply this update again.
209 "missing_export" => <<'END',
210 Ksplice has aborted the upgrade because the symbols exported by the kernel
211 did not match Ksplice's expectations.
213 "unexpected_running_task" => <<'END',
214 Ksplice has aborted the upgrade because of an unexpected failure during the
215 kernel stack check. Please consider reporting a bug to PACKAGE_BUGREPORT.
217 "target_not_loaded" => <<'END',
218 Ksplice has aborted the upgrade because one of the modules to be
219 patched by the update was not loaded. If you want to apply this
220 update only to those modules that are loaded, then you should use the
221 --partial option.
223 "out_of_memory" => <<'END',
224 Ksplice has aborted the upgrade because the kernel ran out of memory.
226 "call_failed" => <<'END',
227 Ksplice has aborted the upgrade at the request of a one of the
228 pre-application hooks that were included as part of this Ksplice
229 update. This is likely the result of a bug in the patch used to
230 generate this update.
232 "unexpected" => <<'END',
233 Ksplice has aborted because of an unexpected error.
234 Please consider reporting a bug to PACKAGE_BUGREPORT.
236 "UNKNOWN" => <<'END',
237 The Ksplice kernel component has returned an error code that this version of
238 ksplice-apply does not understand.
240 "ok" => <<'END',
241 Ksplice has aborted the upgrade for unknown reasons.
242 Please consider reporting a bug to PACKAGE_BUGREPORT.
245 $error = "UNKNOWN" if (!exists $errors{$error});
246 print STDERR "\n$errors{$error}\n";
249 =head1 NAME
251 ksplice-apply - Apply an on-disk Ksplice update to the running kernel
253 =head1 SYNOPSIS
255 B<ksplice-apply> I<UPDATE_TARBALL>
257 =head1 DESCRIPTION
259 B<ksplice-apply> takes as input a Ksplice update tarball, as generated by
260 L<ksplice-create(8)>, and it applies the update to the running binary kernel.
262 The update tarball used with B<ksplice-apply> must have been generated for the
263 running kernel's version.
265 =head1 OPTIONS
267 =over 8
269 =item B<--debug>
271 Applies the update with debugging output enabled. Recommended only for
272 debugging.
274 =item B<--partial>
276 Applies the update only to those modules which are loaded. Any
277 modules patched by the update that are not loaded are ignored (without
278 this option, Ksplice aborts if any modules patched by the update are
279 not loaded).
281 =back
283 =head1 SEE ALSO
285 L<ksplice-create(8)>, L<ksplice-view(8)>, L<ksplice-undo(8)>
287 =head1 BUGS
289 Please report bugs to <PACKAGE_BUGREPORT>.
291 =head1 COPYRIGHT
293 Copyright (C) 2007-2008 Jeffrey Brian Arnold <jbarnold@mit.edu>
295 Copyright (C) 2008 Anders Kaseorg <andersk@mit.edu>,
296 Tim Abbott <tabbott@mit.edu>
298 This is free software and documentation. You can redistribute and/or modify it
299 under the terms of the GNU General Public License, version 2.
301 =cut