]>
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" | |
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 | |
42 | static 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 |
51 | static 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 |
71 | static 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 | ||
108 | static 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 |
128 | static 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 |
147 | /* |
148 | * Handles generic errors like ENOMEM, ... | |
149 | * | |
150 | * rc = 0 success | |
151 | * <0 error (usually -errno) | |
152 | * | |
153 | * Returns exit status (MOUNT_EX_*) and prints error message. | |
154 | */ | |
155 | static int handle_generic_errors(int rc, const char *msg, ...) | |
156 | { | |
157 | va_list va; | |
158 | ||
159 | va_start(va, msg); | |
160 | errno = -rc; | |
161 | ||
162 | switch(errno) { | |
163 | case EINVAL: | |
164 | case EPERM: | |
165 | vwarn(msg, va); | |
166 | rc = MOUNT_EX_USAGE; | |
167 | break; | |
168 | case ENOMEM: | |
169 | vwarn(msg, va); | |
170 | rc = MOUNT_EX_SYSERR; | |
171 | break; | |
172 | default: | |
173 | vwarn(msg, va); | |
174 | rc = MOUNT_EX_FAIL; | |
175 | break; | |
176 | } | |
177 | va_end(va); | |
178 | return rc; | |
179 | } | |
180 | ||
181 | static int mk_exit_code(struct libmnt_context *cxt, int rc) | |
182 | { | |
183 | int syserr; | |
184 | const char *tgt = mnt_context_get_target(cxt); | |
185 | ||
186 | if (mnt_context_helper_executed(cxt)) | |
187 | /* | |
188 | * /sbin/umount.<type> called, return status | |
189 | */ | |
190 | return mnt_context_get_helper_status(cxt); | |
191 | ||
192 | if (rc == 0 && mnt_context_get_status(cxt) == 1) | |
193 | /* | |
194 | * Libmount success && syscall success. | |
195 | */ | |
196 | return MOUNT_EX_SUCCESS; | |
197 | ||
198 | ||
199 | if (!mnt_context_syscall_called(cxt)) { | |
200 | /* | |
201 | * libmount errors (extra library checks) | |
202 | */ | |
726f9fbf KZ |
203 | if (rc == -EPERM && !mnt_context_tab_applied(cxt)) { |
204 | /* failed to evaluate permissions because not found | |
205 | * relevant entry in mtab */ | |
206 | warnx(_("%s: not mounted"), tgt); | |
207 | return MOUNT_EX_USAGE; | |
208 | } | |
73f9a114 KZ |
209 | return handle_generic_errors(rc, _("%s: umount failed"), tgt); |
210 | ||
211 | } else if (mnt_context_get_syscall_errno(cxt) == 0) { | |
212 | /* | |
213 | * umount(2) syscall success, but something else failed | |
214 | * (probably error in mtab processing). | |
215 | */ | |
216 | if (rc < 0) | |
217 | return handle_generic_errors(rc, | |
4ce393f4 | 218 | _("%s: filesystem was unmounted, but mount(8) failed"), |
73f9a114 KZ |
219 | tgt); |
220 | ||
221 | return MOUNT_EX_SOFTWARE; /* internal error */ | |
222 | ||
223 | } | |
224 | ||
225 | /* | |
226 | * umount(2) errors | |
227 | */ | |
228 | syserr = mnt_context_get_syscall_errno(cxt); | |
229 | ||
230 | switch(syserr) { | |
231 | case ENXIO: | |
232 | warnx(_("%s: invalid block device"), tgt); /* ??? */ | |
233 | break; | |
234 | case EINVAL: | |
235 | warnx(_("%s: not mounted"), tgt); | |
236 | break; | |
237 | case EIO: | |
238 | warnx(_("%s: can't write superblock"), tgt); | |
239 | break; | |
240 | case EBUSY: | |
4ce393f4 BS |
241 | warnx(_("%s: target is busy\n" |
242 | " (In some cases useful info about processes that\n" | |
243 | " use the device is found by lsof(8) or fuser(1).)"), | |
73f9a114 | 244 | tgt); |
7e1b1446 | 245 | break; |
73f9a114 | 246 | case ENOENT: |
7ba207e7 KZ |
247 | if (tgt && *tgt) |
248 | warnx(_("%s: mountpoint not found"), tgt); | |
249 | else | |
250 | warnx(_("undefined mountpoint")); | |
73f9a114 KZ |
251 | break; |
252 | case EPERM: | |
4ce393f4 | 253 | warnx(_("%s: must be superuser to unmount"), tgt); |
73f9a114 KZ |
254 | break; |
255 | case EACCES: | |
4ce393f4 | 256 | warnx(_("%s: block devices are not permitted on filesystem"), tgt); |
73f9a114 KZ |
257 | break; |
258 | default: | |
259 | errno = syserr; | |
4ce393f4 | 260 | warn("%s", tgt); |
73f9a114 KZ |
261 | break; |
262 | } | |
263 | return MOUNT_EX_FAIL; | |
db216e68 KZ |
264 | } |
265 | ||
190c342a | 266 | static int umount_all(struct libmnt_context *cxt) |
db216e68 | 267 | { |
190c342a KZ |
268 | struct libmnt_iter *itr; |
269 | struct libmnt_fs *fs; | |
270 | int mntrc, ignored, rc = 0; | |
271 | ||
272 | itr = mnt_new_iter(MNT_ITER_BACKWARD); | |
273 | if (!itr) { | |
274 | warn(_("failed to initialize libmount iterator")); | |
06069d5f | 275 | return MOUNT_EX_SYSERR; |
190c342a KZ |
276 | } |
277 | ||
278 | while (mnt_context_next_umount(cxt, itr, &fs, &mntrc, &ignored) == 0) { | |
279 | ||
280 | const char *tgt = mnt_fs_get_target(fs); | |
281 | ||
282 | if (ignored) { | |
283 | if (mnt_context_is_verbose(cxt)) | |
284 | printf(_("%-25s: ignored\n"), tgt); | |
190c342a | 285 | } else { |
0ce2fe87 | 286 | int xrc = mk_exit_code(cxt, mntrc); |
73f9a114 | 287 | |
0ce2fe87 KZ |
288 | if (xrc == MOUNT_EX_SUCCESS |
289 | && mnt_context_is_verbose(cxt)) | |
4ce393f4 | 290 | printf("%-25s: successfully unmounted\n", tgt); |
0ce2fe87 | 291 | rc |= xrc; |
190c342a KZ |
292 | } |
293 | } | |
294 | ||
0f2d6476 | 295 | mnt_free_iter(itr); |
190c342a | 296 | return rc; |
db216e68 KZ |
297 | } |
298 | ||
299 | static int umount_one(struct libmnt_context *cxt, const char *spec) | |
300 | { | |
301 | int rc; | |
302 | ||
303 | if (!spec) | |
06069d5f | 304 | return MOUNT_EX_SOFTWARE; |
db216e68 KZ |
305 | |
306 | if (mnt_context_set_target(cxt, spec)) | |
73f9a114 | 307 | err(MOUNT_EX_SYSERR, _("failed to set umount target")); |
db216e68 KZ |
308 | |
309 | rc = mnt_context_umount(cxt); | |
73f9a114 | 310 | rc = mk_exit_code(cxt, rc); |
db216e68 | 311 | |
84600ddc KZ |
312 | if (rc == MOUNT_EX_SUCCESS && mnt_context_is_verbose(cxt)) |
313 | success_message(cxt); | |
314 | ||
db216e68 KZ |
315 | mnt_reset_context(cxt); |
316 | return rc; | |
317 | } | |
318 | ||
4eb49f63 KZ |
319 | static struct libmnt_table *new_mountinfo(struct libmnt_context *cxt) |
320 | { | |
321 | struct libmnt_table *tb = mnt_new_table(); | |
322 | if (!tb) | |
323 | err(MOUNT_EX_SYSERR, _("libmount table allocation failed")); | |
324 | ||
325 | mnt_table_set_parser_errcb(tb, table_parser_errcb); | |
326 | mnt_table_set_cache(tb, mnt_context_get_cache(cxt)); | |
327 | ||
328 | if (mnt_table_parse_file(tb, _PATH_PROC_MOUNTINFO)) { | |
329 | warn(_("failed to parse %s"), _PATH_PROC_MOUNTINFO); | |
50fccba1 | 330 | mnt_unref_table(tb); |
4eb49f63 KZ |
331 | tb = NULL; |
332 | } | |
333 | ||
334 | return tb; | |
335 | } | |
336 | ||
337 | /* | |
338 | * like umount_one() but does not return error is @spec not mounted | |
339 | */ | |
340 | static int umount_one_if_mounted(struct libmnt_context *cxt, const char *spec) | |
341 | { | |
342 | int rc; | |
343 | struct libmnt_fs *fs; | |
344 | ||
345 | rc = mnt_context_find_umount_fs(cxt, spec, &fs); | |
346 | if (rc == 1) { | |
9e930041 | 347 | rc = MOUNT_EX_SUCCESS; /* already unmounted */ |
4eb49f63 KZ |
348 | mnt_reset_context(cxt); |
349 | } else if (rc < 0) { | |
350 | rc = mk_exit_code(cxt, rc); /* error */ | |
351 | mnt_reset_context(cxt); | |
352 | } else | |
353 | rc = umount_one(cxt, mnt_fs_get_target(fs)); | |
354 | ||
355 | return rc; | |
356 | } | |
357 | ||
13ee1c91 | 358 | static int umount_do_recurse(struct libmnt_context *cxt, |
4eb49f63 | 359 | struct libmnt_table *tb, struct libmnt_fs *fs) |
13ee1c91 | 360 | { |
13ee1c91 | 361 | struct libmnt_fs *child; |
13ee1c91 | 362 | struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_BACKWARD); |
4eb49f63 | 363 | int rc; |
13ee1c91 DR |
364 | |
365 | if (!itr) | |
366 | err(MOUNT_EX_SYSERR, _("libmount iterator allocation failed")); | |
4eb49f63 | 367 | |
9e930041 | 368 | /* umount all children */ |
13ee1c91 | 369 | for (;;) { |
4eb49f63 | 370 | rc = mnt_table_next_child_fs(tb, itr, fs, &child); |
13ee1c91 | 371 | if (rc < 0) { |
4eb49f63 KZ |
372 | warnx(_("failed to get child fs of %s"), |
373 | mnt_fs_get_target(fs)); | |
13ee1c91 DR |
374 | rc = MOUNT_EX_SOFTWARE; |
375 | goto done; | |
376 | } else if (rc == 1) | |
377 | break; /* no more children */ | |
378 | ||
379 | rc = umount_do_recurse(cxt, tb, child); | |
380 | if (rc != MOUNT_EX_SUCCESS) | |
381 | goto done; | |
382 | } | |
383 | ||
4eb49f63 | 384 | rc = umount_one_if_mounted(cxt, mnt_fs_get_target(fs)); |
13ee1c91 DR |
385 | done: |
386 | mnt_free_iter(itr); | |
387 | return rc; | |
388 | } | |
389 | ||
390 | static int umount_recursive(struct libmnt_context *cxt, const char *spec) | |
391 | { | |
392 | struct libmnt_table *tb; | |
4eb49f63 | 393 | struct libmnt_fs *fs; |
13ee1c91 DR |
394 | int rc; |
395 | ||
4eb49f63 KZ |
396 | tb = new_mountinfo(cxt); |
397 | if (!tb) | |
398 | return MOUNT_EX_SOFTWARE; | |
399 | ||
ae978c4d KZ |
400 | /* it's always real mountpoint, don't assume that the target maybe a device */ |
401 | mnt_context_disable_swapmatch(cxt, 1); | |
402 | ||
4eb49f63 KZ |
403 | fs = mnt_table_find_target(tb, spec, MNT_ITER_BACKWARD); |
404 | if (fs) | |
405 | rc = umount_do_recurse(cxt, tb, fs); | |
406 | else { | |
407 | rc = MOUNT_EX_USAGE; | |
408 | warnx(access(spec, F_OK) == 0 ? | |
409 | _("%s: not mounted") : | |
410 | _("%s: not found"), spec); | |
411 | } | |
a8cc72de | 412 | |
50fccba1 | 413 | mnt_unref_table(tb); |
4eb49f63 KZ |
414 | return rc; |
415 | } | |
7b4a2697 | 416 | |
4eb49f63 KZ |
417 | static int umount_alltargets(struct libmnt_context *cxt, const char *spec, int rec) |
418 | { | |
419 | struct libmnt_fs *fs; | |
420 | struct libmnt_table *tb; | |
421 | struct libmnt_iter *itr = NULL; | |
f697d61b | 422 | dev_t devno = 0; |
4eb49f63 KZ |
423 | int rc; |
424 | ||
425 | /* Convert @spec to device name, Use the same logic like regular | |
426 | * "umount <spec>". | |
13ee1c91 | 427 | */ |
4eb49f63 KZ |
428 | rc = mnt_context_find_umount_fs(cxt, spec, &fs); |
429 | if (rc == 1) { | |
430 | rc = MOUNT_EX_USAGE; | |
431 | warnx(access(spec, F_OK) == 0 ? | |
432 | _("%s: not mounted") : | |
433 | _("%s: not found"), spec); | |
434 | return rc; | |
435 | } | |
436 | if (rc < 0) | |
437 | return mk_exit_code(cxt, rc); /* error */ | |
13ee1c91 | 438 | |
f697d61b | 439 | if (!mnt_fs_get_srcpath(fs) || !mnt_fs_get_devno(fs)) |
fd7c4924 KZ |
440 | errx(MOUNT_EX_USAGE, _("%s: failed to determine source " |
441 | "(--all-targets is unsupported on systems with " | |
442 | "regular mtab file)."), spec); | |
4eb49f63 KZ |
443 | |
444 | itr = mnt_new_iter(MNT_ITER_BACKWARD); | |
445 | if (!itr) | |
446 | err(MOUNT_EX_SYSERR, _("libmount iterator allocation failed")); | |
447 | ||
448 | /* get on @cxt independent mountinfo */ | |
449 | tb = new_mountinfo(cxt); | |
86c58c4a KZ |
450 | if (!tb) { |
451 | rc = MOUNT_EX_SOFTWARE; | |
452 | goto done; | |
453 | } | |
4eb49f63 | 454 | |
9e930041 | 455 | /* Note that @fs is from mount context and the context will be reset |
4eb49f63 | 456 | * after each umount() call */ |
f697d61b | 457 | devno = mnt_fs_get_devno(fs); |
4eb49f63 KZ |
458 | fs = NULL; |
459 | ||
460 | mnt_reset_context(cxt); | |
461 | ||
462 | while (mnt_table_next_fs(tb, itr, &fs) == 0) { | |
f697d61b | 463 | if (mnt_fs_get_devno(fs) != devno) |
4eb49f63 KZ |
464 | continue; |
465 | mnt_context_disable_swapmatch(cxt, 1); | |
466 | if (rec) | |
13ee1c91 | 467 | rc = umount_do_recurse(cxt, tb, fs); |
4eb49f63 KZ |
468 | else |
469 | rc = umount_one_if_mounted(cxt, mnt_fs_get_target(fs)); | |
470 | ||
471 | if (rc != MOUNT_EX_SUCCESS) | |
472 | break; | |
13ee1c91 DR |
473 | } |
474 | ||
86c58c4a | 475 | done: |
4eb49f63 | 476 | mnt_free_iter(itr); |
50fccba1 | 477 | mnt_unref_table(tb); |
4eb49f63 | 478 | |
13ee1c91 DR |
479 | return rc; |
480 | } | |
481 | ||
cc8cc8f3 KZ |
482 | /* |
483 | * Check path -- non-root user should not be able to resolve path which is | |
484 | * unreadable for him. | |
485 | */ | |
486 | static char *sanitize_path(const char *path) | |
487 | { | |
488 | char *p; | |
489 | ||
490 | if (!path) | |
491 | return NULL; | |
492 | ||
493 | p = canonicalize_path_restricted(path); | |
494 | if (!p) | |
495 | err(MOUNT_EX_USAGE, "%s", path); | |
496 | ||
497 | return p; | |
498 | } | |
499 | ||
db216e68 KZ |
500 | int main(int argc, char **argv) |
501 | { | |
4eb49f63 | 502 | int c, rc = 0, all = 0, recursive = 0, alltargets = 0; |
db216e68 KZ |
503 | struct libmnt_context *cxt; |
504 | char *types = NULL; | |
505 | ||
506 | enum { | |
507 | UMOUNT_OPT_FAKE = CHAR_MAX + 1, | |
508 | }; | |
509 | ||
510 | static const struct option longopts[] = { | |
511 | { "all", 0, 0, 'a' }, | |
4eb49f63 | 512 | { "all-targets", 0, 0, 'A' }, |
db216e68 KZ |
513 | { "detach-loop", 0, 0, 'd' }, |
514 | { "fake", 0, 0, UMOUNT_OPT_FAKE }, | |
515 | { "force", 0, 0, 'f' }, | |
516 | { "help", 0, 0, 'h' }, | |
517 | { "internal-only", 0, 0, 'i' }, | |
518 | { "lazy", 0, 0, 'l' }, | |
519 | { "no-canonicalize", 0, 0, 'c' }, | |
520 | { "no-mtab", 0, 0, 'n' }, | |
521 | { "read-only", 0, 0, 'r' }, | |
13ee1c91 | 522 | { "recursive", 0, 0, 'R' }, |
db216e68 KZ |
523 | { "test-opts", 1, 0, 'O' }, |
524 | { "types", 1, 0, 't' }, | |
525 | { "verbose", 0, 0, 'v' }, | |
526 | { "version", 0, 0, 'V' }, | |
527 | { NULL, 0, 0, 0 } | |
528 | }; | |
529 | ||
a8cc72de | 530 | static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */ |
4eb49f63 | 531 | { 'A','a' }, /* all-targets,all */ |
a8cc72de KZ |
532 | { 'R','a' }, /* recursive,all */ |
533 | { 'O','R','t'}, /* options,recursive,types */ | |
534 | { 'R','r' }, /* recursive,read-only */ | |
535 | { 0 } | |
536 | }; | |
537 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
538 | ||
db216e68 KZ |
539 | sanitize_env(); |
540 | setlocale(LC_ALL, ""); | |
541 | bindtextdomain(PACKAGE, LOCALEDIR); | |
542 | textdomain(PACKAGE); | |
efb8854f | 543 | atexit(close_stdout); |
db216e68 KZ |
544 | |
545 | mnt_init_debug(0); | |
546 | cxt = mnt_new_context(); | |
547 | if (!cxt) | |
73f9a114 | 548 | err(MOUNT_EX_SYSERR, _("libmount context allocation failed")); |
db216e68 KZ |
549 | |
550 | mnt_context_set_tables_errcb(cxt, table_parser_errcb); | |
551 | ||
4eb49f63 | 552 | while ((c = getopt_long(argc, argv, "aAcdfhilnRrO:t:vV", |
db216e68 KZ |
553 | longopts, NULL)) != -1) { |
554 | ||
555 | ||
556 | /* only few options are allowed for non-root users */ | |
557 | if (mnt_context_is_restricted(cxt) && !strchr("hdilVv", c)) | |
558 | exit_non_root(option_to_longopt(c, longopts)); | |
559 | ||
a8cc72de KZ |
560 | err_exclusive_options(c, longopts, excl, excl_st); |
561 | ||
db216e68 KZ |
562 | switch(c) { |
563 | case 'a': | |
564 | all = 1; | |
565 | break; | |
4eb49f63 KZ |
566 | case 'A': |
567 | alltargets = 1; | |
568 | break; | |
db216e68 KZ |
569 | case 'c': |
570 | mnt_context_disable_canonicalize(cxt, TRUE); | |
571 | break; | |
572 | case 'd': | |
573 | mnt_context_enable_loopdel(cxt, TRUE); | |
574 | break; | |
575 | case UMOUNT_OPT_FAKE: | |
576 | mnt_context_enable_fake(cxt, TRUE); | |
577 | break; | |
578 | case 'f': | |
579 | mnt_context_enable_force(cxt, TRUE); | |
580 | break; | |
581 | case 'h': | |
582 | usage(stdout); | |
583 | break; | |
584 | case 'i': | |
585 | mnt_context_disable_helpers(cxt, TRUE); | |
586 | break; | |
587 | case 'l': | |
588 | mnt_context_enable_lazy(cxt, TRUE); | |
589 | break; | |
590 | case 'n': | |
591 | mnt_context_disable_mtab(cxt, TRUE); | |
592 | break; | |
593 | case 'r': | |
594 | mnt_context_enable_rdonly_umount(cxt, TRUE); | |
595 | break; | |
13ee1c91 DR |
596 | case 'R': |
597 | recursive = TRUE; | |
598 | break; | |
db216e68 KZ |
599 | case 'O': |
600 | if (mnt_context_set_options_pattern(cxt, optarg)) | |
73f9a114 | 601 | err(MOUNT_EX_SYSERR, _("failed to set options pattern")); |
db216e68 KZ |
602 | break; |
603 | case 't': | |
604 | types = optarg; | |
605 | break; | |
606 | case 'v': | |
607 | mnt_context_enable_verbose(cxt, TRUE); | |
608 | break; | |
609 | case 'V': | |
610 | print_version(); | |
611 | break; | |
612 | default: | |
677ec86c | 613 | errtryhelp(MOUNT_EX_USAGE); |
db216e68 KZ |
614 | } |
615 | } | |
616 | ||
617 | argc -= optind; | |
618 | argv += optind; | |
619 | ||
620 | if (all) { | |
621 | if (!types) | |
9e66fd30 | 622 | types = "noproc,nodevfs,nodevpts,nosysfs,norpc_pipefs,nonfsd,noselinuxfs"; |
db216e68 KZ |
623 | |
624 | mnt_context_set_fstype_pattern(cxt, types); | |
625 | rc = umount_all(cxt); | |
626 | ||
627 | } else if (argc < 1) { | |
628 | usage(stderr); | |
629 | ||
4eb49f63 KZ |
630 | } else if (alltargets) { |
631 | while (argc--) | |
632 | rc += umount_alltargets(cxt, *argv++, recursive); | |
13ee1c91 DR |
633 | } else if (recursive) { |
634 | while (argc--) | |
635 | rc += umount_recursive(cxt, *argv++); | |
636 | } else { | |
cc8cc8f3 | 637 | while (argc--) { |
d41acf74 | 638 | char *path = *argv; |
cc8cc8f3 | 639 | |
d41acf74 KZ |
640 | if (mnt_context_is_restricted(cxt) |
641 | && !mnt_tag_is_valid(path)) | |
cc8cc8f3 KZ |
642 | path = sanitize_path(path); |
643 | ||
644 | rc += umount_one(cxt, path); | |
645 | ||
d41acf74 | 646 | if (path != *argv) |
cc8cc8f3 | 647 | free(path); |
d41acf74 | 648 | argv++; |
cc8cc8f3 | 649 | } |
13ee1c91 | 650 | } |
db216e68 KZ |
651 | |
652 | mnt_free_context(cxt); | |
a9add961 | 653 | return (rc < 256) ? rc : 255; |
db216e68 KZ |
654 | } |
655 |