]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/mount.c
su: change error message
[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) && !mnt_fs_is_netfs(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(void)
388 {
389 FILE *out = stdout;
390 fputs(USAGE_HEADER, out);
391 fprintf(out, _(
392 " %1$s [-lhV]\n"
393 " %1$s -a [options]\n"
394 " %1$s [options] [--source] <source> | [--target] <directory>\n"
395 " %1$s [options] <source> <directory>\n"
396 " %1$s <operation> <mountpoint> [<target>]\n"),
397 program_invocation_short_name);
398
399 fputs(USAGE_SEPARATOR, out);
400 fputs(_("Mount a filesystem.\n"), out);
401
402 fputs(USAGE_OPTIONS, out);
403 fprintf(out, _(
404 " -a, --all mount all filesystems mentioned in fstab\n"
405 " -c, --no-canonicalize don't canonicalize paths\n"
406 " -f, --fake dry run; skip the mount(2) syscall\n"
407 " -F, --fork fork off for each device (use with -a)\n"
408 " -T, --fstab <path> alternative file to /etc/fstab\n"));
409 fprintf(out, _(
410 " -i, --internal-only don't call the mount.<type> helpers\n"));
411 fprintf(out, _(
412 " -l, --show-labels show also filesystem labels\n"));
413 fprintf(out, _(
414 " -n, --no-mtab don't write to /etc/mtab\n"));
415 fprintf(out, _(
416 " --options-mode <mode>\n"
417 " what to do with options loaded from fstab\n"
418 " --options-source <source>\n"
419 " mount options source\n"
420 " --options-source-force\n"
421 " force use of options from fstab/mtab\n"));
422 fprintf(out, _(
423 " -o, --options <list> comma-separated list of mount options\n"
424 " -O, --test-opts <list> limit the set of filesystems (use with -a)\n"
425 " -r, --read-only mount the filesystem read-only (same as -o ro)\n"
426 " -t, --types <list> limit the set of filesystem types\n"));
427 fprintf(out, _(
428 " --source <src> explicitly specifies source (path, label, uuid)\n"
429 " --target <target> explicitly specifies mountpoint\n"));
430 fprintf(out, _(
431 " -v, --verbose say what is being done\n"));
432 fprintf(out, _(
433 " -w, --rw, --read-write mount the filesystem read-write (default)\n"));
434 fprintf(out, _(
435 " -N, --namespace <ns> perform mount in another namespace\n"));
436
437 fputs(USAGE_SEPARATOR, out);
438 printf(USAGE_HELP_OPTIONS(25));
439
440 fprintf(out, _(
441 "\nSource:\n"
442 " -L, --label <label> synonym for LABEL=<label>\n"
443 " -U, --uuid <uuid> synonym for UUID=<uuid>\n"
444 " LABEL=<label> specifies device by filesystem label\n"
445 " UUID=<uuid> specifies device by filesystem UUID\n"
446 " PARTLABEL=<label> specifies device by partition label\n"
447 " PARTUUID=<uuid> specifies device by partition UUID\n"));
448
449 fprintf(out, _(
450 " <device> specifies device by path\n"
451 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
452 " <file> regular file for loopdev setup\n"));
453
454 fprintf(out, _(
455 "\nOperations:\n"
456 " -B, --bind mount a subtree somewhere else (same as -o bind)\n"
457 " -M, --move move a subtree to some other place\n"
458 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
459 fprintf(out, _(
460 " --make-shared mark a subtree as shared\n"
461 " --make-slave mark a subtree as slave\n"
462 " --make-private mark a subtree as private\n"
463 " --make-unbindable mark a subtree as unbindable\n"));
464 fprintf(out, _(
465 " --make-rshared recursively mark a whole subtree as shared\n"
466 " --make-rslave recursively mark a whole subtree as slave\n"
467 " --make-rprivate recursively mark a whole subtree as private\n"
468 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
469
470 printf(USAGE_MAN_TAIL("mount(8)"));
471
472 exit(MNT_EX_SUCCESS);
473 }
474
475 struct flag_str {
476 int value;
477 char *str;
478 };
479
480 static int omode2mask(const char *str)
481 {
482 size_t i;
483
484 static const struct flag_str flags[] = {
485 { MNT_OMODE_IGNORE, "ignore" },
486 { MNT_OMODE_APPEND, "append" },
487 { MNT_OMODE_PREPEND, "prepend" },
488 { MNT_OMODE_REPLACE, "replace" },
489 };
490
491 for (i = 0; i < ARRAY_SIZE(flags); i++) {
492 if (!strcmp(str, flags[i].str))
493 return flags[i].value;
494 }
495 return -EINVAL;
496 }
497
498 static long osrc2mask(const char *str, size_t len)
499 {
500 size_t i;
501
502 static const struct flag_str flags[] = {
503 { MNT_OMODE_FSTAB, "fstab" },
504 { MNT_OMODE_MTAB, "mtab" },
505 { MNT_OMODE_NOTAB, "disable" },
506 };
507
508 for (i = 0; i < ARRAY_SIZE(flags); i++) {
509 if (!strncmp(str, flags[i].str, len) && !flags[i].str[len])
510 return flags[i].value;
511 }
512 return -EINVAL;
513 }
514
515 static pid_t parse_pid(const char *str)
516 {
517 char *end;
518 pid_t ret;
519
520 errno = 0;
521 ret = strtoul(str, &end, 10);
522
523 if (ret < 0 || errno || end == str || (end && *end))
524 return 0;
525 return ret;
526 }
527
528 int main(int argc, char **argv)
529 {
530 int c, rc = MNT_EX_SUCCESS, all = 0, show_labels = 0;
531 struct libmnt_context *cxt;
532 struct libmnt_table *fstab = NULL;
533 char *srcbuf = NULL;
534 char *types = NULL;
535 int oper = 0, is_move = 0;
536 int propa = 0;
537 int optmode = 0, optmode_mode = 0, optmode_src = 0;
538
539 enum {
540 MOUNT_OPT_SHARED = CHAR_MAX + 1,
541 MOUNT_OPT_SLAVE,
542 MOUNT_OPT_PRIVATE,
543 MOUNT_OPT_UNBINDABLE,
544 MOUNT_OPT_RSHARED,
545 MOUNT_OPT_RSLAVE,
546 MOUNT_OPT_RPRIVATE,
547 MOUNT_OPT_RUNBINDABLE,
548 MOUNT_OPT_TARGET,
549 MOUNT_OPT_SOURCE,
550 MOUNT_OPT_OPTMODE,
551 MOUNT_OPT_OPTSRC,
552 MOUNT_OPT_OPTSRC_FORCE
553 };
554
555 static const struct option longopts[] = {
556 { "all", no_argument, NULL, 'a' },
557 { "fake", no_argument, NULL, 'f' },
558 { "fstab", required_argument, NULL, 'T' },
559 { "fork", no_argument, NULL, 'F' },
560 { "help", no_argument, NULL, 'h' },
561 { "no-mtab", no_argument, NULL, 'n' },
562 { "read-only", no_argument, NULL, 'r' },
563 { "ro", no_argument, NULL, 'r' },
564 { "verbose", no_argument, NULL, 'v' },
565 { "version", no_argument, NULL, 'V' },
566 { "read-write", no_argument, NULL, 'w' },
567 { "rw", no_argument, NULL, 'w' },
568 { "options", required_argument, NULL, 'o' },
569 { "test-opts", required_argument, NULL, 'O' },
570 { "types", required_argument, NULL, 't' },
571 { "uuid", required_argument, NULL, 'U' },
572 { "label", required_argument, NULL, 'L' },
573 { "bind", no_argument, NULL, 'B' },
574 { "move", no_argument, NULL, 'M' },
575 { "rbind", no_argument, NULL, 'R' },
576 { "make-shared", no_argument, NULL, MOUNT_OPT_SHARED },
577 { "make-slave", no_argument, NULL, MOUNT_OPT_SLAVE },
578 { "make-private", no_argument, NULL, MOUNT_OPT_PRIVATE },
579 { "make-unbindable", no_argument, NULL, MOUNT_OPT_UNBINDABLE },
580 { "make-rshared", no_argument, NULL, MOUNT_OPT_RSHARED },
581 { "make-rslave", no_argument, NULL, MOUNT_OPT_RSLAVE },
582 { "make-rprivate", no_argument, NULL, MOUNT_OPT_RPRIVATE },
583 { "make-runbindable", no_argument, NULL, MOUNT_OPT_RUNBINDABLE },
584 { "no-canonicalize", no_argument, NULL, 'c' },
585 { "internal-only", no_argument, NULL, 'i' },
586 { "show-labels", no_argument, NULL, 'l' },
587 { "target", required_argument, NULL, MOUNT_OPT_TARGET },
588 { "source", required_argument, NULL, MOUNT_OPT_SOURCE },
589 { "options-mode", required_argument, NULL, MOUNT_OPT_OPTMODE },
590 { "options-source", required_argument, NULL, MOUNT_OPT_OPTSRC },
591 { "options-source-force", no_argument, NULL, MOUNT_OPT_OPTSRC_FORCE},
592 { "namespace", required_argument, NULL, 'N' },
593 { NULL, 0, NULL, 0 }
594 };
595
596 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
597 { 'B','M','R' }, /* bind,move,rbind */
598 { 'L','U', MOUNT_OPT_SOURCE }, /* label,uuid,source */
599 { 0 }
600 };
601 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
602
603 sanitize_env();
604 setlocale(LC_ALL, "");
605 bindtextdomain(PACKAGE, LOCALEDIR);
606 textdomain(PACKAGE);
607 atexit(close_stdout);
608
609 strutils_set_exitcode(MNT_EX_USAGE);
610
611 mnt_init_debug(0);
612 cxt = mnt_new_context();
613 if (!cxt)
614 err(MNT_EX_SYSERR, _("libmount context allocation failed"));
615
616 mnt_context_set_tables_errcb(cxt, table_parser_errcb);
617
618 while ((c = getopt_long(argc, argv, "aBcfFhilL:Mno:O:rRsU:vVwt:T:N:",
619 longopts, NULL)) != -1) {
620
621 /* only few options are allowed for non-root users */
622 if (mnt_context_is_restricted(cxt) &&
623 !strchr("hlLUVvrist", c) &&
624 c != MOUNT_OPT_TARGET &&
625 c != MOUNT_OPT_SOURCE)
626 exit_non_root(option_to_longopt(c, longopts));
627
628 err_exclusive_options(c, longopts, excl, excl_st);
629
630 switch(c) {
631 case 'a':
632 all = 1;
633 break;
634 case 'c':
635 mnt_context_disable_canonicalize(cxt, TRUE);
636 break;
637 case 'f':
638 mnt_context_enable_fake(cxt, TRUE);
639 break;
640 case 'F':
641 mnt_context_enable_fork(cxt, TRUE);
642 break;
643 case 'h':
644 usage();
645 break;
646 case 'i':
647 mnt_context_disable_helpers(cxt, TRUE);
648 break;
649 case 'n':
650 mnt_context_disable_mtab(cxt, TRUE);
651 break;
652 case 'r':
653 append_option(cxt, "ro");
654 mnt_context_enable_rwonly_mount(cxt, FALSE);
655 break;
656 case 'v':
657 mnt_context_enable_verbose(cxt, TRUE);
658 break;
659 case 'V':
660 print_version();
661 break;
662 case 'w':
663 append_option(cxt, "rw");
664 mnt_context_enable_rwonly_mount(cxt, TRUE);
665 break;
666 case 'o':
667 append_option(cxt, optarg);
668 break;
669 case 'O':
670 if (mnt_context_set_options_pattern(cxt, optarg))
671 err(MNT_EX_SYSERR, _("failed to set options pattern"));
672 break;
673 case 'L':
674 xasprintf(&srcbuf, "LABEL=\"%s\"", optarg);
675 mnt_context_disable_swapmatch(cxt, 1);
676 mnt_context_set_source(cxt, srcbuf);
677 free(srcbuf);
678 break;
679 case 'U':
680 xasprintf(&srcbuf, "UUID=\"%s\"", optarg);
681 mnt_context_disable_swapmatch(cxt, 1);
682 mnt_context_set_source(cxt, srcbuf);
683 free(srcbuf);
684 break;
685 case 'l':
686 show_labels = 1;
687 break;
688 case 't':
689 types = optarg;
690 break;
691 case 'T':
692 fstab = append_fstab(cxt, fstab, optarg);
693 break;
694 case 's':
695 mnt_context_enable_sloppy(cxt, TRUE);
696 break;
697 case 'B':
698 oper = 1;
699 append_option(cxt, "bind");
700 break;
701 case 'M':
702 oper = 1;
703 is_move = 1;
704 break;
705 case 'R':
706 oper = 1;
707 append_option(cxt, "rbind");
708 break;
709 case 'N':
710 {
711 char path[PATH_MAX];
712 pid_t pid = parse_pid(optarg);
713
714 if (pid)
715 snprintf(path, sizeof(path), "/proc/%i/ns/mnt", pid);
716
717 if (mnt_context_set_target_ns(cxt, pid ? path : optarg))
718 err(MNT_EX_SYSERR, _("failed to set target namespace to %s"), pid ? path : optarg);
719 break;
720 }
721 case MOUNT_OPT_SHARED:
722 append_option(cxt, "shared");
723 propa = 1;
724 break;
725 case MOUNT_OPT_SLAVE:
726 append_option(cxt, "slave");
727 propa = 1;
728 break;
729 case MOUNT_OPT_PRIVATE:
730 append_option(cxt, "private");
731 propa = 1;
732 break;
733 case MOUNT_OPT_UNBINDABLE:
734 append_option(cxt, "unbindable");
735 propa = 1;
736 break;
737 case MOUNT_OPT_RSHARED:
738 append_option(cxt, "rshared");
739 propa = 1;
740 break;
741 case MOUNT_OPT_RSLAVE:
742 append_option(cxt, "rslave");
743 propa = 1;
744 break;
745 case MOUNT_OPT_RPRIVATE:
746 append_option(cxt, "rprivate");
747 propa = 1;
748 break;
749 case MOUNT_OPT_RUNBINDABLE:
750 append_option(cxt, "runbindable");
751 propa = 1;
752 break;
753 case MOUNT_OPT_TARGET:
754 mnt_context_disable_swapmatch(cxt, 1);
755 mnt_context_set_target(cxt, optarg);
756 break;
757 case MOUNT_OPT_SOURCE:
758 mnt_context_disable_swapmatch(cxt, 1);
759 mnt_context_set_source(cxt, optarg);
760 break;
761 case MOUNT_OPT_OPTMODE:
762 optmode_mode = omode2mask(optarg);
763 if (optmode_mode == -EINVAL) {
764 warnx(_("bad usage"));
765 errtryhelp(MNT_EX_USAGE);
766 }
767 break;
768 case MOUNT_OPT_OPTSRC:
769 {
770 unsigned long tmp = 0;
771 if (string_to_bitmask(optarg, &tmp, osrc2mask)) {
772 warnx(_("bad usage"));
773 errtryhelp(MNT_EX_USAGE);
774 }
775 optmode_src = tmp;
776 break;
777 }
778 case MOUNT_OPT_OPTSRC_FORCE:
779 optmode |= MNT_OMODE_FORCE;
780 break;
781 default:
782 errtryhelp(MNT_EX_USAGE);
783 }
784 }
785
786 argc -= optind;
787 argv += optind;
788
789 optmode |= optmode_mode | optmode_src;
790 if (optmode) {
791 if (!optmode_mode)
792 optmode |= MNT_OMODE_PREPEND;
793 if (!optmode_src)
794 optmode |= MNT_OMODE_FSTAB | MNT_OMODE_MTAB;
795 mnt_context_set_optsmode(cxt, optmode);
796 }
797
798 if (fstab && !mnt_context_is_nocanonicalize(cxt)) {
799 /*
800 * We have external (context independent) fstab instance, let's
801 * make a connection between the fstab and the canonicalization
802 * cache.
803 */
804 mnt_table_set_cache(fstab, mnt_context_get_cache(cxt));
805 }
806
807 if (!mnt_context_get_source(cxt) &&
808 !mnt_context_get_target(cxt) &&
809 !argc &&
810 !all) {
811 if (oper || mnt_context_get_options(cxt)) {
812 warnx(_("bad usage"));
813 errtryhelp(MNT_EX_USAGE);
814 }
815 print_all(cxt, types, show_labels);
816 goto done;
817 }
818
819 /* Non-root users are allowed to use -t to print_all(),
820 but not to mount */
821 if (mnt_context_is_restricted(cxt) && types)
822 exit_non_root("types");
823
824 if (oper && (types || all || mnt_context_get_source(cxt))) {
825 warnx(_("bad usage"));
826 errtryhelp(MNT_EX_USAGE);
827 }
828
829 if (types && (all || strchr(types, ',') ||
830 strncmp(types, "no", 2) == 0))
831 mnt_context_set_fstype_pattern(cxt, types);
832 else if (types)
833 mnt_context_set_fstype(cxt, types);
834
835 if (all) {
836 /*
837 * A) Mount all
838 */
839 rc = mount_all(cxt);
840 goto done;
841
842 } else if (argc == 0 && (mnt_context_get_source(cxt) ||
843 mnt_context_get_target(cxt))) {
844 /*
845 * B) mount -L|-U|--source|--target
846 *
847 * non-root may specify source *or* target, but not both
848 */
849 if (mnt_context_is_restricted(cxt) &&
850 mnt_context_get_source(cxt) &&
851 mnt_context_get_target(cxt))
852 exit_non_root(NULL);
853
854 } else if (argc == 1 && (!mnt_context_get_source(cxt) ||
855 !mnt_context_get_target(cxt))) {
856 /*
857 * C) mount [-L|-U|--source] <target>
858 * mount [--target <dir>] <source>
859 * mount <source|target>
860 *
861 * non-root may specify source *or* target, but not both
862 *
863 * It does not matter for libmount if we set source or target
864 * here (the library is able to swap it), but it matters for
865 * sanitize_paths().
866 */
867 int istag = mnt_tag_is_valid(argv[0]);
868
869 if (istag && mnt_context_get_source(cxt))
870 /* -L, -U or --source together with LABEL= or UUID= */
871 errx(MNT_EX_USAGE, _("source specified more than once"));
872 else if (istag || mnt_context_get_target(cxt))
873 mnt_context_set_source(cxt, argv[0]);
874 else
875 mnt_context_set_target(cxt, argv[0]);
876
877 if (mnt_context_is_restricted(cxt) &&
878 mnt_context_get_source(cxt) &&
879 mnt_context_get_target(cxt))
880 exit_non_root(NULL);
881
882 } else if (argc == 2 && !mnt_context_get_source(cxt)
883 && !mnt_context_get_target(cxt)) {
884 /*
885 * D) mount <source> <target>
886 */
887 if (mnt_context_is_restricted(cxt))
888 exit_non_root(NULL);
889
890 mnt_context_set_source(cxt, argv[0]);
891 mnt_context_set_target(cxt, argv[1]);
892
893 } else {
894 warnx(_("bad usage"));
895 errtryhelp(MNT_EX_USAGE);
896 }
897
898 if (mnt_context_is_restricted(cxt))
899 sanitize_paths(cxt);
900
901 if (is_move)
902 /* "move" as option string is not supported by libmount */
903 mnt_context_set_mflags(cxt, MS_MOVE);
904
905 if ((oper && !has_remount_flag(cxt)) || propa)
906 /* For --make-* or --bind is fstab/mtab unnecessary */
907 mnt_context_set_optsmode(cxt, MNT_OMODE_NOTAB);
908
909 rc = mnt_context_mount(cxt);
910 rc = mk_exit_code(cxt, rc);
911
912 if (rc == MNT_EX_SUCCESS && mnt_context_is_verbose(cxt))
913 success_message(cxt);
914 done:
915 mnt_free_context(cxt);
916 return rc;
917 }
918