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