]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/umount.c
docs: update year in libs docs
[thirdparty/util-linux.git] / sys-utils / umount.c
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 *
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.
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"
35 #include "closestream.h"
36 #include "pathnames.h"
37 #include "canonicalize.h"
38
39 #define XALLOC_EXIT_CODE MNT_EX_SYSERR
40 #include "xalloc.h"
41
42 #define OPTUTILS_EXIT_CODE MNT_EX_USAGE
43 #include "optutils.h"
44
45 static int quiet;
46
47 static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
48 const char *filename, int line)
49 {
50 if (filename)
51 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
52 return 1;
53 }
54
55
56 static void __attribute__((__noreturn__)) umount_print_version(void)
57 {
58 const char *ver = NULL;
59 const char **features = NULL, **p;
60
61 mnt_get_library_version(&ver);
62 mnt_get_library_features(&features);
63
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);
74 exit(MNT_EX_SUCCESS);
75 }
76 static void __attribute__((__noreturn__)) usage(void)
77 {
78 FILE *out = stdout;
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
86 fputs(USAGE_SEPARATOR, out);
87 fputs(_("Unmount filesystems.\n"), out);
88
89 fputs(USAGE_OPTIONS, out);
90 fputs(_(" -a, --all unmount all filesystems\n"), out);
91 fputs(_(" -A, --all-targets unmount all mountpoints for the given device in the\n"
92 " current namespace\n"), out);
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);
99 fputs(_(" -l, --lazy detach the filesystem now, clean up things later\n"), out);
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);
102 fputs(_(" -r, --read-only in case unmounting fails, try to remount read-only\n"), out);
103 fputs(_(" -t, --types <list> limit the set of filesystem types\n"), out);
104 fputs(_(" -v, --verbose say what is being done\n"), out);
105 fputs(_(" -q, --quiet suppress 'not mounted' error messages\n"), out);
106 fputs(_(" -N, --namespace <ns> perform umount in another namespace\n"), out);
107
108 fputs(USAGE_SEPARATOR, out);
109 printf(USAGE_HELP_OPTIONS(25));
110 printf(USAGE_MAN_TAIL("umount(8)"));
111
112 exit(MNT_EX_SUCCESS);
113 }
114
115 static void suid_drop(struct libmnt_context *cxt)
116 {
117 const uid_t ruid = getuid();
118 const uid_t euid = geteuid();
119
120 if (ruid != 0 && euid == 0) {
121 if (setgid(getgid()) < 0)
122 err(MNT_EX_FAIL, _("setgid() failed"));
123
124 if (setuid(getuid()) < 0)
125 err(MNT_EX_FAIL, _("setuid() failed"));
126 }
127
128 /* be paranoid and check it, setuid(0) has to fail */
129 if (ruid != 0 && setuid(0) == 0)
130 errx(MNT_EX_FAIL, _("drop permissions failed."));
131
132 mnt_context_force_unrestricted(cxt);
133 }
134
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
154 static int mk_exit_code(struct libmnt_context *cxt, int rc)
155 {
156 char buf[BUFSIZ] = { 0 };
157
158 rc = mnt_context_get_excode(cxt, rc, buf, sizeof(buf));
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 */
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 = "???";
174 warnx("%s: %s.", spec, buf);
175 }
176 return rc;
177 }
178
179 static int umount_all(struct libmnt_context *cxt)
180 {
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"));
188 return MNT_EX_SYSERR;
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);
198 } else {
199 int xrc = mk_exit_code(cxt, mntrc);
200
201 if (xrc == MNT_EX_SUCCESS
202 && mnt_context_is_verbose(cxt))
203 printf("%-25s: successfully unmounted\n", tgt);
204 rc |= xrc;
205 }
206 }
207
208 mnt_free_iter(itr);
209 return rc;
210 }
211
212 static int umount_one(struct libmnt_context *cxt, const char *spec)
213 {
214 int rc;
215
216 if (!spec)
217 return MNT_EX_SOFTWARE;
218
219 if (mnt_context_set_target(cxt, spec))
220 err(MNT_EX_SYSERR, _("failed to set umount target"));
221
222 rc = mnt_context_umount(cxt);
223
224 if (rc == -EPERM
225 && mnt_context_is_restricted(cxt)
226 && !mnt_context_syscall_called(cxt)) {
227 /* Failed somewhere in libmount, drop perms and try it again */
228 suid_drop(cxt);
229 rc = mnt_context_umount(cxt);
230 }
231
232 rc = mk_exit_code(cxt, rc);
233
234 if (rc == MNT_EX_SUCCESS && mnt_context_is_verbose(cxt))
235 success_message(cxt);
236
237 mnt_reset_context(cxt);
238 return rc;
239 }
240
241 static struct libmnt_table *new_mountinfo(struct libmnt_context *cxt)
242 {
243 struct libmnt_table *tb;
244 struct libmnt_ns *ns_old = mnt_context_switch_target_ns(cxt);
245
246 if (!ns_old)
247 err(MNT_EX_SYSERR, _("failed to switch namespace"));
248
249 tb = mnt_new_table();
250 if (!tb)
251 err(MNT_EX_SYSERR, _("libmount table allocation failed"));
252
253 mnt_table_set_parser_errcb(tb, table_parser_errcb);
254 mnt_table_set_cache(tb, mnt_context_get_cache(cxt));
255
256 if (mnt_table_parse_file(tb, _PATH_PROC_MOUNTINFO)) {
257 warn(_("failed to parse %s"), _PATH_PROC_MOUNTINFO);
258 mnt_unref_table(tb);
259 tb = NULL;
260 }
261
262 if (!mnt_context_switch_ns(cxt, ns_old))
263 err(MNT_EX_SYSERR, _("failed to switch namespace"));
264
265 return tb;
266 }
267
268 /*
269 * like umount_one() but does not return error is @spec not mounted
270 */
271 static int umount_one_if_mounted(struct libmnt_context *cxt, const char *spec)
272 {
273 int rc;
274 struct libmnt_fs *fs;
275
276 rc = mnt_context_find_umount_fs(cxt, spec, &fs);
277 if (rc == 1) {
278 rc = MNT_EX_SUCCESS; /* already unmounted */
279 mnt_reset_context(cxt);
280 } else if (rc < 0) {
281 rc = mk_exit_code(cxt, rc); /* error */
282 mnt_reset_context(cxt);
283 } else
284 rc = umount_one(cxt, mnt_fs_get_target(fs));
285
286 return rc;
287 }
288
289 static int umount_do_recurse(struct libmnt_context *cxt,
290 struct libmnt_table *tb, struct libmnt_fs *fs)
291 {
292 struct libmnt_fs *child;
293 struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_BACKWARD);
294 int rc;
295
296 if (!itr)
297 err(MNT_EX_SYSERR, _("libmount iterator allocation failed"));
298
299 /* umount all children */
300 for (;;) {
301 rc = mnt_table_next_child_fs(tb, itr, fs, &child);
302 if (rc < 0) {
303 warnx(_("failed to get child fs of %s"),
304 mnt_fs_get_target(fs));
305 rc = MNT_EX_SOFTWARE;
306 goto done;
307 } else if (rc == 1)
308 break; /* no more children */
309
310 rc = umount_do_recurse(cxt, tb, child);
311 if (rc != MNT_EX_SUCCESS)
312 goto done;
313 }
314
315 rc = umount_one_if_mounted(cxt, mnt_fs_get_target(fs));
316 done:
317 mnt_free_iter(itr);
318 return rc;
319 }
320
321 static int umount_recursive(struct libmnt_context *cxt, const char *spec)
322 {
323 struct libmnt_table *tb;
324 struct libmnt_fs *fs;
325 int rc;
326
327 tb = new_mountinfo(cxt);
328 if (!tb)
329 return MNT_EX_SOFTWARE;
330
331 /* it's always real mountpoint, don't assume that the target maybe a device */
332 mnt_context_disable_swapmatch(cxt, 1);
333
334 fs = mnt_table_find_target(tb, spec, MNT_ITER_BACKWARD);
335 if (fs)
336 rc = umount_do_recurse(cxt, tb, fs);
337 else {
338 rc = MNT_EX_USAGE;
339 if (!quiet)
340 warnx(access(spec, F_OK) == 0 ?
341 _("%s: not mounted") :
342 _("%s: not found"), spec);
343 }
344
345 mnt_unref_table(tb);
346 return rc;
347 }
348
349 static int umount_alltargets(struct libmnt_context *cxt, const char *spec, int rec)
350 {
351 struct libmnt_fs *fs;
352 struct libmnt_table *tb;
353 struct libmnt_iter *itr = NULL;
354 dev_t devno = 0;
355 int rc;
356
357 /* Convert @spec to device name, Use the same logic like regular
358 * "umount <spec>".
359 */
360 rc = mnt_context_find_umount_fs(cxt, spec, &fs);
361 if (rc == 1) {
362 rc = MNT_EX_USAGE;
363 if (!quiet)
364 warnx(access(spec, F_OK) == 0 ?
365 _("%s: not mounted") :
366 _("%s: not found"), spec);
367 return rc;
368 }
369 if (rc < 0)
370 return mk_exit_code(cxt, rc); /* error */
371
372 if (!mnt_fs_get_srcpath(fs) || !mnt_fs_get_devno(fs))
373 errx(MNT_EX_USAGE, _("%s: failed to determine source "
374 "(--all-targets is unsupported on systems with "
375 "regular mtab file)."), spec);
376
377 itr = mnt_new_iter(MNT_ITER_BACKWARD);
378 if (!itr)
379 err(MNT_EX_SYSERR, _("libmount iterator allocation failed"));
380
381 /* get on @cxt independent mountinfo */
382 tb = new_mountinfo(cxt);
383 if (!tb) {
384 rc = MNT_EX_SOFTWARE;
385 goto done;
386 }
387
388 /* Note that @fs is from mount context and the context will be reset
389 * after each umount() call */
390 devno = mnt_fs_get_devno(fs);
391 fs = NULL;
392
393 mnt_reset_context(cxt);
394
395 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
396 if (mnt_fs_get_devno(fs) != devno)
397 continue;
398 mnt_context_disable_swapmatch(cxt, 1);
399 if (rec)
400 rc = umount_do_recurse(cxt, tb, fs);
401 else
402 rc = umount_one_if_mounted(cxt, mnt_fs_get_target(fs));
403
404 if (rc != MNT_EX_SUCCESS)
405 break;
406 }
407
408 done:
409 mnt_free_iter(itr);
410 mnt_unref_table(tb);
411
412 return rc;
413 }
414
415 /*
416 * Check path -- non-root user should not be able to resolve path which is
417 * unreadable for him.
418 */
419 static char *sanitize_path(const char *path)
420 {
421 char *p;
422
423 if (!path)
424 return NULL;
425
426 p = canonicalize_path_restricted(path);
427 if (!p)
428 err(MNT_EX_USAGE, "%s", path);
429
430 return p;
431 }
432
433 static pid_t parse_pid(const char *str)
434 {
435 char *end;
436 pid_t ret;
437
438 errno = 0;
439 ret = strtoul(str, &end, 10);
440
441 if (ret < 0 || errno || end == str || (end && *end))
442 return 0;
443 return ret;
444 }
445
446 int main(int argc, char **argv)
447 {
448 int c, rc = 0, all = 0, recursive = 0, alltargets = 0;
449 struct libmnt_context *cxt;
450 char *types = NULL;
451
452 enum {
453 UMOUNT_OPT_FAKE = CHAR_MAX + 1,
454 };
455
456 static const struct option longopts[] = {
457 { "all", no_argument, NULL, 'a' },
458 { "all-targets", no_argument, NULL, 'A' },
459 { "detach-loop", no_argument, NULL, 'd' },
460 { "fake", no_argument, NULL, UMOUNT_OPT_FAKE },
461 { "force", no_argument, NULL, 'f' },
462 { "help", no_argument, NULL, 'h' },
463 { "internal-only", no_argument, NULL, 'i' },
464 { "lazy", no_argument, NULL, 'l' },
465 { "no-canonicalize", no_argument, NULL, 'c' },
466 { "no-mtab", no_argument, NULL, 'n' },
467 { "quiet", no_argument, NULL, 'q' },
468 { "read-only", no_argument, NULL, 'r' },
469 { "recursive", no_argument, NULL, 'R' },
470 { "test-opts", required_argument, NULL, 'O' },
471 { "types", required_argument, NULL, 't' },
472 { "verbose", no_argument, NULL, 'v' },
473 { "version", no_argument, NULL, 'V' },
474 { "namespace", required_argument, NULL, 'N' },
475 { NULL, 0, NULL, 0 }
476 };
477
478 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
479 { 'A','a' }, /* all-targets,all */
480 { 'R','a' }, /* recursive,all */
481 { 'O','R','t'}, /* options,recursive,types */
482 { 'R','r' }, /* recursive,read-only */
483 { 0 }
484 };
485 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
486
487 sanitize_env();
488 setlocale(LC_ALL, "");
489 bindtextdomain(PACKAGE, LOCALEDIR);
490 textdomain(PACKAGE);
491 close_stdout_atexit();
492
493 mnt_init_debug(0);
494 cxt = mnt_new_context();
495 if (!cxt)
496 err(MNT_EX_SYSERR, _("libmount context allocation failed"));
497
498 mnt_context_set_tables_errcb(cxt, table_parser_errcb);
499
500 while ((c = getopt_long(argc, argv, "aAcdfhilnqRrO:t:vVN:",
501 longopts, NULL)) != -1) {
502
503
504 /* only few options are allowed for non-root users */
505 if (mnt_context_is_restricted(cxt) && !strchr("hdilqVv", c))
506 suid_drop(cxt);
507
508 err_exclusive_options(c, longopts, excl, excl_st);
509
510 switch(c) {
511 case 'a':
512 all = 1;
513 break;
514 case 'A':
515 alltargets = 1;
516 break;
517 case 'c':
518 mnt_context_disable_canonicalize(cxt, TRUE);
519 break;
520 case 'd':
521 mnt_context_enable_loopdel(cxt, TRUE);
522 break;
523 case UMOUNT_OPT_FAKE:
524 mnt_context_enable_fake(cxt, TRUE);
525 break;
526 case 'f':
527 mnt_context_enable_force(cxt, TRUE);
528 break;
529 case 'i':
530 mnt_context_disable_helpers(cxt, TRUE);
531 break;
532 case 'l':
533 mnt_context_enable_lazy(cxt, TRUE);
534 break;
535 case 'n':
536 mnt_context_disable_mtab(cxt, TRUE);
537 break;
538 case 'q':
539 quiet = 1;
540 break;
541 case 'r':
542 mnt_context_enable_rdonly_umount(cxt, TRUE);
543 break;
544 case 'R':
545 recursive = TRUE;
546 break;
547 case 'O':
548 if (mnt_context_set_options_pattern(cxt, optarg))
549 err(MNT_EX_SYSERR, _("failed to set options pattern"));
550 break;
551 case 't':
552 types = optarg;
553 break;
554 case 'v':
555 mnt_context_enable_verbose(cxt, TRUE);
556 break;
557 case 'N':
558 {
559 char path[PATH_MAX];
560 pid_t pid = parse_pid(optarg);
561
562 if (pid)
563 snprintf(path, sizeof(path), "/proc/%i/ns/mnt", pid);
564
565 if (mnt_context_set_target_ns(cxt, pid ? path : optarg))
566 err(MNT_EX_SYSERR, _("failed to set target namespace to %s"), pid ? path : optarg);
567 break;
568 }
569
570 case 'h':
571 mnt_free_context(cxt);
572 usage();
573 case 'V':
574 mnt_free_context(cxt);
575 umount_print_version();
576 default:
577 errtryhelp(MNT_EX_USAGE);
578 }
579 }
580
581 argc -= optind;
582 argv += optind;
583
584 if (all) {
585 if (argc) {
586 warnx(_("unexpected number of arguments"));
587 errtryhelp(MNT_EX_USAGE);
588 }
589 if (!types)
590 types = "noproc,nodevfs,nodevpts,nosysfs,norpc_pipefs,nonfsd,noselinuxfs";
591
592 mnt_context_set_fstype_pattern(cxt, types);
593 rc = umount_all(cxt);
594
595 } else if (argc < 1) {
596 warnx(_("bad usage"));
597 errtryhelp(MNT_EX_USAGE);
598
599 } else if (alltargets) {
600 while (argc--)
601 rc += umount_alltargets(cxt, *argv++, recursive);
602 } else if (recursive) {
603 while (argc--)
604 rc += umount_recursive(cxt, *argv++);
605 } else {
606 while (argc--) {
607 char *path = *argv;
608
609 if (mnt_context_is_restricted(cxt)
610 && !mnt_tag_is_valid(path))
611 path = sanitize_path(path);
612
613 rc += umount_one(cxt, path);
614
615 if (path != *argv)
616 free(path);
617 argv++;
618 }
619 }
620
621 mnt_free_context(cxt);
622 return (rc < 256) ? rc : 255;
623 }
624