]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/mount.c
misc: fix xalloc.h related exit codes
[thirdparty/util-linux.git] / sys-utils / mount.c
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 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 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <stdarg.h>
32 #include <libmount.h>
33 #include <ctype.h>
34
35 #include "nls.h"
36 #include "c.h"
37 #include "env.h"
38 #include "strutils.h"
39 #include "closestream.h"
40 #include "canonicalize.h"
41
42 #define XALLOC_EXIT_CODE MNT_EX_SYSERR
43 #include "xalloc.h"
44
45 #define OPTUTILS_EXIT_CODE MNT_EX_USAGE
46 #include "optutils.h"
47
48 /*** TODO: DOCS:
49 *
50 * --options-mode={ignore,append,prepend,replace} MNT_OMODE_{IGNORE, ...}
51 * --options-source={fstab,mtab,disable} MNT_OMODE_{FSTAB,MTAB,NOTAB}
52 * --options-source-force MNT_OMODE_FORCE
53 */
54
55 static int mk_exit_code(struct libmnt_context *cxt, int rc);
56
57 static void __attribute__((__noreturn__)) exit_non_root(const char *option)
58 {
59 const uid_t ruid = getuid();
60 const uid_t euid = geteuid();
61
62 if (ruid == 0 && euid != 0) {
63 /* user is root, but setuid to non-root */
64 if (option)
65 errx(MNT_EX_USAGE, _("only root can use \"--%s\" option "
66 "(effective UID is %u)"),
67 option, euid);
68 errx(MNT_EX_USAGE, _("only root can do that "
69 "(effective UID is %u)"), euid);
70 }
71 if (option)
72 errx(MNT_EX_USAGE, _("only root can use \"--%s\" option"), option);
73 errx(MNT_EX_USAGE, _("only root can do that"));
74 }
75
76 static void __attribute__((__noreturn__)) print_version(void)
77 {
78 const char *ver = NULL;
79 const char **features = NULL, **p;
80
81 mnt_get_library_version(&ver);
82 mnt_get_library_features(&features);
83
84 printf(_("%s from %s (libmount %s"),
85 program_invocation_short_name,
86 PACKAGE_STRING,
87 ver);
88 p = features;
89 while (p && *p) {
90 fputs(p == features ? ": " : ", ", stdout);
91 fputs(*p++, stdout);
92 }
93 fputs(")\n", stdout);
94 exit(MNT_EX_SUCCESS);
95 }
96
97 static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
98 const char *filename, int line)
99 {
100 if (filename)
101 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
102 return 1;
103 }
104
105 /*
106 * Replace control chars with '?' to be compatible with coreutils. For more
107 * robust solution use findmnt(1) where we use \x?? hex encoding.
108 */
109 static void safe_fputs(const char *data)
110 {
111 const char *p;
112
113 for (p = data; p && *p; p++) {
114 if (iscntrl((unsigned char) *p))
115 fputc('?', stdout);
116 else
117 fputc(*p, stdout);
118 }
119 }
120
121 static void print_all(struct libmnt_context *cxt, char *pattern, int show_label)
122 {
123 struct libmnt_table *tb;
124 struct libmnt_iter *itr = NULL;
125 struct libmnt_fs *fs;
126 struct libmnt_cache *cache = NULL;
127
128 if (mnt_context_get_mtab(cxt, &tb))
129 err(MNT_EX_SYSERR, _("failed to read mtab"));
130
131 itr = mnt_new_iter(MNT_ITER_FORWARD);
132 if (!itr)
133 err(MNT_EX_SYSERR, _("failed to initialize libmount iterator"));
134 if (show_label)
135 cache = mnt_new_cache();
136
137 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
138 const char *type = mnt_fs_get_fstype(fs);
139 const char *src = mnt_fs_get_source(fs);
140 const char *optstr = mnt_fs_get_options(fs);
141 char *xsrc = NULL;
142
143 if (type && pattern && !mnt_match_fstype(type, pattern))
144 continue;
145
146 if (!mnt_fs_is_pseudofs(fs))
147 xsrc = mnt_pretty_path(src, cache);
148 printf ("%s on ", xsrc ? xsrc : src);
149 safe_fputs(mnt_fs_get_target(fs));
150
151 if (type)
152 printf (" type %s", type);
153 if (optstr)
154 printf (" (%s)", optstr);
155 if (show_label && src) {
156 char *lb = mnt_cache_find_tag_value(cache, src, "LABEL");
157 if (lb)
158 printf (" [%s]", lb);
159 }
160 fputc('\n', stdout);
161 free(xsrc);
162 }
163
164 mnt_unref_cache(cache);
165 mnt_free_iter(itr);
166 }
167
168 /*
169 * mount -a [-F]
170 */
171 static int mount_all(struct libmnt_context *cxt)
172 {
173 struct libmnt_iter *itr;
174 struct libmnt_fs *fs;
175 int mntrc, ignored, rc = MNT_EX_SUCCESS;
176
177 int nsucc = 0, nerrs = 0;
178
179 itr = mnt_new_iter(MNT_ITER_FORWARD);
180 if (!itr) {
181 warn(_("failed to initialize libmount iterator"));
182 return MNT_EX_SYSERR;
183 }
184
185 while (mnt_context_next_mount(cxt, itr, &fs, &mntrc, &ignored) == 0) {
186
187 const char *tgt = mnt_fs_get_target(fs);
188
189 if (ignored) {
190 if (mnt_context_is_verbose(cxt))
191 printf(ignored == 1 ? _("%-25s: ignored\n") :
192 _("%-25s: already mounted\n"),
193 tgt);
194 } else if (mnt_context_is_fork(cxt)) {
195 if (mnt_context_is_verbose(cxt))
196 printf("%-25s: mount successfully forked\n", tgt);
197 } else {
198 if (mk_exit_code(cxt, mntrc) == MNT_EX_SUCCESS) {
199 nsucc++;
200
201 /* Note that MNT_EX_SUCCESS return code does
202 * not mean that FS has been really mounted
203 * (e.g. nofail option) */
204 if (mnt_context_get_status(cxt)
205 && mnt_context_is_verbose(cxt))
206 printf("%-25s: successfully mounted\n", tgt);
207 } else
208 nerrs++;
209 }
210 }
211
212 if (mnt_context_is_parent(cxt)) {
213 /* wait for mount --fork children */
214 int nchildren = 0;
215
216 nerrs = 0, nsucc = 0;
217
218 rc = mnt_context_wait_for_children(cxt, &nchildren, &nerrs);
219 if (!rc && nchildren)
220 nsucc = nchildren - nerrs;
221 }
222
223 if (nerrs == 0)
224 rc = MNT_EX_SUCCESS; /* all success */
225 else if (nsucc == 0)
226 rc = MNT_EX_FAIL; /* all failed */
227 else
228 rc = MNT_EX_SOMEOK; /* some success, some failed */
229
230 mnt_free_iter(itr);
231 return rc;
232 }
233
234 static void success_message(struct libmnt_context *cxt)
235 {
236 unsigned long mflags = 0;
237 const char *tgt, *src, *pr = program_invocation_short_name;
238
239 if (mnt_context_helper_executed(cxt)
240 || mnt_context_get_status(cxt) != 1)
241 return;
242
243 mnt_context_get_mflags(cxt, &mflags);
244 tgt = mnt_context_get_target(cxt);
245 src = mnt_context_get_source(cxt);
246
247 if (mflags & MS_MOVE)
248 printf(_("%s: %s moved to %s.\n"), pr, src, tgt);
249 else if (mflags & MS_BIND)
250 printf(_("%s: %s bound on %s.\n"), pr, src, tgt);
251 else if (mflags & MS_PROPAGATION) {
252 if (src && strcmp(src, "none") != 0 && tgt)
253 printf(_("%s: %s mounted on %s.\n"), pr, src, tgt);
254
255 printf(_("%s: %s propagation flags changed.\n"), pr, tgt);
256 } else
257 printf(_("%s: %s mounted on %s.\n"), pr, src, tgt);
258 }
259
260 #if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
261 #include <selinux/selinux.h>
262 #include <selinux/context.h>
263
264 static void selinux_warning(struct libmnt_context *cxt, const char *tgt)
265 {
266
267 if (tgt && mnt_context_is_verbose(cxt) && is_selinux_enabled() > 0) {
268 security_context_t raw = NULL, def = NULL;
269
270 if (getfilecon(tgt, &raw) > 0
271 && security_get_initial_context("file", &def) == 0) {
272
273 if (!selinux_file_context_cmp(raw, def))
274 printf(_(
275 "mount: %s does not contain SELinux labels.\n"
276 " You just mounted an file system that supports labels which does not\n"
277 " contain labels, onto an SELinux box. It is likely that confined\n"
278 " applications will generate AVC messages and not be allowed access to\n"
279 " this file system. For more details see restorecon(8) and mount(8).\n"),
280 tgt);
281 }
282 freecon(raw);
283 freecon(def);
284 }
285 }
286 #else
287 # define selinux_warning(_x, _y)
288 #endif
289
290 /*
291 * Returns exit status (MNT_EX_*) and/or prints error message.
292 */
293 static int mk_exit_code(struct libmnt_context *cxt, int rc)
294 {
295 const char *tgt;
296 char buf[BUFSIZ] = { 0 };
297
298 rc = mnt_context_get_excode(cxt, rc, buf, sizeof(buf));
299 tgt = mnt_context_get_target(cxt);
300
301 if (*buf) {
302 const char *spec = tgt;
303 if (!spec)
304 spec = mnt_context_get_source(cxt);
305 if (!spec)
306 spec = "???";
307 warnx(_("%s: %s."), spec, buf);
308 }
309
310 if (rc == MNT_EX_SUCCESS && mnt_context_get_status(cxt) == 1) {
311 selinux_warning(cxt, tgt);
312 }
313 return rc;
314 }
315
316 static struct libmnt_table *append_fstab(struct libmnt_context *cxt,
317 struct libmnt_table *fstab,
318 const char *path)
319 {
320
321 if (!fstab) {
322 fstab = mnt_new_table();
323 if (!fstab)
324 err(MNT_EX_SYSERR, _("failed to initialize libmount table"));
325
326 mnt_table_set_parser_errcb(fstab, table_parser_errcb);
327 mnt_context_set_fstab(cxt, fstab);
328
329 mnt_unref_table(fstab); /* reference is handled by @cxt now */
330 }
331
332 if (mnt_table_parse_fstab(fstab, path))
333 errx(MNT_EX_USAGE,_("%s: failed to parse"), path);
334
335 return fstab;
336 }
337
338 /*
339 * Check source and target paths -- non-root user should not be able to
340 * resolve paths which are unreadable for him.
341 */
342 static void sanitize_paths(struct libmnt_context *cxt)
343 {
344 const char *p;
345 struct libmnt_fs *fs = mnt_context_get_fs(cxt);
346
347 if (!fs)
348 return;
349
350 p = mnt_fs_get_target(fs);
351 if (p) {
352 char *np = canonicalize_path_restricted(p);
353 if (!np)
354 err(MNT_EX_USAGE, "%s", p);
355 mnt_fs_set_target(fs, np);
356 free(np);
357 }
358
359 p = mnt_fs_get_srcpath(fs);
360 if (p) {
361 char *np = canonicalize_path_restricted(p);
362 if (!np)
363 err(MNT_EX_USAGE, "%s", p);
364 mnt_fs_set_source(fs, np);
365 free(np);
366 }
367 }
368
369 static void append_option(struct libmnt_context *cxt, const char *opt)
370 {
371 if (opt && (*opt == '=' || *opt == '\'' || *opt == '\"' || isblank(*opt)))
372 errx(MNT_EX_USAGE, _("unsupported option format: %s"), opt);
373 if (mnt_context_append_options(cxt, opt))
374 err(MNT_EX_SYSERR, _("failed to append option '%s'"), opt);
375 }
376
377 static int has_remount_flag(struct libmnt_context *cxt)
378 {
379 unsigned long mflags = 0;
380
381 if (mnt_context_get_mflags(cxt, &mflags))
382 return 0;
383
384 return mflags & MS_REMOUNT;
385 }
386
387 static void __attribute__((__noreturn__)) usage(FILE *out)
388 {
389 fputs(USAGE_HEADER, out);
390 fprintf(out, _(
391 " %1$s [-lhV]\n"
392 " %1$s -a [options]\n"
393 " %1$s [options] [--source] <source> | [--target] <directory>\n"
394 " %1$s [options] <source> <directory>\n"
395 " %1$s <operation> <mountpoint> [<target>]\n"),
396 program_invocation_short_name);
397
398 fputs(USAGE_SEPARATOR, out);
399 fputs(_("Mount a filesystem.\n"), out);
400
401 fputs(USAGE_OPTIONS, out);
402 fprintf(out, _(
403 " -a, --all mount all filesystems mentioned in fstab\n"
404 " -c, --no-canonicalize don't canonicalize paths\n"
405 " -f, --fake dry run; skip the mount(2) syscall\n"
406 " -F, --fork fork off for each device (use with -a)\n"
407 " -T, --fstab <path> alternative file to /etc/fstab\n"));
408 fprintf(out, _(
409 " -i, --internal-only don't call the mount.<type> helpers\n"));
410 fprintf(out, _(
411 " -l, --show-labels show also filesystem labels\n"));
412 fprintf(out, _(
413 " -n, --no-mtab don't write to /etc/mtab\n"));
414 fprintf(out, _(
415 " -o, --options <list> comma-separated list of mount options\n"
416 " -O, --test-opts <list> limit the set of filesystems (use with -a)\n"
417 " -r, --read-only mount the filesystem read-only (same as -o ro)\n"
418 " -t, --types <list> limit the set of filesystem types\n"));
419 fprintf(out, _(
420 " --source <src> explicitly specifies source (path, label, uuid)\n"
421 " --target <target> explicitly specifies mountpoint\n"));
422 fprintf(out, _(
423 " -v, --verbose say what is being done\n"));
424 fprintf(out, _(
425 " -w, --rw, --read-write mount the filesystem read-write (default)\n"));
426
427 fputs(USAGE_SEPARATOR, out);
428 fputs(USAGE_HELP, out);
429 fputs(USAGE_VERSION, out);
430
431 fprintf(out, _(
432 "\nSource:\n"
433 " -L, --label <label> synonym for LABEL=<label>\n"
434 " -U, --uuid <uuid> synonym for UUID=<uuid>\n"
435 " LABEL=<label> specifies device by filesystem label\n"
436 " UUID=<uuid> specifies device by filesystem UUID\n"
437 " PARTLABEL=<label> specifies device by partition label\n"
438 " PARTUUID=<uuid> specifies device by partition UUID\n"));
439
440 fprintf(out, _(
441 " <device> specifies device by path\n"
442 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
443 " <file> regular file for loopdev setup\n"));
444
445 fprintf(out, _(
446 "\nOperations:\n"
447 " -B, --bind mount a subtree somewhere else (same as -o bind)\n"
448 " -M, --move move a subtree to some other place\n"
449 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
450 fprintf(out, _(
451 " --make-shared mark a subtree as shared\n"
452 " --make-slave mark a subtree as slave\n"
453 " --make-private mark a subtree as private\n"
454 " --make-unbindable mark a subtree as unbindable\n"));
455 fprintf(out, _(
456 " --make-rshared recursively mark a whole subtree as shared\n"
457 " --make-rslave recursively mark a whole subtree as slave\n"
458 " --make-rprivate recursively mark a whole subtree as private\n"
459 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
460
461 fprintf(out, USAGE_MAN_TAIL("mount(8)"));
462
463 exit(out == stderr ? MNT_EX_USAGE : MNT_EX_SUCCESS);
464 }
465
466 int main(int argc, char **argv)
467 {
468 int c, rc = MNT_EX_SUCCESS, all = 0, show_labels = 0;
469 struct libmnt_context *cxt;
470 struct libmnt_table *fstab = NULL;
471 char *srcbuf = NULL;
472 char *types = NULL;
473 unsigned long oper = 0;
474 int propa = 0;
475
476 enum {
477 MOUNT_OPT_SHARED = CHAR_MAX + 1,
478 MOUNT_OPT_SLAVE,
479 MOUNT_OPT_PRIVATE,
480 MOUNT_OPT_UNBINDABLE,
481 MOUNT_OPT_RSHARED,
482 MOUNT_OPT_RSLAVE,
483 MOUNT_OPT_RPRIVATE,
484 MOUNT_OPT_RUNBINDABLE,
485 MOUNT_OPT_TARGET,
486 MOUNT_OPT_SOURCE
487 };
488
489 static const struct option longopts[] = {
490 { "all", no_argument, NULL, 'a' },
491 { "fake", no_argument, NULL, 'f' },
492 { "fstab", required_argument, NULL, 'T' },
493 { "fork", no_argument, NULL, 'F' },
494 { "help", no_argument, NULL, 'h' },
495 { "no-mtab", no_argument, NULL, 'n' },
496 { "read-only", no_argument, NULL, 'r' },
497 { "ro", no_argument, NULL, 'r' },
498 { "verbose", no_argument, NULL, 'v' },
499 { "version", no_argument, NULL, 'V' },
500 { "read-write", no_argument, NULL, 'w' },
501 { "rw", no_argument, NULL, 'w' },
502 { "options", required_argument, NULL, 'o' },
503 { "test-opts", required_argument, NULL, 'O' },
504 { "types", required_argument, NULL, 't' },
505 { "uuid", required_argument, NULL, 'U' },
506 { "label", required_argument, NULL, 'L' },
507 { "bind", no_argument, NULL, 'B' },
508 { "move", no_argument, NULL, 'M' },
509 { "rbind", no_argument, NULL, 'R' },
510 { "make-shared", no_argument, NULL, MOUNT_OPT_SHARED },
511 { "make-slave", no_argument, NULL, MOUNT_OPT_SLAVE },
512 { "make-private", no_argument, NULL, MOUNT_OPT_PRIVATE },
513 { "make-unbindable", no_argument, NULL, MOUNT_OPT_UNBINDABLE },
514 { "make-rshared", no_argument, NULL, MOUNT_OPT_RSHARED },
515 { "make-rslave", no_argument, NULL, MOUNT_OPT_RSLAVE },
516 { "make-rprivate", no_argument, NULL, MOUNT_OPT_RPRIVATE },
517 { "make-runbindable", no_argument, NULL, MOUNT_OPT_RUNBINDABLE },
518 { "no-canonicalize", no_argument, NULL, 'c' },
519 { "internal-only", no_argument, NULL, 'i' },
520 { "show-labels", no_argument, NULL, 'l' },
521 { "target", required_argument, NULL, MOUNT_OPT_TARGET },
522 { "source", required_argument, NULL, MOUNT_OPT_SOURCE },
523 { NULL, 0, NULL, 0 }
524 };
525
526 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
527 { 'B','M','R' }, /* bind,move,rbind */
528 { 'L','U', MOUNT_OPT_SOURCE }, /* label,uuid,source */
529 { 0 }
530 };
531 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
532
533 sanitize_env();
534 setlocale(LC_ALL, "");
535 bindtextdomain(PACKAGE, LOCALEDIR);
536 textdomain(PACKAGE);
537 atexit(close_stdout);
538
539 strutils_set_exitcode(MNT_EX_USAGE);
540
541 mnt_init_debug(0);
542 cxt = mnt_new_context();
543 if (!cxt)
544 err(MNT_EX_SYSERR, _("libmount context allocation failed"));
545
546 mnt_context_set_tables_errcb(cxt, table_parser_errcb);
547
548 while ((c = getopt_long(argc, argv, "aBcfFhilL:Mno:O:rRsU:vVwt:T:",
549 longopts, NULL)) != -1) {
550
551 /* only few options are allowed for non-root users */
552 if (mnt_context_is_restricted(cxt) &&
553 !strchr("hlLUVvrist", c) &&
554 c != MOUNT_OPT_TARGET &&
555 c != MOUNT_OPT_SOURCE)
556 exit_non_root(option_to_longopt(c, longopts));
557
558 err_exclusive_options(c, longopts, excl, excl_st);
559
560 switch(c) {
561 case 'a':
562 all = 1;
563 break;
564 case 'c':
565 mnt_context_disable_canonicalize(cxt, TRUE);
566 break;
567 case 'f':
568 mnt_context_enable_fake(cxt, TRUE);
569 break;
570 case 'F':
571 mnt_context_enable_fork(cxt, TRUE);
572 break;
573 case 'h':
574 usage(stdout);
575 break;
576 case 'i':
577 mnt_context_disable_helpers(cxt, TRUE);
578 break;
579 case 'n':
580 mnt_context_disable_mtab(cxt, TRUE);
581 break;
582 case 'r':
583 append_option(cxt, "ro");
584 mnt_context_enable_rwonly_mount(cxt, FALSE);
585 break;
586 case 'v':
587 mnt_context_enable_verbose(cxt, TRUE);
588 break;
589 case 'V':
590 print_version();
591 break;
592 case 'w':
593 append_option(cxt, "rw");
594 mnt_context_enable_rwonly_mount(cxt, TRUE);
595 break;
596 case 'o':
597 append_option(cxt, optarg);
598 break;
599 case 'O':
600 if (mnt_context_set_options_pattern(cxt, optarg))
601 err(MNT_EX_SYSERR, _("failed to set options pattern"));
602 break;
603 case 'L':
604 xasprintf(&srcbuf, "LABEL=\"%s\"", optarg);
605 mnt_context_disable_swapmatch(cxt, 1);
606 mnt_context_set_source(cxt, srcbuf);
607 free(srcbuf);
608 break;
609 case 'U':
610 xasprintf(&srcbuf, "UUID=\"%s\"", optarg);
611 mnt_context_disable_swapmatch(cxt, 1);
612 mnt_context_set_source(cxt, srcbuf);
613 free(srcbuf);
614 break;
615 case 'l':
616 show_labels = 1;
617 break;
618 case 't':
619 types = optarg;
620 break;
621 case 'T':
622 fstab = append_fstab(cxt, fstab, optarg);
623 break;
624 case 's':
625 mnt_context_enable_sloppy(cxt, TRUE);
626 break;
627 case 'B':
628 oper |= MS_BIND;
629 break;
630 case 'M':
631 oper |= MS_MOVE;
632 break;
633 case 'R':
634 oper |= (MS_BIND | MS_REC);
635 break;
636 case MOUNT_OPT_SHARED:
637 append_option(cxt, "shared");
638 propa = 1;
639 break;
640 case MOUNT_OPT_SLAVE:
641 append_option(cxt, "slave");
642 propa = 1;
643 break;
644 case MOUNT_OPT_PRIVATE:
645 append_option(cxt, "private");
646 propa = 1;
647 break;
648 case MOUNT_OPT_UNBINDABLE:
649 append_option(cxt, "unbindable");
650 propa = 1;
651 break;
652 case MOUNT_OPT_RSHARED:
653 append_option(cxt, "rshared");
654 propa = 1;
655 break;
656 case MOUNT_OPT_RSLAVE:
657 append_option(cxt, "rslave");
658 propa = 1;
659 break;
660 case MOUNT_OPT_RPRIVATE:
661 append_option(cxt, "rprivate");
662 propa = 1;
663 break;
664 case MOUNT_OPT_RUNBINDABLE:
665 append_option(cxt, "runbindable");
666 propa = 1;
667 break;
668 case MOUNT_OPT_TARGET:
669 mnt_context_disable_swapmatch(cxt, 1);
670 mnt_context_set_target(cxt, optarg);
671 break;
672 case MOUNT_OPT_SOURCE:
673 mnt_context_disable_swapmatch(cxt, 1);
674 mnt_context_set_source(cxt, optarg);
675 break;
676 default:
677 errtryhelp(MNT_EX_USAGE);
678 }
679 }
680
681 argc -= optind;
682 argv += optind;
683
684 if (fstab && !mnt_context_is_nocanonicalize(cxt)) {
685 /*
686 * We have external (context independent) fstab instance, let's
687 * make a connection between the fstab and the canonicalization
688 * cache.
689 */
690 mnt_table_set_cache(fstab, mnt_context_get_cache(cxt));
691 }
692
693 if (!mnt_context_get_source(cxt) &&
694 !mnt_context_get_target(cxt) &&
695 !argc &&
696 !all) {
697 if (oper || mnt_context_get_options(cxt))
698 usage(stderr);
699 print_all(cxt, types, show_labels);
700 goto done;
701 }
702
703 /* Non-root users are allowed to use -t to print_all(),
704 but not to mount */
705 if (mnt_context_is_restricted(cxt) && types)
706 exit_non_root("types");
707
708 if (oper && (types || all || mnt_context_get_source(cxt)))
709 usage(stderr);
710
711 if (types && (all || strchr(types, ',') ||
712 strncmp(types, "no", 2) == 0))
713 mnt_context_set_fstype_pattern(cxt, types);
714 else if (types)
715 mnt_context_set_fstype(cxt, types);
716
717 if (all) {
718 /*
719 * A) Mount all
720 */
721 rc = mount_all(cxt);
722 goto done;
723
724 } else if (argc == 0 && (mnt_context_get_source(cxt) ||
725 mnt_context_get_target(cxt))) {
726 /*
727 * B) mount -L|-U|--source|--target
728 *
729 * non-root may specify source *or* target, but not both
730 */
731 if (mnt_context_is_restricted(cxt) &&
732 mnt_context_get_source(cxt) &&
733 mnt_context_get_target(cxt))
734 exit_non_root(NULL);
735
736 } else if (argc == 1 && (!mnt_context_get_source(cxt) ||
737 !mnt_context_get_target(cxt))) {
738 /*
739 * C) mount [-L|-U|--source] <target>
740 * mount [--target <dir>] <source>
741 * mount <source|target>
742 *
743 * non-root may specify source *or* target, but not both
744 *
745 * It does not matter for libmount if we set source or target
746 * here (the library is able to swap it), but it matters for
747 * sanitize_paths().
748 */
749 int istag = mnt_tag_is_valid(argv[0]);
750
751 if (istag && mnt_context_get_source(cxt))
752 /* -L, -U or --source together with LABEL= or UUID= */
753 errx(MNT_EX_USAGE, _("source specified more than once"));
754 else if (istag || mnt_context_get_target(cxt))
755 mnt_context_set_source(cxt, argv[0]);
756 else
757 mnt_context_set_target(cxt, argv[0]);
758
759 if (mnt_context_is_restricted(cxt) &&
760 mnt_context_get_source(cxt) &&
761 mnt_context_get_target(cxt))
762 exit_non_root(NULL);
763
764 } else if (argc == 2 && !mnt_context_get_source(cxt)
765 && !mnt_context_get_target(cxt)) {
766 /*
767 * D) mount <source> <target>
768 */
769 if (mnt_context_is_restricted(cxt))
770 exit_non_root(NULL);
771
772 mnt_context_set_source(cxt, argv[0]);
773 mnt_context_set_target(cxt, argv[1]);
774
775 } else
776 usage(stderr);
777
778 if (mnt_context_is_restricted(cxt))
779 sanitize_paths(cxt);
780
781 if (oper)
782 /* BIND/MOVE operations, let's set the mount flags */
783 mnt_context_set_mflags(cxt, oper);
784
785 if ((oper && !has_remount_flag(cxt)) || propa)
786 /* For --make-* or --bind is fstab/mtab unnecessary */
787 mnt_context_set_optsmode(cxt, MNT_OMODE_NOTAB);
788
789 rc = mnt_context_mount(cxt);
790 rc = mk_exit_code(cxt, rc);
791
792 if (rc == MNT_EX_SUCCESS && mnt_context_is_verbose(cxt))
793 success_message(cxt);
794 done:
795 mnt_free_context(cxt);
796 return rc;
797 }
798