]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/mount.c
fsck.cramfs: fix utimes() usage
[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 *
7cebf0bb
SK
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
97073441
KZ
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 30#include <sys/stat.h>
2e86597f 31#include <stdarg.h>
2a1f429a 32#include <libmount.h>
5f7c1890 33#include <ctype.h>
2a1f429a 34
97073441
KZ
35#include "nls.h"
36#include "c.h"
6189ace3 37#include "env.h"
dbae36fe 38#include "strutils.h"
efb8854f 39#include "closestream.h"
5ebbc386 40#include "canonicalize.h"
97073441 41
778ca2a0
RM
42#define XALLOC_EXIT_CODE MNT_EX_SYSERR
43#include "xalloc.h"
44
e3a7a5f8 45#define OPTUTILS_EXIT_CODE MNT_EX_USAGE
38483b86
SK
46#include "optutils.h"
47
97073441 48/*** TODO: DOCS:
374fd21a
KZ
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
97073441
KZ
53 */
54
d946359a
KZ
55static int mk_exit_code(struct libmnt_context *cxt, int rc);
56
97073441
KZ
57static 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)
e3a7a5f8 65 errx(MNT_EX_USAGE, _("only root can use \"--%s\" option "
97073441
KZ
66 "(effective UID is %u)"),
67 option, euid);
e3a7a5f8 68 errx(MNT_EX_USAGE, _("only root can do that "
97073441
KZ
69 "(effective UID is %u)"), euid);
70 }
71 if (option)
e3a7a5f8
KZ
72 errx(MNT_EX_USAGE, _("only root can use \"--%s\" option"), option);
73 errx(MNT_EX_USAGE, _("only root can do that"));
97073441
KZ
74}
75
76static void __attribute__((__noreturn__)) print_version(void)
77{
78 const char *ver = NULL;
ac8ecab4 79 const char **features = NULL, **p;
97073441
KZ
80
81 mnt_get_library_version(&ver);
ac8ecab4
KZ
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);
e3a7a5f8 94 exit(MNT_EX_SUCCESS);
97073441
KZ
95}
96
7fc6d2b8 97static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
9f7472b0
KZ
98 const char *filename, int line)
99{
100 if (filename)
b779c1ae 101 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
1cd9d0d7 102 return 1;
9f7472b0
KZ
103}
104
5f7c1890
KZ
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 */
109static 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
11754572 121static void print_all(struct libmnt_context *cxt, char *pattern, int show_label)
97073441 122{
68164f6c 123 struct libmnt_table *tb;
a0c014dc 124 struct libmnt_iter *itr = NULL;
68164f6c
KZ
125 struct libmnt_fs *fs;
126 struct libmnt_cache *cache = NULL;
97073441 127
11754572 128 if (mnt_context_get_mtab(cxt, &tb))
e3a7a5f8 129 err(MNT_EX_SYSERR, _("failed to read mtab"));
97073441
KZ
130
131 itr = mnt_new_iter(MNT_ITER_FORWARD);
11754572 132 if (!itr)
e3a7a5f8 133 err(MNT_EX_SYSERR, _("failed to initialize libmount iterator"));
97073441
KZ
134 if (show_label)
135 cache = mnt_new_cache();
136
9f7472b0 137 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
97073441
KZ
138 const char *type = mnt_fs_get_fstype(fs);
139 const char *src = mnt_fs_get_source(fs);
7cf389f7 140 const char *optstr = mnt_fs_get_options(fs);
aa397ce5 141 char *xsrc = NULL;
97073441
KZ
142
143 if (type && pattern && !mnt_match_fstype(type, pattern))
144 continue;
145
aa397ce5
DR
146 if (!mnt_fs_is_pseudofs(fs))
147 xsrc = mnt_pretty_path(src, cache);
5f7c1890
KZ
148 printf ("%s on ", xsrc ? xsrc : src);
149 safe_fputs(mnt_fs_get_target(fs));
150
97073441
KZ
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);
2576b4e7 161 free(xsrc);
97073441 162 }
11754572 163
6195f9e6 164 mnt_unref_cache(cache);
b192b7b9 165 mnt_free_iter(itr);
97073441
KZ
166}
167
9f7472b0
KZ
168/*
169 * mount -a [-F]
9f7472b0 170 */
d2c97887 171static int mount_all(struct libmnt_context *cxt)
a9ae3955 172{
9f7472b0
KZ
173 struct libmnt_iter *itr;
174 struct libmnt_fs *fs;
e3a7a5f8 175 int mntrc, ignored, rc = MNT_EX_SUCCESS;
9f7472b0 176
16b73aae
KZ
177 int nsucc = 0, nerrs = 0;
178
9f7472b0
KZ
179 itr = mnt_new_iter(MNT_ITER_FORWARD);
180 if (!itr) {
181 warn(_("failed to initialize libmount iterator"));
e3a7a5f8 182 return MNT_EX_SYSERR;
9f7472b0
KZ
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))
b3f7a0ec
KZ
191 printf(ignored == 1 ? _("%-25s: ignored\n") :
192 _("%-25s: already mounted\n"),
9f7472b0 193 tgt);
d2c97887 194 } else if (mnt_context_is_fork(cxt)) {
d946359a
KZ
195 if (mnt_context_is_verbose(cxt))
196 printf("%-25s: mount successfully forked\n", tgt);
9f7472b0 197 } else {
e3a7a5f8 198 if (mk_exit_code(cxt, mntrc) == MNT_EX_SUCCESS) {
16b73aae 199 nsucc++;
d946359a 200
e3a7a5f8 201 /* Note that MNT_EX_SUCCESS return code does
8ab82185
KZ
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))
d946359a 206 printf("%-25s: successfully mounted\n", tgt);
16b73aae
KZ
207 } else
208 nerrs++;
9f7472b0
KZ
209 }
210 }
211
d2c97887
KZ
212 if (mnt_context_is_parent(cxt)) {
213 /* wait for mount --fork children */
16b73aae
KZ
214 int nchildren = 0;
215
216 nerrs = 0, nsucc = 0;
d2c97887
KZ
217
218 rc = mnt_context_wait_for_children(cxt, &nchildren, &nerrs);
219 if (!rc && nchildren)
16b73aae 220 nsucc = nchildren - nerrs;
d2c97887
KZ
221 }
222
16b73aae 223 if (nerrs == 0)
e3a7a5f8 224 rc = MNT_EX_SUCCESS; /* all success */
16b73aae 225 else if (nsucc == 0)
e3a7a5f8 226 rc = MNT_EX_FAIL; /* all failed */
16b73aae 227 else
e3a7a5f8 228 rc = MNT_EX_SOMEOK; /* some success, some failed */
16b73aae 229
25609ee1 230 mnt_free_iter(itr);
9f7472b0 231 return rc;
a9ae3955
KZ
232}
233
84600ddc
KZ
234static void success_message(struct libmnt_context *cxt)
235{
236 unsigned long mflags = 0;
f5ae1d70 237 const char *tgt, *src, *pr = program_invocation_short_name;
84600ddc
KZ
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)
f5ae1d70 248 printf(_("%s: %s moved to %s.\n"), pr, src, tgt);
84600ddc 249 else if (mflags & MS_BIND)
9b4257c8 250 printf(_("%s: %s bound on %s.\n"), pr, src, tgt);
b4ec4573
KZ
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
f5ae1d70 255 printf(_("%s: %s propagation flags changed.\n"), pr, tgt);
b4ec4573 256 } else
f5ae1d70 257 printf(_("%s: %s mounted on %s.\n"), pr, src, tgt);
84600ddc
KZ
258}
259
4e45dfb9
KZ
260#if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
261#include <selinux/selinux.h>
262#include <selinux/context.h>
263
264static 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
b8c0f4fb 287# define selinux_warning(_x, _y)
4e45dfb9
KZ
288#endif
289
fcc0413a 290/*
e1706a67 291 * Returns exit status (MNT_EX_*) and/or prints error message.
ce433404
KZ
292 */
293static int mk_exit_code(struct libmnt_context *cxt, int rc)
294{
e1706a67
KZ
295 const char *tgt;
296 char buf[BUFSIZ] = { 0 };
6dede2f2 297
e1706a67
KZ
298 rc = mnt_context_get_excode(cxt, rc, buf, sizeof(buf));
299 tgt = mnt_context_get_target(cxt);
ce433404 300
e1706a67
KZ
301 if (*buf) {
302 const char *spec = tgt;
303 if (!spec)
304 spec = mnt_context_get_source(cxt);
305 if (!spec)
306 spec = "???";
14056588 307 warnx("%s: %s.", spec, buf);
ce433404 308 }
ce433404 309
0361cb6f
KZ
310 if (rc == MNT_EX_SUCCESS && mnt_context_get_status(cxt) == 1) {
311 selinux_warning(cxt, tgt);
ce433404 312 }
e1706a67 313 return rc;
ce433404
KZ
314}
315
64b6bc4f
KZ
316static 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)
e3a7a5f8 324 err(MNT_EX_SYSERR, _("failed to initialize libmount table"));
64b6bc4f
KZ
325
326 mnt_table_set_parser_errcb(fstab, table_parser_errcb);
327 mnt_context_set_fstab(cxt, fstab);
50fccba1
KZ
328
329 mnt_unref_table(fstab); /* reference is handled by @cxt now */
64b6bc4f
KZ
330 }
331
332 if (mnt_table_parse_fstab(fstab, path))
e3a7a5f8 333 errx(MNT_EX_USAGE,_("%s: failed to parse"), path);
64b6bc4f
KZ
334
335 return fstab;
336}
337
5ebbc386
KZ
338/*
339 * Check source and target paths -- non-root user should not be able to
340 * resolve paths which are unreadable for him.
341 */
342static 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)
e3a7a5f8 354 err(MNT_EX_USAGE, "%s", p);
5ebbc386
KZ
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)
e3a7a5f8 363 err(MNT_EX_USAGE, "%s", p);
5ebbc386
KZ
364 mnt_fs_set_source(fs, np);
365 free(np);
366 }
367}
368
be6904b9
KZ
369static void append_option(struct libmnt_context *cxt, const char *opt)
370{
8225bb78 371 if (opt && (*opt == '=' || *opt == '\'' || *opt == '\"' || isblank(*opt)))
e3a7a5f8 372 errx(MNT_EX_USAGE, _("unsupported option format: %s"), opt);
be6904b9 373 if (mnt_context_append_options(cxt, opt))
e3a7a5f8 374 err(MNT_EX_SYSERR, _("failed to append option '%s'"), opt);
be6904b9
KZ
375}
376
ba986e81
KZ
377static 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
6e1eda6f 387static void __attribute__((__noreturn__)) usage(void)
97073441 388{
6e1eda6f 389 FILE *out = stdout;
e4c92d06
KZ
390 fputs(USAGE_HEADER, out);
391 fprintf(out, _(
97073441
KZ
392 " %1$s [-lhV]\n"
393 " %1$s -a [options]\n"
aedeaa40 394 " %1$s [options] [--source] <source> | [--target] <directory>\n"
97073441
KZ
395 " %1$s [options] <source> <directory>\n"
396 " %1$s <operation> <mountpoint> [<target>]\n"),
397 program_invocation_short_name);
398
451dbcfa
BS
399 fputs(USAGE_SEPARATOR, out);
400 fputs(_("Mount a filesystem.\n"), out);
401
e4c92d06 402 fputs(USAGE_OPTIONS, out);
97073441 403 fprintf(out, _(
97073441 404 " -a, --all mount all filesystems mentioned in fstab\n"
97073441 405 " -c, --no-canonicalize don't canonicalize paths\n"
eaca47f7 406 " -f, --fake dry run; skip the mount(2) syscall\n"
64b6bc4f
KZ
407 " -F, --fork fork off for each device (use with -a)\n"
408 " -T, --fstab <path> alternative file to /etc/fstab\n"));
eaca47f7 409 fprintf(out, _(
89de71b3
BS
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, _(
eaca47f7
BS
414 " -n, --no-mtab don't write to /etc/mtab\n"));
415 fprintf(out, _(
7238285b
VD
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, _(
eaca47f7
BS
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, _(
aedeaa40
KZ
428 " --source <src> explicitly specifies source (path, label, uuid)\n"
429 " --target <target> explicitly specifies mountpoint\n"));
430 fprintf(out, _(
89de71b3
BS
431 " -v, --verbose say what is being done\n"));
432 fprintf(out, _(
6d402bbe 433 " -w, --rw, --read-write mount the filesystem read-write (default)\n"));
d45e8ef9
VD
434 fprintf(out, _(
435 " -N, --namespace <ns> perform mount in another namespace\n"));
97073441 436
e4c92d06 437 fputs(USAGE_SEPARATOR, out);
f45f3ec3 438 printf(USAGE_HELP_OPTIONS(25));
e4c92d06 439
eaca47f7 440 fprintf(out, _(
97073441
KZ
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"
eb0eb262
KZ
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
eaca47f7 449 fprintf(out, _(
97073441
KZ
450 " <device> specifies device by path\n"
451 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
eaca47f7 452 " <file> regular file for loopdev setup\n"));
97073441 453
eaca47f7 454 fprintf(out, _(
97073441
KZ
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"
eaca47f7
BS
458 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
459 fprintf(out, _(
97073441
KZ
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"
eaca47f7
BS
463 " --make-unbindable mark a subtree as unbindable\n"));
464 fprintf(out, _(
97073441
KZ
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"
eaca47f7 468 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
97073441 469
f45f3ec3 470 printf(USAGE_MAN_TAIL("mount(8)"));
97073441 471
6e1eda6f 472 exit(MNT_EX_SUCCESS);
97073441
KZ
473}
474
db9185bf
VD
475struct flag_str {
476 int value;
477 char *str;
478};
479
9730aa40 480static int omode2mask(const char *str)
db9185bf 481{
9730aa40
VD
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;
db9185bf
VD
494 }
495 return -EINVAL;
496}
497
9730aa40 498static long osrc2mask(const char *str, size_t len)
db9185bf 499{
9730aa40
VD
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;
db9185bf 511 }
9730aa40 512 return -EINVAL;
db9185bf
VD
513}
514
d59766a6
VD
515static 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
97073441
KZ
528int main(int argc, char **argv)
529{
e3a7a5f8 530 int c, rc = MNT_EX_SUCCESS, all = 0, show_labels = 0;
68164f6c 531 struct libmnt_context *cxt;
64b6bc4f 532 struct libmnt_table *fstab = NULL;
aedeaa40 533 char *srcbuf = NULL;
97073441 534 char *types = NULL;
6691d537 535 int oper = 0, is_move = 0;
be6904b9 536 int propa = 0;
db9185bf 537 int optmode = 0, optmode_mode = 0, optmode_src = 0;
97073441 538
2492f713
KZ
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,
aedeaa40
KZ
547 MOUNT_OPT_RUNBINDABLE,
548 MOUNT_OPT_TARGET,
db9185bf
VD
549 MOUNT_OPT_SOURCE,
550 MOUNT_OPT_OPTMODE,
551 MOUNT_OPT_OPTSRC,
552 MOUNT_OPT_OPTSRC_FORCE
2492f713
KZ
553 };
554
6c7d5ae9 555 static const struct option longopts[] = {
87918040
SK
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 },
db9185bf
VD
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},
21edc0f7 592 { "namespace", required_argument, NULL, 'N' },
87918040 593 { NULL, 0, NULL, 0 }
97073441
KZ
594 };
595
a7349ee3 596 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
be6904b9 597 { 'B','M','R' }, /* bind,move,rbind */
51a37c19
KZ
598 { 'L','U', MOUNT_OPT_SOURCE }, /* label,uuid,source */
599 { 0 }
600 };
601 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
602
6189ace3 603 sanitize_env();
97073441
KZ
604 setlocale(LC_ALL, "");
605 bindtextdomain(PACKAGE, LOCALEDIR);
606 textdomain(PACKAGE);
efb8854f 607 atexit(close_stdout);
97073441 608
0b2b32e8
RM
609 strutils_set_exitcode(MNT_EX_USAGE);
610
97073441
KZ
611 mnt_init_debug(0);
612 cxt = mnt_new_context();
613 if (!cxt)
e3a7a5f8 614 err(MNT_EX_SYSERR, _("libmount context allocation failed"));
97073441 615
9f7472b0
KZ
616 mnt_context_set_tables_errcb(cxt, table_parser_errcb);
617
21edc0f7 618 while ((c = getopt_long(argc, argv, "aBcfFhilL:Mno:O:rRsU:vVwt:T:N:",
97073441
KZ
619 longopts, NULL)) != -1) {
620
621 /* only few options are allowed for non-root users */
aedeaa40 622 if (mnt_context_is_restricted(cxt) &&
961d69f7 623 !strchr("hlLUVvrist", c) &&
aedeaa40
KZ
624 c != MOUNT_OPT_TARGET &&
625 c != MOUNT_OPT_SOURCE)
732a6311 626 exit_non_root(option_to_longopt(c, longopts));
97073441 627
51a37c19
KZ
628 err_exclusive_options(c, longopts, excl, excl_st);
629
97073441
KZ
630 switch(c) {
631 case 'a':
632 all = 1;
97073441
KZ
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':
d2c97887 641 mnt_context_enable_fork(cxt, TRUE);
97073441
KZ
642 break;
643 case 'h':
6e1eda6f 644 usage();
97073441
KZ
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':
be6904b9 653 append_option(cxt, "ro");
6dede2f2 654 mnt_context_enable_rwonly_mount(cxt, FALSE);
97073441
KZ
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':
be6904b9 663 append_option(cxt, "rw");
6dede2f2 664 mnt_context_enable_rwonly_mount(cxt, TRUE);
97073441
KZ
665 break;
666 case 'o':
be6904b9 667 append_option(cxt, optarg);
97073441
KZ
668 break;
669 case 'O':
670 if (mnt_context_set_options_pattern(cxt, optarg))
e3a7a5f8 671 err(MNT_EX_SYSERR, _("failed to set options pattern"));
97073441
KZ
672 break;
673 case 'L':
aedeaa40
KZ
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;
97073441 679 case 'U':
aedeaa40
KZ
680 xasprintf(&srcbuf, "UUID=\"%s\"", optarg);
681 mnt_context_disable_swapmatch(cxt, 1);
682 mnt_context_set_source(cxt, srcbuf);
683 free(srcbuf);
97073441
KZ
684 break;
685 case 'l':
686 show_labels = 1;
687 break;
688 case 't':
689 types = optarg;
690 break;
64b6bc4f
KZ
691 case 'T':
692 fstab = append_fstab(cxt, fstab, optarg);
693 break;
97073441
KZ
694 case 's':
695 mnt_context_enable_sloppy(cxt, TRUE);
696 break;
697 case 'B':
4ebea84b
KZ
698 oper = 1;
699 append_option(cxt, "bind");
97073441
KZ
700 break;
701 case 'M':
4ebea84b 702 oper = 1;
6691d537 703 is_move = 1;
97073441
KZ
704 break;
705 case 'R':
4ebea84b
KZ
706 oper = 1;
707 append_option(cxt, "rbind");
97073441 708 break;
21edc0f7 709 case 'N':
d59766a6
VD
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);
21edc0f7 719 break;
d59766a6 720 }
2492f713 721 case MOUNT_OPT_SHARED:
be6904b9
KZ
722 append_option(cxt, "shared");
723 propa = 1;
97073441 724 break;
2492f713 725 case MOUNT_OPT_SLAVE:
be6904b9
KZ
726 append_option(cxt, "slave");
727 propa = 1;
97073441 728 break;
2492f713 729 case MOUNT_OPT_PRIVATE:
be6904b9
KZ
730 append_option(cxt, "private");
731 propa = 1;
97073441 732 break;
2492f713 733 case MOUNT_OPT_UNBINDABLE:
be6904b9
KZ
734 append_option(cxt, "unbindable");
735 propa = 1;
97073441 736 break;
2492f713 737 case MOUNT_OPT_RSHARED:
be6904b9
KZ
738 append_option(cxt, "rshared");
739 propa = 1;
97073441 740 break;
2492f713 741 case MOUNT_OPT_RSLAVE:
be6904b9
KZ
742 append_option(cxt, "rslave");
743 propa = 1;
97073441 744 break;
2492f713 745 case MOUNT_OPT_RPRIVATE:
be6904b9
KZ
746 append_option(cxt, "rprivate");
747 propa = 1;
97073441 748 break;
2492f713 749 case MOUNT_OPT_RUNBINDABLE:
be6904b9
KZ
750 append_option(cxt, "runbindable");
751 propa = 1;
97073441 752 break;
aedeaa40
KZ
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:
aedeaa40
KZ
758 mnt_context_disable_swapmatch(cxt, 1);
759 mnt_context_set_source(cxt, optarg);
760 break;
db9185bf 761 case MOUNT_OPT_OPTMODE:
9730aa40 762 optmode_mode = omode2mask(optarg);
db9185bf
VD
763 if (optmode_mode == -EINVAL) {
764 warnx(_("bad usage"));
765 errtryhelp(MNT_EX_USAGE);
766 }
767 break;
768 case MOUNT_OPT_OPTSRC:
9730aa40
VD
769 {
770 unsigned long tmp = 0;
771 if (string_to_bitmask(optarg, &tmp, osrc2mask)) {
db9185bf
VD
772 warnx(_("bad usage"));
773 errtryhelp(MNT_EX_USAGE);
774 }
9730aa40 775 optmode_src = tmp;
db9185bf 776 break;
9730aa40 777 }
db9185bf
VD
778 case MOUNT_OPT_OPTSRC_FORCE:
779 optmode |= MNT_OMODE_FORCE;
780 break;
97073441 781 default:
e3a7a5f8 782 errtryhelp(MNT_EX_USAGE);
97073441
KZ
783 }
784 }
785
786 argc -= optind;
787 argv += optind;
788
db9185bf
VD
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
59414c6b
KZ
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 */
6195f9e6 804 mnt_table_set_cache(fstab, mnt_context_get_cache(cxt));
59414c6b
KZ
805 }
806
aedeaa40
KZ
807 if (!mnt_context_get_source(cxt) &&
808 !mnt_context_get_target(cxt) &&
809 !argc &&
810 !all) {
6e1eda6f
RM
811 if (oper || mnt_context_get_options(cxt)) {
812 warnx(_("bad usage"));
813 errtryhelp(MNT_EX_USAGE);
814 }
11754572 815 print_all(cxt, types, show_labels);
97073441
KZ
816 goto done;
817 }
818
1707b9b1
RT
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
6e1eda6f
RM
824 if (oper && (types || all || mnt_context_get_source(cxt))) {
825 warnx(_("bad usage"));
826 errtryhelp(MNT_EX_USAGE);
827 }
97073441 828
9f7472b0
KZ
829 if (types && (all || strchr(types, ',') ||
830 strncmp(types, "no", 2) == 0))
97073441
KZ
831 mnt_context_set_fstype_pattern(cxt, types);
832 else if (types)
833 mnt_context_set_fstype(cxt, types);
834
a9ae3955
KZ
835 if (all) {
836 /*
837 * A) Mount all
838 */
d2c97887 839 rc = mount_all(cxt);
11754572 840 goto done;
a9ae3955 841
aedeaa40
KZ
842 } else if (argc == 0 && (mnt_context_get_source(cxt) ||
843 mnt_context_get_target(cxt))) {
a9ae3955 844 /*
aedeaa40 845 * B) mount -L|-U|--source|--target
6751f062
KZ
846 *
847 * non-root may specify source *or* target, but not both
a9ae3955 848 */
aedeaa40
KZ
849 if (mnt_context_is_restricted(cxt) &&
850 mnt_context_get_source(cxt) &&
851 mnt_context_get_target(cxt))
852 exit_non_root(NULL);
97073441 853
6751f062
KZ
854 } else if (argc == 1 && (!mnt_context_get_source(cxt) ||
855 !mnt_context_get_target(cxt))) {
a9ae3955 856 /*
aedeaa40 857 * C) mount [-L|-U|--source] <target>
6751f062 858 * mount [--target <dir>] <source>
a9ae3955 859 * mount <source|target>
aedeaa40
KZ
860 *
861 * non-root may specify source *or* target, but not both
6751f062
KZ
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().
a9ae3955 866 */
6751f062
KZ
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= */
e3a7a5f8 871 errx(MNT_EX_USAGE, _("source specified more than once"));
6751f062
KZ
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
aedeaa40 877 if (mnt_context_is_restricted(cxt) &&
6751f062
KZ
878 mnt_context_get_source(cxt) &&
879 mnt_context_get_target(cxt))
aedeaa40
KZ
880 exit_non_root(NULL);
881
aedeaa40
KZ
882 } else if (argc == 2 && !mnt_context_get_source(cxt)
883 && !mnt_context_get_target(cxt)) {
a9ae3955
KZ
884 /*
885 * D) mount <source> <target>
886 */
97073441
KZ
887 if (mnt_context_is_restricted(cxt))
888 exit_non_root(NULL);
6751f062 889
97073441
KZ
890 mnt_context_set_source(cxt, argv[0]);
891 mnt_context_set_target(cxt, argv[1]);
aedeaa40 892
6e1eda6f
RM
893 } else {
894 warnx(_("bad usage"));
895 errtryhelp(MNT_EX_USAGE);
896 }
97073441 897
5ebbc386
KZ
898 if (mnt_context_is_restricted(cxt))
899 sanitize_paths(cxt);
900
6691d537
KZ
901 if (is_move)
902 /* "move" as option string is not supported by libmount */
903 mnt_context_set_mflags(cxt, MS_MOVE);
904
ba986e81
KZ
905 if ((oper && !has_remount_flag(cxt)) || propa)
906 /* For --make-* or --bind is fstab/mtab unnecessary */
374fd21a 907 mnt_context_set_optsmode(cxt, MNT_OMODE_NOTAB);
374fd21a 908
cfb9db30 909 rc = mnt_context_mount(cxt);
ce433404
KZ
910 rc = mk_exit_code(cxt, rc);
911
e3a7a5f8 912 if (rc == MNT_EX_SUCCESS && mnt_context_is_verbose(cxt))
84600ddc 913 success_message(cxt);
97073441 914done:
97073441
KZ
915 mnt_free_context(cxt);
916 return rc;
917}
918