]>
Commit | Line | Data |
---|---|---|
db216e68 KZ |
1 | /* |
2 | * umount(8) -- mount a filesystem | |
3 | * | |
4 | * Copyright (C) 2011 Red Hat, Inc. All rights reserved. | |
5 | * Written by Karel Zak <kzak@redhat.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it would be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
7cebf0bb SK |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
db216e68 KZ |
20 | */ |
21 | ||
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <errno.h> | |
25 | #include <string.h> | |
26 | #include <getopt.h> | |
27 | #include <unistd.h> | |
28 | #include <sys/types.h> | |
29 | ||
30 | #include <libmount.h> | |
31 | ||
32 | #include "nls.h" | |
33 | #include "c.h" | |
34 | #include "env.h" | |
efb8854f | 35 | #include "closestream.h" |
13ee1c91 | 36 | #include "pathnames.h" |
cc8cc8f3 | 37 | #include "canonicalize.h" |
778ca2a0 RM |
38 | |
39 | #define XALLOC_EXIT_CODE MNT_EX_SYSERR | |
4eb49f63 | 40 | #include "xalloc.h" |
db216e68 | 41 | |
39aa4e39 RM |
42 | #define OPTUTILS_EXIT_CODE MNT_EX_USAGE |
43 | #include "optutils.h" | |
44 | ||
d5fd9ac6 KZ |
45 | static int quiet; |
46 | ||
db216e68 KZ |
47 | static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)), |
48 | const char *filename, int line) | |
49 | { | |
50 | if (filename) | |
b779c1ae | 51 | warnx(_("%s: parse error at line %d -- ignored"), filename, line); |
1cd9d0d7 | 52 | return 1; |
db216e68 KZ |
53 | } |
54 | ||
ac8ecab4 | 55 | |
db216e68 KZ |
56 | static void __attribute__((__noreturn__)) print_version(void) |
57 | { | |
58 | const char *ver = NULL; | |
ac8ecab4 | 59 | const char **features = NULL, **p; |
db216e68 KZ |
60 | |
61 | mnt_get_library_version(&ver); | |
ac8ecab4 | 62 | mnt_get_library_features(&features); |
db216e68 | 63 | |
ac8ecab4 KZ |
64 | printf(_("%s from %s (libmount %s"), |
65 | program_invocation_short_name, | |
66 | PACKAGE_STRING, | |
67 | ver); | |
68 | p = features; | |
69 | while (p && *p) { | |
70 | fputs(p == features ? ": " : ", ", stdout); | |
71 | fputs(*p++, stdout); | |
72 | } | |
73 | fputs(")\n", stdout); | |
e058a6c1 | 74 | exit(MNT_EX_SUCCESS); |
db216e68 | 75 | } |
6e1eda6f | 76 | static void __attribute__((__noreturn__)) usage(void) |
db216e68 | 77 | { |
6e1eda6f | 78 | FILE *out = stdout; |
db216e68 KZ |
79 | fputs(USAGE_HEADER, out); |
80 | fprintf(out, _( | |
81 | " %1$s [-hV]\n" | |
82 | " %1$s -a [options]\n" | |
83 | " %1$s [options] <source> | <directory>\n"), | |
84 | program_invocation_short_name); | |
85 | ||
451dbcfa BS |
86 | fputs(USAGE_SEPARATOR, out); |
87 | fputs(_("Unmount filesystems.\n"), out); | |
88 | ||
db216e68 | 89 | fputs(USAGE_OPTIONS, out); |
4eb49f63 | 90 | fputs(_(" -a, --all unmount all filesystems\n"), out); |
8356d27d | 91 | fputs(_(" -A, --all-targets unmount all mountpoints for the given device in the\n" |
4ce393f4 | 92 | " current namespace\n"), out); |
83d91100 SK |
93 | fputs(_(" -c, --no-canonicalize don't canonicalize paths\n"), out); |
94 | fputs(_(" -d, --detach-loop if mounted loop device, also free this loop device\n"), out); | |
95 | fputs(_(" --fake dry run; skip the umount(2) syscall\n"), out); | |
96 | fputs(_(" -f, --force force unmount (in case of an unreachable NFS system)\n"), out); | |
97 | fputs(_(" -i, --internal-only don't call the umount.<type> helpers\n"), out); | |
98 | fputs(_(" -n, --no-mtab don't write to /etc/mtab\n"), out); | |
4ce393f4 | 99 | fputs(_(" -l, --lazy detach the filesystem now, clean up things later\n"), out); |
83d91100 SK |
100 | fputs(_(" -O, --test-opts <list> limit the set of filesystems (use with -a)\n"), out); |
101 | fputs(_(" -R, --recursive recursively unmount a target with all its children\n"), out); | |
4ce393f4 | 102 | fputs(_(" -r, --read-only in case unmounting fails, try to remount read-only\n"), out); |
83d91100 SK |
103 | fputs(_(" -t, --types <list> limit the set of filesystem types\n"), out); |
104 | fputs(_(" -v, --verbose say what is being done\n"), out); | |
d5fd9ac6 | 105 | fputs(_(" -q, --quiet suppress 'not mounted' error messages\n"), out); |
d45e8ef9 | 106 | fputs(_(" -N, --namespace <ns> perform umount in another namespace\n"), out); |
db216e68 KZ |
107 | |
108 | fputs(USAGE_SEPARATOR, out); | |
f45f3ec3 RM |
109 | printf(USAGE_HELP_OPTIONS(25)); |
110 | printf(USAGE_MAN_TAIL("umount(8)")); | |
db216e68 | 111 | |
6e1eda6f | 112 | exit(MNT_EX_SUCCESS); |
db216e68 KZ |
113 | } |
114 | ||
115 | static void __attribute__((__noreturn__)) exit_non_root(const char *option) | |
116 | { | |
117 | const uid_t ruid = getuid(); | |
118 | const uid_t euid = geteuid(); | |
119 | ||
120 | if (ruid == 0 && euid != 0) { | |
121 | /* user is root, but setuid to non-root */ | |
122 | if (option) | |
e058a6c1 | 123 | errx(MNT_EX_USAGE, |
db216e68 KZ |
124 | _("only root can use \"--%s\" option " |
125 | "(effective UID is %u)"), | |
126 | option, euid); | |
e058a6c1 | 127 | errx(MNT_EX_USAGE, _("only root can do that " |
db216e68 KZ |
128 | "(effective UID is %u)"), euid); |
129 | } | |
130 | if (option) | |
e058a6c1 KZ |
131 | errx(MNT_EX_USAGE, _("only root can use \"--%s\" option"), option); |
132 | errx(MNT_EX_USAGE, _("only root can do that")); | |
73f9a114 KZ |
133 | } |
134 | ||
84600ddc KZ |
135 | static void success_message(struct libmnt_context *cxt) |
136 | { | |
137 | const char *tgt, *src; | |
138 | ||
139 | if (mnt_context_helper_executed(cxt) | |
140 | || mnt_context_get_status(cxt) != 1) | |
141 | return; | |
142 | ||
143 | tgt = mnt_context_get_target(cxt); | |
144 | if (!tgt) | |
145 | return; | |
146 | ||
147 | src = mnt_context_get_source(cxt); | |
148 | if (src) | |
149 | warnx(_("%s (%s) unmounted"), tgt, src); | |
150 | else | |
151 | warnx(_("%s unmounted"), tgt); | |
152 | } | |
153 | ||
73f9a114 KZ |
154 | static int mk_exit_code(struct libmnt_context *cxt, int rc) |
155 | { | |
e1706a67 KZ |
156 | char buf[BUFSIZ] = { 0 }; |
157 | ||
158 | rc = mnt_context_get_excode(cxt, rc, buf, sizeof(buf)); | |
d5fd9ac6 KZ |
159 | |
160 | /* suppress "not mounted" error message */ | |
161 | if (quiet && | |
162 | rc == MNT_EX_FAIL && | |
163 | mnt_context_syscall_called(cxt) && | |
164 | mnt_context_get_syscall_errno(cxt) == EINVAL) | |
165 | return rc; | |
166 | ||
167 | /* print errors/warnings */ | |
e1706a67 KZ |
168 | if (*buf) { |
169 | const char *spec = mnt_context_get_target(cxt); | |
170 | if (!spec) | |
171 | spec = mnt_context_get_source(cxt); | |
172 | if (!spec) | |
173 | spec = "???"; | |
14056588 | 174 | warnx("%s: %s.", spec, buf); |
73f9a114 | 175 | } |
e1706a67 | 176 | return rc; |
db216e68 KZ |
177 | } |
178 | ||
190c342a | 179 | static int umount_all(struct libmnt_context *cxt) |
db216e68 | 180 | { |
190c342a KZ |
181 | struct libmnt_iter *itr; |
182 | struct libmnt_fs *fs; | |
183 | int mntrc, ignored, rc = 0; | |
184 | ||
185 | itr = mnt_new_iter(MNT_ITER_BACKWARD); | |
186 | if (!itr) { | |
187 | warn(_("failed to initialize libmount iterator")); | |
e058a6c1 | 188 | return MNT_EX_SYSERR; |
190c342a KZ |
189 | } |
190 | ||
191 | while (mnt_context_next_umount(cxt, itr, &fs, &mntrc, &ignored) == 0) { | |
192 | ||
193 | const char *tgt = mnt_fs_get_target(fs); | |
194 | ||
195 | if (ignored) { | |
196 | if (mnt_context_is_verbose(cxt)) | |
197 | printf(_("%-25s: ignored\n"), tgt); | |
190c342a | 198 | } else { |
0ce2fe87 | 199 | int xrc = mk_exit_code(cxt, mntrc); |
73f9a114 | 200 | |
e058a6c1 | 201 | if (xrc == MNT_EX_SUCCESS |
0ce2fe87 | 202 | && mnt_context_is_verbose(cxt)) |
4ce393f4 | 203 | printf("%-25s: successfully unmounted\n", tgt); |
0ce2fe87 | 204 | rc |= xrc; |
190c342a KZ |
205 | } |
206 | } | |
207 | ||
0f2d6476 | 208 | mnt_free_iter(itr); |
190c342a | 209 | return rc; |
db216e68 KZ |
210 | } |
211 | ||
212 | static int umount_one(struct libmnt_context *cxt, const char *spec) | |
213 | { | |
214 | int rc; | |
215 | ||
216 | if (!spec) | |
e058a6c1 | 217 | return MNT_EX_SOFTWARE; |
db216e68 KZ |
218 | |
219 | if (mnt_context_set_target(cxt, spec)) | |
e058a6c1 | 220 | err(MNT_EX_SYSERR, _("failed to set umount target")); |
db216e68 KZ |
221 | |
222 | rc = mnt_context_umount(cxt); | |
73f9a114 | 223 | rc = mk_exit_code(cxt, rc); |
db216e68 | 224 | |
e058a6c1 | 225 | if (rc == MNT_EX_SUCCESS && mnt_context_is_verbose(cxt)) |
84600ddc KZ |
226 | success_message(cxt); |
227 | ||
db216e68 KZ |
228 | mnt_reset_context(cxt); |
229 | return rc; | |
230 | } | |
231 | ||
4eb49f63 KZ |
232 | static struct libmnt_table *new_mountinfo(struct libmnt_context *cxt) |
233 | { | |
6e965d0f VD |
234 | struct libmnt_table *tb; |
235 | struct libmnt_ns *ns_old = mnt_context_switch_target_ns(cxt); | |
236 | ||
237 | if (!ns_old) | |
238 | err(MNT_EX_SYSERR, _("failed to switch namespace")); | |
239 | ||
240 | tb = mnt_new_table(); | |
4eb49f63 | 241 | if (!tb) |
e058a6c1 | 242 | err(MNT_EX_SYSERR, _("libmount table allocation failed")); |
4eb49f63 KZ |
243 | |
244 | mnt_table_set_parser_errcb(tb, table_parser_errcb); | |
245 | mnt_table_set_cache(tb, mnt_context_get_cache(cxt)); | |
246 | ||
247 | if (mnt_table_parse_file(tb, _PATH_PROC_MOUNTINFO)) { | |
248 | warn(_("failed to parse %s"), _PATH_PROC_MOUNTINFO); | |
50fccba1 | 249 | mnt_unref_table(tb); |
4eb49f63 KZ |
250 | tb = NULL; |
251 | } | |
252 | ||
6e965d0f VD |
253 | if (!mnt_context_switch_ns(cxt, ns_old)) |
254 | err(MNT_EX_SYSERR, _("failed to switch namespace")); | |
255 | ||
4eb49f63 KZ |
256 | return tb; |
257 | } | |
258 | ||
259 | /* | |
260 | * like umount_one() but does not return error is @spec not mounted | |
261 | */ | |
262 | static int umount_one_if_mounted(struct libmnt_context *cxt, const char *spec) | |
263 | { | |
264 | int rc; | |
265 | struct libmnt_fs *fs; | |
266 | ||
267 | rc = mnt_context_find_umount_fs(cxt, spec, &fs); | |
268 | if (rc == 1) { | |
e058a6c1 | 269 | rc = MNT_EX_SUCCESS; /* already unmounted */ |
4eb49f63 KZ |
270 | mnt_reset_context(cxt); |
271 | } else if (rc < 0) { | |
272 | rc = mk_exit_code(cxt, rc); /* error */ | |
273 | mnt_reset_context(cxt); | |
274 | } else | |
275 | rc = umount_one(cxt, mnt_fs_get_target(fs)); | |
276 | ||
277 | return rc; | |
278 | } | |
279 | ||
13ee1c91 | 280 | static int umount_do_recurse(struct libmnt_context *cxt, |
4eb49f63 | 281 | struct libmnt_table *tb, struct libmnt_fs *fs) |
13ee1c91 | 282 | { |
13ee1c91 | 283 | struct libmnt_fs *child; |
13ee1c91 | 284 | struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_BACKWARD); |
4eb49f63 | 285 | int rc; |
13ee1c91 DR |
286 | |
287 | if (!itr) | |
e058a6c1 | 288 | err(MNT_EX_SYSERR, _("libmount iterator allocation failed")); |
4eb49f63 | 289 | |
9e930041 | 290 | /* umount all children */ |
13ee1c91 | 291 | for (;;) { |
4eb49f63 | 292 | rc = mnt_table_next_child_fs(tb, itr, fs, &child); |
13ee1c91 | 293 | if (rc < 0) { |
4eb49f63 KZ |
294 | warnx(_("failed to get child fs of %s"), |
295 | mnt_fs_get_target(fs)); | |
e058a6c1 | 296 | rc = MNT_EX_SOFTWARE; |
13ee1c91 DR |
297 | goto done; |
298 | } else if (rc == 1) | |
299 | break; /* no more children */ | |
300 | ||
301 | rc = umount_do_recurse(cxt, tb, child); | |
e058a6c1 | 302 | if (rc != MNT_EX_SUCCESS) |
13ee1c91 DR |
303 | goto done; |
304 | } | |
305 | ||
4eb49f63 | 306 | rc = umount_one_if_mounted(cxt, mnt_fs_get_target(fs)); |
13ee1c91 DR |
307 | done: |
308 | mnt_free_iter(itr); | |
309 | return rc; | |
310 | } | |
311 | ||
312 | static int umount_recursive(struct libmnt_context *cxt, const char *spec) | |
313 | { | |
314 | struct libmnt_table *tb; | |
4eb49f63 | 315 | struct libmnt_fs *fs; |
13ee1c91 DR |
316 | int rc; |
317 | ||
4eb49f63 KZ |
318 | tb = new_mountinfo(cxt); |
319 | if (!tb) | |
e058a6c1 | 320 | return MNT_EX_SOFTWARE; |
4eb49f63 | 321 | |
ae978c4d KZ |
322 | /* it's always real mountpoint, don't assume that the target maybe a device */ |
323 | mnt_context_disable_swapmatch(cxt, 1); | |
324 | ||
4eb49f63 KZ |
325 | fs = mnt_table_find_target(tb, spec, MNT_ITER_BACKWARD); |
326 | if (fs) | |
327 | rc = umount_do_recurse(cxt, tb, fs); | |
328 | else { | |
e058a6c1 | 329 | rc = MNT_EX_USAGE; |
d5fd9ac6 KZ |
330 | if (!quiet) |
331 | warnx(access(spec, F_OK) == 0 ? | |
4eb49f63 KZ |
332 | _("%s: not mounted") : |
333 | _("%s: not found"), spec); | |
334 | } | |
a8cc72de | 335 | |
50fccba1 | 336 | mnt_unref_table(tb); |
4eb49f63 KZ |
337 | return rc; |
338 | } | |
7b4a2697 | 339 | |
4eb49f63 KZ |
340 | static int umount_alltargets(struct libmnt_context *cxt, const char *spec, int rec) |
341 | { | |
342 | struct libmnt_fs *fs; | |
343 | struct libmnt_table *tb; | |
344 | struct libmnt_iter *itr = NULL; | |
f697d61b | 345 | dev_t devno = 0; |
4eb49f63 KZ |
346 | int rc; |
347 | ||
348 | /* Convert @spec to device name, Use the same logic like regular | |
349 | * "umount <spec>". | |
13ee1c91 | 350 | */ |
4eb49f63 KZ |
351 | rc = mnt_context_find_umount_fs(cxt, spec, &fs); |
352 | if (rc == 1) { | |
e058a6c1 | 353 | rc = MNT_EX_USAGE; |
d5fd9ac6 KZ |
354 | if (!quiet) |
355 | warnx(access(spec, F_OK) == 0 ? | |
4eb49f63 KZ |
356 | _("%s: not mounted") : |
357 | _("%s: not found"), spec); | |
358 | return rc; | |
359 | } | |
360 | if (rc < 0) | |
361 | return mk_exit_code(cxt, rc); /* error */ | |
13ee1c91 | 362 | |
f697d61b | 363 | if (!mnt_fs_get_srcpath(fs) || !mnt_fs_get_devno(fs)) |
e058a6c1 | 364 | errx(MNT_EX_USAGE, _("%s: failed to determine source " |
fd7c4924 KZ |
365 | "(--all-targets is unsupported on systems with " |
366 | "regular mtab file)."), spec); | |
4eb49f63 KZ |
367 | |
368 | itr = mnt_new_iter(MNT_ITER_BACKWARD); | |
369 | if (!itr) | |
e058a6c1 | 370 | err(MNT_EX_SYSERR, _("libmount iterator allocation failed")); |
4eb49f63 KZ |
371 | |
372 | /* get on @cxt independent mountinfo */ | |
373 | tb = new_mountinfo(cxt); | |
86c58c4a | 374 | if (!tb) { |
e058a6c1 | 375 | rc = MNT_EX_SOFTWARE; |
86c58c4a KZ |
376 | goto done; |
377 | } | |
4eb49f63 | 378 | |
9e930041 | 379 | /* Note that @fs is from mount context and the context will be reset |
4eb49f63 | 380 | * after each umount() call */ |
f697d61b | 381 | devno = mnt_fs_get_devno(fs); |
4eb49f63 KZ |
382 | fs = NULL; |
383 | ||
384 | mnt_reset_context(cxt); | |
385 | ||
386 | while (mnt_table_next_fs(tb, itr, &fs) == 0) { | |
f697d61b | 387 | if (mnt_fs_get_devno(fs) != devno) |
4eb49f63 KZ |
388 | continue; |
389 | mnt_context_disable_swapmatch(cxt, 1); | |
390 | if (rec) | |
13ee1c91 | 391 | rc = umount_do_recurse(cxt, tb, fs); |
4eb49f63 KZ |
392 | else |
393 | rc = umount_one_if_mounted(cxt, mnt_fs_get_target(fs)); | |
394 | ||
e058a6c1 | 395 | if (rc != MNT_EX_SUCCESS) |
4eb49f63 | 396 | break; |
13ee1c91 DR |
397 | } |
398 | ||
86c58c4a | 399 | done: |
4eb49f63 | 400 | mnt_free_iter(itr); |
50fccba1 | 401 | mnt_unref_table(tb); |
4eb49f63 | 402 | |
13ee1c91 DR |
403 | return rc; |
404 | } | |
405 | ||
cc8cc8f3 KZ |
406 | /* |
407 | * Check path -- non-root user should not be able to resolve path which is | |
408 | * unreadable for him. | |
409 | */ | |
410 | static char *sanitize_path(const char *path) | |
411 | { | |
412 | char *p; | |
413 | ||
414 | if (!path) | |
415 | return NULL; | |
416 | ||
417 | p = canonicalize_path_restricted(path); | |
418 | if (!p) | |
e058a6c1 | 419 | err(MNT_EX_USAGE, "%s", path); |
cc8cc8f3 KZ |
420 | |
421 | return p; | |
422 | } | |
423 | ||
c1e70afe VD |
424 | static pid_t parse_pid(const char *str) |
425 | { | |
426 | char *end; | |
427 | pid_t ret; | |
428 | ||
429 | errno = 0; | |
430 | ret = strtoul(str, &end, 10); | |
431 | ||
432 | if (ret < 0 || errno || end == str || (end && *end)) | |
433 | return 0; | |
434 | return ret; | |
435 | } | |
436 | ||
db216e68 KZ |
437 | int main(int argc, char **argv) |
438 | { | |
4eb49f63 | 439 | int c, rc = 0, all = 0, recursive = 0, alltargets = 0; |
db216e68 KZ |
440 | struct libmnt_context *cxt; |
441 | char *types = NULL; | |
442 | ||
443 | enum { | |
444 | UMOUNT_OPT_FAKE = CHAR_MAX + 1, | |
445 | }; | |
446 | ||
447 | static const struct option longopts[] = { | |
87918040 SK |
448 | { "all", no_argument, NULL, 'a' }, |
449 | { "all-targets", no_argument, NULL, 'A' }, | |
450 | { "detach-loop", no_argument, NULL, 'd' }, | |
451 | { "fake", no_argument, NULL, UMOUNT_OPT_FAKE }, | |
452 | { "force", no_argument, NULL, 'f' }, | |
453 | { "help", no_argument, NULL, 'h' }, | |
454 | { "internal-only", no_argument, NULL, 'i' }, | |
455 | { "lazy", no_argument, NULL, 'l' }, | |
456 | { "no-canonicalize", no_argument, NULL, 'c' }, | |
457 | { "no-mtab", no_argument, NULL, 'n' }, | |
d5fd9ac6 | 458 | { "quiet", no_argument, NULL, 'q' }, |
87918040 SK |
459 | { "read-only", no_argument, NULL, 'r' }, |
460 | { "recursive", no_argument, NULL, 'R' }, | |
461 | { "test-opts", required_argument, NULL, 'O' }, | |
462 | { "types", required_argument, NULL, 't' }, | |
463 | { "verbose", no_argument, NULL, 'v' }, | |
464 | { "version", no_argument, NULL, 'V' }, | |
c3dc9a8b | 465 | { "namespace", required_argument, NULL, 'N' }, |
87918040 | 466 | { NULL, 0, NULL, 0 } |
db216e68 KZ |
467 | }; |
468 | ||
a7349ee3 | 469 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
4eb49f63 | 470 | { 'A','a' }, /* all-targets,all */ |
a8cc72de KZ |
471 | { 'R','a' }, /* recursive,all */ |
472 | { 'O','R','t'}, /* options,recursive,types */ | |
473 | { 'R','r' }, /* recursive,read-only */ | |
474 | { 0 } | |
475 | }; | |
476 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
477 | ||
db216e68 KZ |
478 | sanitize_env(); |
479 | setlocale(LC_ALL, ""); | |
480 | bindtextdomain(PACKAGE, LOCALEDIR); | |
481 | textdomain(PACKAGE); | |
efb8854f | 482 | atexit(close_stdout); |
db216e68 KZ |
483 | |
484 | mnt_init_debug(0); | |
485 | cxt = mnt_new_context(); | |
486 | if (!cxt) | |
e058a6c1 | 487 | err(MNT_EX_SYSERR, _("libmount context allocation failed")); |
db216e68 KZ |
488 | |
489 | mnt_context_set_tables_errcb(cxt, table_parser_errcb); | |
490 | ||
4a40afe2 | 491 | while ((c = getopt_long(argc, argv, "aAcdfhilnqRrO:t:vVN:", |
db216e68 KZ |
492 | longopts, NULL)) != -1) { |
493 | ||
494 | ||
495 | /* only few options are allowed for non-root users */ | |
d5fd9ac6 | 496 | if (mnt_context_is_restricted(cxt) && !strchr("hdilqVv", c)) |
db216e68 KZ |
497 | exit_non_root(option_to_longopt(c, longopts)); |
498 | ||
a8cc72de KZ |
499 | err_exclusive_options(c, longopts, excl, excl_st); |
500 | ||
db216e68 KZ |
501 | switch(c) { |
502 | case 'a': | |
503 | all = 1; | |
504 | break; | |
4eb49f63 KZ |
505 | case 'A': |
506 | alltargets = 1; | |
507 | break; | |
db216e68 KZ |
508 | case 'c': |
509 | mnt_context_disable_canonicalize(cxt, TRUE); | |
510 | break; | |
511 | case 'd': | |
512 | mnt_context_enable_loopdel(cxt, TRUE); | |
513 | break; | |
514 | case UMOUNT_OPT_FAKE: | |
515 | mnt_context_enable_fake(cxt, TRUE); | |
516 | break; | |
517 | case 'f': | |
518 | mnt_context_enable_force(cxt, TRUE); | |
519 | break; | |
520 | case 'h': | |
6e1eda6f | 521 | usage(); |
db216e68 KZ |
522 | break; |
523 | case 'i': | |
524 | mnt_context_disable_helpers(cxt, TRUE); | |
525 | break; | |
526 | case 'l': | |
527 | mnt_context_enable_lazy(cxt, TRUE); | |
528 | break; | |
529 | case 'n': | |
530 | mnt_context_disable_mtab(cxt, TRUE); | |
531 | break; | |
d5fd9ac6 KZ |
532 | case 'q': |
533 | quiet = 1; | |
534 | break; | |
db216e68 KZ |
535 | case 'r': |
536 | mnt_context_enable_rdonly_umount(cxt, TRUE); | |
537 | break; | |
13ee1c91 DR |
538 | case 'R': |
539 | recursive = TRUE; | |
540 | break; | |
db216e68 KZ |
541 | case 'O': |
542 | if (mnt_context_set_options_pattern(cxt, optarg)) | |
e058a6c1 | 543 | err(MNT_EX_SYSERR, _("failed to set options pattern")); |
db216e68 KZ |
544 | break; |
545 | case 't': | |
546 | types = optarg; | |
547 | break; | |
548 | case 'v': | |
549 | mnt_context_enable_verbose(cxt, TRUE); | |
550 | break; | |
551 | case 'V': | |
552 | print_version(); | |
553 | break; | |
c3dc9a8b | 554 | case 'N': |
c1e70afe VD |
555 | { |
556 | char path[PATH_MAX]; | |
557 | pid_t pid = parse_pid(optarg); | |
558 | ||
559 | if (pid) | |
560 | snprintf(path, sizeof(path), "/proc/%i/ns/mnt", pid); | |
561 | ||
562 | if (mnt_context_set_target_ns(cxt, pid ? path : optarg)) | |
563 | err(MNT_EX_SYSERR, _("failed to set target namespace to %s"), pid ? path : optarg); | |
de7ccabd | 564 | break; |
c3dc9a8b | 565 | } |
db216e68 | 566 | default: |
e058a6c1 | 567 | errtryhelp(MNT_EX_USAGE); |
db216e68 KZ |
568 | } |
569 | } | |
570 | ||
571 | argc -= optind; | |
572 | argv += optind; | |
573 | ||
574 | if (all) { | |
575 | if (!types) | |
9e66fd30 | 576 | types = "noproc,nodevfs,nodevpts,nosysfs,norpc_pipefs,nonfsd,noselinuxfs"; |
db216e68 KZ |
577 | |
578 | mnt_context_set_fstype_pattern(cxt, types); | |
579 | rc = umount_all(cxt); | |
580 | ||
581 | } else if (argc < 1) { | |
6e1eda6f RM |
582 | warnx(_("bad usage")); |
583 | errtryhelp(MNT_EX_USAGE); | |
db216e68 | 584 | |
4eb49f63 KZ |
585 | } else if (alltargets) { |
586 | while (argc--) | |
587 | rc += umount_alltargets(cxt, *argv++, recursive); | |
13ee1c91 DR |
588 | } else if (recursive) { |
589 | while (argc--) | |
590 | rc += umount_recursive(cxt, *argv++); | |
591 | } else { | |
cc8cc8f3 | 592 | while (argc--) { |
d41acf74 | 593 | char *path = *argv; |
cc8cc8f3 | 594 | |
d41acf74 KZ |
595 | if (mnt_context_is_restricted(cxt) |
596 | && !mnt_tag_is_valid(path)) | |
cc8cc8f3 KZ |
597 | path = sanitize_path(path); |
598 | ||
599 | rc += umount_one(cxt, path); | |
600 | ||
d41acf74 | 601 | if (path != *argv) |
cc8cc8f3 | 602 | free(path); |
d41acf74 | 603 | argv++; |
cc8cc8f3 | 604 | } |
13ee1c91 | 605 | } |
db216e68 KZ |
606 | |
607 | mnt_free_context(cxt); | |
a9add961 | 608 | return (rc < 256) ? rc : 255; |
db216e68 KZ |
609 | } |
610 |