]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/mount.c
mount: (new) print error messages
[thirdparty/util-linux.git] / sys-utils / mount.c
CommitLineData
97073441
KZ
1/*
2 * mount(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
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <errno.h>
25#include <string.h>
26#include <getopt.h>
97073441
KZ
27#include <unistd.h>
28#include <sys/types.h>
dbae36fe 29#include <sys/mman.h>
ce433404
KZ
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <unistd.h>
97073441 33
2a1f429a
KZ
34#include <libmount.h>
35
97073441
KZ
36#include "nls.h"
37#include "c.h"
6189ace3 38#include "env.h"
732a6311 39#include "optutils.h"
dbae36fe
KZ
40#include "strutils.h"
41#include "xgetpass.h"
97073441
KZ
42
43/*** TODO: DOCS:
44 *
97073441 45 * --guess-fstype is unsupported
97073441
KZ
46 */
47
48/* exit status */
49#define EX_SUCCESS 0
50#define EX_USAGE 1 /* incorrect invocation or permission */
51#define EX_SYSERR 2 /* out of memory, cannot fork, ... */
52#define EX_SOFTWARE 4 /* internal mount bug or wrong version */
53#define EX_USER 8 /* user interrupt */
54#define EX_FILEIO 16 /* problems writing, locking, ... mtab/fstab */
55#define EX_FAIL 32 /* mount failure */
56#define EX_SOMEOK 64 /* some mount succeeded */
57
dbae36fe 58static int passfd = -1;
ce433404 59static int readwrite;
dbae36fe 60
97073441
KZ
61static void __attribute__((__noreturn__)) exit_non_root(const char *option)
62{
63 const uid_t ruid = getuid();
64 const uid_t euid = geteuid();
65
66 if (ruid == 0 && euid != 0) {
67 /* user is root, but setuid to non-root */
68 if (option)
69 errx(EX_USAGE, _("only root can use \"--%s\" option "
70 "(effective UID is %u)"),
71 option, euid);
72 errx(EX_USAGE, _("only root can do that "
73 "(effective UID is %u)"), euid);
74 }
75 if (option)
76 errx(EX_USAGE, _("only root can use \"--%s\" option"), option);
77 errx(EX_USAGE, _("only root can do that"));
78}
79
80static void __attribute__((__noreturn__)) print_version(void)
81{
82 const char *ver = NULL;
83
84 mnt_get_library_version(&ver);
85
732a6311 86 printf(_("%s from %s (libmount %s)\n"),
97073441
KZ
87 program_invocation_short_name, PACKAGE_STRING, ver);
88 exit(EX_SUCCESS);
89}
90
7fc6d2b8 91static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
9f7472b0
KZ
92 const char *filename, int line)
93{
94 if (filename)
95 warnx(_("%s: parse error: ignore entry at line %d."),
96 filename, line);
97 return 0;
98}
99
dbae36fe
KZ
100static char *encrypt_pass_get(struct libmnt_context *cxt)
101{
102 if (!cxt)
103 return 0;
104
105#ifdef MCL_FUTURE
106 if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
107 warn(_("couldn't lock into memory"));
108 return NULL;
109 }
110#endif
111 return xgetpass(passfd, _("Password: "));
112}
113
114static void encrypt_pass_release(struct libmnt_context *cxt, char *pwd)
115{
116 char *p = pwd;
117
118 while (p && *p)
119 *p++ = '\0';
120
121 free(pwd);
122 munlockall();
123}
124
11754572 125static void print_all(struct libmnt_context *cxt, char *pattern, int show_label)
97073441 126{
68164f6c 127 struct libmnt_table *tb;
a0c014dc 128 struct libmnt_iter *itr = NULL;
68164f6c
KZ
129 struct libmnt_fs *fs;
130 struct libmnt_cache *cache = NULL;
97073441 131
11754572
KZ
132 if (mnt_context_get_mtab(cxt, &tb))
133 err(EX_SYSERR, _("failed to read mtab"));
97073441
KZ
134
135 itr = mnt_new_iter(MNT_ITER_FORWARD);
11754572
KZ
136 if (!itr)
137 err(EX_SYSERR, _("failed to initialize libmount iterator"));
97073441
KZ
138 if (show_label)
139 cache = mnt_new_cache();
140
9f7472b0 141 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
97073441
KZ
142 const char *type = mnt_fs_get_fstype(fs);
143 const char *src = mnt_fs_get_source(fs);
7cf389f7 144 const char *optstr = mnt_fs_get_options(fs);
2576b4e7 145 char *xsrc;
97073441
KZ
146
147 if (type && pattern && !mnt_match_fstype(type, pattern))
148 continue;
149
2576b4e7
KZ
150 xsrc = mnt_pretty_path(src, cache);
151 printf ("%s on %s", xsrc, mnt_fs_get_target(fs));
97073441
KZ
152 if (type)
153 printf (" type %s", type);
154 if (optstr)
155 printf (" (%s)", optstr);
156 if (show_label && src) {
157 char *lb = mnt_cache_find_tag_value(cache, src, "LABEL");
158 if (lb)
159 printf (" [%s]", lb);
160 }
161 fputc('\n', stdout);
2576b4e7 162 free(xsrc);
97073441 163 }
11754572 164
97073441 165 mnt_free_cache(cache);
b192b7b9 166 mnt_free_iter(itr);
97073441
KZ
167}
168
9f7472b0
KZ
169/*
170 * mount -a [-F]
9f7472b0 171 */
d2c97887 172static int mount_all(struct libmnt_context *cxt)
a9ae3955 173{
9f7472b0
KZ
174 struct libmnt_iter *itr;
175 struct libmnt_fs *fs;
176 int mntrc, ignored, rc = EX_SUCCESS;
177
178 itr = mnt_new_iter(MNT_ITER_FORWARD);
179 if (!itr) {
180 warn(_("failed to initialize libmount iterator"));
181 return EX_SYSERR;
182 }
183
184 while (mnt_context_next_mount(cxt, itr, &fs, &mntrc, &ignored) == 0) {
185
186 const char *tgt = mnt_fs_get_target(fs);
187
188 if (ignored) {
189 if (mnt_context_is_verbose(cxt))
b3f7a0ec
KZ
190 printf(ignored == 1 ? _("%-25s: ignored\n") :
191 _("%-25s: already mounted\n"),
9f7472b0 192 tgt);
d2c97887
KZ
193
194 } else if (mnt_context_is_fork(cxt)) {
195 printf("%-25s: mount successfully forked\n", tgt);
196
9f7472b0 197 } else {
d2c97887
KZ
198 if (!mnt_context_get_status(cxt)) {
199 if (mntrc > 0) {
200 errno = mntrc;
201 printf(_("%-25s: failed: %s\n"), tgt,
202 strerror(mntrc));
203 rc |= EX_FAIL;
204 } else {
205 printf(_("%-25s: failed\n"), tgt);
206 rc |= EX_SYSERR;
207 }
208 } else {
209 if (mnt_context_is_verbose(cxt))
210 printf("%-25s: successfully mounted\n", tgt);
9f7472b0 211
d2c97887
KZ
212 rc |= EX_SOMEOK;
213 }
9f7472b0
KZ
214 }
215 }
216
d2c97887
KZ
217 if (mnt_context_is_parent(cxt)) {
218 /* wait for mount --fork children */
219 int nerrs = 0, nchildren = 0;
220
221 rc = mnt_context_wait_for_children(cxt, &nchildren, &nerrs);
222 if (!rc && nchildren)
223 rc = nchildren == nerrs ? EX_FAIL : EX_SOMEOK;
224 }
225
9f7472b0 226 return rc;
a9ae3955
KZ
227}
228
ce433404
KZ
229/*
230 * Handles generic errors like ENOMEM, ...
231 *
232 * rc = 0 success
233 * <0 error (usually -errno)
234 *
235 * Returns exit status (EX_*) and prints error message.
236 */
237static int handle_generic_errors(int rc, const char *msg)
238{
239 errno = -rc;
240
241 switch(errno) {
242 case EINVAL:
243 case EPERM:
244 warn(msg);
245 return EX_USAGE;
246 case ENOMEM:
247 warn(msg);
248 return EX_SYSERR;
249 default:
250 break;
251 }
252
253 warn(msg);
254 return EX_FAIL;
255}
256
257/*
258 * rc = 0 success
259 * <0 error (usually -errno or -1)
260 *
261 * Returns exit status (EX_*) and/or prints error message.
262 */
263static int mk_exit_code(struct libmnt_context *cxt, int rc)
264{
265 int syserr;
266 struct stat st;
267 unsigned long uflags = 0, mflags = 0;
268
269 int restricted = mnt_context_is_restricted(cxt);
270 const char *tgt = mnt_context_get_target(cxt);
271 const char *src = mnt_context_get_source(cxt);
272
273try_readonly:
274
275 if (mnt_context_helper_executed(cxt))
276 /*
277 * /sbin/mount.<type> called, return status
278 */
279 return mnt_context_get_helper_status(cxt);
280
281 if (rc == 0 && mnt_context_get_status(cxt) == 1)
282 /*
283 * Libmount success && syscall success.
284 */
285 return EX_SUCCESS; /* mount(2) success */
286
287 if (!mnt_context_syscall_called(cxt)) {
288 /*
289 * libmount errors
290 */
291 if (rc == -EPERM) {
292 warnx(_("only root can mount %s on %s"), src, tgt);
293 return EX_USAGE;
294 }
295
296 if (src == NULL || tgt == NULL) {
297 if (mflags & MS_REMOUNT)
298 warnx(_("%s not mounted"), src ? src : tgt);
299 else
300 warnx(_("can't find %s in %s"), src ? src : tgt,
301 mnt_get_fstab_path());
302 return EX_USAGE;
303 }
304
305 if (!mnt_context_get_fstype(cxt)) {
306 if (restricted)
307 warnx(_("I could not determine the filesystem type, "
308 "and none was specified"));
309 else
310 warnx(_("you must specify the filesystem type"));
311 return EX_USAGE;
312 }
313 return handle_generic_errors(rc, _("mount failed"));
314
315 } else if (mnt_context_get_syscall_errno(cxt) == 0) {
316 /*
317 * mount(2) syscall success, but something else failed
318 * (probably error in mtab processing).
319 */
320 if (rc < 0)
321 return handle_generic_errors(rc,
322 _("filesystem mounted, but mount(8) failed"));
323
324 return EX_SOFTWARE; /* internal error */
325
326 }
327
328 /*
329 * mount(2) errors
330 */
331 syserr = mnt_context_get_syscall_errno(cxt);
332
333 mnt_context_get_mflags(cxt, &mflags); /* mount(2) flags */
334 mnt_context_get_user_mflags(cxt, &uflags); /* userspace flags */
335
336 switch(syserr) {
337 case EPERM:
338 if (geteuid() == 0) {
339 if (stat(tgt, &st) || !S_ISDIR(st.st_mode))
340 warnx(_("mount point %s is not a directory"), tgt);
341 else
342 warnx(_("permission denied"));
343 } else
344 warnx(_("must be superuser to use mount"));
345 break;
346
347 case EBUSY:
348 {
349 struct libmnt_table *tb;
350 int count = 0;
351
352 if (mflags & MS_REMOUNT) {
353 warnx(_("%s is busy"), tgt);
354 break;
355 }
356
357 warnx(_("%s is already mounted or %s busy"), src, tgt);
358
359 if (src && mnt_context_get_mtab(cxt, &tb) == 0) {
360 struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_FORWARD);
361 struct libmnt_fs *fs;
362
363 while(mnt_table_next_fs(tb, itr, &fs) == 0) {
364 const char *s = mnt_fs_get_srcpath(fs),
365 *t = mnt_fs_get_target(fs);
366
367 if (t && s && streq_except_trailing_slash(s, src))
368 fprintf(stderr, _(
369 " %s is already mounted on %s\n"), s, t);
370 }
371 mnt_free_iter(itr);
372 }
373 break;
374 }
375 case ENOENT:
376 if (lstat(tgt, &st))
377 warnx(_("mount point %s does not exist"), tgt);
378 else if (stat(tgt, &st))
379 warnx(_("mount point %s is a symbolic link to nowhere"), tgt);
380 else if (stat(src, &st)) {
381 if (uflags & MNT_MS_NOFAIL)
382 return EX_SUCCESS;
383
384 warnx(_("special device %s does not exist"), src);
385 } else {
386 errno = syserr;
387 warn(_("mount(2) failed")); /* print errno */
388 }
389 break;
390
391 case ENOTDIR:
392 if (stat(tgt, &st) || ! S_ISDIR(st.st_mode))
393 warnx(_("mount point %s is not a directory"), tgt);
394 else if (stat(src, &st) && errno == ENOTDIR) {
395 if (uflags & MNT_MS_NOFAIL)
396 return EX_SUCCESS;
397
398 warnx(_("special device %s does not exist "
399 "(a path prefix is not a directory)"), src);
400 } else {
401 errno = syserr;
402 warn(_("mount(2) failed")); /* print errno */
403 }
404 break;
405
406 case EINVAL:
407 if (mflags & MS_REMOUNT)
408 warnx(_("%s not mounted or bad option"), tgt);
409 else
410 warnx(_("wrong fs type, bad option, bad superblock on %s,\n"
411 " missing codepage or helper program, or other error"),
412 src);
413
414 if (mnt_fs_is_netfs(mnt_context_get_fs(cxt)))
415 fprintf(stderr, _(
416 " (for several filesystems (e.g. nfs, cifs) you might\n"
417 " need a /sbin/mount.<type> helper program)\n"));
418
419 fprintf(stderr, _(
420 " In some cases useful info is found in syslog - try\n"
421 " dmesg | tail or so\n"));
422 break;
423
424 case EMFILE:
425 warnx(_("mount table full"));
426 break;
427
428 case EIO:
429 warnx(_("%s: can't read superblock"), src);
430 break;
431
432 case ENODEV:
433 warnx(_("unknown filesystem type '%s'"), mnt_context_get_fstype(cxt));
434 break;
435
436 case ENOTBLK:
437 if (uflags & MNT_MS_NOFAIL)
438 return EX_SUCCESS;
439
440 if (stat(src, &st))
441 warnx(_("%s is not a block device, and stat(2) fails?"), src);
442 else if (S_ISBLK(st.st_mode))
443 warnx(_("the kernel does not recognize %s as a block device\n"
444 " (maybe `modprobe driver'?)"), src);
445 else if (S_ISREG(st.st_mode))
446 warnx(_("%s is not a block device (maybe try `-o loop'?)"), src);
447 else
448 warnx(_(" %s is not a block device"), src);
449 break;
450
451 case ENXIO:
452 if (uflags & MNT_MS_NOFAIL)
453 return EX_SUCCESS;
454
455 warnx(_("%s is not a valid block device"), src);
456 break;
457
458 case EACCES:
459 case EROFS:
460 if (mflags & MS_RDONLY)
461 warnx(_("cannot mount %s read-only"), src);
462
463 else if (readwrite)
464 warnx(_("%s is write-protected but explicit `-w' flag given"), src);
465
466 else if (mflags & MS_REMOUNT)
467 warnx(_("cannot remount %s read-write, is write-protected"), src);
468
469 else {
470 warnx(_("%s is write-protected, mounting read-only"), src);
471
472 mnt_context_reset_status(cxt);
473 mnt_context_set_mflags(cxt, mflags | MS_RDONLY);
474 rc = mnt_context_do_mount(cxt);
475 if (!rc)
476 rc = mnt_context_finalize_mount(cxt);
477
478 goto try_readonly;
479 }
480 break;
481
482 case ENOMEDIUM:
483 warnx(_("no medium found on %s"), src);
484 break;
485
486 default:
487 warn(_("mount %s on %s failed"), src, tgt);
488 break;
489 }
490
491 return EX_FAIL;
492}
493
97073441
KZ
494static void __attribute__((__noreturn__)) usage(FILE *out)
495{
e4c92d06
KZ
496 fputs(USAGE_HEADER, out);
497 fprintf(out, _(
97073441
KZ
498 " %1$s [-lhV]\n"
499 " %1$s -a [options]\n"
500 " %1$s [options] <source> | <directory>\n"
501 " %1$s [options] <source> <directory>\n"
502 " %1$s <operation> <mountpoint> [<target>]\n"),
503 program_invocation_short_name);
504
e4c92d06 505 fputs(USAGE_OPTIONS, out);
97073441 506 fprintf(out, _(
97073441 507 " -a, --all mount all filesystems mentioned in fstab\n"
97073441 508 " -c, --no-canonicalize don't canonicalize paths\n"
eaca47f7
BS
509 " -f, --fake dry run; skip the mount(2) syscall\n"
510 " -F, --fork fork off for each device (use with -a)\n"));
511 fprintf(out, _(
512 " -h, --help display this help text and exit\n"
97073441
KZ
513 " -i, --internal-only don't call the mount.<type> helpers\n"
514 " -l, --show-labels lists all mounts with LABELs\n"
eaca47f7
BS
515 " -n, --no-mtab don't write to /etc/mtab\n"));
516 fprintf(out, _(
517 " -o, --options <list> comma-separated list of mount options\n"
518 " -O, --test-opts <list> limit the set of filesystems (use with -a)\n"
dbae36fe 519 " -p, --pass-fd <num> read the passphrase from file descriptor\n"
eaca47f7
BS
520 " -r, --read-only mount the filesystem read-only (same as -o ro)\n"
521 " -t, --types <list> limit the set of filesystem types\n"));
522 fprintf(out, _(
523 " -v, --verbose say what is being done\n"
524 " -V, --version display version information and exit\n"
525 " -w, --read-write mount the filesystem read-write (default)\n"));
97073441 526
e4c92d06
KZ
527 fputs(USAGE_SEPARATOR, out);
528 fputs(USAGE_HELP, out);
529 fputs(USAGE_VERSION, out);
530
eaca47f7 531 fprintf(out, _(
97073441
KZ
532 "\nSource:\n"
533 " -L, --label <label> synonym for LABEL=<label>\n"
534 " -U, --uuid <uuid> synonym for UUID=<uuid>\n"
535 " LABEL=<label> specifies device by filesystem label\n"
eaca47f7
BS
536 " UUID=<uuid> specifies device by filesystem UUID\n"));
537 fprintf(out, _(
97073441
KZ
538 " <device> specifies device by path\n"
539 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
eaca47f7 540 " <file> regular file for loopdev setup\n"));
97073441 541
eaca47f7 542 fprintf(out, _(
97073441
KZ
543 "\nOperations:\n"
544 " -B, --bind mount a subtree somewhere else (same as -o bind)\n"
545 " -M, --move move a subtree to some other place\n"
eaca47f7
BS
546 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
547 fprintf(out, _(
97073441
KZ
548 " --make-shared mark a subtree as shared\n"
549 " --make-slave mark a subtree as slave\n"
550 " --make-private mark a subtree as private\n"
eaca47f7
BS
551 " --make-unbindable mark a subtree as unbindable\n"));
552 fprintf(out, _(
97073441
KZ
553 " --make-rshared recursively mark a whole subtree as shared\n"
554 " --make-rslave recursively mark a whole subtree as slave\n"
555 " --make-rprivate recursively mark a whole subtree as private\n"
eaca47f7 556 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
97073441 557
e4c92d06 558 fprintf(out, USAGE_MAN_TAIL("mount(8)"));
97073441
KZ
559
560 exit(out == stderr ? EX_USAGE : EX_SUCCESS);
561}
562
563int main(int argc, char **argv)
564{
d2c97887 565 int c, rc = EX_SUCCESS, all = 0, show_labels = 0;
68164f6c 566 struct libmnt_context *cxt;
97073441
KZ
567 char *source = NULL, *srcbuf = NULL;
568 char *types = NULL;
569 unsigned long oper = 0;
570
2492f713
KZ
571 enum {
572 MOUNT_OPT_SHARED = CHAR_MAX + 1,
573 MOUNT_OPT_SLAVE,
574 MOUNT_OPT_PRIVATE,
575 MOUNT_OPT_UNBINDABLE,
576 MOUNT_OPT_RSHARED,
577 MOUNT_OPT_RSLAVE,
578 MOUNT_OPT_RPRIVATE,
579 MOUNT_OPT_RUNBINDABLE
580 };
581
6c7d5ae9 582 static const struct option longopts[] = {
97073441
KZ
583 { "all", 0, 0, 'a' },
584 { "fake", 0, 0, 'f' },
585 { "fork", 0, 0, 'F' },
586 { "help", 0, 0, 'h' },
587 { "no-mtab", 0, 0, 'n' },
588 { "read-only", 0, 0, 'r' },
589 { "ro", 0, 0, 'r' },
590 { "verbose", 0, 0, 'v' },
591 { "version", 0, 0, 'V' },
592 { "read-write", 0, 0, 'w' },
593 { "rw", 0, 0, 'w' },
594 { "options", 1, 0, 'o' },
595 { "test-opts", 1, 0, 'O' },
dbae36fe 596 { "pass-fd", 1, 0, 'p' },
97073441
KZ
597 { "types", 1, 0, 't' },
598 { "uuid", 1, 0, 'U' },
599 { "label", 1, 0, 'L'},
600 { "bind", 0, 0, 'B' },
601 { "move", 0, 0, 'M' },
602 { "rbind", 0, 0, 'R' },
2492f713
KZ
603 { "make-shared", 0, 0, MOUNT_OPT_SHARED },
604 { "make-slave", 0, 0, MOUNT_OPT_SLAVE },
605 { "make-private", 0, 0, MOUNT_OPT_PRIVATE },
606 { "make-unbindable", 0, 0, MOUNT_OPT_UNBINDABLE },
607 { "make-rshared", 0, 0, MOUNT_OPT_RSHARED },
608 { "make-rslave", 0, 0, MOUNT_OPT_RSLAVE },
609 { "make-rprivate", 0, 0, MOUNT_OPT_RPRIVATE },
610 { "make-runbindable", 0, 0, MOUNT_OPT_RUNBINDABLE },
97073441
KZ
611 { "no-canonicalize", 0, 0, 'c' },
612 { "internal-only", 0, 0, 'i' },
613 { "show-labels", 0, 0, 'l' },
614 { NULL, 0, 0, 0 }
615 };
616
6189ace3 617 sanitize_env();
97073441
KZ
618 setlocale(LC_ALL, "");
619 bindtextdomain(PACKAGE, LOCALEDIR);
620 textdomain(PACKAGE);
621
622 mnt_init_debug(0);
623 cxt = mnt_new_context();
624 if (!cxt)
625 err(EX_SYSERR, _("libmount context allocation failed"));
626
9f7472b0
KZ
627 mnt_context_set_tables_errcb(cxt, table_parser_errcb);
628
dbae36fe 629 while ((c = getopt_long(argc, argv, "aBcfFhilL:Mno:O:p:rRsU:vVwt:",
97073441
KZ
630 longopts, NULL)) != -1) {
631
632 /* only few options are allowed for non-root users */
ce433404 633 if (mnt_context_is_restricted(cxt) && !strchr("hlLUVvpr", c))
732a6311 634 exit_non_root(option_to_longopt(c, longopts));
97073441
KZ
635
636 switch(c) {
637 case 'a':
638 all = 1;
97073441
KZ
639 break;
640 case 'c':
641 mnt_context_disable_canonicalize(cxt, TRUE);
642 break;
643 case 'f':
644 mnt_context_enable_fake(cxt, TRUE);
645 break;
646 case 'F':
d2c97887 647 mnt_context_enable_fork(cxt, TRUE);
97073441
KZ
648 break;
649 case 'h':
650 usage(stdout);
651 break;
652 case 'i':
653 mnt_context_disable_helpers(cxt, TRUE);
654 break;
655 case 'n':
656 mnt_context_disable_mtab(cxt, TRUE);
657 break;
658 case 'r':
659 if (mnt_context_append_options(cxt, "ro"))
660 err(EX_SYSERR, _("failed to append options"));
ce433404 661 readwrite = 0;
97073441
KZ
662 break;
663 case 'v':
664 mnt_context_enable_verbose(cxt, TRUE);
665 break;
666 case 'V':
667 print_version();
668 break;
669 case 'w':
670 if (mnt_context_append_options(cxt, "rw"))
671 err(EX_SYSERR, _("failed to append options"));
ce433404 672 readwrite = 1;
97073441
KZ
673 break;
674 case 'o':
675 if (mnt_context_append_options(cxt, optarg))
676 err(EX_SYSERR, _("failed to append options"));
677 break;
678 case 'O':
679 if (mnt_context_set_options_pattern(cxt, optarg))
680 err(EX_SYSERR, _("failed to set options pattern"));
681 break;
dbae36fe
KZ
682 case 'p':
683 passfd = strtol_or_err(optarg,
684 _("invalid passphrase file descriptor"));
685 break;
97073441
KZ
686 case 'L':
687 case 'U':
688 if (source)
e8ab5ce3 689 errx(EX_USAGE, _("only one <source> may be specified"));
97073441
KZ
690 if (asprintf(&srcbuf, "%s=\"%s\"",
691 c == 'L' ? "LABEL" : "UUID", optarg) <= 0)
692 err(EX_SYSERR, _("failed to allocate source buffer"));
693 source = srcbuf;
694 break;
695 case 'l':
696 show_labels = 1;
697 break;
698 case 't':
699 types = optarg;
700 break;
701 case 's':
702 mnt_context_enable_sloppy(cxt, TRUE);
703 break;
704 case 'B':
705 oper = MS_BIND;
706 break;
707 case 'M':
708 oper = MS_MOVE;
709 break;
710 case 'R':
711 oper = (MS_BIND | MS_REC);
712 break;
2492f713 713 case MOUNT_OPT_SHARED:
97073441
KZ
714 oper = MS_SHARED;
715 break;
2492f713 716 case MOUNT_OPT_SLAVE:
97073441
KZ
717 oper = MS_SLAVE;
718 break;
2492f713 719 case MOUNT_OPT_PRIVATE:
97073441
KZ
720 oper = MS_PRIVATE;
721 break;
2492f713 722 case MOUNT_OPT_UNBINDABLE:
97073441
KZ
723 oper = MS_UNBINDABLE;
724 break;
2492f713 725 case MOUNT_OPT_RSHARED:
97073441
KZ
726 oper = (MS_SHARED | MS_REC);
727 break;
2492f713 728 case MOUNT_OPT_RSLAVE:
97073441
KZ
729 oper = (MS_SLAVE | MS_REC);
730 break;
2492f713 731 case MOUNT_OPT_RPRIVATE:
97073441
KZ
732 oper = (MS_PRIVATE | MS_REC);
733 break;
2492f713 734 case MOUNT_OPT_RUNBINDABLE:
97073441
KZ
735 oper = (MS_UNBINDABLE | MS_REC);
736 break;
737 default:
738 usage(stderr);
739 break;
740 }
741 }
742
743 argc -= optind;
744 argv += optind;
745
746 if (!source && !argc && !all) {
747 if (oper)
748 usage(stderr);
11754572 749 print_all(cxt, types, show_labels);
97073441
KZ
750 goto done;
751 }
752
753 if (oper && (types || all || source))
754 usage(stderr);
755
9f7472b0
KZ
756 if (types && (all || strchr(types, ',') ||
757 strncmp(types, "no", 2) == 0))
97073441
KZ
758 mnt_context_set_fstype_pattern(cxt, types);
759 else if (types)
760 mnt_context_set_fstype(cxt, types);
761
dbae36fe
KZ
762 mnt_context_set_passwd_cb(cxt, encrypt_pass_get, encrypt_pass_release);
763
a9ae3955
KZ
764 if (all) {
765 /*
766 * A) Mount all
767 */
d2c97887 768 rc = mount_all(cxt);
11754572 769 goto done;
a9ae3955
KZ
770
771 } else if (argc == 0 && source) {
772 /*
773 * B) mount -L|-U
774 */
97073441
KZ
775 mnt_context_set_source(cxt, source);
776
777 } else if (argc == 1) {
a9ae3955
KZ
778 /*
779 * C) mount [-L|-U] <target>
780 * mount <source|target>
781 */
97073441
KZ
782 if (source) {
783 if (mnt_context_is_restricted(cxt))
784 exit_non_root(NULL);
785 mnt_context_set_source(cxt, source);
786 }
787 mnt_context_set_target(cxt, argv[0]);
788
789 } else if (argc == 2 && !source) {
a9ae3955
KZ
790 /*
791 * D) mount <source> <target>
792 */
97073441
KZ
793 if (mnt_context_is_restricted(cxt))
794 exit_non_root(NULL);
795 mnt_context_set_source(cxt, argv[0]);
796 mnt_context_set_target(cxt, argv[1]);
797 } else
798 usage(stderr);
799
800 if (oper)
68164f6c 801 mnt_context_set_mflags(cxt, oper);
97073441 802
cfb9db30 803 rc = mnt_context_mount(cxt);
ce433404
KZ
804 rc = mk_exit_code(cxt, rc);
805
97073441
KZ
806done:
807 free(srcbuf);
808 mnt_free_context(cxt);
809 return rc;
810}
811