]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/mount.c
cleanup: Remove some spurious spaces
[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__)) mount_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
235 /*
236 * mount -a -o remount
237 */
238 static int remount_all(struct libmnt_context *cxt)
239 {
240 struct libmnt_iter *itr;
241 struct libmnt_fs *fs;
242 int mntrc, ignored, rc = MNT_EX_SUCCESS;
243
244 int nsucc = 0, nerrs = 0;
245
246 itr = mnt_new_iter(MNT_ITER_FORWARD);
247 if (!itr) {
248 warn(_("failed to initialize libmount iterator"));
249 return MNT_EX_SYSERR;
250 }
251
252 while (mnt_context_next_remount(cxt, itr, &fs, &mntrc, &ignored) == 0) {
253
254 const char *tgt = mnt_fs_get_target(fs);
255
256 if (ignored) {
257 if (mnt_context_is_verbose(cxt))
258 printf(_("%-25s: ignored\n"), tgt);
259 } else {
260 if (mk_exit_code(cxt, mntrc) == MNT_EX_SUCCESS) {
261 nsucc++;
262
263 /* Note that MNT_EX_SUCCESS return code does
264 * not mean that FS has been really mounted
265 * (e.g. nofail option) */
266 if (mnt_context_get_status(cxt)
267 && mnt_context_is_verbose(cxt))
268 printf("%-25s: successfully remounted\n", tgt);
269 } else
270 nerrs++;
271 }
272 }
273
274 if (nerrs == 0)
275 rc = MNT_EX_SUCCESS; /* all success */
276 else if (nsucc == 0)
277 rc = MNT_EX_FAIL; /* all failed */
278 else
279 rc = MNT_EX_SOMEOK; /* some success, some failed */
280
281 mnt_free_iter(itr);
282 return rc;
283 }
284
285 static void success_message(struct libmnt_context *cxt)
286 {
287 unsigned long mflags = 0;
288 const char *tgt, *src, *pr = program_invocation_short_name;
289
290 if (mnt_context_helper_executed(cxt)
291 || mnt_context_get_status(cxt) != 1)
292 return;
293
294 mnt_context_get_mflags(cxt, &mflags);
295 tgt = mnt_context_get_target(cxt);
296 src = mnt_context_get_source(cxt);
297
298 if (mflags & MS_MOVE)
299 printf(_("%s: %s moved to %s.\n"), pr, src, tgt);
300 else if (mflags & MS_BIND)
301 printf(_("%s: %s bound on %s.\n"), pr, src, tgt);
302 else if (mflags & MS_PROPAGATION) {
303 if (src && strcmp(src, "none") != 0 && tgt)
304 printf(_("%s: %s mounted on %s.\n"), pr, src, tgt);
305
306 printf(_("%s: %s propagation flags changed.\n"), pr, tgt);
307 } else
308 printf(_("%s: %s mounted on %s.\n"), pr, src, tgt);
309 }
310
311 #if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
312 #include <selinux/selinux.h>
313 #include <selinux/context.h>
314
315 static void selinux_warning(struct libmnt_context *cxt, const char *tgt)
316 {
317
318 if (tgt && mnt_context_is_verbose(cxt) && is_selinux_enabled() > 0) {
319 security_context_t raw = NULL, def = NULL;
320
321 if (getfilecon(tgt, &raw) > 0
322 && security_get_initial_context("file", &def) == 0) {
323
324 if (!selinux_file_context_cmp(raw, def))
325 printf(_(
326 "mount: %s does not contain SELinux labels.\n"
327 " You just mounted an file system that supports labels which does not\n"
328 " contain labels, onto an SELinux box. It is likely that confined\n"
329 " applications will generate AVC messages and not be allowed access to\n"
330 " this file system. For more details see restorecon(8) and mount(8).\n"),
331 tgt);
332 }
333 freecon(raw);
334 freecon(def);
335 }
336 }
337 #else
338 # define selinux_warning(_x, _y)
339 #endif
340
341 /*
342 * Returns exit status (MNT_EX_*) and/or prints error message.
343 */
344 static int mk_exit_code(struct libmnt_context *cxt, int rc)
345 {
346 const char *tgt;
347 char buf[BUFSIZ] = { 0 };
348
349 rc = mnt_context_get_excode(cxt, rc, buf, sizeof(buf));
350 tgt = mnt_context_get_target(cxt);
351
352 if (*buf) {
353 const char *spec = tgt;
354 if (!spec)
355 spec = mnt_context_get_source(cxt);
356 if (!spec)
357 spec = "???";
358 warnx("%s: %s.", spec, buf);
359 }
360
361 if (rc == MNT_EX_SUCCESS && mnt_context_get_status(cxt) == 1) {
362 selinux_warning(cxt, tgt);
363 }
364 return rc;
365 }
366
367 static struct libmnt_table *append_fstab(struct libmnt_context *cxt,
368 struct libmnt_table *fstab,
369 const char *path)
370 {
371
372 if (!fstab) {
373 fstab = mnt_new_table();
374 if (!fstab)
375 err(MNT_EX_SYSERR, _("failed to initialize libmount table"));
376
377 mnt_table_set_parser_errcb(fstab, table_parser_errcb);
378 mnt_context_set_fstab(cxt, fstab);
379
380 mnt_unref_table(fstab); /* reference is handled by @cxt now */
381 }
382
383 if (mnt_table_parse_fstab(fstab, path))
384 errx(MNT_EX_USAGE,_("%s: failed to parse"), path);
385
386 return fstab;
387 }
388
389 /*
390 * Check source and target paths -- non-root user should not be able to
391 * resolve paths which are unreadable for him.
392 */
393 static void sanitize_paths(struct libmnt_context *cxt)
394 {
395 const char *p;
396 struct libmnt_fs *fs = mnt_context_get_fs(cxt);
397
398 if (!fs)
399 return;
400
401 p = mnt_fs_get_target(fs);
402 if (p) {
403 char *np = canonicalize_path_restricted(p);
404 if (!np)
405 err(MNT_EX_USAGE, "%s", p);
406 mnt_fs_set_target(fs, np);
407 free(np);
408 }
409
410 p = mnt_fs_get_srcpath(fs);
411 if (p) {
412 char *np = canonicalize_path_restricted(p);
413 if (!np)
414 err(MNT_EX_USAGE, "%s", p);
415 mnt_fs_set_source(fs, np);
416 free(np);
417 }
418 }
419
420 static void append_option(struct libmnt_context *cxt, const char *opt)
421 {
422 if (opt && (*opt == '=' || *opt == '\'' || *opt == '\"' || isblank(*opt)))
423 errx(MNT_EX_USAGE, _("unsupported option format: %s"), opt);
424 if (mnt_context_append_options(cxt, opt))
425 err(MNT_EX_SYSERR, _("failed to append option '%s'"), opt);
426 }
427
428 static int has_remount_flag(struct libmnt_context *cxt)
429 {
430 unsigned long mflags = 0;
431
432 if (mnt_context_get_mflags(cxt, &mflags))
433 return 0;
434
435 return mflags & MS_REMOUNT;
436 }
437
438 static void __attribute__((__noreturn__)) usage(void)
439 {
440 FILE *out = stdout;
441 fputs(USAGE_HEADER, out);
442 fprintf(out, _(
443 " %1$s [-lhV]\n"
444 " %1$s -a [options]\n"
445 " %1$s [options] [--source] <source> | [--target] <directory>\n"
446 " %1$s [options] <source> <directory>\n"
447 " %1$s <operation> <mountpoint> [<target>]\n"),
448 program_invocation_short_name);
449
450 fputs(USAGE_SEPARATOR, out);
451 fputs(_("Mount a filesystem.\n"), out);
452
453 fputs(USAGE_OPTIONS, out);
454 fprintf(out, _(
455 " -a, --all mount all filesystems mentioned in fstab\n"
456 " -c, --no-canonicalize don't canonicalize paths\n"
457 " -f, --fake dry run; skip the mount(2) syscall\n"
458 " -F, --fork fork off for each device (use with -a)\n"
459 " -T, --fstab <path> alternative file to /etc/fstab\n"));
460 fprintf(out, _(
461 " -i, --internal-only don't call the mount.<type> helpers\n"));
462 fprintf(out, _(
463 " -l, --show-labels show also filesystem labels\n"));
464 fprintf(out, _(
465 " -n, --no-mtab don't write to /etc/mtab\n"));
466 fprintf(out, _(
467 " --options-mode <mode>\n"
468 " what to do with options loaded from fstab\n"
469 " --options-source <source>\n"
470 " mount options source\n"
471 " --options-source-force\n"
472 " force use of options from fstab/mtab\n"));
473 fprintf(out, _(
474 " -o, --options <list> comma-separated list of mount options\n"
475 " -O, --test-opts <list> limit the set of filesystems (use with -a)\n"
476 " -r, --read-only mount the filesystem read-only (same as -o ro)\n"
477 " -t, --types <list> limit the set of filesystem types\n"));
478 fprintf(out, _(
479 " --source <src> explicitly specifies source (path, label, uuid)\n"
480 " --target <target> explicitly specifies mountpoint\n"));
481 fprintf(out, _(
482 " -v, --verbose say what is being done\n"));
483 fprintf(out, _(
484 " -w, --rw, --read-write mount the filesystem read-write (default)\n"));
485 fprintf(out, _(
486 " -N, --namespace <ns> perform mount in another namespace\n"));
487
488 fputs(USAGE_SEPARATOR, out);
489 printf(USAGE_HELP_OPTIONS(25));
490
491 fprintf(out, _(
492 "\nSource:\n"
493 " -L, --label <label> synonym for LABEL=<label>\n"
494 " -U, --uuid <uuid> synonym for UUID=<uuid>\n"
495 " LABEL=<label> specifies device by filesystem label\n"
496 " UUID=<uuid> specifies device by filesystem UUID\n"
497 " PARTLABEL=<label> specifies device by partition label\n"
498 " PARTUUID=<uuid> specifies device by partition UUID\n"));
499
500 fprintf(out, _(
501 " <device> specifies device by path\n"
502 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
503 " <file> regular file for loopdev setup\n"));
504
505 fprintf(out, _(
506 "\nOperations:\n"
507 " -B, --bind mount a subtree somewhere else (same as -o bind)\n"
508 " -M, --move move a subtree to some other place\n"
509 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
510 fprintf(out, _(
511 " --make-shared mark a subtree as shared\n"
512 " --make-slave mark a subtree as slave\n"
513 " --make-private mark a subtree as private\n"
514 " --make-unbindable mark a subtree as unbindable\n"));
515 fprintf(out, _(
516 " --make-rshared recursively mark a whole subtree as shared\n"
517 " --make-rslave recursively mark a whole subtree as slave\n"
518 " --make-rprivate recursively mark a whole subtree as private\n"
519 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
520
521 printf(USAGE_MAN_TAIL("mount(8)"));
522
523 exit(MNT_EX_SUCCESS);
524 }
525
526 struct flag_str {
527 int value;
528 char *str;
529 };
530
531 static int omode2mask(const char *str)
532 {
533 size_t i;
534
535 static const struct flag_str flags[] = {
536 { MNT_OMODE_IGNORE, "ignore" },
537 { MNT_OMODE_APPEND, "append" },
538 { MNT_OMODE_PREPEND, "prepend" },
539 { MNT_OMODE_REPLACE, "replace" },
540 };
541
542 for (i = 0; i < ARRAY_SIZE(flags); i++) {
543 if (!strcmp(str, flags[i].str))
544 return flags[i].value;
545 }
546 return -EINVAL;
547 }
548
549 static long osrc2mask(const char *str, size_t len)
550 {
551 size_t i;
552
553 static const struct flag_str flags[] = {
554 { MNT_OMODE_FSTAB, "fstab" },
555 { MNT_OMODE_MTAB, "mtab" },
556 { MNT_OMODE_NOTAB, "disable" },
557 };
558
559 for (i = 0; i < ARRAY_SIZE(flags); i++) {
560 if (!strncmp(str, flags[i].str, len) && !flags[i].str[len])
561 return flags[i].value;
562 }
563 return -EINVAL;
564 }
565
566 static pid_t parse_pid(const char *str)
567 {
568 char *end;
569 pid_t ret;
570
571 errno = 0;
572 ret = strtoul(str, &end, 10);
573
574 if (ret < 0 || errno || end == str || (end && *end))
575 return 0;
576 return ret;
577 }
578
579 int main(int argc, char **argv)
580 {
581 int c, rc = MNT_EX_SUCCESS, all = 0, show_labels = 0;
582 struct libmnt_context *cxt;
583 struct libmnt_table *fstab = NULL;
584 char *srcbuf = NULL;
585 char *types = NULL;
586 int oper = 0, is_move = 0;
587 int propa = 0;
588 int optmode = 0, optmode_mode = 0, optmode_src = 0;
589
590 enum {
591 MOUNT_OPT_SHARED = CHAR_MAX + 1,
592 MOUNT_OPT_SLAVE,
593 MOUNT_OPT_PRIVATE,
594 MOUNT_OPT_UNBINDABLE,
595 MOUNT_OPT_RSHARED,
596 MOUNT_OPT_RSLAVE,
597 MOUNT_OPT_RPRIVATE,
598 MOUNT_OPT_RUNBINDABLE,
599 MOUNT_OPT_TARGET,
600 MOUNT_OPT_SOURCE,
601 MOUNT_OPT_OPTMODE,
602 MOUNT_OPT_OPTSRC,
603 MOUNT_OPT_OPTSRC_FORCE
604 };
605
606 static const struct option longopts[] = {
607 { "all", no_argument, NULL, 'a' },
608 { "fake", no_argument, NULL, 'f' },
609 { "fstab", required_argument, NULL, 'T' },
610 { "fork", no_argument, NULL, 'F' },
611 { "help", no_argument, NULL, 'h' },
612 { "no-mtab", no_argument, NULL, 'n' },
613 { "read-only", no_argument, NULL, 'r' },
614 { "ro", no_argument, NULL, 'r' },
615 { "verbose", no_argument, NULL, 'v' },
616 { "version", no_argument, NULL, 'V' },
617 { "read-write", no_argument, NULL, 'w' },
618 { "rw", no_argument, NULL, 'w' },
619 { "options", required_argument, NULL, 'o' },
620 { "test-opts", required_argument, NULL, 'O' },
621 { "types", required_argument, NULL, 't' },
622 { "uuid", required_argument, NULL, 'U' },
623 { "label", required_argument, NULL, 'L' },
624 { "bind", no_argument, NULL, 'B' },
625 { "move", no_argument, NULL, 'M' },
626 { "rbind", no_argument, NULL, 'R' },
627 { "make-shared", no_argument, NULL, MOUNT_OPT_SHARED },
628 { "make-slave", no_argument, NULL, MOUNT_OPT_SLAVE },
629 { "make-private", no_argument, NULL, MOUNT_OPT_PRIVATE },
630 { "make-unbindable", no_argument, NULL, MOUNT_OPT_UNBINDABLE },
631 { "make-rshared", no_argument, NULL, MOUNT_OPT_RSHARED },
632 { "make-rslave", no_argument, NULL, MOUNT_OPT_RSLAVE },
633 { "make-rprivate", no_argument, NULL, MOUNT_OPT_RPRIVATE },
634 { "make-runbindable", no_argument, NULL, MOUNT_OPT_RUNBINDABLE },
635 { "no-canonicalize", no_argument, NULL, 'c' },
636 { "internal-only", no_argument, NULL, 'i' },
637 { "show-labels", no_argument, NULL, 'l' },
638 { "target", required_argument, NULL, MOUNT_OPT_TARGET },
639 { "source", required_argument, NULL, MOUNT_OPT_SOURCE },
640 { "options-mode", required_argument, NULL, MOUNT_OPT_OPTMODE },
641 { "options-source", required_argument, NULL, MOUNT_OPT_OPTSRC },
642 { "options-source-force", no_argument, NULL, MOUNT_OPT_OPTSRC_FORCE},
643 { "namespace", required_argument, NULL, 'N' },
644 { NULL, 0, NULL, 0 }
645 };
646
647 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
648 { 'B','M','R' }, /* bind,move,rbind */
649 { 'L','U', MOUNT_OPT_SOURCE }, /* label,uuid,source */
650 { 0 }
651 };
652 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
653
654 sanitize_env();
655 setlocale(LC_ALL, "");
656 bindtextdomain(PACKAGE, LOCALEDIR);
657 textdomain(PACKAGE);
658 close_stdout_atexit();
659
660 strutils_set_exitcode(MNT_EX_USAGE);
661
662 mnt_init_debug(0);
663 cxt = mnt_new_context();
664 if (!cxt)
665 err(MNT_EX_SYSERR, _("libmount context allocation failed"));
666
667 mnt_context_set_tables_errcb(cxt, table_parser_errcb);
668
669 while ((c = getopt_long(argc, argv, "aBcfFhilL:Mno:O:rRsU:vVwt:T:N:",
670 longopts, NULL)) != -1) {
671
672 /* only few options are allowed for non-root users */
673 if (mnt_context_is_restricted(cxt) &&
674 !strchr("hlLUVvrist", c) &&
675 c != MOUNT_OPT_TARGET &&
676 c != MOUNT_OPT_SOURCE)
677 exit_non_root(option_to_longopt(c, longopts));
678
679 err_exclusive_options(c, longopts, excl, excl_st);
680
681 switch(c) {
682 case 'a':
683 all = 1;
684 break;
685 case 'c':
686 mnt_context_disable_canonicalize(cxt, TRUE);
687 break;
688 case 'f':
689 mnt_context_enable_fake(cxt, TRUE);
690 break;
691 case 'F':
692 mnt_context_enable_fork(cxt, TRUE);
693 break;
694 case 'i':
695 mnt_context_disable_helpers(cxt, TRUE);
696 break;
697 case 'n':
698 mnt_context_disable_mtab(cxt, TRUE);
699 break;
700 case 'r':
701 append_option(cxt, "ro");
702 mnt_context_enable_rwonly_mount(cxt, FALSE);
703 break;
704 case 'v':
705 mnt_context_enable_verbose(cxt, TRUE);
706 break;
707 case 'w':
708 append_option(cxt, "rw");
709 mnt_context_enable_rwonly_mount(cxt, TRUE);
710 break;
711 case 'o':
712 append_option(cxt, optarg);
713 break;
714 case 'O':
715 if (mnt_context_set_options_pattern(cxt, optarg))
716 err(MNT_EX_SYSERR, _("failed to set options pattern"));
717 break;
718 case 'L':
719 xasprintf(&srcbuf, "LABEL=\"%s\"", optarg);
720 mnt_context_disable_swapmatch(cxt, 1);
721 mnt_context_set_source(cxt, srcbuf);
722 free(srcbuf);
723 break;
724 case 'U':
725 xasprintf(&srcbuf, "UUID=\"%s\"", optarg);
726 mnt_context_disable_swapmatch(cxt, 1);
727 mnt_context_set_source(cxt, srcbuf);
728 free(srcbuf);
729 break;
730 case 'l':
731 show_labels = 1;
732 break;
733 case 't':
734 types = optarg;
735 break;
736 case 'T':
737 fstab = append_fstab(cxt, fstab, optarg);
738 break;
739 case 's':
740 mnt_context_enable_sloppy(cxt, TRUE);
741 break;
742 case 'B':
743 oper = 1;
744 append_option(cxt, "bind");
745 break;
746 case 'M':
747 oper = 1;
748 is_move = 1;
749 break;
750 case 'R':
751 oper = 1;
752 append_option(cxt, "rbind");
753 break;
754 case 'N':
755 {
756 char path[PATH_MAX];
757 pid_t pid = parse_pid(optarg);
758
759 if (pid)
760 snprintf(path, sizeof(path), "/proc/%i/ns/mnt", pid);
761
762 if (mnt_context_set_target_ns(cxt, pid ? path : optarg))
763 err(MNT_EX_SYSERR, _("failed to set target namespace to %s"), pid ? path : optarg);
764 break;
765 }
766 case MOUNT_OPT_SHARED:
767 append_option(cxt, "shared");
768 propa = 1;
769 break;
770 case MOUNT_OPT_SLAVE:
771 append_option(cxt, "slave");
772 propa = 1;
773 break;
774 case MOUNT_OPT_PRIVATE:
775 append_option(cxt, "private");
776 propa = 1;
777 break;
778 case MOUNT_OPT_UNBINDABLE:
779 append_option(cxt, "unbindable");
780 propa = 1;
781 break;
782 case MOUNT_OPT_RSHARED:
783 append_option(cxt, "rshared");
784 propa = 1;
785 break;
786 case MOUNT_OPT_RSLAVE:
787 append_option(cxt, "rslave");
788 propa = 1;
789 break;
790 case MOUNT_OPT_RPRIVATE:
791 append_option(cxt, "rprivate");
792 propa = 1;
793 break;
794 case MOUNT_OPT_RUNBINDABLE:
795 append_option(cxt, "runbindable");
796 propa = 1;
797 break;
798 case MOUNT_OPT_TARGET:
799 mnt_context_disable_swapmatch(cxt, 1);
800 mnt_context_set_target(cxt, optarg);
801 break;
802 case MOUNT_OPT_SOURCE:
803 mnt_context_disable_swapmatch(cxt, 1);
804 mnt_context_set_source(cxt, optarg);
805 break;
806 case MOUNT_OPT_OPTMODE:
807 optmode_mode = omode2mask(optarg);
808 if (optmode_mode == -EINVAL) {
809 warnx(_("bad usage"));
810 errtryhelp(MNT_EX_USAGE);
811 }
812 break;
813 case MOUNT_OPT_OPTSRC:
814 {
815 unsigned long tmp = 0;
816 if (string_to_bitmask(optarg, &tmp, osrc2mask)) {
817 warnx(_("bad usage"));
818 errtryhelp(MNT_EX_USAGE);
819 }
820 optmode_src = tmp;
821 break;
822 }
823 case MOUNT_OPT_OPTSRC_FORCE:
824 optmode |= MNT_OMODE_FORCE;
825 break;
826
827 case 'h':
828 mnt_free_context(cxt);
829 usage();
830 case 'V':
831 mnt_free_context(cxt);
832 mount_print_version();
833 default:
834 errtryhelp(MNT_EX_USAGE);
835 }
836 }
837
838 argc -= optind;
839 argv += optind;
840
841 optmode |= optmode_mode | optmode_src;
842 if (optmode) {
843 if (!optmode_mode)
844 optmode |= MNT_OMODE_PREPEND;
845 if (!optmode_src)
846 optmode |= MNT_OMODE_FSTAB | MNT_OMODE_MTAB;
847 mnt_context_set_optsmode(cxt, optmode);
848 }
849
850 if (fstab && !mnt_context_is_nocanonicalize(cxt)) {
851 /*
852 * We have external (context independent) fstab instance, let's
853 * make a connection between the fstab and the canonicalization
854 * cache.
855 */
856 mnt_table_set_cache(fstab, mnt_context_get_cache(cxt));
857 }
858
859 if (!mnt_context_get_source(cxt) &&
860 !mnt_context_get_target(cxt) &&
861 !argc &&
862 !all) {
863 if (oper || mnt_context_get_options(cxt)) {
864 warnx(_("bad usage"));
865 errtryhelp(MNT_EX_USAGE);
866 }
867 print_all(cxt, types, show_labels);
868 goto done;
869 }
870
871 /* Non-root users are allowed to use -t to print_all(),
872 but not to mount */
873 if (mnt_context_is_restricted(cxt) && types)
874 exit_non_root("types");
875
876 if (oper && (types || all || mnt_context_get_source(cxt))) {
877 warnx(_("bad usage"));
878 errtryhelp(MNT_EX_USAGE);
879 }
880
881 if (types && (all || strchr(types, ',') ||
882 strncmp(types, "no", 2) == 0))
883 mnt_context_set_fstype_pattern(cxt, types);
884 else if (types)
885 mnt_context_set_fstype(cxt, types);
886
887 if (all) {
888 /*
889 * A) Mount all
890 */
891 if (has_remount_flag(cxt))
892 rc = remount_all(cxt);
893 else
894 rc = mount_all(cxt);
895 goto done;
896
897 } else if (argc == 0 && (mnt_context_get_source(cxt) ||
898 mnt_context_get_target(cxt))) {
899 /*
900 * B) mount -L|-U|--source|--target
901 *
902 * non-root may specify source *or* target, but not both
903 */
904 if (mnt_context_is_restricted(cxt) &&
905 mnt_context_get_source(cxt) &&
906 mnt_context_get_target(cxt))
907 exit_non_root(NULL);
908
909 } else if (argc == 1 && (!mnt_context_get_source(cxt) ||
910 !mnt_context_get_target(cxt))) {
911 /*
912 * C) mount [-L|-U|--source] <target>
913 * mount [--target <dir>] <source>
914 * mount <source|target>
915 *
916 * non-root may specify source *or* target, but not both
917 *
918 * It does not matter for libmount if we set source or target
919 * here (the library is able to swap it), but it matters for
920 * sanitize_paths().
921 */
922 int istag = mnt_tag_is_valid(argv[0]);
923
924 if (istag && mnt_context_get_source(cxt))
925 /* -L, -U or --source together with LABEL= or UUID= */
926 errx(MNT_EX_USAGE, _("source specified more than once"));
927 else if (istag || mnt_context_get_target(cxt))
928 mnt_context_set_source(cxt, argv[0]);
929 else
930 mnt_context_set_target(cxt, argv[0]);
931
932 if (mnt_context_is_restricted(cxt) &&
933 mnt_context_get_source(cxt) &&
934 mnt_context_get_target(cxt))
935 exit_non_root(NULL);
936
937 } else if (argc == 2 && !mnt_context_get_source(cxt)
938 && !mnt_context_get_target(cxt)) {
939 /*
940 * D) mount <source> <target>
941 */
942 if (mnt_context_is_restricted(cxt))
943 exit_non_root(NULL);
944
945 mnt_context_set_source(cxt, argv[0]);
946 mnt_context_set_target(cxt, argv[1]);
947
948 } else {
949 warnx(_("bad usage"));
950 errtryhelp(MNT_EX_USAGE);
951 }
952
953 if (mnt_context_is_restricted(cxt))
954 sanitize_paths(cxt);
955
956 if (is_move)
957 /* "move" as option string is not supported by libmount */
958 mnt_context_set_mflags(cxt, MS_MOVE);
959
960 if ((oper && !has_remount_flag(cxt)) || propa)
961 /* For --make-* or --bind is fstab/mtab unnecessary */
962 mnt_context_set_optsmode(cxt, MNT_OMODE_NOTAB);
963
964 rc = mnt_context_mount(cxt);
965 rc = mk_exit_code(cxt, rc);
966
967 if (rc == MNT_EX_SUCCESS && mnt_context_is_verbose(cxt))
968 success_message(cxt);
969 done:
970 mnt_free_context(cxt);
971 return rc;
972 }
973