]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/mount.c
libmount: make mnt_context_is_fs_mounted work for /proc
[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"
a5ebeca5 39#include "xalloc.h"
efb8854f 40#include "closestream.h"
5ebbc386 41#include "canonicalize.h"
97073441 42
e3a7a5f8 43#define OPTUTILS_EXIT_CODE MNT_EX_USAGE
38483b86
SK
44#include "optutils.h"
45
97073441 46/*** TODO: DOCS:
374fd21a
KZ
47 *
48 * --options-mode={ignore,append,prepend,replace} MNT_OMODE_{IGNORE, ...}
49 * --options-source={fstab,mtab,disable} MNT_OMODE_{FSTAB,MTAB,NOTAB}
50 * --options-source-force MNT_OMODE_FORCE
97073441
KZ
51 */
52
d946359a
KZ
53static int mk_exit_code(struct libmnt_context *cxt, int rc);
54
97073441
KZ
55static void __attribute__((__noreturn__)) exit_non_root(const char *option)
56{
57 const uid_t ruid = getuid();
58 const uid_t euid = geteuid();
59
60 if (ruid == 0 && euid != 0) {
61 /* user is root, but setuid to non-root */
62 if (option)
e3a7a5f8 63 errx(MNT_EX_USAGE, _("only root can use \"--%s\" option "
97073441
KZ
64 "(effective UID is %u)"),
65 option, euid);
e3a7a5f8 66 errx(MNT_EX_USAGE, _("only root can do that "
97073441
KZ
67 "(effective UID is %u)"), euid);
68 }
69 if (option)
e3a7a5f8
KZ
70 errx(MNT_EX_USAGE, _("only root can use \"--%s\" option"), option);
71 errx(MNT_EX_USAGE, _("only root can do that"));
97073441
KZ
72}
73
74static void __attribute__((__noreturn__)) print_version(void)
75{
76 const char *ver = NULL;
ac8ecab4 77 const char **features = NULL, **p;
97073441
KZ
78
79 mnt_get_library_version(&ver);
ac8ecab4
KZ
80 mnt_get_library_features(&features);
81
82 printf(_("%s from %s (libmount %s"),
83 program_invocation_short_name,
84 PACKAGE_STRING,
85 ver);
86 p = features;
87 while (p && *p) {
88 fputs(p == features ? ": " : ", ", stdout);
89 fputs(*p++, stdout);
90 }
91 fputs(")\n", stdout);
e3a7a5f8 92 exit(MNT_EX_SUCCESS);
97073441
KZ
93}
94
7fc6d2b8 95static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
9f7472b0
KZ
96 const char *filename, int line)
97{
98 if (filename)
b779c1ae 99 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
1cd9d0d7 100 return 1;
9f7472b0
KZ
101}
102
5f7c1890
KZ
103/*
104 * Replace control chars with '?' to be compatible with coreutils. For more
105 * robust solution use findmnt(1) where we use \x?? hex encoding.
106 */
107static void safe_fputs(const char *data)
108{
109 const char *p;
110
111 for (p = data; p && *p; p++) {
112 if (iscntrl((unsigned char) *p))
113 fputc('?', stdout);
114 else
115 fputc(*p, stdout);
116 }
117}
118
11754572 119static void print_all(struct libmnt_context *cxt, char *pattern, int show_label)
97073441 120{
68164f6c 121 struct libmnt_table *tb;
a0c014dc 122 struct libmnt_iter *itr = NULL;
68164f6c
KZ
123 struct libmnt_fs *fs;
124 struct libmnt_cache *cache = NULL;
97073441 125
11754572 126 if (mnt_context_get_mtab(cxt, &tb))
e3a7a5f8 127 err(MNT_EX_SYSERR, _("failed to read mtab"));
97073441
KZ
128
129 itr = mnt_new_iter(MNT_ITER_FORWARD);
11754572 130 if (!itr)
e3a7a5f8 131 err(MNT_EX_SYSERR, _("failed to initialize libmount iterator"));
97073441
KZ
132 if (show_label)
133 cache = mnt_new_cache();
134
9f7472b0 135 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
97073441
KZ
136 const char *type = mnt_fs_get_fstype(fs);
137 const char *src = mnt_fs_get_source(fs);
7cf389f7 138 const char *optstr = mnt_fs_get_options(fs);
aa397ce5 139 char *xsrc = NULL;
97073441
KZ
140
141 if (type && pattern && !mnt_match_fstype(type, pattern))
142 continue;
143
aa397ce5
DR
144 if (!mnt_fs_is_pseudofs(fs))
145 xsrc = mnt_pretty_path(src, cache);
5f7c1890
KZ
146 printf ("%s on ", xsrc ? xsrc : src);
147 safe_fputs(mnt_fs_get_target(fs));
148
97073441
KZ
149 if (type)
150 printf (" type %s", type);
151 if (optstr)
152 printf (" (%s)", optstr);
153 if (show_label && src) {
154 char *lb = mnt_cache_find_tag_value(cache, src, "LABEL");
155 if (lb)
156 printf (" [%s]", lb);
157 }
158 fputc('\n', stdout);
2576b4e7 159 free(xsrc);
97073441 160 }
11754572 161
6195f9e6 162 mnt_unref_cache(cache);
b192b7b9 163 mnt_free_iter(itr);
97073441
KZ
164}
165
9f7472b0
KZ
166/*
167 * mount -a [-F]
9f7472b0 168 */
d2c97887 169static int mount_all(struct libmnt_context *cxt)
a9ae3955 170{
9f7472b0
KZ
171 struct libmnt_iter *itr;
172 struct libmnt_fs *fs;
e3a7a5f8 173 int mntrc, ignored, rc = MNT_EX_SUCCESS;
9f7472b0 174
16b73aae
KZ
175 int nsucc = 0, nerrs = 0;
176
9f7472b0
KZ
177 itr = mnt_new_iter(MNT_ITER_FORWARD);
178 if (!itr) {
179 warn(_("failed to initialize libmount iterator"));
e3a7a5f8 180 return MNT_EX_SYSERR;
9f7472b0
KZ
181 }
182
183 while (mnt_context_next_mount(cxt, itr, &fs, &mntrc, &ignored) == 0) {
184
185 const char *tgt = mnt_fs_get_target(fs);
186
187 if (ignored) {
188 if (mnt_context_is_verbose(cxt))
b3f7a0ec
KZ
189 printf(ignored == 1 ? _("%-25s: ignored\n") :
190 _("%-25s: already mounted\n"),
9f7472b0 191 tgt);
d2c97887 192 } else if (mnt_context_is_fork(cxt)) {
d946359a
KZ
193 if (mnt_context_is_verbose(cxt))
194 printf("%-25s: mount successfully forked\n", tgt);
9f7472b0 195 } else {
e3a7a5f8 196 if (mk_exit_code(cxt, mntrc) == MNT_EX_SUCCESS) {
16b73aae 197 nsucc++;
d946359a 198
e3a7a5f8 199 /* Note that MNT_EX_SUCCESS return code does
8ab82185
KZ
200 * not mean that FS has been really mounted
201 * (e.g. nofail option) */
202 if (mnt_context_get_status(cxt)
203 && mnt_context_is_verbose(cxt))
d946359a 204 printf("%-25s: successfully mounted\n", tgt);
16b73aae
KZ
205 } else
206 nerrs++;
9f7472b0
KZ
207 }
208 }
209
d2c97887
KZ
210 if (mnt_context_is_parent(cxt)) {
211 /* wait for mount --fork children */
16b73aae
KZ
212 int nchildren = 0;
213
214 nerrs = 0, nsucc = 0;
d2c97887
KZ
215
216 rc = mnt_context_wait_for_children(cxt, &nchildren, &nerrs);
217 if (!rc && nchildren)
16b73aae 218 nsucc = nchildren - nerrs;
d2c97887
KZ
219 }
220
16b73aae 221 if (nerrs == 0)
e3a7a5f8 222 rc = MNT_EX_SUCCESS; /* all success */
16b73aae 223 else if (nsucc == 0)
e3a7a5f8 224 rc = MNT_EX_FAIL; /* all failed */
16b73aae 225 else
e3a7a5f8 226 rc = MNT_EX_SOMEOK; /* some success, some failed */
16b73aae 227
25609ee1 228 mnt_free_iter(itr);
9f7472b0 229 return rc;
a9ae3955
KZ
230}
231
84600ddc
KZ
232static void success_message(struct libmnt_context *cxt)
233{
234 unsigned long mflags = 0;
f5ae1d70 235 const char *tgt, *src, *pr = program_invocation_short_name;
84600ddc
KZ
236
237 if (mnt_context_helper_executed(cxt)
238 || mnt_context_get_status(cxt) != 1)
239 return;
240
241 mnt_context_get_mflags(cxt, &mflags);
242 tgt = mnt_context_get_target(cxt);
243 src = mnt_context_get_source(cxt);
244
245 if (mflags & MS_MOVE)
f5ae1d70 246 printf(_("%s: %s moved to %s.\n"), pr, src, tgt);
84600ddc 247 else if (mflags & MS_BIND)
9b4257c8 248 printf(_("%s: %s bound on %s.\n"), pr, src, tgt);
b4ec4573
KZ
249 else if (mflags & MS_PROPAGATION) {
250 if (src && strcmp(src, "none") != 0 && tgt)
251 printf(_("%s: %s mounted on %s.\n"), pr, src, tgt);
252
f5ae1d70 253 printf(_("%s: %s propagation flags changed.\n"), pr, tgt);
b4ec4573 254 } else
f5ae1d70 255 printf(_("%s: %s mounted on %s.\n"), pr, src, tgt);
84600ddc
KZ
256}
257
4e45dfb9
KZ
258#if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
259#include <selinux/selinux.h>
260#include <selinux/context.h>
261
262static void selinux_warning(struct libmnt_context *cxt, const char *tgt)
263{
264
265 if (tgt && mnt_context_is_verbose(cxt) && is_selinux_enabled() > 0) {
266 security_context_t raw = NULL, def = NULL;
267
268 if (getfilecon(tgt, &raw) > 0
269 && security_get_initial_context("file", &def) == 0) {
270
271 if (!selinux_file_context_cmp(raw, def))
272 printf(_(
273 "mount: %s does not contain SELinux labels.\n"
274 " You just mounted an file system that supports labels which does not\n"
275 " contain labels, onto an SELinux box. It is likely that confined\n"
276 " applications will generate AVC messages and not be allowed access to\n"
277 " this file system. For more details see restorecon(8) and mount(8).\n"),
278 tgt);
279 }
280 freecon(raw);
281 freecon(def);
282 }
283}
284#else
b8c0f4fb 285# define selinux_warning(_x, _y)
4e45dfb9
KZ
286#endif
287
fcc0413a 288/*
e1706a67 289 * Returns exit status (MNT_EX_*) and/or prints error message.
ce433404
KZ
290 */
291static int mk_exit_code(struct libmnt_context *cxt, int rc)
292{
e1706a67
KZ
293 const char *tgt;
294 char buf[BUFSIZ] = { 0 };
6dede2f2 295
e1706a67
KZ
296 rc = mnt_context_get_excode(cxt, rc, buf, sizeof(buf));
297 tgt = mnt_context_get_target(cxt);
ce433404 298
e1706a67
KZ
299 if (*buf) {
300 const char *spec = tgt;
301 if (!spec)
302 spec = mnt_context_get_source(cxt);
303 if (!spec)
304 spec = "???";
305 warnx(_("%s: %s."), spec, buf);
ce433404 306 }
ce433404 307
0361cb6f
KZ
308 if (rc == MNT_EX_SUCCESS && mnt_context_get_status(cxt) == 1) {
309 selinux_warning(cxt, tgt);
ce433404 310 }
e1706a67 311 return rc;
ce433404
KZ
312}
313
64b6bc4f
KZ
314static struct libmnt_table *append_fstab(struct libmnt_context *cxt,
315 struct libmnt_table *fstab,
316 const char *path)
317{
318
319 if (!fstab) {
320 fstab = mnt_new_table();
321 if (!fstab)
e3a7a5f8 322 err(MNT_EX_SYSERR, _("failed to initialize libmount table"));
64b6bc4f
KZ
323
324 mnt_table_set_parser_errcb(fstab, table_parser_errcb);
325 mnt_context_set_fstab(cxt, fstab);
50fccba1
KZ
326
327 mnt_unref_table(fstab); /* reference is handled by @cxt now */
64b6bc4f
KZ
328 }
329
330 if (mnt_table_parse_fstab(fstab, path))
e3a7a5f8 331 errx(MNT_EX_USAGE,_("%s: failed to parse"), path);
64b6bc4f
KZ
332
333 return fstab;
334}
335
5ebbc386
KZ
336/*
337 * Check source and target paths -- non-root user should not be able to
338 * resolve paths which are unreadable for him.
339 */
340static void sanitize_paths(struct libmnt_context *cxt)
341{
342 const char *p;
343 struct libmnt_fs *fs = mnt_context_get_fs(cxt);
344
345 if (!fs)
346 return;
347
348 p = mnt_fs_get_target(fs);
349 if (p) {
350 char *np = canonicalize_path_restricted(p);
351 if (!np)
e3a7a5f8 352 err(MNT_EX_USAGE, "%s", p);
5ebbc386
KZ
353 mnt_fs_set_target(fs, np);
354 free(np);
355 }
356
357 p = mnt_fs_get_srcpath(fs);
358 if (p) {
359 char *np = canonicalize_path_restricted(p);
360 if (!np)
e3a7a5f8 361 err(MNT_EX_USAGE, "%s", p);
5ebbc386
KZ
362 mnt_fs_set_source(fs, np);
363 free(np);
364 }
365}
366
be6904b9
KZ
367static void append_option(struct libmnt_context *cxt, const char *opt)
368{
8225bb78 369 if (opt && (*opt == '=' || *opt == '\'' || *opt == '\"' || isblank(*opt)))
e3a7a5f8 370 errx(MNT_EX_USAGE, _("unsupported option format: %s"), opt);
be6904b9 371 if (mnt_context_append_options(cxt, opt))
e3a7a5f8 372 err(MNT_EX_SYSERR, _("failed to append option '%s'"), opt);
be6904b9
KZ
373}
374
ba986e81
KZ
375static int has_remount_flag(struct libmnt_context *cxt)
376{
377 unsigned long mflags = 0;
378
379 if (mnt_context_get_mflags(cxt, &mflags))
380 return 0;
381
382 return mflags & MS_REMOUNT;
383}
384
97073441
KZ
385static void __attribute__((__noreturn__)) usage(FILE *out)
386{
e4c92d06
KZ
387 fputs(USAGE_HEADER, out);
388 fprintf(out, _(
97073441
KZ
389 " %1$s [-lhV]\n"
390 " %1$s -a [options]\n"
aedeaa40 391 " %1$s [options] [--source] <source> | [--target] <directory>\n"
97073441
KZ
392 " %1$s [options] <source> <directory>\n"
393 " %1$s <operation> <mountpoint> [<target>]\n"),
394 program_invocation_short_name);
395
451dbcfa
BS
396 fputs(USAGE_SEPARATOR, out);
397 fputs(_("Mount a filesystem.\n"), out);
398
e4c92d06 399 fputs(USAGE_OPTIONS, out);
97073441 400 fprintf(out, _(
97073441 401 " -a, --all mount all filesystems mentioned in fstab\n"
97073441 402 " -c, --no-canonicalize don't canonicalize paths\n"
eaca47f7 403 " -f, --fake dry run; skip the mount(2) syscall\n"
64b6bc4f
KZ
404 " -F, --fork fork off for each device (use with -a)\n"
405 " -T, --fstab <path> alternative file to /etc/fstab\n"));
eaca47f7 406 fprintf(out, _(
89de71b3
BS
407 " -i, --internal-only don't call the mount.<type> helpers\n"));
408 fprintf(out, _(
409 " -l, --show-labels show also filesystem labels\n"));
410 fprintf(out, _(
eaca47f7
BS
411 " -n, --no-mtab don't write to /etc/mtab\n"));
412 fprintf(out, _(
413 " -o, --options <list> comma-separated list of mount options\n"
414 " -O, --test-opts <list> limit the set of filesystems (use with -a)\n"
415 " -r, --read-only mount the filesystem read-only (same as -o ro)\n"
416 " -t, --types <list> limit the set of filesystem types\n"));
417 fprintf(out, _(
aedeaa40
KZ
418 " --source <src> explicitly specifies source (path, label, uuid)\n"
419 " --target <target> explicitly specifies mountpoint\n"));
420 fprintf(out, _(
89de71b3
BS
421 " -v, --verbose say what is being done\n"));
422 fprintf(out, _(
6d402bbe 423 " -w, --rw, --read-write mount the filesystem read-write (default)\n"));
97073441 424
e4c92d06
KZ
425 fputs(USAGE_SEPARATOR, out);
426 fputs(USAGE_HELP, out);
427 fputs(USAGE_VERSION, out);
428
eaca47f7 429 fprintf(out, _(
97073441
KZ
430 "\nSource:\n"
431 " -L, --label <label> synonym for LABEL=<label>\n"
432 " -U, --uuid <uuid> synonym for UUID=<uuid>\n"
433 " LABEL=<label> specifies device by filesystem label\n"
eb0eb262
KZ
434 " UUID=<uuid> specifies device by filesystem UUID\n"
435 " PARTLABEL=<label> specifies device by partition label\n"
436 " PARTUUID=<uuid> specifies device by partition UUID\n"));
437
eaca47f7 438 fprintf(out, _(
97073441
KZ
439 " <device> specifies device by path\n"
440 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
eaca47f7 441 " <file> regular file for loopdev setup\n"));
97073441 442
eaca47f7 443 fprintf(out, _(
97073441
KZ
444 "\nOperations:\n"
445 " -B, --bind mount a subtree somewhere else (same as -o bind)\n"
446 " -M, --move move a subtree to some other place\n"
eaca47f7
BS
447 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
448 fprintf(out, _(
97073441
KZ
449 " --make-shared mark a subtree as shared\n"
450 " --make-slave mark a subtree as slave\n"
451 " --make-private mark a subtree as private\n"
eaca47f7
BS
452 " --make-unbindable mark a subtree as unbindable\n"));
453 fprintf(out, _(
97073441
KZ
454 " --make-rshared recursively mark a whole subtree as shared\n"
455 " --make-rslave recursively mark a whole subtree as slave\n"
456 " --make-rprivate recursively mark a whole subtree as private\n"
eaca47f7 457 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
97073441 458
e4c92d06 459 fprintf(out, USAGE_MAN_TAIL("mount(8)"));
97073441 460
e3a7a5f8 461 exit(out == stderr ? MNT_EX_USAGE : MNT_EX_SUCCESS);
97073441
KZ
462}
463
464int main(int argc, char **argv)
465{
e3a7a5f8 466 int c, rc = MNT_EX_SUCCESS, all = 0, show_labels = 0;
68164f6c 467 struct libmnt_context *cxt;
64b6bc4f 468 struct libmnt_table *fstab = NULL;
aedeaa40 469 char *srcbuf = NULL;
97073441
KZ
470 char *types = NULL;
471 unsigned long oper = 0;
be6904b9 472 int propa = 0;
97073441 473
2492f713
KZ
474 enum {
475 MOUNT_OPT_SHARED = CHAR_MAX + 1,
476 MOUNT_OPT_SLAVE,
477 MOUNT_OPT_PRIVATE,
478 MOUNT_OPT_UNBINDABLE,
479 MOUNT_OPT_RSHARED,
480 MOUNT_OPT_RSLAVE,
481 MOUNT_OPT_RPRIVATE,
aedeaa40
KZ
482 MOUNT_OPT_RUNBINDABLE,
483 MOUNT_OPT_TARGET,
484 MOUNT_OPT_SOURCE
2492f713
KZ
485 };
486
6c7d5ae9 487 static const struct option longopts[] = {
87918040
SK
488 { "all", no_argument, NULL, 'a' },
489 { "fake", no_argument, NULL, 'f' },
490 { "fstab", required_argument, NULL, 'T' },
491 { "fork", no_argument, NULL, 'F' },
492 { "help", no_argument, NULL, 'h' },
493 { "no-mtab", no_argument, NULL, 'n' },
494 { "read-only", no_argument, NULL, 'r' },
495 { "ro", no_argument, NULL, 'r' },
496 { "verbose", no_argument, NULL, 'v' },
497 { "version", no_argument, NULL, 'V' },
498 { "read-write", no_argument, NULL, 'w' },
499 { "rw", no_argument, NULL, 'w' },
500 { "options", required_argument, NULL, 'o' },
501 { "test-opts", required_argument, NULL, 'O' },
502 { "types", required_argument, NULL, 't' },
503 { "uuid", required_argument, NULL, 'U' },
504 { "label", required_argument, NULL, 'L' },
505 { "bind", no_argument, NULL, 'B' },
506 { "move", no_argument, NULL, 'M' },
507 { "rbind", no_argument, NULL, 'R' },
508 { "make-shared", no_argument, NULL, MOUNT_OPT_SHARED },
509 { "make-slave", no_argument, NULL, MOUNT_OPT_SLAVE },
510 { "make-private", no_argument, NULL, MOUNT_OPT_PRIVATE },
511 { "make-unbindable", no_argument, NULL, MOUNT_OPT_UNBINDABLE },
512 { "make-rshared", no_argument, NULL, MOUNT_OPT_RSHARED },
513 { "make-rslave", no_argument, NULL, MOUNT_OPT_RSLAVE },
514 { "make-rprivate", no_argument, NULL, MOUNT_OPT_RPRIVATE },
515 { "make-runbindable", no_argument, NULL, MOUNT_OPT_RUNBINDABLE },
516 { "no-canonicalize", no_argument, NULL, 'c' },
517 { "internal-only", no_argument, NULL, 'i' },
518 { "show-labels", no_argument, NULL, 'l' },
519 { "target", required_argument, NULL, MOUNT_OPT_TARGET },
520 { "source", required_argument, NULL, MOUNT_OPT_SOURCE },
521 { NULL, 0, NULL, 0 }
97073441
KZ
522 };
523
a7349ee3 524 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
be6904b9 525 { 'B','M','R' }, /* bind,move,rbind */
51a37c19
KZ
526 { 'L','U', MOUNT_OPT_SOURCE }, /* label,uuid,source */
527 { 0 }
528 };
529 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
530
6189ace3 531 sanitize_env();
97073441
KZ
532 setlocale(LC_ALL, "");
533 bindtextdomain(PACKAGE, LOCALEDIR);
534 textdomain(PACKAGE);
efb8854f 535 atexit(close_stdout);
97073441
KZ
536
537 mnt_init_debug(0);
538 cxt = mnt_new_context();
539 if (!cxt)
e3a7a5f8 540 err(MNT_EX_SYSERR, _("libmount context allocation failed"));
97073441 541
9f7472b0
KZ
542 mnt_context_set_tables_errcb(cxt, table_parser_errcb);
543
961d69f7 544 while ((c = getopt_long(argc, argv, "aBcfFhilL:Mno:O:rRsU:vVwt:T:",
97073441
KZ
545 longopts, NULL)) != -1) {
546
547 /* only few options are allowed for non-root users */
aedeaa40 548 if (mnt_context_is_restricted(cxt) &&
961d69f7 549 !strchr("hlLUVvrist", c) &&
aedeaa40
KZ
550 c != MOUNT_OPT_TARGET &&
551 c != MOUNT_OPT_SOURCE)
732a6311 552 exit_non_root(option_to_longopt(c, longopts));
97073441 553
51a37c19
KZ
554 err_exclusive_options(c, longopts, excl, excl_st);
555
97073441
KZ
556 switch(c) {
557 case 'a':
558 all = 1;
97073441
KZ
559 break;
560 case 'c':
561 mnt_context_disable_canonicalize(cxt, TRUE);
562 break;
563 case 'f':
564 mnt_context_enable_fake(cxt, TRUE);
565 break;
566 case 'F':
d2c97887 567 mnt_context_enable_fork(cxt, TRUE);
97073441
KZ
568 break;
569 case 'h':
570 usage(stdout);
571 break;
572 case 'i':
573 mnt_context_disable_helpers(cxt, TRUE);
574 break;
575 case 'n':
576 mnt_context_disable_mtab(cxt, TRUE);
577 break;
578 case 'r':
be6904b9 579 append_option(cxt, "ro");
6dede2f2 580 mnt_context_enable_rwonly_mount(cxt, FALSE);
97073441
KZ
581 break;
582 case 'v':
583 mnt_context_enable_verbose(cxt, TRUE);
584 break;
585 case 'V':
586 print_version();
587 break;
588 case 'w':
be6904b9 589 append_option(cxt, "rw");
6dede2f2 590 mnt_context_enable_rwonly_mount(cxt, TRUE);
97073441
KZ
591 break;
592 case 'o':
be6904b9 593 append_option(cxt, optarg);
97073441
KZ
594 break;
595 case 'O':
596 if (mnt_context_set_options_pattern(cxt, optarg))
e3a7a5f8 597 err(MNT_EX_SYSERR, _("failed to set options pattern"));
97073441
KZ
598 break;
599 case 'L':
aedeaa40
KZ
600 xasprintf(&srcbuf, "LABEL=\"%s\"", optarg);
601 mnt_context_disable_swapmatch(cxt, 1);
602 mnt_context_set_source(cxt, srcbuf);
603 free(srcbuf);
604 break;
97073441 605 case 'U':
aedeaa40
KZ
606 xasprintf(&srcbuf, "UUID=\"%s\"", optarg);
607 mnt_context_disable_swapmatch(cxt, 1);
608 mnt_context_set_source(cxt, srcbuf);
609 free(srcbuf);
97073441
KZ
610 break;
611 case 'l':
612 show_labels = 1;
613 break;
614 case 't':
615 types = optarg;
616 break;
64b6bc4f
KZ
617 case 'T':
618 fstab = append_fstab(cxt, fstab, optarg);
619 break;
97073441
KZ
620 case 's':
621 mnt_context_enable_sloppy(cxt, TRUE);
622 break;
623 case 'B':
d7890778 624 oper |= MS_BIND;
97073441
KZ
625 break;
626 case 'M':
d7890778 627 oper |= MS_MOVE;
97073441
KZ
628 break;
629 case 'R':
d7890778 630 oper |= (MS_BIND | MS_REC);
97073441 631 break;
2492f713 632 case MOUNT_OPT_SHARED:
be6904b9
KZ
633 append_option(cxt, "shared");
634 propa = 1;
97073441 635 break;
2492f713 636 case MOUNT_OPT_SLAVE:
be6904b9
KZ
637 append_option(cxt, "slave");
638 propa = 1;
97073441 639 break;
2492f713 640 case MOUNT_OPT_PRIVATE:
be6904b9
KZ
641 append_option(cxt, "private");
642 propa = 1;
97073441 643 break;
2492f713 644 case MOUNT_OPT_UNBINDABLE:
be6904b9
KZ
645 append_option(cxt, "unbindable");
646 propa = 1;
97073441 647 break;
2492f713 648 case MOUNT_OPT_RSHARED:
be6904b9
KZ
649 append_option(cxt, "rshared");
650 propa = 1;
97073441 651 break;
2492f713 652 case MOUNT_OPT_RSLAVE:
be6904b9
KZ
653 append_option(cxt, "rslave");
654 propa = 1;
97073441 655 break;
2492f713 656 case MOUNT_OPT_RPRIVATE:
be6904b9
KZ
657 append_option(cxt, "rprivate");
658 propa = 1;
97073441 659 break;
2492f713 660 case MOUNT_OPT_RUNBINDABLE:
be6904b9
KZ
661 append_option(cxt, "runbindable");
662 propa = 1;
97073441 663 break;
aedeaa40
KZ
664 case MOUNT_OPT_TARGET:
665 mnt_context_disable_swapmatch(cxt, 1);
666 mnt_context_set_target(cxt, optarg);
667 break;
668 case MOUNT_OPT_SOURCE:
aedeaa40
KZ
669 mnt_context_disable_swapmatch(cxt, 1);
670 mnt_context_set_source(cxt, optarg);
671 break;
97073441 672 default:
e3a7a5f8 673 errtryhelp(MNT_EX_USAGE);
97073441
KZ
674 }
675 }
676
677 argc -= optind;
678 argv += optind;
679
59414c6b
KZ
680 if (fstab && !mnt_context_is_nocanonicalize(cxt)) {
681 /*
682 * We have external (context independent) fstab instance, let's
683 * make a connection between the fstab and the canonicalization
684 * cache.
685 */
6195f9e6 686 mnt_table_set_cache(fstab, mnt_context_get_cache(cxt));
59414c6b
KZ
687 }
688
aedeaa40
KZ
689 if (!mnt_context_get_source(cxt) &&
690 !mnt_context_get_target(cxt) &&
691 !argc &&
692 !all) {
be6904b9 693 if (oper || mnt_context_get_options(cxt))
97073441 694 usage(stderr);
11754572 695 print_all(cxt, types, show_labels);
97073441
KZ
696 goto done;
697 }
698
1707b9b1
RT
699 /* Non-root users are allowed to use -t to print_all(),
700 but not to mount */
701 if (mnt_context_is_restricted(cxt) && types)
702 exit_non_root("types");
703
aedeaa40 704 if (oper && (types || all || mnt_context_get_source(cxt)))
97073441
KZ
705 usage(stderr);
706
9f7472b0
KZ
707 if (types && (all || strchr(types, ',') ||
708 strncmp(types, "no", 2) == 0))
97073441
KZ
709 mnt_context_set_fstype_pattern(cxt, types);
710 else if (types)
711 mnt_context_set_fstype(cxt, types);
712
a9ae3955
KZ
713 if (all) {
714 /*
715 * A) Mount all
716 */
d2c97887 717 rc = mount_all(cxt);
11754572 718 goto done;
a9ae3955 719
aedeaa40
KZ
720 } else if (argc == 0 && (mnt_context_get_source(cxt) ||
721 mnt_context_get_target(cxt))) {
a9ae3955 722 /*
aedeaa40 723 * B) mount -L|-U|--source|--target
6751f062
KZ
724 *
725 * non-root may specify source *or* target, but not both
a9ae3955 726 */
aedeaa40
KZ
727 if (mnt_context_is_restricted(cxt) &&
728 mnt_context_get_source(cxt) &&
729 mnt_context_get_target(cxt))
730 exit_non_root(NULL);
97073441 731
6751f062
KZ
732 } else if (argc == 1 && (!mnt_context_get_source(cxt) ||
733 !mnt_context_get_target(cxt))) {
a9ae3955 734 /*
aedeaa40 735 * C) mount [-L|-U|--source] <target>
6751f062 736 * mount [--target <dir>] <source>
a9ae3955 737 * mount <source|target>
aedeaa40
KZ
738 *
739 * non-root may specify source *or* target, but not both
6751f062
KZ
740 *
741 * It does not matter for libmount if we set source or target
742 * here (the library is able to swap it), but it matters for
743 * sanitize_paths().
a9ae3955 744 */
6751f062
KZ
745 int istag = mnt_tag_is_valid(argv[0]);
746
747 if (istag && mnt_context_get_source(cxt))
748 /* -L, -U or --source together with LABEL= or UUID= */
e3a7a5f8 749 errx(MNT_EX_USAGE, _("source specified more than once"));
6751f062
KZ
750 else if (istag || mnt_context_get_target(cxt))
751 mnt_context_set_source(cxt, argv[0]);
752 else
753 mnt_context_set_target(cxt, argv[0]);
754
aedeaa40 755 if (mnt_context_is_restricted(cxt) &&
6751f062
KZ
756 mnt_context_get_source(cxt) &&
757 mnt_context_get_target(cxt))
aedeaa40
KZ
758 exit_non_root(NULL);
759
aedeaa40
KZ
760 } else if (argc == 2 && !mnt_context_get_source(cxt)
761 && !mnt_context_get_target(cxt)) {
a9ae3955
KZ
762 /*
763 * D) mount <source> <target>
764 */
97073441
KZ
765 if (mnt_context_is_restricted(cxt))
766 exit_non_root(NULL);
6751f062 767
97073441
KZ
768 mnt_context_set_source(cxt, argv[0]);
769 mnt_context_set_target(cxt, argv[1]);
aedeaa40 770
97073441
KZ
771 } else
772 usage(stderr);
773
5ebbc386
KZ
774 if (mnt_context_is_restricted(cxt))
775 sanitize_paths(cxt);
776
be6904b9
KZ
777 if (oper)
778 /* BIND/MOVE operations, let's set the mount flags */
68164f6c 779 mnt_context_set_mflags(cxt, oper);
97073441 780
ba986e81
KZ
781 if ((oper && !has_remount_flag(cxt)) || propa)
782 /* For --make-* or --bind is fstab/mtab unnecessary */
374fd21a 783 mnt_context_set_optsmode(cxt, MNT_OMODE_NOTAB);
374fd21a 784
cfb9db30 785 rc = mnt_context_mount(cxt);
ce433404
KZ
786 rc = mk_exit_code(cxt, rc);
787
e3a7a5f8 788 if (rc == MNT_EX_SUCCESS && mnt_context_is_verbose(cxt))
84600ddc 789 success_message(cxt);
97073441 790done:
97073441
KZ
791 mnt_free_context(cxt);
792 return rc;
793}
794