apply: handle /var/run directory near-atomically too
[ksplice.git] / ksplice-apply.in
blob547d68d6cb4d8715fc90a02e83d636781fb6afa2
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 my $debug;
26 my $debugon = 0;
27 my $partial = 0;
28 GetOptions(@common_options,
29 "partial" => \$partial,
30 "debug" => \$debugon,
31 "debugfile=s" => \$debug) or pod2usage(1);
33 pod2usage(1) if($help || scalar(@ARGV) != 1);
35 my $file = abs_path($ARGV[0]);
36 $debugon = 1 if(defined $debug);
37 $debug = abs_path($debug) if (defined $debug);
39 my $tmpdir = tempdir('ksplice-tmp-XXXXXX', TMPDIR => 1, CLEANUP => 1);
40 chdir($tmpdir);
41 my $ksplice = unpack_update($file);
42 chdir($ksplice);
44 die "No contents file in $file\n" if (!-e "contents");
45 open(CONTENTS, '<', "contents");
46 my $core;
47 my @changes;
48 while (<CONTENTS>) {
49 my @line = split(' ', $_);
50 if ($line[0] eq 'core') {
51 die "Multiple core modules in $file!" if (defined $core);
52 $core = {
53 'module' => $line[1],
54 'file' => $line[2],
57 if ($line[0] eq 'change') {
58 my $change = {
59 'target' => $line[1],
60 'new_code' => $line[2],
61 'new_code_file' => $line[3],
62 'old_code' => $line[4],
63 'old_code_file' => $line[5],
65 push @changes, $change;
68 close(CONTENTS);
70 die "Update was built using an old version of Ksplice" if (@changes == 0);
72 my $nounload = runstr("lsmod") =~ m/- $/m;
74 (my $kid = $ksplice) =~s|ksplice[-_]([^-_]+)|$1|;
75 my $update = "ksplice_$kid";
76 if(update_loaded($kid)) {
77 my $stage = get_stage($kid);
78 if ($stage eq "applied") {
79 print STDERR "Ksplice update $kid already applied.\n";
80 exit(0);
82 die "Reversed Ksplice module $update already loaded!" if ($stage eq "reversed");
85 runstr_err(qw(modprobe -q ksplice)) eq "" || die;
86 if (defined $core) {
87 die "Could not find Ksplice core module $core->{file}\n" if (!-e $core->{file});
88 if (runstr("lsmod") =~ m/^\Q$core->{module}\E\s+/) {
89 die "Ksplice core module $core already loaded.";
91 if (!load_module($core->{file}, "debug=$debugon")) {
92 die "Error loading Ksplice core module $core->{module} for update $kid";
96 foreach my $change (@changes) {
97 die unless (-e $change->{old_code_file} && -e $change->{new_code_file});
98 if ($change->{'target'} ne 'vmlinux' &&
99 runstr("lsmod") !~ m/^\Q$change->{target}\E\s+/m) {
100 if (!$partial) {
101 cleanup_modules();
102 print_error("target_not_loaded");
103 die "Module $change->{target} to be patched not loaded";
108 foreach my $change (@changes) {
109 if(!load_module($change->{new_code_file})) {
110 die "Error loading new code module $change->{new_code}";
112 if(!load_module($change->{old_code_file})) {
113 my $debugfile = get_debug_output("init_$kid", $debug);
114 print STDERR "Error loading old code module $change->{old_code}\n";
115 cleanup_modules();
116 if($debugon && defined $debugfile) {
117 print("Debugging output saved to $debugfile\n");
119 die;
123 mkpath("/var/run/ksplice/updates/$kid");
124 copy("patch", "/var/run/ksplice/updates/$kid/patch") if (-e "patch");
125 copy("description", "/var/run/ksplice/updates/$kid/description") if (-e "description");
127 set_debug_level($kid, $debugon);
128 set_partial($kid, $partial);
129 set_stage($kid, "applied");
130 my $stage = get_stage($kid);
131 if($stage ne 'applied') {
132 my $debugfile = get_debug_output($kid, $debug);
133 my $abort_cause = get_abort_cause($kid);
134 my $conflicts = get_conflicts($kid);
135 rmtree("/var/run/ksplice/updates/$kid") if (-e "/var/run/ksplice/updates/$kid");
136 cleanup_modules();
137 print STDERR "Error applying Ksplice update $kid:\n";
138 print_error($abort_cause);
139 if ($abort_cause eq 'code_busy') {
140 print $conflicts;
142 if ($debugon && defined $debugfile) {
143 print("Debugging output saved to $debugfile\n");
145 die;
148 if (!$nounload) {
149 foreach my $change (@changes) {
150 runval('rmmod', $change->{old_code});
151 runval('rmmod', $change->{new_code}) if ($partial && refcount($change->{new_code}) == 0);
155 print "Done!\n";
156 exit(0);
158 my @modules_loaded = qw();
160 sub handler {
161 my ($sig) = @_;
162 die "caught SIG$sig, abort\n" if (!defined($kid));
163 die "caught SIG$sig after finished\n"
164 if (update_loaded($kid) && (get_stage($kid) eq 'applied'));
165 print STDERR "caught SIG$sig, aborting\n";
166 rmtree("/var/run/ksplice/updates/$kid") if (-e "/var/run/ksplice/updates/$kid");
167 cleanup_modules();
168 exit(1);
171 sub load_module {
172 my ($module, @params) = @_;
173 push @modules_loaded, ($module =~ m/^(.*)\.ko$/);
174 if (runval_raw("insmod", $module, @params) != 0) {
175 pop @modules_loaded;
176 child_error();
177 return 0;
179 return 1;
182 sub refcount {
183 my ($module) = @_;
184 $module =~ s/-/_/g;
185 foreach(split(/\n/, runstr("lsmod"))) {
186 if (m/^(\S+)\s+[0-9]+\s+([0-9])+\s/) {
187 return $2 if ($1 eq $module);
190 return -1;
193 sub cleanup_modules {
194 foreach my $module (reverse(@modules_loaded)) {
195 runval_raw("rmmod", $module) if(!$nounload);
199 sub print_error {
200 my ($error) = @_;
201 my %errors = (
202 "no_match" => <<'END',
203 Ksplice has aborted the upgrade because Ksplice has been unable to match the
204 object code produced by your current compiler and assembler against the running
205 kernel's object code. If you provided the exact kernel source to the running
206 kernel, then it appears that your current compiler and/or assembler are
207 behaving differently from the compiler and assembler used to build the running
208 kernel. If possible, please use the exact compiler and assembler that were
209 used to build the running kernel. If you are using exactly the same compiler
210 and assembler, consider reporting a bug to PACKAGE_BUGREPORT.
212 "code_busy" => <<'END',
213 Ksplice has aborted the upgrade because it appears that the code that you are
214 trying to patch is continuously in use by the system. More specifically,
215 Ksplice has been unable to find a moment when one or more of the to-be-patched
216 functions is not on a thread's kernel stack.
218 "bad_system_map" => <<'END',
219 Ksplice has aborted the upgrade because it appears that the System.map file
220 provided to ksplice-create does not match the running kernel.
222 "failed_to_find" => <<'END',
223 Ksplice has aborted the upgrade because it was unable to resolve some of the
224 symbols used in the update.
226 "already_reversed" => <<'END',
227 The Ksplice update that you are attempting to apply has already been applied
228 and reversed. You need to unload the Ksplice modules associated with this
229 update before you can apply this update again.
231 "missing_export" => <<'END',
232 Ksplice has aborted the upgrade because the symbols exported by the kernel
233 did not match Ksplice's expectations.
235 "unexpected_running_task" => <<'END',
236 Ksplice has aborted the upgrade because of an unexpected failure during the
237 kernel stack check. Please consider reporting a bug to PACKAGE_BUGREPORT.
239 "target_not_loaded" => <<'END',
240 Ksplice has aborted the upgrade because one of the modules to be
241 patched by the update was not loaded. If you want to apply this
242 update only to those modules that are loaded, then you should use the
243 --partial option.
245 "out_of_memory" => <<'END',
246 Ksplice has aborted the upgrade because the kernel ran out of memory.
248 "call_failed" => <<'END',
249 Ksplice has aborted the upgrade at the request of a one of the
250 pre-application hooks that were included as part of this Ksplice
251 update. This is likely the result of a bug in the patch used to
252 generate this update.
254 "unexpected" => <<'END',
255 Ksplice has aborted because of an unexpected error.
256 Please consider reporting a bug to PACKAGE_BUGREPORT.
258 "UNKNOWN" => <<'END',
259 The Ksplice kernel component has returned an error code that this version of
260 ksplice-apply does not understand.
262 "ok" => <<'END',
263 Ksplice has aborted the upgrade for unknown reasons.
264 Please consider reporting a bug to PACKAGE_BUGREPORT.
267 $error = "UNKNOWN" if (!exists $errors{$error});
268 print STDERR "\n$errors{$error}\n";
271 =head1 NAME
273 ksplice-apply - Apply an on-disk Ksplice update to the running kernel
275 =head1 SYNOPSIS
277 B<ksplice-apply> [I<OPTIONS>] I<UPDATE_TARBALL>
279 =head1 DESCRIPTION
281 B<ksplice-apply> takes as input a Ksplice update tarball, as generated by
282 L<ksplice-create(8)>, and it applies the update to the running binary kernel.
284 The update tarball used with B<ksplice-apply> must have been generated for the
285 running kernel's version.
287 =head1 OPTIONS
289 =over 8
291 =item B<--debug>
293 Applies the update with debugging output enabled. Recommended only for
294 debugging.
296 =item B<--debugfile=>I<filename>
298 Sets the location where debugging output should be saved. Implies --debug.
300 =item B<--partial>
302 Applies the update only to those modules which are loaded. Any
303 modules patched by the update that are not loaded are ignored (without
304 this option, Ksplice aborts if any modules patched by the update are
305 not loaded).
307 =back
309 =head1 SEE ALSO
311 L<ksplice-create(8)>, L<ksplice-view(8)>, L<ksplice-undo(8)>
313 =head1 BUGS
315 Please report bugs to <PACKAGE_BUGREPORT>.
317 =head1 AUTHORS
319 Jeff Arnold, Anders Kaseorg, and Tim Abbott
321 =head1 COPYRIGHT
323 Copyright (C) 2007-2009 Ksplice, Inc.
325 This is free software and documentation. You can redistribute and/or modify it
326 under the terms of the GNU General Public License, version 2.
328 =cut