]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/mount.c
Merge branch 'libblkid-atari' of https://github.com/yontalcar/util-linux
[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 = "???";
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, _(
416 " -o, --options <list> comma-separated list of mount options\n"
417 " -O, --test-opts <list> limit the set of filesystems (use with -a)\n"
418 " -r, --read-only mount the filesystem read-only (same as -o ro)\n"
419 " -t, --types <list> limit the set of filesystem types\n"));
420 fprintf(out, _(
aedeaa40
KZ
421 " --source <src> explicitly specifies source (path, label, uuid)\n"
422 " --target <target> explicitly specifies mountpoint\n"));
423 fprintf(out, _(
89de71b3
BS
424 " -v, --verbose say what is being done\n"));
425 fprintf(out, _(
6d402bbe 426 " -w, --rw, --read-write mount the filesystem read-write (default)\n"));
97073441 427
e4c92d06 428 fputs(USAGE_SEPARATOR, out);
f45f3ec3 429 printf(USAGE_HELP_OPTIONS(25));
e4c92d06 430
eaca47f7 431 fprintf(out, _(
97073441
KZ
432 "\nSource:\n"
433 " -L, --label <label> synonym for LABEL=<label>\n"
434 " -U, --uuid <uuid> synonym for UUID=<uuid>\n"
435 " LABEL=<label> specifies device by filesystem label\n"
eb0eb262
KZ
436 " UUID=<uuid> specifies device by filesystem UUID\n"
437 " PARTLABEL=<label> specifies device by partition label\n"
438 " PARTUUID=<uuid> specifies device by partition UUID\n"));
439
eaca47f7 440 fprintf(out, _(
97073441
KZ
441 " <device> specifies device by path\n"
442 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
eaca47f7 443 " <file> regular file for loopdev setup\n"));
97073441 444
eaca47f7 445 fprintf(out, _(
97073441
KZ
446 "\nOperations:\n"
447 " -B, --bind mount a subtree somewhere else (same as -o bind)\n"
448 " -M, --move move a subtree to some other place\n"
eaca47f7
BS
449 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
450 fprintf(out, _(
97073441
KZ
451 " --make-shared mark a subtree as shared\n"
452 " --make-slave mark a subtree as slave\n"
453 " --make-private mark a subtree as private\n"
eaca47f7
BS
454 " --make-unbindable mark a subtree as unbindable\n"));
455 fprintf(out, _(
97073441
KZ
456 " --make-rshared recursively mark a whole subtree as shared\n"
457 " --make-rslave recursively mark a whole subtree as slave\n"
458 " --make-rprivate recursively mark a whole subtree as private\n"
eaca47f7 459 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
97073441 460
f45f3ec3 461 printf(USAGE_MAN_TAIL("mount(8)"));
97073441 462
6e1eda6f 463 exit(MNT_EX_SUCCESS);
97073441
KZ
464}
465
466int main(int argc, char **argv)
467{
e3a7a5f8 468 int c, rc = MNT_EX_SUCCESS, all = 0, show_labels = 0;
68164f6c 469 struct libmnt_context *cxt;
64b6bc4f 470 struct libmnt_table *fstab = NULL;
aedeaa40 471 char *srcbuf = NULL;
97073441
KZ
472 char *types = NULL;
473 unsigned long oper = 0;
be6904b9 474 int propa = 0;
97073441 475
2492f713
KZ
476 enum {
477 MOUNT_OPT_SHARED = CHAR_MAX + 1,
478 MOUNT_OPT_SLAVE,
479 MOUNT_OPT_PRIVATE,
480 MOUNT_OPT_UNBINDABLE,
481 MOUNT_OPT_RSHARED,
482 MOUNT_OPT_RSLAVE,
483 MOUNT_OPT_RPRIVATE,
aedeaa40
KZ
484 MOUNT_OPT_RUNBINDABLE,
485 MOUNT_OPT_TARGET,
486 MOUNT_OPT_SOURCE
2492f713
KZ
487 };
488
6c7d5ae9 489 static const struct option longopts[] = {
87918040
SK
490 { "all", no_argument, NULL, 'a' },
491 { "fake", no_argument, NULL, 'f' },
492 { "fstab", required_argument, NULL, 'T' },
493 { "fork", no_argument, NULL, 'F' },
494 { "help", no_argument, NULL, 'h' },
495 { "no-mtab", no_argument, NULL, 'n' },
496 { "read-only", no_argument, NULL, 'r' },
497 { "ro", no_argument, NULL, 'r' },
498 { "verbose", no_argument, NULL, 'v' },
499 { "version", no_argument, NULL, 'V' },
500 { "read-write", no_argument, NULL, 'w' },
501 { "rw", no_argument, NULL, 'w' },
502 { "options", required_argument, NULL, 'o' },
503 { "test-opts", required_argument, NULL, 'O' },
504 { "types", required_argument, NULL, 't' },
505 { "uuid", required_argument, NULL, 'U' },
506 { "label", required_argument, NULL, 'L' },
507 { "bind", no_argument, NULL, 'B' },
508 { "move", no_argument, NULL, 'M' },
509 { "rbind", no_argument, NULL, 'R' },
510 { "make-shared", no_argument, NULL, MOUNT_OPT_SHARED },
511 { "make-slave", no_argument, NULL, MOUNT_OPT_SLAVE },
512 { "make-private", no_argument, NULL, MOUNT_OPT_PRIVATE },
513 { "make-unbindable", no_argument, NULL, MOUNT_OPT_UNBINDABLE },
514 { "make-rshared", no_argument, NULL, MOUNT_OPT_RSHARED },
515 { "make-rslave", no_argument, NULL, MOUNT_OPT_RSLAVE },
516 { "make-rprivate", no_argument, NULL, MOUNT_OPT_RPRIVATE },
517 { "make-runbindable", no_argument, NULL, MOUNT_OPT_RUNBINDABLE },
518 { "no-canonicalize", no_argument, NULL, 'c' },
519 { "internal-only", no_argument, NULL, 'i' },
520 { "show-labels", no_argument, NULL, 'l' },
521 { "target", required_argument, NULL, MOUNT_OPT_TARGET },
522 { "source", required_argument, NULL, MOUNT_OPT_SOURCE },
523 { NULL, 0, NULL, 0 }
97073441
KZ
524 };
525
a7349ee3 526 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
be6904b9 527 { 'B','M','R' }, /* bind,move,rbind */
51a37c19
KZ
528 { 'L','U', MOUNT_OPT_SOURCE }, /* label,uuid,source */
529 { 0 }
530 };
531 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
532
6189ace3 533 sanitize_env();
97073441
KZ
534 setlocale(LC_ALL, "");
535 bindtextdomain(PACKAGE, LOCALEDIR);
536 textdomain(PACKAGE);
efb8854f 537 atexit(close_stdout);
97073441 538
0b2b32e8
RM
539 strutils_set_exitcode(MNT_EX_USAGE);
540
97073441
KZ
541 mnt_init_debug(0);
542 cxt = mnt_new_context();
543 if (!cxt)
e3a7a5f8 544 err(MNT_EX_SYSERR, _("libmount context allocation failed"));
97073441 545
9f7472b0
KZ
546 mnt_context_set_tables_errcb(cxt, table_parser_errcb);
547
961d69f7 548 while ((c = getopt_long(argc, argv, "aBcfFhilL:Mno:O:rRsU:vVwt:T:",
97073441
KZ
549 longopts, NULL)) != -1) {
550
551 /* only few options are allowed for non-root users */
aedeaa40 552 if (mnt_context_is_restricted(cxt) &&
961d69f7 553 !strchr("hlLUVvrist", c) &&
aedeaa40
KZ
554 c != MOUNT_OPT_TARGET &&
555 c != MOUNT_OPT_SOURCE)
732a6311 556 exit_non_root(option_to_longopt(c, longopts));
97073441 557
51a37c19
KZ
558 err_exclusive_options(c, longopts, excl, excl_st);
559
97073441
KZ
560 switch(c) {
561 case 'a':
562 all = 1;
97073441
KZ
563 break;
564 case 'c':
565 mnt_context_disable_canonicalize(cxt, TRUE);
566 break;
567 case 'f':
568 mnt_context_enable_fake(cxt, TRUE);
569 break;
570 case 'F':
d2c97887 571 mnt_context_enable_fork(cxt, TRUE);
97073441
KZ
572 break;
573 case 'h':
6e1eda6f 574 usage();
97073441
KZ
575 break;
576 case 'i':
577 mnt_context_disable_helpers(cxt, TRUE);
578 break;
579 case 'n':
580 mnt_context_disable_mtab(cxt, TRUE);
581 break;
582 case 'r':
be6904b9 583 append_option(cxt, "ro");
6dede2f2 584 mnt_context_enable_rwonly_mount(cxt, FALSE);
97073441
KZ
585 break;
586 case 'v':
587 mnt_context_enable_verbose(cxt, TRUE);
588 break;
589 case 'V':
590 print_version();
591 break;
592 case 'w':
be6904b9 593 append_option(cxt, "rw");
6dede2f2 594 mnt_context_enable_rwonly_mount(cxt, TRUE);
97073441
KZ
595 break;
596 case 'o':
be6904b9 597 append_option(cxt, optarg);
97073441
KZ
598 break;
599 case 'O':
600 if (mnt_context_set_options_pattern(cxt, optarg))
e3a7a5f8 601 err(MNT_EX_SYSERR, _("failed to set options pattern"));
97073441
KZ
602 break;
603 case 'L':
aedeaa40
KZ
604 xasprintf(&srcbuf, "LABEL=\"%s\"", optarg);
605 mnt_context_disable_swapmatch(cxt, 1);
606 mnt_context_set_source(cxt, srcbuf);
607 free(srcbuf);
608 break;
97073441 609 case 'U':
aedeaa40
KZ
610 xasprintf(&srcbuf, "UUID=\"%s\"", optarg);
611 mnt_context_disable_swapmatch(cxt, 1);
612 mnt_context_set_source(cxt, srcbuf);
613 free(srcbuf);
97073441
KZ
614 break;
615 case 'l':
616 show_labels = 1;
617 break;
618 case 't':
619 types = optarg;
620 break;
64b6bc4f
KZ
621 case 'T':
622 fstab = append_fstab(cxt, fstab, optarg);
623 break;
97073441
KZ
624 case 's':
625 mnt_context_enable_sloppy(cxt, TRUE);
626 break;
627 case 'B':
d7890778 628 oper |= MS_BIND;
97073441
KZ
629 break;
630 case 'M':
d7890778 631 oper |= MS_MOVE;
97073441
KZ
632 break;
633 case 'R':
d7890778 634 oper |= (MS_BIND | MS_REC);
97073441 635 break;
2492f713 636 case MOUNT_OPT_SHARED:
be6904b9
KZ
637 append_option(cxt, "shared");
638 propa = 1;
97073441 639 break;
2492f713 640 case MOUNT_OPT_SLAVE:
be6904b9
KZ
641 append_option(cxt, "slave");
642 propa = 1;
97073441 643 break;
2492f713 644 case MOUNT_OPT_PRIVATE:
be6904b9
KZ
645 append_option(cxt, "private");
646 propa = 1;
97073441 647 break;
2492f713 648 case MOUNT_OPT_UNBINDABLE:
be6904b9
KZ
649 append_option(cxt, "unbindable");
650 propa = 1;
97073441 651 break;
2492f713 652 case MOUNT_OPT_RSHARED:
be6904b9
KZ
653 append_option(cxt, "rshared");
654 propa = 1;
97073441 655 break;
2492f713 656 case MOUNT_OPT_RSLAVE:
be6904b9
KZ
657 append_option(cxt, "rslave");
658 propa = 1;
97073441 659 break;
2492f713 660 case MOUNT_OPT_RPRIVATE:
be6904b9
KZ
661 append_option(cxt, "rprivate");
662 propa = 1;
97073441 663 break;
2492f713 664 case MOUNT_OPT_RUNBINDABLE:
be6904b9
KZ
665 append_option(cxt, "runbindable");
666 propa = 1;
97073441 667 break;
aedeaa40
KZ
668 case MOUNT_OPT_TARGET:
669 mnt_context_disable_swapmatch(cxt, 1);
670 mnt_context_set_target(cxt, optarg);
671 break;
672 case MOUNT_OPT_SOURCE:
aedeaa40
KZ
673 mnt_context_disable_swapmatch(cxt, 1);
674 mnt_context_set_source(cxt, optarg);
675 break;
97073441 676 default:
e3a7a5f8 677 errtryhelp(MNT_EX_USAGE);
97073441
KZ
678 }
679 }
680
681 argc -= optind;
682 argv += optind;
683
59414c6b
KZ
684 if (fstab && !mnt_context_is_nocanonicalize(cxt)) {
685 /*
686 * We have external (context independent) fstab instance, let's
687 * make a connection between the fstab and the canonicalization
688 * cache.
689 */
6195f9e6 690 mnt_table_set_cache(fstab, mnt_context_get_cache(cxt));
59414c6b
KZ
691 }
692
aedeaa40
KZ
693 if (!mnt_context_get_source(cxt) &&
694 !mnt_context_get_target(cxt) &&
695 !argc &&
696 !all) {
6e1eda6f
RM
697 if (oper || mnt_context_get_options(cxt)) {
698 warnx(_("bad usage"));
699 errtryhelp(MNT_EX_USAGE);
700 }
11754572 701 print_all(cxt, types, show_labels);
97073441
KZ
702 goto done;
703 }
704
1707b9b1
RT
705 /* Non-root users are allowed to use -t to print_all(),
706 but not to mount */
707 if (mnt_context_is_restricted(cxt) && types)
708 exit_non_root("types");
709
6e1eda6f
RM
710 if (oper && (types || all || mnt_context_get_source(cxt))) {
711 warnx(_("bad usage"));
712 errtryhelp(MNT_EX_USAGE);
713 }
97073441 714
9f7472b0
KZ
715 if (types && (all || strchr(types, ',') ||
716 strncmp(types, "no", 2) == 0))
97073441
KZ
717 mnt_context_set_fstype_pattern(cxt, types);
718 else if (types)
719 mnt_context_set_fstype(cxt, types);
720
a9ae3955
KZ
721 if (all) {
722 /*
723 * A) Mount all
724 */
d2c97887 725 rc = mount_all(cxt);
11754572 726 goto done;
a9ae3955 727
aedeaa40
KZ
728 } else if (argc == 0 && (mnt_context_get_source(cxt) ||
729 mnt_context_get_target(cxt))) {
a9ae3955 730 /*
aedeaa40 731 * B) mount -L|-U|--source|--target
6751f062
KZ
732 *
733 * non-root may specify source *or* target, but not both
a9ae3955 734 */
aedeaa40
KZ
735 if (mnt_context_is_restricted(cxt) &&
736 mnt_context_get_source(cxt) &&
737 mnt_context_get_target(cxt))
738 exit_non_root(NULL);
97073441 739
6751f062
KZ
740 } else if (argc == 1 && (!mnt_context_get_source(cxt) ||
741 !mnt_context_get_target(cxt))) {
a9ae3955 742 /*
aedeaa40 743 * C) mount [-L|-U|--source] <target>
6751f062 744 * mount [--target <dir>] <source>
a9ae3955 745 * mount <source|target>
aedeaa40
KZ
746 *
747 * non-root may specify source *or* target, but not both
6751f062
KZ
748 *
749 * It does not matter for libmount if we set source or target
750 * here (the library is able to swap it), but it matters for
751 * sanitize_paths().
a9ae3955 752 */
6751f062
KZ
753 int istag = mnt_tag_is_valid(argv[0]);
754
755 if (istag && mnt_context_get_source(cxt))
756 /* -L, -U or --source together with LABEL= or UUID= */
e3a7a5f8 757 errx(MNT_EX_USAGE, _("source specified more than once"));
6751f062
KZ
758 else if (istag || mnt_context_get_target(cxt))
759 mnt_context_set_source(cxt, argv[0]);
760 else
761 mnt_context_set_target(cxt, argv[0]);
762
aedeaa40 763 if (mnt_context_is_restricted(cxt) &&
6751f062
KZ
764 mnt_context_get_source(cxt) &&
765 mnt_context_get_target(cxt))
aedeaa40
KZ
766 exit_non_root(NULL);
767
aedeaa40
KZ
768 } else if (argc == 2 && !mnt_context_get_source(cxt)
769 && !mnt_context_get_target(cxt)) {
a9ae3955
KZ
770 /*
771 * D) mount <source> <target>
772 */
97073441
KZ
773 if (mnt_context_is_restricted(cxt))
774 exit_non_root(NULL);
6751f062 775
97073441
KZ
776 mnt_context_set_source(cxt, argv[0]);
777 mnt_context_set_target(cxt, argv[1]);
aedeaa40 778
6e1eda6f
RM
779 } else {
780 warnx(_("bad usage"));
781 errtryhelp(MNT_EX_USAGE);
782 }
97073441 783
5ebbc386
KZ
784 if (mnt_context_is_restricted(cxt))
785 sanitize_paths(cxt);
786
be6904b9
KZ
787 if (oper)
788 /* BIND/MOVE operations, let's set the mount flags */
68164f6c 789 mnt_context_set_mflags(cxt, oper);
97073441 790
ba986e81
KZ
791 if ((oper && !has_remount_flag(cxt)) || propa)
792 /* For --make-* or --bind is fstab/mtab unnecessary */
374fd21a 793 mnt_context_set_optsmode(cxt, MNT_OMODE_NOTAB);
374fd21a 794
cfb9db30 795 rc = mnt_context_mount(cxt);
ce433404
KZ
796 rc = mk_exit_code(cxt, rc);
797
e3a7a5f8 798 if (rc == MNT_EX_SUCCESS && mnt_context_is_verbose(cxt))
84600ddc 799 success_message(cxt);
97073441 800done:
97073441
KZ
801 mnt_free_context(cxt);
802 return rc;
803}
804