/
guestunmount.c
348 lines (290 loc) · 8.79 KB
/
guestunmount.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* guestunmount
* Copyright (C) 2013 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <error.h>
#include <locale.h>
#include <libintl.h>
#include <poll.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "guestfs.h"
#include "guestfs-utils.h"
#include "ignore-value.h"
#include "getprogname.h"
#include "display-options.h"
static int do_fusermount (const char *mountpoint, char **error_rtn);
static void do_fuser (const char *mountpoint);
static bool quiet = false;
static size_t retries = 5;
static bool verbose = false;
static void __attribute__((noreturn))
usage (int status)
{
if (status != EXIT_SUCCESS)
fprintf (stderr, _("Try ‘%s --help’ for more information.\n"),
getprogname ());
else {
printf (_("%s: clean up a mounted filesystem\n"
"Copyright (C) 2013 Red Hat Inc.\n"
"Usage:\n"
" %s [--fd=FD] mountpoint\n"
"Options:\n"
" --fd=FD Pipe file descriptor to monitor\n"
" --help Display help message and exit\n"
" -q|--quiet Don't print fusermount errors\n"
" --no-retry Don't retry fusermount\n"
" --retry=N Retry fusermount N times (default: 5)\n"
" -v|--verbose Verbose messages\n"
" -V|--version Display version and exit\n"
),
getprogname (), getprogname ());
}
exit (status);
}
int
main (int argc, char *argv[])
{
enum { HELP_OPTION = CHAR_MAX + 1 };
static const char options[] = "qvV";
static const struct option long_options[] = {
{ "fd", 1, 0, 0 },
{ "help", 0, 0, HELP_OPTION },
{ "quiet", 0, 0, 'q' },
{ "long-options", 0, 0, 0 },
{ "no-retry", 0, 0, 0 },
{ "retry", 1, 0, 0 },
{ "short-options", 0, 0, 0 },
{ "verbose", 0, 0, 'v' },
{ "version", 0, 0, 'V' },
{ 0, 0, 0, 0 }
};
int c, fd = -1;
int option_index;
const char *mountpoint;
struct sigaction sa;
struct pollfd pollfd;
char *error_str = NULL;
size_t i;
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEBASEDIR);
textdomain (PACKAGE);
for (;;) {
c = getopt_long (argc, argv, options, long_options, &option_index);
if (c == -1) break;
switch (c) {
case 0: /* options which are long only */
if (STREQ (long_options[option_index].name, "long-options"))
display_long_options (long_options);
else if (STREQ (long_options[option_index].name, "short-options"))
display_short_options (options);
else if (STREQ (long_options[option_index].name, "fd")) {
if (sscanf (optarg, "%d", &fd) != 1 || fd < 0)
error (EXIT_FAILURE, 0, _("cannot parse fd option ‘%s’"), optarg);
} else if (STREQ (long_options[option_index].name, "no-retry")) {
retries = 0;
} else if (STREQ (long_options[option_index].name, "retry")) {
if (sscanf (optarg, "%zu", &retries) != 1 || retries >= 64)
error (EXIT_FAILURE, 0,
_("cannot parse retries option or value is too large ‘%s’"),
optarg);
} else
error (EXIT_FAILURE, 0,
_("unknown long option: %s (%d)"),
long_options[option_index].name, option_index);
break;
case 'q':
quiet = true;
break;
case 'v':
verbose = true;
break;
case 'V':
printf ("guestunmount %s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
exit (EXIT_SUCCESS);
case HELP_OPTION:
usage (EXIT_SUCCESS);
default:
usage (EXIT_FAILURE);
}
}
/* We'd better have a mountpoint. */
if (optind+1 != argc)
error (EXIT_FAILURE, 0,
_("you must specify a mountpoint in the host filesystem\n"));
mountpoint = argv[optind];
/* Monitor the pipe until we get POLLHUP. */
if (fd >= 0) {
ignore_value (chdir ("/"));
/* Ignore keyboard signals. */
memset (&sa, 0, sizeof sa);
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_RESTART;
sigaction (SIGINT, &sa, NULL);
sigaction (SIGQUIT, &sa, NULL);
while (1) {
pollfd.fd = fd;
pollfd.events = POLLIN;
pollfd.revents = 0;
if (poll (&pollfd, 1, -1) == -1) {
if (errno != EAGAIN && errno != EINTR)
error (EXIT_FAILURE, errno, "poll");
}
else {
if ((pollfd.revents & POLLHUP) != 0)
break;
}
}
}
/* Unmount the filesystem. We may have to try a few times. */
for (i = 0; i <= retries; ++i) {
if (i > 0)
sleep (1 << (i-1));
free (error_str);
error_str = NULL;
if (do_fusermount (mountpoint, &error_str) == 0)
goto done;
/* Did fusermount fail because the mountpoint is not mounted? */
if (error_str &&
strstr (error_str, "fusermount: entry for") != NULL) {
goto not_mounted;
}
}
/* fusermount failed after N retries */
if (!quiet) {
fprintf (stderr, _("%s: failed to unmount %s: %s\n"),
getprogname (), mountpoint, error_str);
do_fuser (mountpoint);
}
free (error_str);
exit (2);
/* not mounted */
not_mounted:
if (!quiet)
fprintf (stderr, _("%s: %s is not mounted: %s\n"),
getprogname (), mountpoint, error_str);
free (error_str);
exit (3);
/* success */
done:
exit (EXIT_SUCCESS);
}
static int
do_fusermount (const char *mountpoint, char **error_rtn)
{
int fd[2];
pid_t pid;
int r;
char *buf = NULL;
size_t allocsize = 0, len = 0;
if (pipe (fd) == -1)
error (EXIT_FAILURE, errno, "pipe");
if (verbose)
fprintf (stderr, "%s: running: fusermount -u %s\n",
getprogname (), mountpoint);
pid = fork ();
if (pid == -1)
error (EXIT_FAILURE, errno, "fork");
if (pid == 0) { /* Child - run fusermount. */
close (fd[0]);
dup2 (fd[1], 1);
dup2 (fd[1], 2);
close (fd[1]);
/* We have to parse error messages from fusermount, so ... */
setenv ("LC_ALL", "C", 1);
#ifdef __linux__
execlp ("fusermount", "fusermount", "-u", mountpoint, NULL);
#else
/* use umount where fusermount is not available */
execlp ("umount", "umount", mountpoint, NULL);
#endif
perror ("exec");
_exit (EXIT_FAILURE);
}
/* Parent - read from the pipe any errors etc. */
close (fd[1]);
while (1) {
if (len >= allocsize) {
allocsize += 256;
buf = realloc (buf, allocsize);
if (buf == NULL)
error (EXIT_FAILURE, errno, "realloc");
}
/* Leave space in the buffer for a terminating \0 character. */
r = read (fd[0], &buf[len], allocsize - len - 1);
if (r == -1)
error (EXIT_FAILURE, errno, "read");
if (r == 0)
break;
len += r;
}
if (close (fd[0]) == -1)
error (EXIT_FAILURE, errno, "close");
if (buf) {
/* Remove any trailing \n from the error message. */
while (len > 0 && buf[len-1] == '\n') {
buf[len-1] = '\0';
len--;
}
/* Make sure the error message is \0 terminated. */
if (len < allocsize)
buf[len] = '\0';
}
if (waitpid (pid, &r, 0) == -1)
error (EXIT_FAILURE, errno, "waitpid");
if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) {
if (verbose)
fprintf (stderr, "%s\n", buf);
*error_rtn = buf;
return 1; /* fusermount or exec failed */
}
if (verbose)
fprintf (stderr, "%s: fusermount successful\n",
getprogname ());
free (buf);
return 0; /* fusermount successful */
}
/* Try running 'fuser' on the mountpoint. This is for information
* only so don't fail if we can't run it.
*/
static void
do_fuser (const char *mountpoint)
{
pid_t pid;
pid = fork ();
if (pid == -1)
error (EXIT_FAILURE, errno, "fork");
if (pid == 0) { /* Child - run fuser. */
#ifdef __linux__
execlp (FUSER, "fuser", "-v", "-m", mountpoint, NULL);
#else
execlp (FUSER, "fuser", "-c", mountpoint, NULL);
#endif
_exit (EXIT_FAILURE);
}
waitpid (pid, NULL, 0);
}