]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/umount.c
libmount: (docs) add unused declarations
[thirdparty/util-linux.git] / sys-utils / umount.c
CommitLineData
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"
35#include "optutils.h"
73f9a114 36#include "exitcodes.h"
efb8854f 37#include "closestream.h"
13ee1c91 38#include "pathnames.h"
cc8cc8f3 39#include "canonicalize.h"
4eb49f63 40#include "xalloc.h"
db216e68
KZ
41
42static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
43 const char *filename, int line)
44{
45 if (filename)
b779c1ae 46 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
1cd9d0d7 47 return 1;
db216e68
KZ
48}
49
ac8ecab4 50
db216e68
KZ
51static void __attribute__((__noreturn__)) print_version(void)
52{
53 const char *ver = NULL;
ac8ecab4 54 const char **features = NULL, **p;
db216e68
KZ
55
56 mnt_get_library_version(&ver);
ac8ecab4 57 mnt_get_library_features(&features);
db216e68 58
ac8ecab4
KZ
59 printf(_("%s from %s (libmount %s"),
60 program_invocation_short_name,
61 PACKAGE_STRING,
62 ver);
63 p = features;
64 while (p && *p) {
65 fputs(p == features ? ": " : ", ", stdout);
66 fputs(*p++, stdout);
67 }
68 fputs(")\n", stdout);
73f9a114 69 exit(MOUNT_EX_SUCCESS);
db216e68 70}
db216e68
KZ
71static void __attribute__((__noreturn__)) usage(FILE *out)
72{
73 fputs(USAGE_HEADER, out);
74 fprintf(out, _(
75 " %1$s [-hV]\n"
76 " %1$s -a [options]\n"
77 " %1$s [options] <source> | <directory>\n"),
78 program_invocation_short_name);
79
451dbcfa
BS
80 fputs(USAGE_SEPARATOR, out);
81 fputs(_("Unmount filesystems.\n"), out);
82
db216e68 83 fputs(USAGE_OPTIONS, out);
4eb49f63 84 fputs(_(" -a, --all unmount all filesystems\n"), out);
8356d27d 85 fputs(_(" -A, --all-targets unmount all mountpoints for the given device in the\n"
4ce393f4 86 " current namespace\n"), out);
83d91100
SK
87 fputs(_(" -c, --no-canonicalize don't canonicalize paths\n"), out);
88 fputs(_(" -d, --detach-loop if mounted loop device, also free this loop device\n"), out);
89 fputs(_(" --fake dry run; skip the umount(2) syscall\n"), out);
90 fputs(_(" -f, --force force unmount (in case of an unreachable NFS system)\n"), out);
91 fputs(_(" -i, --internal-only don't call the umount.<type> helpers\n"), out);
92 fputs(_(" -n, --no-mtab don't write to /etc/mtab\n"), out);
4ce393f4 93 fputs(_(" -l, --lazy detach the filesystem now, clean up things later\n"), out);
83d91100
SK
94 fputs(_(" -O, --test-opts <list> limit the set of filesystems (use with -a)\n"), out);
95 fputs(_(" -R, --recursive recursively unmount a target with all its children\n"), out);
4ce393f4 96 fputs(_(" -r, --read-only in case unmounting fails, try to remount read-only\n"), out);
83d91100
SK
97 fputs(_(" -t, --types <list> limit the set of filesystem types\n"), out);
98 fputs(_(" -v, --verbose say what is being done\n"), out);
db216e68
KZ
99
100 fputs(USAGE_SEPARATOR, out);
101 fputs(USAGE_HELP, out);
102 fputs(USAGE_VERSION, out);
103 fprintf(out, USAGE_MAN_TAIL("umount(8)"));
104
73f9a114 105 exit(out == stderr ? MOUNT_EX_USAGE : MOUNT_EX_SUCCESS);
db216e68
KZ
106}
107
108static void __attribute__((__noreturn__)) exit_non_root(const char *option)
109{
110 const uid_t ruid = getuid();
111 const uid_t euid = geteuid();
112
113 if (ruid == 0 && euid != 0) {
114 /* user is root, but setuid to non-root */
115 if (option)
73f9a114 116 errx(MOUNT_EX_USAGE,
db216e68
KZ
117 _("only root can use \"--%s\" option "
118 "(effective UID is %u)"),
119 option, euid);
73f9a114 120 errx(MOUNT_EX_USAGE, _("only root can do that "
db216e68
KZ
121 "(effective UID is %u)"), euid);
122 }
123 if (option)
73f9a114
KZ
124 errx(MOUNT_EX_USAGE, _("only root can use \"--%s\" option"), option);
125 errx(MOUNT_EX_USAGE, _("only root can do that"));
126}
127
84600ddc
KZ
128static void success_message(struct libmnt_context *cxt)
129{
130 const char *tgt, *src;
131
132 if (mnt_context_helper_executed(cxt)
133 || mnt_context_get_status(cxt) != 1)
134 return;
135
136 tgt = mnt_context_get_target(cxt);
137 if (!tgt)
138 return;
139
140 src = mnt_context_get_source(cxt);
141 if (src)
142 warnx(_("%s (%s) unmounted"), tgt, src);
143 else
144 warnx(_("%s unmounted"), tgt);
145}
146
73f9a114
KZ
147static int mk_exit_code(struct libmnt_context *cxt, int rc)
148{
e1706a67
KZ
149 char buf[BUFSIZ] = { 0 };
150
151 rc = mnt_context_get_excode(cxt, rc, buf, sizeof(buf));
152 if (*buf) {
153 const char *spec = mnt_context_get_target(cxt);
154 if (!spec)
155 spec = mnt_context_get_source(cxt);
156 if (!spec)
157 spec = "???";
158 warnx(_("%s: %s."), spec, buf);
73f9a114 159 }
e1706a67 160 return rc;
db216e68
KZ
161}
162
190c342a 163static int umount_all(struct libmnt_context *cxt)
db216e68 164{
190c342a
KZ
165 struct libmnt_iter *itr;
166 struct libmnt_fs *fs;
167 int mntrc, ignored, rc = 0;
168
169 itr = mnt_new_iter(MNT_ITER_BACKWARD);
170 if (!itr) {
171 warn(_("failed to initialize libmount iterator"));
06069d5f 172 return MOUNT_EX_SYSERR;
190c342a
KZ
173 }
174
175 while (mnt_context_next_umount(cxt, itr, &fs, &mntrc, &ignored) == 0) {
176
177 const char *tgt = mnt_fs_get_target(fs);
178
179 if (ignored) {
180 if (mnt_context_is_verbose(cxt))
181 printf(_("%-25s: ignored\n"), tgt);
190c342a 182 } else {
0ce2fe87 183 int xrc = mk_exit_code(cxt, mntrc);
73f9a114 184
0ce2fe87
KZ
185 if (xrc == MOUNT_EX_SUCCESS
186 && mnt_context_is_verbose(cxt))
4ce393f4 187 printf("%-25s: successfully unmounted\n", tgt);
0ce2fe87 188 rc |= xrc;
190c342a
KZ
189 }
190 }
191
0f2d6476 192 mnt_free_iter(itr);
190c342a 193 return rc;
db216e68
KZ
194}
195
196static int umount_one(struct libmnt_context *cxt, const char *spec)
197{
198 int rc;
199
200 if (!spec)
06069d5f 201 return MOUNT_EX_SOFTWARE;
db216e68
KZ
202
203 if (mnt_context_set_target(cxt, spec))
73f9a114 204 err(MOUNT_EX_SYSERR, _("failed to set umount target"));
db216e68
KZ
205
206 rc = mnt_context_umount(cxt);
73f9a114 207 rc = mk_exit_code(cxt, rc);
db216e68 208
84600ddc
KZ
209 if (rc == MOUNT_EX_SUCCESS && mnt_context_is_verbose(cxt))
210 success_message(cxt);
211
db216e68
KZ
212 mnt_reset_context(cxt);
213 return rc;
214}
215
4eb49f63
KZ
216static struct libmnt_table *new_mountinfo(struct libmnt_context *cxt)
217{
218 struct libmnt_table *tb = mnt_new_table();
219 if (!tb)
220 err(MOUNT_EX_SYSERR, _("libmount table allocation failed"));
221
222 mnt_table_set_parser_errcb(tb, table_parser_errcb);
223 mnt_table_set_cache(tb, mnt_context_get_cache(cxt));
224
225 if (mnt_table_parse_file(tb, _PATH_PROC_MOUNTINFO)) {
226 warn(_("failed to parse %s"), _PATH_PROC_MOUNTINFO);
50fccba1 227 mnt_unref_table(tb);
4eb49f63
KZ
228 tb = NULL;
229 }
230
231 return tb;
232}
233
234/*
235 * like umount_one() but does not return error is @spec not mounted
236 */
237static int umount_one_if_mounted(struct libmnt_context *cxt, const char *spec)
238{
239 int rc;
240 struct libmnt_fs *fs;
241
242 rc = mnt_context_find_umount_fs(cxt, spec, &fs);
243 if (rc == 1) {
9e930041 244 rc = MOUNT_EX_SUCCESS; /* already unmounted */
4eb49f63
KZ
245 mnt_reset_context(cxt);
246 } else if (rc < 0) {
247 rc = mk_exit_code(cxt, rc); /* error */
248 mnt_reset_context(cxt);
249 } else
250 rc = umount_one(cxt, mnt_fs_get_target(fs));
251
252 return rc;
253}
254
13ee1c91 255static int umount_do_recurse(struct libmnt_context *cxt,
4eb49f63 256 struct libmnt_table *tb, struct libmnt_fs *fs)
13ee1c91 257{
13ee1c91 258 struct libmnt_fs *child;
13ee1c91 259 struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_BACKWARD);
4eb49f63 260 int rc;
13ee1c91
DR
261
262 if (!itr)
263 err(MOUNT_EX_SYSERR, _("libmount iterator allocation failed"));
4eb49f63 264
9e930041 265 /* umount all children */
13ee1c91 266 for (;;) {
4eb49f63 267 rc = mnt_table_next_child_fs(tb, itr, fs, &child);
13ee1c91 268 if (rc < 0) {
4eb49f63
KZ
269 warnx(_("failed to get child fs of %s"),
270 mnt_fs_get_target(fs));
13ee1c91
DR
271 rc = MOUNT_EX_SOFTWARE;
272 goto done;
273 } else if (rc == 1)
274 break; /* no more children */
275
276 rc = umount_do_recurse(cxt, tb, child);
277 if (rc != MOUNT_EX_SUCCESS)
278 goto done;
279 }
280
4eb49f63 281 rc = umount_one_if_mounted(cxt, mnt_fs_get_target(fs));
13ee1c91
DR
282done:
283 mnt_free_iter(itr);
284 return rc;
285}
286
287static int umount_recursive(struct libmnt_context *cxt, const char *spec)
288{
289 struct libmnt_table *tb;
4eb49f63 290 struct libmnt_fs *fs;
13ee1c91
DR
291 int rc;
292
4eb49f63
KZ
293 tb = new_mountinfo(cxt);
294 if (!tb)
295 return MOUNT_EX_SOFTWARE;
296
ae978c4d
KZ
297 /* it's always real mountpoint, don't assume that the target maybe a device */
298 mnt_context_disable_swapmatch(cxt, 1);
299
4eb49f63
KZ
300 fs = mnt_table_find_target(tb, spec, MNT_ITER_BACKWARD);
301 if (fs)
302 rc = umount_do_recurse(cxt, tb, fs);
303 else {
304 rc = MOUNT_EX_USAGE;
305 warnx(access(spec, F_OK) == 0 ?
306 _("%s: not mounted") :
307 _("%s: not found"), spec);
308 }
a8cc72de 309
50fccba1 310 mnt_unref_table(tb);
4eb49f63
KZ
311 return rc;
312}
7b4a2697 313
4eb49f63
KZ
314static int umount_alltargets(struct libmnt_context *cxt, const char *spec, int rec)
315{
316 struct libmnt_fs *fs;
317 struct libmnt_table *tb;
318 struct libmnt_iter *itr = NULL;
f697d61b 319 dev_t devno = 0;
4eb49f63
KZ
320 int rc;
321
322 /* Convert @spec to device name, Use the same logic like regular
323 * "umount <spec>".
13ee1c91 324 */
4eb49f63
KZ
325 rc = mnt_context_find_umount_fs(cxt, spec, &fs);
326 if (rc == 1) {
327 rc = MOUNT_EX_USAGE;
328 warnx(access(spec, F_OK) == 0 ?
329 _("%s: not mounted") :
330 _("%s: not found"), spec);
331 return rc;
332 }
333 if (rc < 0)
334 return mk_exit_code(cxt, rc); /* error */
13ee1c91 335
f697d61b 336 if (!mnt_fs_get_srcpath(fs) || !mnt_fs_get_devno(fs))
fd7c4924
KZ
337 errx(MOUNT_EX_USAGE, _("%s: failed to determine source "
338 "(--all-targets is unsupported on systems with "
339 "regular mtab file)."), spec);
4eb49f63
KZ
340
341 itr = mnt_new_iter(MNT_ITER_BACKWARD);
342 if (!itr)
343 err(MOUNT_EX_SYSERR, _("libmount iterator allocation failed"));
344
345 /* get on @cxt independent mountinfo */
346 tb = new_mountinfo(cxt);
86c58c4a
KZ
347 if (!tb) {
348 rc = MOUNT_EX_SOFTWARE;
349 goto done;
350 }
4eb49f63 351
9e930041 352 /* Note that @fs is from mount context and the context will be reset
4eb49f63 353 * after each umount() call */
f697d61b 354 devno = mnt_fs_get_devno(fs);
4eb49f63
KZ
355 fs = NULL;
356
357 mnt_reset_context(cxt);
358
359 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
f697d61b 360 if (mnt_fs_get_devno(fs) != devno)
4eb49f63
KZ
361 continue;
362 mnt_context_disable_swapmatch(cxt, 1);
363 if (rec)
13ee1c91 364 rc = umount_do_recurse(cxt, tb, fs);
4eb49f63
KZ
365 else
366 rc = umount_one_if_mounted(cxt, mnt_fs_get_target(fs));
367
368 if (rc != MOUNT_EX_SUCCESS)
369 break;
13ee1c91
DR
370 }
371
86c58c4a 372done:
4eb49f63 373 mnt_free_iter(itr);
50fccba1 374 mnt_unref_table(tb);
4eb49f63 375
13ee1c91
DR
376 return rc;
377}
378
cc8cc8f3
KZ
379/*
380 * Check path -- non-root user should not be able to resolve path which is
381 * unreadable for him.
382 */
383static char *sanitize_path(const char *path)
384{
385 char *p;
386
387 if (!path)
388 return NULL;
389
390 p = canonicalize_path_restricted(path);
391 if (!p)
392 err(MOUNT_EX_USAGE, "%s", path);
393
394 return p;
395}
396
db216e68
KZ
397int main(int argc, char **argv)
398{
4eb49f63 399 int c, rc = 0, all = 0, recursive = 0, alltargets = 0;
db216e68
KZ
400 struct libmnt_context *cxt;
401 char *types = NULL;
402
403 enum {
404 UMOUNT_OPT_FAKE = CHAR_MAX + 1,
405 };
406
407 static const struct option longopts[] = {
87918040
SK
408 { "all", no_argument, NULL, 'a' },
409 { "all-targets", no_argument, NULL, 'A' },
410 { "detach-loop", no_argument, NULL, 'd' },
411 { "fake", no_argument, NULL, UMOUNT_OPT_FAKE },
412 { "force", no_argument, NULL, 'f' },
413 { "help", no_argument, NULL, 'h' },
414 { "internal-only", no_argument, NULL, 'i' },
415 { "lazy", no_argument, NULL, 'l' },
416 { "no-canonicalize", no_argument, NULL, 'c' },
417 { "no-mtab", no_argument, NULL, 'n' },
418 { "read-only", no_argument, NULL, 'r' },
419 { "recursive", no_argument, NULL, 'R' },
420 { "test-opts", required_argument, NULL, 'O' },
421 { "types", required_argument, NULL, 't' },
422 { "verbose", no_argument, NULL, 'v' },
423 { "version", no_argument, NULL, 'V' },
424 { NULL, 0, NULL, 0 }
db216e68
KZ
425 };
426
a7349ee3 427 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
4eb49f63 428 { 'A','a' }, /* all-targets,all */
a8cc72de
KZ
429 { 'R','a' }, /* recursive,all */
430 { 'O','R','t'}, /* options,recursive,types */
431 { 'R','r' }, /* recursive,read-only */
432 { 0 }
433 };
434 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
435
db216e68
KZ
436 sanitize_env();
437 setlocale(LC_ALL, "");
438 bindtextdomain(PACKAGE, LOCALEDIR);
439 textdomain(PACKAGE);
efb8854f 440 atexit(close_stdout);
db216e68
KZ
441
442 mnt_init_debug(0);
443 cxt = mnt_new_context();
444 if (!cxt)
73f9a114 445 err(MOUNT_EX_SYSERR, _("libmount context allocation failed"));
db216e68
KZ
446
447 mnt_context_set_tables_errcb(cxt, table_parser_errcb);
448
4eb49f63 449 while ((c = getopt_long(argc, argv, "aAcdfhilnRrO:t:vV",
db216e68
KZ
450 longopts, NULL)) != -1) {
451
452
453 /* only few options are allowed for non-root users */
454 if (mnt_context_is_restricted(cxt) && !strchr("hdilVv", c))
455 exit_non_root(option_to_longopt(c, longopts));
456
a8cc72de
KZ
457 err_exclusive_options(c, longopts, excl, excl_st);
458
db216e68
KZ
459 switch(c) {
460 case 'a':
461 all = 1;
462 break;
4eb49f63
KZ
463 case 'A':
464 alltargets = 1;
465 break;
db216e68
KZ
466 case 'c':
467 mnt_context_disable_canonicalize(cxt, TRUE);
468 break;
469 case 'd':
470 mnt_context_enable_loopdel(cxt, TRUE);
471 break;
472 case UMOUNT_OPT_FAKE:
473 mnt_context_enable_fake(cxt, TRUE);
474 break;
475 case 'f':
476 mnt_context_enable_force(cxt, TRUE);
477 break;
478 case 'h':
479 usage(stdout);
480 break;
481 case 'i':
482 mnt_context_disable_helpers(cxt, TRUE);
483 break;
484 case 'l':
485 mnt_context_enable_lazy(cxt, TRUE);
486 break;
487 case 'n':
488 mnt_context_disable_mtab(cxt, TRUE);
489 break;
490 case 'r':
491 mnt_context_enable_rdonly_umount(cxt, TRUE);
492 break;
13ee1c91
DR
493 case 'R':
494 recursive = TRUE;
495 break;
db216e68
KZ
496 case 'O':
497 if (mnt_context_set_options_pattern(cxt, optarg))
73f9a114 498 err(MOUNT_EX_SYSERR, _("failed to set options pattern"));
db216e68
KZ
499 break;
500 case 't':
501 types = optarg;
502 break;
503 case 'v':
504 mnt_context_enable_verbose(cxt, TRUE);
505 break;
506 case 'V':
507 print_version();
508 break;
509 default:
677ec86c 510 errtryhelp(MOUNT_EX_USAGE);
db216e68
KZ
511 }
512 }
513
514 argc -= optind;
515 argv += optind;
516
517 if (all) {
518 if (!types)
9e66fd30 519 types = "noproc,nodevfs,nodevpts,nosysfs,norpc_pipefs,nonfsd,noselinuxfs";
db216e68
KZ
520
521 mnt_context_set_fstype_pattern(cxt, types);
522 rc = umount_all(cxt);
523
524 } else if (argc < 1) {
525 usage(stderr);
526
4eb49f63
KZ
527 } else if (alltargets) {
528 while (argc--)
529 rc += umount_alltargets(cxt, *argv++, recursive);
13ee1c91
DR
530 } else if (recursive) {
531 while (argc--)
532 rc += umount_recursive(cxt, *argv++);
533 } else {
cc8cc8f3 534 while (argc--) {
d41acf74 535 char *path = *argv;
cc8cc8f3 536
d41acf74
KZ
537 if (mnt_context_is_restricted(cxt)
538 && !mnt_tag_is_valid(path))
cc8cc8f3
KZ
539 path = sanitize_path(path);
540
541 rc += umount_one(cxt, path);
542
d41acf74 543 if (path != *argv)
cc8cc8f3 544 free(path);
d41acf74 545 argv++;
cc8cc8f3 546 }
13ee1c91 547 }
db216e68
KZ
548
549 mnt_free_context(cxt);
a9add961 550 return (rc < 256) ? rc : 255;
db216e68
KZ
551}
552