undo: clean up if only new_code loaded, too
[ksplice.git] / ksplice-undo.in
blob47fd308608b0b1f112de93a591784cfc24c09e8c
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 lib 'KSPLICE_DATA_DIR';
22 use Ksplice;
23 my $have_usleep = eval { require Time::HiRes };
25 my $debug;
26 my $debugon = 0;
27 GetOptions(@common_options,
28 "debug" => \$debugon,
29 "debugfile=s" => \$debug) or pod2usage(1);
31 pod2usage(1) if($help || scalar(@ARGV) != 1);
32 $debugon = 1 if (defined($debug));
33 $debug = abs_path($debug) if (defined $debug);
35 my $kid = $ARGV[0];
36 $kid =~ s/^ksplice[-_]//;
37 $kid =~ s/[-_].*$//;
38 my $update = "ksplice_$kid";
39 my $nounload = runstr("lsmod") =~ m/- $/m;
41 chdir("/sys/module");
43 if (!update_loaded($kid)
44 && !-e $update && !defined(glob("${update}_*_{old,new}"))) {
45 print "Ksplice id $kid is not present in the kernel\n";
46 exit(1);
48 my $have_sysfs = update_loaded($kid);
50 set_debug_level($kid, $debugon);
51 my $failed = 0;
53 foreach my $module (glob("${update}_*_old")) {
54 if (!$nounload && runval_raw('rmmod', $module) != 0) {
55 child_error();
56 error_die();
60 my $stage = ($have_sysfs && get_stage($kid));
61 if ($stage ne 'applied') {
62 print "Ksplice id $kid is not applied; just cleaning up\n";
64 else {
65 set_stage($kid, "reversed");
67 $stage = ($have_sysfs && get_stage($kid));
68 ($stage eq 'reversed') or !$have_sysfs or error_die();
70 foreach my $module (glob("${update}_*_new")) {
71 if (!$nounload && runval_raw('rmmod', $module) != 0) {
72 child_error();
73 error_die();
77 set_stage($kid, "cleanup") if ($have_sysfs);
78 if ($have_usleep) {
79 my $count = 0;
80 while (update_loaded($kid) && $count < 5) {
81 Time::HiRes::usleep(100000);
82 $count++;
84 } else {
85 sleep(1) if (!update_loaded($kid));
87 if (-e "/sys/module/$update") {
88 if (!$nounload && runval_raw('rmmod', $update) != 0) {
89 child_error();
90 error_die();
94 rmtree("/var/run/ksplice/updates/$kid") if (-e "/var/run/ksplice/updates/$kid");
96 exit(0);
98 sub error_die {
99 if (!update_loaded($kid)) {
100 print STDERR "Error undoing Ksplice update $kid\n";
101 die;
103 my $debugfile = get_debug_output($kid, $debug);
104 print STDERR "Error undoing Ksplice update $kid:\n";
105 my $abort_cause = get_abort_cause($kid);
106 my $conflicts = get_conflicts($kid);
107 print_error($abort_cause);
108 if ($abort_cause eq 'code_busy') {
109 print $conflicts;
111 if($debugon && defined $debugfile) {
112 print("Debugging output saved to $debugfile\n");
114 die;
117 sub print_error {
118 my ($error) = @_;
119 my %errors = (
120 "code_busy" => <<'END',
121 Ksplice has aborted the undo operation because it appears that the code that
122 you are trying to change is continuously in use by the system. More
123 specifically, Ksplice has been unable to find a moment when one or more of the
124 to-be-changed functions is not on a thread's kernel stack.
126 "module_busy" => <<'END',
127 Ksplice has aborted the undo operation because it appears that the Ksplice
128 update that you are trying to unload is in use by another module. You can find
129 out what module is using the Ksplice update using the lsmod command.
131 "unexpected_running_task" => <<'END',
132 Ksplice has aborted the undo operation because of an unexpected failure during
133 the kernel stack check. Please consider reporting a bug to PACKAGE_BUGREPORT.
135 "out_of_memory" => <<'END',
136 Ksplice has aborted the undo operation because the kernel ran out of memory.
138 "call_failed" => <<'END',
139 Ksplice has aborted the undo operation at the request of a one of the
140 pre-application hooks that were included as part of this Ksplice
141 update. This is likely the result of a bug in the patch used to
142 generate this update.
144 "cold_update_loaded" => <<'END',
145 Ksplice has aborted the undo operation because the update was applied using the
146 --partial option, and one of the modules that was changed by the update was loaded
147 since the update was applied. You must first unload that (cold patched) module
148 before you can undo this update.
150 "unexpected" => <<'END',
151 Ksplice has aborted because of an unexpected error.
152 Please consider reporting a bug to PACKAGE_BUGREPORT.
154 "UNKNOWN" => <<'END',
155 The Ksplice kernel component has returned an error code that this version of
156 ksplice-undo does not understand.
158 "ok" => <<'END',
159 Ksplice has aborted the undo operation for unknown reasons.
160 Please consider reporting a bug to PACKAGE_BUGREPORT.
163 $error = "UNKNOWN" if (!exists $errors{$error});
164 print STDERR "\n$errors{$error}\n";
167 =head1 NAME
169 ksplice-undo - Undo a Ksplice update that has been applied to the running kernel
171 =head1 SYNOPSIS
173 B<ksplice-undo> [I<OPTIONS>] I<KSPLICE_ID>
175 =head1 DESCRIPTION
177 B<ksplice-undo> takes as input a Ksplice identification tag, as reported by
178 L<ksplice-view(8)>, and it reverses that update within the running binary kernel.
180 =head1 OPTIONS
182 =over 8
184 =item B<--debug>
186 Reverses the update with debugging output enabled. Recommended only for
187 debugging.
189 =item B<--debugfile=>I<filename>
191 Sets the location where debugging output should be saved. Implies --debug.
193 =back
195 =head1 SEE ALSO
197 L<ksplice-create(8)>, L<ksplice-apply(8)>, L<ksplice-view(8)>
199 =head1 BUGS
201 Please report bugs to <PACKAGE_BUGREPORT>.
203 =head1 AUTHORS
205 Jeff Arnold, Anders Kaseorg, and Tim Abbott
207 =head1 COPYRIGHT
209 Copyright (C) 2007-2009 Ksplice, Inc.
211 This is free software and documentation. You can redistribute and/or modify it
212 under the terms of the GNU General Public License, version 2.
214 =cut