]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/mount.c
misc: consolidate version printing and close_stdout()
[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
68224d10 76static void __attribute__((__noreturn__)) mount_print_version(void)
97073441
KZ
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
8487dbee 146 if (!mnt_fs_is_pseudofs(fs) && !mnt_fs_is_netfs(fs))
aa397ce5 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
189a1bf3
KZ
234
235/*
236 * mount -a -o remount
237 */
238static 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
84600ddc
KZ
285static void success_message(struct libmnt_context *cxt)
286{
287 unsigned long mflags = 0;
f5ae1d70 288 const char *tgt, *src, *pr = program_invocation_short_name;
84600ddc
KZ
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)
f5ae1d70 299 printf(_("%s: %s moved to %s.\n"), pr, src, tgt);
84600ddc 300 else if (mflags & MS_BIND)
9b4257c8 301 printf(_("%s: %s bound on %s.\n"), pr, src, tgt);
b4ec4573
KZ
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
f5ae1d70 306 printf(_("%s: %s propagation flags changed.\n"), pr, tgt);
b4ec4573 307 } else
f5ae1d70 308 printf(_("%s: %s mounted on %s.\n"), pr, src, tgt);
84600ddc
KZ
309}
310
4e45dfb9
KZ
311#if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
312#include <selinux/selinux.h>
313#include <selinux/context.h>
314
315static 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
b8c0f4fb 338# define selinux_warning(_x, _y)
4e45dfb9
KZ
339#endif
340
fcc0413a 341/*
e1706a67 342 * Returns exit status (MNT_EX_*) and/or prints error message.
ce433404
KZ
343 */
344static int mk_exit_code(struct libmnt_context *cxt, int rc)
345{
e1706a67
KZ
346 const char *tgt;
347 char buf[BUFSIZ] = { 0 };
6dede2f2 348
e1706a67
KZ
349 rc = mnt_context_get_excode(cxt, rc, buf, sizeof(buf));
350 tgt = mnt_context_get_target(cxt);
ce433404 351
e1706a67
KZ
352 if (*buf) {
353 const char *spec = tgt;
354 if (!spec)
355 spec = mnt_context_get_source(cxt);
356 if (!spec)
357 spec = "???";
14056588 358 warnx("%s: %s.", spec, buf);
ce433404 359 }
ce433404 360
0361cb6f
KZ
361 if (rc == MNT_EX_SUCCESS && mnt_context_get_status(cxt) == 1) {
362 selinux_warning(cxt, tgt);
ce433404 363 }
e1706a67 364 return rc;
ce433404
KZ
365}
366
64b6bc4f
KZ
367static 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)
e3a7a5f8 375 err(MNT_EX_SYSERR, _("failed to initialize libmount table"));
64b6bc4f
KZ
376
377 mnt_table_set_parser_errcb(fstab, table_parser_errcb);
378 mnt_context_set_fstab(cxt, fstab);
50fccba1
KZ
379
380 mnt_unref_table(fstab); /* reference is handled by @cxt now */
64b6bc4f
KZ
381 }
382
383 if (mnt_table_parse_fstab(fstab, path))
e3a7a5f8 384 errx(MNT_EX_USAGE,_("%s: failed to parse"), path);
64b6bc4f
KZ
385
386 return fstab;
387}
388
5ebbc386
KZ
389/*
390 * Check source and target paths -- non-root user should not be able to
391 * resolve paths which are unreadable for him.
392 */
393static 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)
e3a7a5f8 405 err(MNT_EX_USAGE, "%s", p);
5ebbc386
KZ
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)
e3a7a5f8 414 err(MNT_EX_USAGE, "%s", p);
5ebbc386
KZ
415 mnt_fs_set_source(fs, np);
416 free(np);
417 }
418}
419
be6904b9
KZ
420static void append_option(struct libmnt_context *cxt, const char *opt)
421{
8225bb78 422 if (opt && (*opt == '=' || *opt == '\'' || *opt == '\"' || isblank(*opt)))
e3a7a5f8 423 errx(MNT_EX_USAGE, _("unsupported option format: %s"), opt);
be6904b9 424 if (mnt_context_append_options(cxt, opt))
e3a7a5f8 425 err(MNT_EX_SYSERR, _("failed to append option '%s'"), opt);
be6904b9
KZ
426}
427
ba986e81
KZ
428static 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
6e1eda6f 438static void __attribute__((__noreturn__)) usage(void)
97073441 439{
6e1eda6f 440 FILE *out = stdout;
e4c92d06
KZ
441 fputs(USAGE_HEADER, out);
442 fprintf(out, _(
97073441
KZ
443 " %1$s [-lhV]\n"
444 " %1$s -a [options]\n"
aedeaa40 445 " %1$s [options] [--source] <source> | [--target] <directory>\n"
97073441
KZ
446 " %1$s [options] <source> <directory>\n"
447 " %1$s <operation> <mountpoint> [<target>]\n"),
448 program_invocation_short_name);
449
451dbcfa
BS
450 fputs(USAGE_SEPARATOR, out);
451 fputs(_("Mount a filesystem.\n"), out);
452
e4c92d06 453 fputs(USAGE_OPTIONS, out);
97073441 454 fprintf(out, _(
97073441 455 " -a, --all mount all filesystems mentioned in fstab\n"
97073441 456 " -c, --no-canonicalize don't canonicalize paths\n"
eaca47f7 457 " -f, --fake dry run; skip the mount(2) syscall\n"
64b6bc4f
KZ
458 " -F, --fork fork off for each device (use with -a)\n"
459 " -T, --fstab <path> alternative file to /etc/fstab\n"));
eaca47f7 460 fprintf(out, _(
89de71b3
BS
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, _(
eaca47f7
BS
465 " -n, --no-mtab don't write to /etc/mtab\n"));
466 fprintf(out, _(
7238285b
VD
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, _(
eaca47f7
BS
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, _(
aedeaa40
KZ
479 " --source <src> explicitly specifies source (path, label, uuid)\n"
480 " --target <target> explicitly specifies mountpoint\n"));
481 fprintf(out, _(
89de71b3
BS
482 " -v, --verbose say what is being done\n"));
483 fprintf(out, _(
6d402bbe 484 " -w, --rw, --read-write mount the filesystem read-write (default)\n"));
d45e8ef9
VD
485 fprintf(out, _(
486 " -N, --namespace <ns> perform mount in another namespace\n"));
97073441 487
e4c92d06 488 fputs(USAGE_SEPARATOR, out);
f45f3ec3 489 printf(USAGE_HELP_OPTIONS(25));
e4c92d06 490
eaca47f7 491 fprintf(out, _(
97073441
KZ
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"
eb0eb262
KZ
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
eaca47f7 500 fprintf(out, _(
97073441
KZ
501 " <device> specifies device by path\n"
502 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
eaca47f7 503 " <file> regular file for loopdev setup\n"));
97073441 504
eaca47f7 505 fprintf(out, _(
97073441
KZ
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"
eaca47f7
BS
509 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
510 fprintf(out, _(
97073441
KZ
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"
eaca47f7
BS
514 " --make-unbindable mark a subtree as unbindable\n"));
515 fprintf(out, _(
97073441
KZ
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"
eaca47f7 519 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
97073441 520
f45f3ec3 521 printf(USAGE_MAN_TAIL("mount(8)"));
97073441 522
6e1eda6f 523 exit(MNT_EX_SUCCESS);
97073441
KZ
524}
525
db9185bf
VD
526struct flag_str {
527 int value;
528 char *str;
529};
530
9730aa40 531static int omode2mask(const char *str)
db9185bf 532{
9730aa40
VD
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;
db9185bf
VD
545 }
546 return -EINVAL;
547}
548
9730aa40 549static long osrc2mask(const char *str, size_t len)
db9185bf 550{
9730aa40
VD
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;
db9185bf 562 }
9730aa40 563 return -EINVAL;
db9185bf
VD
564}
565
d59766a6
VD
566static 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
97073441
KZ
579int main(int argc, char **argv)
580{
e3a7a5f8 581 int c, rc = MNT_EX_SUCCESS, all = 0, show_labels = 0;
68164f6c 582 struct libmnt_context *cxt;
64b6bc4f 583 struct libmnt_table *fstab = NULL;
aedeaa40 584 char *srcbuf = NULL;
97073441 585 char *types = NULL;
6691d537 586 int oper = 0, is_move = 0;
be6904b9 587 int propa = 0;
db9185bf 588 int optmode = 0, optmode_mode = 0, optmode_src = 0;
97073441 589
2492f713
KZ
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,
aedeaa40
KZ
598 MOUNT_OPT_RUNBINDABLE,
599 MOUNT_OPT_TARGET,
db9185bf
VD
600 MOUNT_OPT_SOURCE,
601 MOUNT_OPT_OPTMODE,
602 MOUNT_OPT_OPTSRC,
603 MOUNT_OPT_OPTSRC_FORCE
2492f713
KZ
604 };
605
6c7d5ae9 606 static const struct option longopts[] = {
87918040
SK
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 },
db9185bf
VD
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},
21edc0f7 643 { "namespace", required_argument, NULL, 'N' },
87918040 644 { NULL, 0, NULL, 0 }
97073441
KZ
645 };
646
a7349ee3 647 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
be6904b9 648 { 'B','M','R' }, /* bind,move,rbind */
51a37c19
KZ
649 { 'L','U', MOUNT_OPT_SOURCE }, /* label,uuid,source */
650 { 0 }
651 };
652 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
653
6189ace3 654 sanitize_env();
97073441
KZ
655 setlocale(LC_ALL, "");
656 bindtextdomain(PACKAGE, LOCALEDIR);
657 textdomain(PACKAGE);
2c308875 658 close_stdout_atexit();
97073441 659
0b2b32e8
RM
660 strutils_set_exitcode(MNT_EX_USAGE);
661
97073441
KZ
662 mnt_init_debug(0);
663 cxt = mnt_new_context();
664 if (!cxt)
e3a7a5f8 665 err(MNT_EX_SYSERR, _("libmount context allocation failed"));
97073441 666
9f7472b0
KZ
667 mnt_context_set_tables_errcb(cxt, table_parser_errcb);
668
21edc0f7 669 while ((c = getopt_long(argc, argv, "aBcfFhilL:Mno:O:rRsU:vVwt:T:N:",
97073441
KZ
670 longopts, NULL)) != -1) {
671
672 /* only few options are allowed for non-root users */
aedeaa40 673 if (mnt_context_is_restricted(cxt) &&
961d69f7 674 !strchr("hlLUVvrist", c) &&
aedeaa40
KZ
675 c != MOUNT_OPT_TARGET &&
676 c != MOUNT_OPT_SOURCE)
732a6311 677 exit_non_root(option_to_longopt(c, longopts));
97073441 678
51a37c19
KZ
679 err_exclusive_options(c, longopts, excl, excl_st);
680
97073441
KZ
681 switch(c) {
682 case 'a':
683 all = 1;
97073441
KZ
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':
d2c97887 692 mnt_context_enable_fork(cxt, TRUE);
97073441 693 break;
97073441
KZ
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':
be6904b9 701 append_option(cxt, "ro");
6dede2f2 702 mnt_context_enable_rwonly_mount(cxt, FALSE);
97073441
KZ
703 break;
704 case 'v':
705 mnt_context_enable_verbose(cxt, TRUE);
706 break;
97073441 707 case 'w':
be6904b9 708 append_option(cxt, "rw");
6dede2f2 709 mnt_context_enable_rwonly_mount(cxt, TRUE);
97073441
KZ
710 break;
711 case 'o':
be6904b9 712 append_option(cxt, optarg);
97073441
KZ
713 break;
714 case 'O':
715 if (mnt_context_set_options_pattern(cxt, optarg))
e3a7a5f8 716 err(MNT_EX_SYSERR, _("failed to set options pattern"));
97073441
KZ
717 break;
718 case 'L':
aedeaa40
KZ
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;
97073441 724 case 'U':
aedeaa40
KZ
725 xasprintf(&srcbuf, "UUID=\"%s\"", optarg);
726 mnt_context_disable_swapmatch(cxt, 1);
727 mnt_context_set_source(cxt, srcbuf);
728 free(srcbuf);
97073441
KZ
729 break;
730 case 'l':
731 show_labels = 1;
732 break;
733 case 't':
734 types = optarg;
735 break;
64b6bc4f
KZ
736 case 'T':
737 fstab = append_fstab(cxt, fstab, optarg);
738 break;
97073441
KZ
739 case 's':
740 mnt_context_enable_sloppy(cxt, TRUE);
741 break;
742 case 'B':
4ebea84b
KZ
743 oper = 1;
744 append_option(cxt, "bind");
97073441
KZ
745 break;
746 case 'M':
4ebea84b 747 oper = 1;
6691d537 748 is_move = 1;
97073441
KZ
749 break;
750 case 'R':
4ebea84b
KZ
751 oper = 1;
752 append_option(cxt, "rbind");
97073441 753 break;
21edc0f7 754 case 'N':
d59766a6
VD
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);
21edc0f7 764 break;
d59766a6 765 }
2492f713 766 case MOUNT_OPT_SHARED:
be6904b9
KZ
767 append_option(cxt, "shared");
768 propa = 1;
97073441 769 break;
2492f713 770 case MOUNT_OPT_SLAVE:
be6904b9
KZ
771 append_option(cxt, "slave");
772 propa = 1;
97073441 773 break;
2492f713 774 case MOUNT_OPT_PRIVATE:
be6904b9
KZ
775 append_option(cxt, "private");
776 propa = 1;
97073441 777 break;
2492f713 778 case MOUNT_OPT_UNBINDABLE:
be6904b9
KZ
779 append_option(cxt, "unbindable");
780 propa = 1;
97073441 781 break;
2492f713 782 case MOUNT_OPT_RSHARED:
be6904b9
KZ
783 append_option(cxt, "rshared");
784 propa = 1;
97073441 785 break;
2492f713 786 case MOUNT_OPT_RSLAVE:
be6904b9
KZ
787 append_option(cxt, "rslave");
788 propa = 1;
97073441 789 break;
2492f713 790 case MOUNT_OPT_RPRIVATE:
be6904b9
KZ
791 append_option(cxt, "rprivate");
792 propa = 1;
97073441 793 break;
2492f713 794 case MOUNT_OPT_RUNBINDABLE:
be6904b9
KZ
795 append_option(cxt, "runbindable");
796 propa = 1;
97073441 797 break;
aedeaa40
KZ
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:
aedeaa40
KZ
803 mnt_context_disable_swapmatch(cxt, 1);
804 mnt_context_set_source(cxt, optarg);
805 break;
db9185bf 806 case MOUNT_OPT_OPTMODE:
9730aa40 807 optmode_mode = omode2mask(optarg);
db9185bf
VD
808 if (optmode_mode == -EINVAL) {
809 warnx(_("bad usage"));
810 errtryhelp(MNT_EX_USAGE);
811 }
812 break;
813 case MOUNT_OPT_OPTSRC:
9730aa40
VD
814 {
815 unsigned long tmp = 0;
816 if (string_to_bitmask(optarg, &tmp, osrc2mask)) {
db9185bf
VD
817 warnx(_("bad usage"));
818 errtryhelp(MNT_EX_USAGE);
819 }
9730aa40 820 optmode_src = tmp;
db9185bf 821 break;
9730aa40 822 }
db9185bf
VD
823 case MOUNT_OPT_OPTSRC_FORCE:
824 optmode |= MNT_OMODE_FORCE;
825 break;
2c308875
KZ
826
827 case 'h':
828 mnt_free_context(cxt);
829 usage();
830 case 'V':
831 mnt_free_context(cxt);
832 mount_print_version();
97073441 833 default:
e3a7a5f8 834 errtryhelp(MNT_EX_USAGE);
97073441
KZ
835 }
836 }
837
838 argc -= optind;
839 argv += optind;
840
db9185bf
VD
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
59414c6b
KZ
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 */
6195f9e6 856 mnt_table_set_cache(fstab, mnt_context_get_cache(cxt));
59414c6b
KZ
857 }
858
aedeaa40
KZ
859 if (!mnt_context_get_source(cxt) &&
860 !mnt_context_get_target(cxt) &&
861 !argc &&
862 !all) {
6e1eda6f
RM
863 if (oper || mnt_context_get_options(cxt)) {
864 warnx(_("bad usage"));
865 errtryhelp(MNT_EX_USAGE);
866 }
11754572 867 print_all(cxt, types, show_labels);
97073441
KZ
868 goto done;
869 }
870
1707b9b1
RT
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
6e1eda6f
RM
876 if (oper && (types || all || mnt_context_get_source(cxt))) {
877 warnx(_("bad usage"));
878 errtryhelp(MNT_EX_USAGE);
879 }
97073441 880
9f7472b0
KZ
881 if (types && (all || strchr(types, ',') ||
882 strncmp(types, "no", 2) == 0))
97073441
KZ
883 mnt_context_set_fstype_pattern(cxt, types);
884 else if (types)
885 mnt_context_set_fstype(cxt, types);
886
a9ae3955
KZ
887 if (all) {
888 /*
889 * A) Mount all
890 */
189a1bf3
KZ
891 if (has_remount_flag(cxt))
892 rc = remount_all(cxt);
893 else
894 rc = mount_all(cxt);
11754572 895 goto done;
a9ae3955 896
aedeaa40
KZ
897 } else if (argc == 0 && (mnt_context_get_source(cxt) ||
898 mnt_context_get_target(cxt))) {
a9ae3955 899 /*
aedeaa40 900 * B) mount -L|-U|--source|--target
6751f062
KZ
901 *
902 * non-root may specify source *or* target, but not both
a9ae3955 903 */
aedeaa40
KZ
904 if (mnt_context_is_restricted(cxt) &&
905 mnt_context_get_source(cxt) &&
906 mnt_context_get_target(cxt))
907 exit_non_root(NULL);
97073441 908
6751f062
KZ
909 } else if (argc == 1 && (!mnt_context_get_source(cxt) ||
910 !mnt_context_get_target(cxt))) {
a9ae3955 911 /*
aedeaa40 912 * C) mount [-L|-U|--source] <target>
6751f062 913 * mount [--target <dir>] <source>
a9ae3955 914 * mount <source|target>
aedeaa40
KZ
915 *
916 * non-root may specify source *or* target, but not both
6751f062
KZ
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().
a9ae3955 921 */
6751f062
KZ
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= */
e3a7a5f8 926 errx(MNT_EX_USAGE, _("source specified more than once"));
6751f062
KZ
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
aedeaa40 932 if (mnt_context_is_restricted(cxt) &&
6751f062
KZ
933 mnt_context_get_source(cxt) &&
934 mnt_context_get_target(cxt))
aedeaa40
KZ
935 exit_non_root(NULL);
936
aedeaa40
KZ
937 } else if (argc == 2 && !mnt_context_get_source(cxt)
938 && !mnt_context_get_target(cxt)) {
a9ae3955
KZ
939 /*
940 * D) mount <source> <target>
941 */
97073441
KZ
942 if (mnt_context_is_restricted(cxt))
943 exit_non_root(NULL);
6751f062 944
97073441
KZ
945 mnt_context_set_source(cxt, argv[0]);
946 mnt_context_set_target(cxt, argv[1]);
aedeaa40 947
6e1eda6f
RM
948 } else {
949 warnx(_("bad usage"));
950 errtryhelp(MNT_EX_USAGE);
951 }
97073441 952
5ebbc386
KZ
953 if (mnt_context_is_restricted(cxt))
954 sanitize_paths(cxt);
955
6691d537
KZ
956 if (is_move)
957 /* "move" as option string is not supported by libmount */
958 mnt_context_set_mflags(cxt, MS_MOVE);
959
ba986e81
KZ
960 if ((oper && !has_remount_flag(cxt)) || propa)
961 /* For --make-* or --bind is fstab/mtab unnecessary */
374fd21a 962 mnt_context_set_optsmode(cxt, MNT_OMODE_NOTAB);
374fd21a 963
cfb9db30 964 rc = mnt_context_mount(cxt);
ce433404
KZ
965 rc = mk_exit_code(cxt, rc);
966
e3a7a5f8 967 if (rc == MNT_EX_SUCCESS && mnt_context_is_verbose(cxt))
84600ddc 968 success_message(cxt);
97073441 969done:
97073441
KZ
970 mnt_free_context(cxt);
971 return rc;
972}
973