undo: remove *_old modules early, before reversing
[ksplice.git] / ksplice-undo.in
blobc4e67c8946d1f694212811cd3aeb08503468df41
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 if (!update_loaded($kid)) {
42 print "Ksplice id $kid is not present in the kernel\n";
43 exit(1);
46 chdir("/sys/module/");
47 set_debug_level($kid, $debugon);
48 my $failed = 0;
50 foreach my $module (glob("${update}_*_old")) {
51 if (!$nounload && runval_raw('rmmod', $module) != 0) {
52 child_error();
53 error_die();
57 my $stage = get_stage($kid);
58 if ($stage ne 'applied') {
59 print "Ksplice id $kid is not applied\n";
61 else {
62 set_stage($kid, "reversed");
64 $stage = get_stage($kid);
65 ($stage eq 'reversed') or error_die();
67 foreach my $module (glob("${update}_*_new")) {
68 if (!$nounload && runval_raw('rmmod', $module) != 0) {
69 child_error();
70 error_die();
74 set_stage($kid, "cleanup");
75 if ($have_usleep) {
76 my $count = 0;
77 while (update_loaded($kid) && $count < 5) {
78 Time::HiRes::usleep(100000);
79 $count++;
81 } else {
82 sleep(1) if (!update_loaded($kid));
84 if (-e "/sys/module/$update") {
85 if (!$nounload && runval_raw('rmmod', $update) != 0) {
86 child_error();
87 error_die();
91 rmtree("/var/run/ksplice/updates/$kid");
93 exit(0);
95 sub error_die {
96 if (!update_loaded($kid)) {
97 print STDERR "Error undoing Ksplice update $kid\n";
98 die;
100 my $debugfile = get_debug_output($kid, $debug);
101 print STDERR "Error undoing Ksplice update $kid:\n";
102 my $abort_cause = get_abort_cause($kid);
103 my $conflicts = get_conflicts($kid);
104 print_error($abort_cause);
105 if ($abort_cause eq 'code_busy') {
106 print $conflicts;
108 if($debugon && defined $debugfile) {
109 print("Debugging output saved to $debugfile\n");
111 die;
114 sub print_error {
115 my ($error) = @_;
116 my %errors = (
117 "code_busy" => <<'END',
118 Ksplice has aborted the undo operation because it appears that the code that
119 you are trying to change is continuously in use by the system. More
120 specifically, Ksplice has been unable to find a moment when one or more of the
121 to-be-changed functions is not on a thread's kernel stack.
123 "module_busy" => <<'END',
124 Ksplice has aborted the undo operation because it appears that the Ksplice
125 update that you are trying to unload is in use by another module. You can find
126 out what module is using the Ksplice update using the lsmod command.
128 "unexpected_running_task" => <<'END',
129 Ksplice has aborted the undo operation because of an unexpected failure during
130 the kernel stack check. Please consider reporting a bug to PACKAGE_BUGREPORT.
132 "out_of_memory" => <<'END',
133 Ksplice has aborted the undo operation because the kernel ran out of memory.
135 "call_failed" => <<'END',
136 Ksplice has aborted the undo operation at the request of a one of the
137 pre-application hooks that were included as part of this Ksplice
138 update. This is likely the result of a bug in the patch used to
139 generate this update.
141 "cold_update_loaded" => <<'END',
142 Ksplice has aborted the undo operation because the update was applied using the
143 --partial option, and one of the modules that was changed by the update was loaded
144 since the update was applied. You must first unload that (cold patched) module
145 before you can undo this update.
147 "unexpected" => <<'END',
148 Ksplice has aborted because of an unexpected error.
149 Please consider reporting a bug to PACKAGE_BUGREPORT.
151 "UNKNOWN" => <<'END',
152 The Ksplice kernel component has returned an error code that this version of
153 ksplice-undo does not understand.
155 "ok" => <<'END',
156 Ksplice has aborted the undo operation for unknown reasons.
157 Please consider reporting a bug to PACKAGE_BUGREPORT.
160 $error = "UNKNOWN" if (!exists $errors{$error});
161 print STDERR "\n$errors{$error}\n";
164 =head1 NAME
166 ksplice-undo - Undo a Ksplice update that has been applied to the running kernel
168 =head1 SYNOPSIS
170 B<ksplice-undo> [I<OPTIONS>] I<KSPLICE_ID>
172 =head1 DESCRIPTION
174 B<ksplice-undo> takes as input a Ksplice identification tag, as reported by
175 L<ksplice-view(8)>, and it reverses that update within the running binary kernel.
177 =head1 OPTIONS
179 =over 8
181 =item B<--debug>
183 Reverses the update with debugging output enabled. Recommended only for
184 debugging.
186 =item B<--debugfile=>I<filename>
188 Sets the location where debugging output should be saved. Implies --debug.
190 =back
192 =head1 SEE ALSO
194 L<ksplice-create(8)>, L<ksplice-apply(8)>, L<ksplice-view(8)>
196 =head1 BUGS
198 Please report bugs to <PACKAGE_BUGREPORT>.
200 =head1 AUTHORS
202 Jeff Arnold, Anders Kaseorg, and Tim Abbott
204 =head1 COPYRIGHT
206 Copyright (C) 2007-2009 Ksplice, Inc.
208 This is free software and documentation. You can redistribute and/or modify it
209 under the terms of the GNU General Public License, version 2.
211 =cut