Makefile.ksplice: Rewrite ksplice-cow-check as a wrapper function.
[ksplice.git] / ksplice-undo.in
blob0c89df868ef52efd9f31927e5857d10704b51a8e
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}_*_{o,n,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);
50 set_debug_level($kid, $debugon);
51 my $failed = 0;
53 foreach my $module (glob("${update}_*_{o,old}")) {
54 if (!$nounload && runval_raw('rmmod', $module) != 0) {
55 child_error();
56 error_die();
60 my $have_sysfs = update_loaded($kid);
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}_*_{n,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 die("Error undoing Ksplice update $kid\n");
103 print STDERR "Error undoing Ksplice update $kid:\n" unless $raw_errors;
104 print_error($kid);
105 if($debugon) {
106 my $debugfile = get_debug_output($kid, $debug);
107 print("Debugging output saved to $debugfile\n") if $debugfile;
109 exit(-1);
112 sub print_error {
113 my ($kid) = @_;
114 my %errors = (
115 "code_busy" => <<'END',
116 Ksplice has aborted the undo operation because it appears that the code that
117 you are trying to change is continuously in use by the system. More
118 specifically, Ksplice has been unable to find a moment when one or more of the
119 to-be-changed functions is not on a thread's kernel stack.
121 "module_busy" => <<'END',
122 Ksplice has aborted the undo operation because it appears that the Ksplice
123 update that you are trying to unload is in use by another module. You can find
124 out what module is using the Ksplice update using the lsmod command.
126 "unexpected_running_task" => <<'END',
127 Ksplice has aborted the undo operation because of an unexpected failure during
128 the kernel stack check. Please consider reporting a bug to PACKAGE_BUGREPORT.
130 "out_of_memory" => <<'END',
131 Ksplice has aborted the undo operation because the kernel ran out of memory.
133 "call_failed" => <<'END',
134 Ksplice has aborted the undo operation at the request of a one of the
135 pre-application hooks that were included as part of this Ksplice
136 update. This is likely the result of a bug in the patch used to
137 generate this update.
139 "cold_update_loaded" => <<'END',
140 Ksplice has aborted the undo operation because the update was applied using the
141 --partial option, and one of the modules that was changed by the update was loaded
142 since the update was applied. You must first unload that (cold patched) module
143 before you can undo this update.
145 "unexpected" => <<'END',
146 Ksplice has aborted because of an unexpected error.
147 Please consider reporting a bug to PACKAGE_BUGREPORT.
149 "UNKNOWN" => <<'END',
150 The Ksplice kernel component has returned an error code that this version of
151 ksplice-undo does not understand.
153 "ok" => <<'END',
154 Ksplice has aborted the undo operation for unknown reasons.
155 Please consider reporting a bug to PACKAGE_BUGREPORT.
158 print_abort_error($kid, %errors);
161 =head1 NAME
163 ksplice-undo - Undo a Ksplice update that has been applied to the running kernel
165 =head1 SYNOPSIS
167 B<ksplice-undo> [I<OPTIONS>] I<KSPLICE_ID>
169 =head1 DESCRIPTION
171 B<ksplice-undo> takes as input a Ksplice identification tag, as reported by
172 L<ksplice-view(8)>, and it reverses that update within the running binary kernel.
174 =head1 OPTIONS
176 =over 8
178 =item B<--debug>
180 Reverses the update with debugging output enabled. Recommended only for
181 debugging.
183 =item B<--debugfile=>I<filename>
185 Sets the location where debugging output should be saved. Implies --debug.
187 =item B<--raw-errors>
189 Print only raw error information designed to be machine-readable on
190 standard error (standard output is still intended to be
191 human-readable). If B<ksplice-undo> fails due to an error from the
192 Ksplice kernel modules, the first line on standard error will be a
193 Ksplice B<abort code> (see the Ksplice source code for documentation
194 on these codes). Further lines will vary depending on the abort code.
195 If B<ksplice-undo> fails for any other reason, it will output the line
196 C<"OTHER\n">, followed by a human-readable failure message, to
197 standard error.
199 =back
201 =head1 SEE ALSO
203 L<ksplice-create(8)>, L<ksplice-apply(8)>, L<ksplice-view(8)>
205 =head1 BUGS
207 Please report bugs to <PACKAGE_BUGREPORT>.
209 =head1 AUTHORS
211 Jeff Arnold, Anders Kaseorg, and Tim Abbott
213 =head1 COPYRIGHT
215 Copyright (C) 2007-2009 Ksplice, Inc.
217 This is free software and documentation. You can redistribute and/or modify it
218 under the terms of the GNU General Public License, version 2.
220 =cut