]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/mount.c
su: ignore --preserve-environment, it's mutually exclusive to --login
[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"
497d2ce7 39#include "exitcodes.h"
a5ebeca5 40#include "xalloc.h"
efb8854f 41#include "closestream.h"
5ebbc386 42#include "canonicalize.h"
97073441 43
38483b86
SK
44#define OPTUTILS_EXIT_CODE MOUNT_EX_USAGE
45#include "optutils.h"
46
97073441 47/*** TODO: DOCS:
374fd21a
KZ
48 *
49 * --options-mode={ignore,append,prepend,replace} MNT_OMODE_{IGNORE, ...}
50 * --options-source={fstab,mtab,disable} MNT_OMODE_{FSTAB,MTAB,NOTAB}
51 * --options-source-force MNT_OMODE_FORCE
97073441
KZ
52 */
53
ce433404 54static int readwrite;
dbae36fe 55
d946359a
KZ
56static int mk_exit_code(struct libmnt_context *cxt, int rc);
57
97073441
KZ
58static void __attribute__((__noreturn__)) exit_non_root(const char *option)
59{
60 const uid_t ruid = getuid();
61 const uid_t euid = geteuid();
62
63 if (ruid == 0 && euid != 0) {
64 /* user is root, but setuid to non-root */
65 if (option)
497d2ce7 66 errx(MOUNT_EX_USAGE, _("only root can use \"--%s\" option "
97073441
KZ
67 "(effective UID is %u)"),
68 option, euid);
497d2ce7 69 errx(MOUNT_EX_USAGE, _("only root can do that "
97073441
KZ
70 "(effective UID is %u)"), euid);
71 }
72 if (option)
497d2ce7
KZ
73 errx(MOUNT_EX_USAGE, _("only root can use \"--%s\" option"), option);
74 errx(MOUNT_EX_USAGE, _("only root can do that"));
97073441
KZ
75}
76
77static void __attribute__((__noreturn__)) print_version(void)
78{
79 const char *ver = NULL;
ac8ecab4 80 const char **features = NULL, **p;
97073441
KZ
81
82 mnt_get_library_version(&ver);
ac8ecab4
KZ
83 mnt_get_library_features(&features);
84
85 printf(_("%s from %s (libmount %s"),
86 program_invocation_short_name,
87 PACKAGE_STRING,
88 ver);
89 p = features;
90 while (p && *p) {
91 fputs(p == features ? ": " : ", ", stdout);
92 fputs(*p++, stdout);
93 }
94 fputs(")\n", stdout);
497d2ce7 95 exit(MOUNT_EX_SUCCESS);
97073441
KZ
96}
97
7fc6d2b8 98static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
9f7472b0
KZ
99 const char *filename, int line)
100{
101 if (filename)
102 warnx(_("%s: parse error: ignore entry at line %d."),
103 filename, line);
104 return 0;
105}
106
5f7c1890
KZ
107/*
108 * Replace control chars with '?' to be compatible with coreutils. For more
109 * robust solution use findmnt(1) where we use \x?? hex encoding.
110 */
111static void safe_fputs(const char *data)
112{
113 const char *p;
114
115 for (p = data; p && *p; p++) {
116 if (iscntrl((unsigned char) *p))
117 fputc('?', stdout);
118 else
119 fputc(*p, stdout);
120 }
121}
122
11754572 123static void print_all(struct libmnt_context *cxt, char *pattern, int show_label)
97073441 124{
68164f6c 125 struct libmnt_table *tb;
a0c014dc 126 struct libmnt_iter *itr = NULL;
68164f6c
KZ
127 struct libmnt_fs *fs;
128 struct libmnt_cache *cache = NULL;
97073441 129
11754572 130 if (mnt_context_get_mtab(cxt, &tb))
497d2ce7 131 err(MOUNT_EX_SYSERR, _("failed to read mtab"));
97073441
KZ
132
133 itr = mnt_new_iter(MNT_ITER_FORWARD);
11754572 134 if (!itr)
497d2ce7 135 err(MOUNT_EX_SYSERR, _("failed to initialize libmount iterator"));
97073441
KZ
136 if (show_label)
137 cache = mnt_new_cache();
138
9f7472b0 139 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
97073441
KZ
140 const char *type = mnt_fs_get_fstype(fs);
141 const char *src = mnt_fs_get_source(fs);
7cf389f7 142 const char *optstr = mnt_fs_get_options(fs);
aa397ce5 143 char *xsrc = NULL;
97073441
KZ
144
145 if (type && pattern && !mnt_match_fstype(type, pattern))
146 continue;
147
aa397ce5
DR
148 if (!mnt_fs_is_pseudofs(fs))
149 xsrc = mnt_pretty_path(src, cache);
5f7c1890
KZ
150 printf ("%s on ", xsrc ? xsrc : src);
151 safe_fputs(mnt_fs_get_target(fs));
152
97073441
KZ
153 if (type)
154 printf (" type %s", type);
155 if (optstr)
156 printf (" (%s)", optstr);
157 if (show_label && src) {
158 char *lb = mnt_cache_find_tag_value(cache, src, "LABEL");
159 if (lb)
160 printf (" [%s]", lb);
161 }
162 fputc('\n', stdout);
2576b4e7 163 free(xsrc);
97073441 164 }
11754572 165
97073441 166 mnt_free_cache(cache);
b192b7b9 167 mnt_free_iter(itr);
97073441
KZ
168}
169
9f7472b0
KZ
170/*
171 * mount -a [-F]
9f7472b0 172 */
d2c97887 173static int mount_all(struct libmnt_context *cxt)
a9ae3955 174{
9f7472b0
KZ
175 struct libmnt_iter *itr;
176 struct libmnt_fs *fs;
497d2ce7 177 int mntrc, ignored, rc = MOUNT_EX_SUCCESS;
9f7472b0 178
16b73aae
KZ
179 int nsucc = 0, nerrs = 0;
180
9f7472b0
KZ
181 itr = mnt_new_iter(MNT_ITER_FORWARD);
182 if (!itr) {
183 warn(_("failed to initialize libmount iterator"));
497d2ce7 184 return MOUNT_EX_SYSERR;
9f7472b0
KZ
185 }
186
187 while (mnt_context_next_mount(cxt, itr, &fs, &mntrc, &ignored) == 0) {
188
189 const char *tgt = mnt_fs_get_target(fs);
190
191 if (ignored) {
192 if (mnt_context_is_verbose(cxt))
b3f7a0ec
KZ
193 printf(ignored == 1 ? _("%-25s: ignored\n") :
194 _("%-25s: already mounted\n"),
9f7472b0 195 tgt);
d2c97887 196 } else if (mnt_context_is_fork(cxt)) {
d946359a
KZ
197 if (mnt_context_is_verbose(cxt))
198 printf("%-25s: mount successfully forked\n", tgt);
9f7472b0 199 } else {
16b73aae 200 mk_exit_code(cxt, mntrc); /* to print warnings */
9f7472b0 201
d946359a 202 if (mnt_context_get_status(cxt)) {
16b73aae 203 nsucc++;
d946359a
KZ
204
205 if (mnt_context_is_verbose(cxt))
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
KZ
223 if (nerrs == 0)
224 rc = MOUNT_EX_SUCCESS; /* all success */
225 else if (nsucc == 0)
226 rc = MOUNT_EX_FAIL; /* all failed */
227 else
228 rc = MOUNT_EX_SOMEOK; /* some success, some failed */
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
ce433404
KZ
260/*
261 * Handles generic errors like ENOMEM, ...
262 *
263 * rc = 0 success
264 * <0 error (usually -errno)
265 *
497d2ce7 266 * Returns exit status (MOUNT_EX_*) and prints error message.
ce433404 267 */
2e86597f 268static int handle_generic_errors(int rc, const char *msg, ...)
ce433404 269{
2e86597f
KZ
270 va_list va;
271
272 va_start(va, msg);
ce433404
KZ
273 errno = -rc;
274
275 switch(errno) {
276 case EINVAL:
277 case EPERM:
2e86597f 278 vwarn(msg, va);
497d2ce7 279 rc = MOUNT_EX_USAGE;
2e86597f 280 break;
ce433404 281 case ENOMEM:
2e86597f 282 vwarn(msg, va);
497d2ce7 283 rc = MOUNT_EX_SYSERR;
2e86597f 284 break;
ce433404 285 default:
2e86597f 286 vwarn(msg, va);
497d2ce7 287 rc = MOUNT_EX_FAIL;
ce433404
KZ
288 break;
289 }
2e86597f
KZ
290 va_end(va);
291 return rc;
ce433404
KZ
292}
293
4e45dfb9
KZ
294#if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
295#include <selinux/selinux.h>
296#include <selinux/context.h>
297
298static void selinux_warning(struct libmnt_context *cxt, const char *tgt)
299{
300
301 if (tgt && mnt_context_is_verbose(cxt) && is_selinux_enabled() > 0) {
302 security_context_t raw = NULL, def = NULL;
303
304 if (getfilecon(tgt, &raw) > 0
305 && security_get_initial_context("file", &def) == 0) {
306
307 if (!selinux_file_context_cmp(raw, def))
308 printf(_(
309 "mount: %s does not contain SELinux labels.\n"
310 " You just mounted an file system that supports labels which does not\n"
311 " contain labels, onto an SELinux box. It is likely that confined\n"
312 " applications will generate AVC messages and not be allowed access to\n"
313 " this file system. For more details see restorecon(8) and mount(8).\n"),
314 tgt);
315 }
316 freecon(raw);
317 freecon(def);
318 }
319}
320#else
b8c0f4fb 321# define selinux_warning(_x, _y)
4e45dfb9
KZ
322#endif
323
fcc0413a
KZ
324/*
325 * Returns 1 if @dir parent is shared
326 */
327static int is_shared_tree(struct libmnt_context *cxt, const char *dir)
328{
329 struct libmnt_table *tb = NULL;
330 struct libmnt_fs *fs;
331 unsigned long mflags = 0;
332 char *mnt = NULL, *p;
333 int rc = 0;
334
335 if (!dir)
336 return 0;
337 if (mnt_context_get_mtab(cxt, &tb) || !tb)
338 goto done;
339 mnt = xstrdup(dir);
340 p = strrchr(mnt, '/');
341 if (!p)
342 goto done;
343 if (p > mnt)
344 *p = '\0';
345 fs = mnt_table_find_mountpoint(tb, mnt, MNT_ITER_BACKWARD);
346
347 rc = fs && mnt_fs_is_kernel(fs)
348 && mnt_fs_get_propagation(fs, &mflags) == 0
349 && (mflags & MS_SHARED);
350done:
351 free(mnt);
352 return rc;
353}
354
ce433404
KZ
355/*
356 * rc = 0 success
357 * <0 error (usually -errno or -1)
358 *
497d2ce7 359 * Returns exit status (MOUNT_EX_*) and/or prints error message.
ce433404
KZ
360 */
361static int mk_exit_code(struct libmnt_context *cxt, int rc)
362{
363 int syserr;
364 struct stat st;
365 unsigned long uflags = 0, mflags = 0;
366
367 int restricted = mnt_context_is_restricted(cxt);
368 const char *tgt = mnt_context_get_target(cxt);
369 const char *src = mnt_context_get_source(cxt);
370
371try_readonly:
be6904b9 372 if (mnt_context_helper_executed(cxt)) {
ce433404
KZ
373 /*
374 * /sbin/mount.<type> called, return status
375 */
be6904b9
KZ
376 if (rc == -MNT_ERR_APPLYFLAGS)
377 warnx(_("WARNING: failed to apply propagation flags"));
ce433404 378 return mnt_context_get_helper_status(cxt);
be6904b9 379 }
ce433404 380
4e45dfb9 381 if (rc == 0 && mnt_context_get_status(cxt) == 1) {
ce433404
KZ
382 /*
383 * Libmount success && syscall success.
384 */
4e45dfb9
KZ
385 selinux_warning(cxt, tgt);
386
497d2ce7 387 return MOUNT_EX_SUCCESS; /* mount(2) success */
4e45dfb9 388 }
ce433404 389
10389b1e
KZ
390 mnt_context_get_mflags(cxt, &mflags); /* mount(2) flags */
391 mnt_context_get_user_mflags(cxt, &uflags); /* userspace flags */
392
ce433404
KZ
393 if (!mnt_context_syscall_called(cxt)) {
394 /*
8b470b20 395 * libmount errors (extra library checks)
ce433404 396 */
8b470b20
KZ
397 switch (rc) {
398 case -EPERM:
ce433404 399 warnx(_("only root can mount %s on %s"), src, tgt);
497d2ce7 400 return MOUNT_EX_USAGE;
8b470b20
KZ
401 case -EBUSY:
402 warnx(_("%s is already mounted"), src);
497d2ce7 403 return MOUNT_EX_USAGE;
ba24923e 404 case -MNT_ERR_NOFSTAB:
aedeaa40
KZ
405 if (mnt_context_is_swapmatch(cxt)) {
406 warnx(_("can't find %s in %s"),
407 src ? src : tgt,
ce433404 408 mnt_get_fstab_path());
aedeaa40
KZ
409 return MOUNT_EX_USAGE;
410 }
411 /* source/target explicitly defined */
412 if (tgt)
413 warnx(_("can't find mountpoint %s in %s"),
414 tgt, mnt_get_fstab_path());
415 else
416 warnx(_("can't find mount source %s in %s"),
417 src, mnt_get_fstab_path());
497d2ce7 418 return MOUNT_EX_USAGE;
82a2c160
KZ
419 case -MNT_ERR_AMBIFS:
420 warnx(_("%s: more filesystems detected. This should not happen,\n"
421 " use -t <type> to explicitly specify the filesystem type or\n"
422 " use wipefs(8) to clean up the device."), src);
423 return MOUNT_EX_USAGE;
ba24923e 424 case -MNT_ERR_NOFSTYPE:
ce433404
KZ
425 if (restricted)
426 warnx(_("I could not determine the filesystem type, "
427 "and none was specified"));
428 else
429 warnx(_("you must specify the filesystem type"));
497d2ce7 430 return MOUNT_EX_USAGE;
ba24923e
KZ
431 case -MNT_ERR_NOSOURCE:
432 if (src)
433 warnx(_("can't find %s"), src);
434 else
435 warnx(_("mount source not defined"));
436 return MOUNT_EX_USAGE;
61f5ff6c
KZ
437 case -MNT_ERR_MOUNTOPT:
438 if (errno)
439 warn(_("failed to parse mount options"));
440 else
441 warnx(_("failed to parse mount options"));
442 return MOUNT_EX_USAGE;
10389b1e 443 case -MNT_ERR_LOOPDEV:
5cf05c71 444 warn(_("%s: failed to setup loop device"), src);
10389b1e 445 return MOUNT_EX_FAIL;
ba24923e
KZ
446 default:
447 return handle_generic_errors(rc, _("%s: mount failed"),
448 tgt ? tgt : src);
449 }
ce433404
KZ
450 } else if (mnt_context_get_syscall_errno(cxt) == 0) {
451 /*
452 * mount(2) syscall success, but something else failed
453 * (probably error in mtab processing).
454 */
455 if (rc < 0)
456 return handle_generic_errors(rc,
2e86597f
KZ
457 _("%s: filesystem mounted, but mount(8) failed"),
458 tgt ? tgt : src);
ce433404 459
497d2ce7 460 return MOUNT_EX_SOFTWARE; /* internal error */
ce433404
KZ
461
462 }
463
464 /*
465 * mount(2) errors
466 */
467 syserr = mnt_context_get_syscall_errno(cxt);
468
ce433404
KZ
469
470 switch(syserr) {
471 case EPERM:
472 if (geteuid() == 0) {
473 if (stat(tgt, &st) || !S_ISDIR(st.st_mode))
474 warnx(_("mount point %s is not a directory"), tgt);
475 else
476 warnx(_("permission denied"));
477 } else
478 warnx(_("must be superuser to use mount"));
479 break;
480
481 case EBUSY:
482 {
483 struct libmnt_table *tb;
ce433404
KZ
484
485 if (mflags & MS_REMOUNT) {
486 warnx(_("%s is busy"), tgt);
487 break;
488 }
489
490 warnx(_("%s is already mounted or %s busy"), src, tgt);
491
492 if (src && mnt_context_get_mtab(cxt, &tb) == 0) {
493 struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_FORWARD);
494 struct libmnt_fs *fs;
495
496 while(mnt_table_next_fs(tb, itr, &fs) == 0) {
497 const char *s = mnt_fs_get_srcpath(fs),
498 *t = mnt_fs_get_target(fs);
499
6699e742 500 if (t && s && mnt_fs_streq_srcpath(fs, src))
ce433404
KZ
501 fprintf(stderr, _(
502 " %s is already mounted on %s\n"), s, t);
503 }
504 mnt_free_iter(itr);
505 }
506 break;
507 }
508 case ENOENT:
fcc0413a 509 if (tgt && lstat(tgt, &st))
ce433404 510 warnx(_("mount point %s does not exist"), tgt);
fcc0413a 511 else if (tgt && stat(tgt, &st))
ce433404 512 warnx(_("mount point %s is a symbolic link to nowhere"), tgt);
fcc0413a 513 else if (src && stat(src, &st)) {
ce433404 514 if (uflags & MNT_MS_NOFAIL)
497d2ce7 515 return MOUNT_EX_SUCCESS;
ce433404
KZ
516
517 warnx(_("special device %s does not exist"), src);
518 } else {
519 errno = syserr;
520 warn(_("mount(2) failed")); /* print errno */
521 }
522 break;
523
524 case ENOTDIR:
525 if (stat(tgt, &st) || ! S_ISDIR(st.st_mode))
526 warnx(_("mount point %s is not a directory"), tgt);
fcc0413a 527 else if (src && stat(src, &st) && errno == ENOTDIR) {
ce433404 528 if (uflags & MNT_MS_NOFAIL)
497d2ce7 529 return MOUNT_EX_SUCCESS;
ce433404
KZ
530
531 warnx(_("special device %s does not exist "
532 "(a path prefix is not a directory)"), src);
533 } else {
534 errno = syserr;
535 warn(_("mount(2) failed")); /* print errno */
536 }
537 break;
538
539 case EINVAL:
540 if (mflags & MS_REMOUNT)
541 warnx(_("%s not mounted or bad option"), tgt);
be6904b9 542 else if (rc == -MNT_ERR_APPLYFLAGS)
58f108ef 543 warnx(_("%s is not mountpoint or bad option"), tgt);
fcc0413a
KZ
544 else if ((mflags & MS_MOVE) && is_shared_tree(cxt, src))
545 warnx(_("bad option. Note that moving a mount residing under a shared\n"
546 " mount is unsupported."));
ce433404
KZ
547 else
548 warnx(_("wrong fs type, bad option, bad superblock on %s,\n"
549 " missing codepage or helper program, or other error"),
550 src);
551
552 if (mnt_fs_is_netfs(mnt_context_get_fs(cxt)))
553 fprintf(stderr, _(
554 " (for several filesystems (e.g. nfs, cifs) you might\n"
555 " need a /sbin/mount.<type> helper program)\n"));
556
fcc0413a 557 fprintf(stderr, _("\n"
ce433404 558 " In some cases useful info is found in syslog - try\n"
fcc0413a 559 " dmesg | tail or so.\n"));
ce433404
KZ
560 break;
561
562 case EMFILE:
563 warnx(_("mount table full"));
564 break;
565
566 case EIO:
567 warnx(_("%s: can't read superblock"), src);
568 break;
569
570 case ENODEV:
571 warnx(_("unknown filesystem type '%s'"), mnt_context_get_fstype(cxt));
572 break;
573
574 case ENOTBLK:
575 if (uflags & MNT_MS_NOFAIL)
497d2ce7 576 return MOUNT_EX_SUCCESS;
ce433404
KZ
577
578 if (stat(src, &st))
579 warnx(_("%s is not a block device, and stat(2) fails?"), src);
580 else if (S_ISBLK(st.st_mode))
581 warnx(_("the kernel does not recognize %s as a block device\n"
582 " (maybe `modprobe driver'?)"), src);
583 else if (S_ISREG(st.st_mode))
584 warnx(_("%s is not a block device (maybe try `-o loop'?)"), src);
585 else
586 warnx(_(" %s is not a block device"), src);
587 break;
588
589 case ENXIO:
590 if (uflags & MNT_MS_NOFAIL)
497d2ce7 591 return MOUNT_EX_SUCCESS;
ce433404
KZ
592
593 warnx(_("%s is not a valid block device"), src);
594 break;
595
596 case EACCES:
597 case EROFS:
598 if (mflags & MS_RDONLY)
599 warnx(_("cannot mount %s read-only"), src);
600
601 else if (readwrite)
602 warnx(_("%s is write-protected but explicit `-w' flag given"), src);
603
604 else if (mflags & MS_REMOUNT)
605 warnx(_("cannot remount %s read-write, is write-protected"), src);
606
607 else {
608 warnx(_("%s is write-protected, mounting read-only"), src);
609
610 mnt_context_reset_status(cxt);
611 mnt_context_set_mflags(cxt, mflags | MS_RDONLY);
612 rc = mnt_context_do_mount(cxt);
613 if (!rc)
614 rc = mnt_context_finalize_mount(cxt);
615
616 goto try_readonly;
617 }
618 break;
619
620 case ENOMEDIUM:
621 warnx(_("no medium found on %s"), src);
622 break;
623
624 default:
625 warn(_("mount %s on %s failed"), src, tgt);
626 break;
627 }
628
497d2ce7 629 return MOUNT_EX_FAIL;
ce433404
KZ
630}
631
64b6bc4f
KZ
632static struct libmnt_table *append_fstab(struct libmnt_context *cxt,
633 struct libmnt_table *fstab,
634 const char *path)
635{
636
637 if (!fstab) {
638 fstab = mnt_new_table();
639 if (!fstab)
640 err(MOUNT_EX_SYSERR, _("failed to initialize libmount table"));
641
642 mnt_table_set_parser_errcb(fstab, table_parser_errcb);
643 mnt_context_set_fstab(cxt, fstab);
644 }
645
646 if (mnt_table_parse_fstab(fstab, path))
647 errx(MOUNT_EX_USAGE,_("%s: failed to parse"), path);
648
649 return fstab;
650}
651
5ebbc386
KZ
652/*
653 * Check source and target paths -- non-root user should not be able to
654 * resolve paths which are unreadable for him.
655 */
656static void sanitize_paths(struct libmnt_context *cxt)
657{
658 const char *p;
659 struct libmnt_fs *fs = mnt_context_get_fs(cxt);
660
661 if (!fs)
662 return;
663
664 p = mnt_fs_get_target(fs);
665 if (p) {
666 char *np = canonicalize_path_restricted(p);
667 if (!np)
668 err(MOUNT_EX_USAGE, "%s", p);
669 mnt_fs_set_target(fs, np);
670 free(np);
671 }
672
673 p = mnt_fs_get_srcpath(fs);
674 if (p) {
675 char *np = canonicalize_path_restricted(p);
676 if (!np)
677 err(MOUNT_EX_USAGE, "%s", p);
678 mnt_fs_set_source(fs, np);
679 free(np);
680 }
681}
682
be6904b9
KZ
683static void append_option(struct libmnt_context *cxt, const char *opt)
684{
685 if (mnt_context_append_options(cxt, opt))
686 err(MOUNT_EX_SYSERR, _("failed to append option '%s'"), opt);
687}
688
97073441
KZ
689static void __attribute__((__noreturn__)) usage(FILE *out)
690{
e4c92d06
KZ
691 fputs(USAGE_HEADER, out);
692 fprintf(out, _(
97073441
KZ
693 " %1$s [-lhV]\n"
694 " %1$s -a [options]\n"
aedeaa40 695 " %1$s [options] [--source] <source> | [--target] <directory>\n"
97073441
KZ
696 " %1$s [options] <source> <directory>\n"
697 " %1$s <operation> <mountpoint> [<target>]\n"),
698 program_invocation_short_name);
699
e4c92d06 700 fputs(USAGE_OPTIONS, out);
97073441 701 fprintf(out, _(
97073441 702 " -a, --all mount all filesystems mentioned in fstab\n"
97073441 703 " -c, --no-canonicalize don't canonicalize paths\n"
eaca47f7 704 " -f, --fake dry run; skip the mount(2) syscall\n"
64b6bc4f
KZ
705 " -F, --fork fork off for each device (use with -a)\n"
706 " -T, --fstab <path> alternative file to /etc/fstab\n"));
eaca47f7
BS
707 fprintf(out, _(
708 " -h, --help display this help text and exit\n"
97073441
KZ
709 " -i, --internal-only don't call the mount.<type> helpers\n"
710 " -l, --show-labels lists all mounts with LABELs\n"
eaca47f7
BS
711 " -n, --no-mtab don't write to /etc/mtab\n"));
712 fprintf(out, _(
713 " -o, --options <list> comma-separated list of mount options\n"
714 " -O, --test-opts <list> limit the set of filesystems (use with -a)\n"
715 " -r, --read-only mount the filesystem read-only (same as -o ro)\n"
716 " -t, --types <list> limit the set of filesystem types\n"));
717 fprintf(out, _(
aedeaa40
KZ
718 " --source <src> explicitly specifies source (path, label, uuid)\n"
719 " --target <target> explicitly specifies mountpoint\n"));
720 fprintf(out, _(
eaca47f7
BS
721 " -v, --verbose say what is being done\n"
722 " -V, --version display version information and exit\n"
6d402bbe 723 " -w, --rw, --read-write mount the filesystem read-write (default)\n"));
97073441 724
e4c92d06
KZ
725 fputs(USAGE_SEPARATOR, out);
726 fputs(USAGE_HELP, out);
727 fputs(USAGE_VERSION, out);
728
eaca47f7 729 fprintf(out, _(
97073441
KZ
730 "\nSource:\n"
731 " -L, --label <label> synonym for LABEL=<label>\n"
732 " -U, --uuid <uuid> synonym for UUID=<uuid>\n"
733 " LABEL=<label> specifies device by filesystem label\n"
eb0eb262
KZ
734 " UUID=<uuid> specifies device by filesystem UUID\n"
735 " PARTLABEL=<label> specifies device by partition label\n"
736 " PARTUUID=<uuid> specifies device by partition UUID\n"));
737
eaca47f7 738 fprintf(out, _(
97073441
KZ
739 " <device> specifies device by path\n"
740 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
eaca47f7 741 " <file> regular file for loopdev setup\n"));
97073441 742
eaca47f7 743 fprintf(out, _(
97073441
KZ
744 "\nOperations:\n"
745 " -B, --bind mount a subtree somewhere else (same as -o bind)\n"
746 " -M, --move move a subtree to some other place\n"
eaca47f7
BS
747 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
748 fprintf(out, _(
97073441
KZ
749 " --make-shared mark a subtree as shared\n"
750 " --make-slave mark a subtree as slave\n"
751 " --make-private mark a subtree as private\n"
eaca47f7
BS
752 " --make-unbindable mark a subtree as unbindable\n"));
753 fprintf(out, _(
97073441
KZ
754 " --make-rshared recursively mark a whole subtree as shared\n"
755 " --make-rslave recursively mark a whole subtree as slave\n"
756 " --make-rprivate recursively mark a whole subtree as private\n"
eaca47f7 757 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
97073441 758
e4c92d06 759 fprintf(out, USAGE_MAN_TAIL("mount(8)"));
97073441 760
497d2ce7 761 exit(out == stderr ? MOUNT_EX_USAGE : MOUNT_EX_SUCCESS);
97073441
KZ
762}
763
764int main(int argc, char **argv)
765{
497d2ce7 766 int c, rc = MOUNT_EX_SUCCESS, all = 0, show_labels = 0;
68164f6c 767 struct libmnt_context *cxt;
64b6bc4f 768 struct libmnt_table *fstab = NULL;
aedeaa40 769 char *srcbuf = NULL;
97073441
KZ
770 char *types = NULL;
771 unsigned long oper = 0;
be6904b9 772 int propa = 0;
97073441 773
2492f713
KZ
774 enum {
775 MOUNT_OPT_SHARED = CHAR_MAX + 1,
776 MOUNT_OPT_SLAVE,
777 MOUNT_OPT_PRIVATE,
778 MOUNT_OPT_UNBINDABLE,
779 MOUNT_OPT_RSHARED,
780 MOUNT_OPT_RSLAVE,
781 MOUNT_OPT_RPRIVATE,
aedeaa40
KZ
782 MOUNT_OPT_RUNBINDABLE,
783 MOUNT_OPT_TARGET,
784 MOUNT_OPT_SOURCE
2492f713
KZ
785 };
786
6c7d5ae9 787 static const struct option longopts[] = {
97073441
KZ
788 { "all", 0, 0, 'a' },
789 { "fake", 0, 0, 'f' },
64b6bc4f 790 { "fstab", 1, 0, 'T' },
97073441
KZ
791 { "fork", 0, 0, 'F' },
792 { "help", 0, 0, 'h' },
793 { "no-mtab", 0, 0, 'n' },
794 { "read-only", 0, 0, 'r' },
795 { "ro", 0, 0, 'r' },
796 { "verbose", 0, 0, 'v' },
797 { "version", 0, 0, 'V' },
798 { "read-write", 0, 0, 'w' },
799 { "rw", 0, 0, 'w' },
800 { "options", 1, 0, 'o' },
801 { "test-opts", 1, 0, 'O' },
dbae36fe 802 { "pass-fd", 1, 0, 'p' },
97073441
KZ
803 { "types", 1, 0, 't' },
804 { "uuid", 1, 0, 'U' },
805 { "label", 1, 0, 'L'},
806 { "bind", 0, 0, 'B' },
807 { "move", 0, 0, 'M' },
808 { "rbind", 0, 0, 'R' },
2492f713
KZ
809 { "make-shared", 0, 0, MOUNT_OPT_SHARED },
810 { "make-slave", 0, 0, MOUNT_OPT_SLAVE },
811 { "make-private", 0, 0, MOUNT_OPT_PRIVATE },
812 { "make-unbindable", 0, 0, MOUNT_OPT_UNBINDABLE },
813 { "make-rshared", 0, 0, MOUNT_OPT_RSHARED },
814 { "make-rslave", 0, 0, MOUNT_OPT_RSLAVE },
815 { "make-rprivate", 0, 0, MOUNT_OPT_RPRIVATE },
816 { "make-runbindable", 0, 0, MOUNT_OPT_RUNBINDABLE },
97073441
KZ
817 { "no-canonicalize", 0, 0, 'c' },
818 { "internal-only", 0, 0, 'i' },
819 { "show-labels", 0, 0, 'l' },
aedeaa40
KZ
820 { "target", 1, 0, MOUNT_OPT_TARGET },
821 { "source", 1, 0, MOUNT_OPT_SOURCE },
97073441
KZ
822 { NULL, 0, 0, 0 }
823 };
824
51a37c19 825 static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */
be6904b9 826 { 'B','M','R' }, /* bind,move,rbind */
51a37c19
KZ
827 { 'L','U', MOUNT_OPT_SOURCE }, /* label,uuid,source */
828 { 0 }
829 };
830 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
831
6189ace3 832 sanitize_env();
97073441
KZ
833 setlocale(LC_ALL, "");
834 bindtextdomain(PACKAGE, LOCALEDIR);
835 textdomain(PACKAGE);
efb8854f 836 atexit(close_stdout);
97073441
KZ
837
838 mnt_init_debug(0);
839 cxt = mnt_new_context();
840 if (!cxt)
497d2ce7 841 err(MOUNT_EX_SYSERR, _("libmount context allocation failed"));
97073441 842
9f7472b0
KZ
843 mnt_context_set_tables_errcb(cxt, table_parser_errcb);
844
64b6bc4f 845 while ((c = getopt_long(argc, argv, "aBcfFhilL:Mno:O:p:rRsU:vVwt:T:",
97073441
KZ
846 longopts, NULL)) != -1) {
847
848 /* only few options are allowed for non-root users */
aedeaa40 849 if (mnt_context_is_restricted(cxt) &&
1707b9b1 850 !strchr("hlLUVvprist", c) &&
aedeaa40
KZ
851 c != MOUNT_OPT_TARGET &&
852 c != MOUNT_OPT_SOURCE)
732a6311 853 exit_non_root(option_to_longopt(c, longopts));
97073441 854
51a37c19
KZ
855 err_exclusive_options(c, longopts, excl, excl_st);
856
97073441
KZ
857 switch(c) {
858 case 'a':
859 all = 1;
97073441
KZ
860 break;
861 case 'c':
862 mnt_context_disable_canonicalize(cxt, TRUE);
863 break;
864 case 'f':
865 mnt_context_enable_fake(cxt, TRUE);
866 break;
867 case 'F':
d2c97887 868 mnt_context_enable_fork(cxt, TRUE);
97073441
KZ
869 break;
870 case 'h':
871 usage(stdout);
872 break;
873 case 'i':
874 mnt_context_disable_helpers(cxt, TRUE);
875 break;
876 case 'n':
877 mnt_context_disable_mtab(cxt, TRUE);
878 break;
879 case 'r':
be6904b9 880 append_option(cxt, "ro");
ce433404 881 readwrite = 0;
97073441
KZ
882 break;
883 case 'v':
884 mnt_context_enable_verbose(cxt, TRUE);
885 break;
886 case 'V':
887 print_version();
888 break;
889 case 'w':
be6904b9 890 append_option(cxt, "rw");
ce433404 891 readwrite = 1;
97073441
KZ
892 break;
893 case 'o':
be6904b9 894 append_option(cxt, optarg);
97073441
KZ
895 break;
896 case 'O':
897 if (mnt_context_set_options_pattern(cxt, optarg))
497d2ce7 898 err(MOUNT_EX_SYSERR, _("failed to set options pattern"));
97073441 899 break;
dbae36fe 900 case 'p':
5cf05c71 901 warnx(_("--pass-fd is no longer supported"));
dbae36fe 902 break;
97073441 903 case 'L':
aedeaa40
KZ
904 xasprintf(&srcbuf, "LABEL=\"%s\"", optarg);
905 mnt_context_disable_swapmatch(cxt, 1);
906 mnt_context_set_source(cxt, srcbuf);
907 free(srcbuf);
908 break;
97073441 909 case 'U':
aedeaa40
KZ
910 xasprintf(&srcbuf, "UUID=\"%s\"", optarg);
911 mnt_context_disable_swapmatch(cxt, 1);
912 mnt_context_set_source(cxt, srcbuf);
913 free(srcbuf);
97073441
KZ
914 break;
915 case 'l':
916 show_labels = 1;
917 break;
918 case 't':
919 types = optarg;
920 break;
64b6bc4f
KZ
921 case 'T':
922 fstab = append_fstab(cxt, fstab, optarg);
923 break;
97073441
KZ
924 case 's':
925 mnt_context_enable_sloppy(cxt, TRUE);
926 break;
927 case 'B':
d7890778 928 oper |= MS_BIND;
97073441
KZ
929 break;
930 case 'M':
d7890778 931 oper |= MS_MOVE;
97073441
KZ
932 break;
933 case 'R':
d7890778 934 oper |= (MS_BIND | MS_REC);
97073441 935 break;
2492f713 936 case MOUNT_OPT_SHARED:
be6904b9
KZ
937 append_option(cxt, "shared");
938 propa = 1;
97073441 939 break;
2492f713 940 case MOUNT_OPT_SLAVE:
be6904b9
KZ
941 append_option(cxt, "slave");
942 propa = 1;
97073441 943 break;
2492f713 944 case MOUNT_OPT_PRIVATE:
be6904b9
KZ
945 append_option(cxt, "private");
946 propa = 1;
97073441 947 break;
2492f713 948 case MOUNT_OPT_UNBINDABLE:
be6904b9
KZ
949 append_option(cxt, "unbindable");
950 propa = 1;
97073441 951 break;
2492f713 952 case MOUNT_OPT_RSHARED:
be6904b9
KZ
953 append_option(cxt, "rshared");
954 propa = 1;
97073441 955 break;
2492f713 956 case MOUNT_OPT_RSLAVE:
be6904b9
KZ
957 append_option(cxt, "rslave");
958 propa = 1;
97073441 959 break;
2492f713 960 case MOUNT_OPT_RPRIVATE:
be6904b9
KZ
961 append_option(cxt, "rprivate");
962 propa = 1;
97073441 963 break;
2492f713 964 case MOUNT_OPT_RUNBINDABLE:
be6904b9
KZ
965 append_option(cxt, "runbindable");
966 propa = 1;
97073441 967 break;
aedeaa40
KZ
968 case MOUNT_OPT_TARGET:
969 mnt_context_disable_swapmatch(cxt, 1);
970 mnt_context_set_target(cxt, optarg);
971 break;
972 case MOUNT_OPT_SOURCE:
aedeaa40
KZ
973 mnt_context_disable_swapmatch(cxt, 1);
974 mnt_context_set_source(cxt, optarg);
975 break;
97073441
KZ
976 default:
977 usage(stderr);
978 break;
979 }
980 }
981
982 argc -= optind;
983 argv += optind;
984
59414c6b
KZ
985 if (fstab && !mnt_context_is_nocanonicalize(cxt)) {
986 /*
987 * We have external (context independent) fstab instance, let's
988 * make a connection between the fstab and the canonicalization
989 * cache.
990 */
991 struct libmnt_cache *cache = mnt_context_get_cache(cxt);
992 mnt_table_set_cache(fstab, cache);
993 }
994
aedeaa40
KZ
995 if (!mnt_context_get_source(cxt) &&
996 !mnt_context_get_target(cxt) &&
997 !argc &&
998 !all) {
be6904b9 999 if (oper || mnt_context_get_options(cxt))
97073441 1000 usage(stderr);
11754572 1001 print_all(cxt, types, show_labels);
97073441
KZ
1002 goto done;
1003 }
1004
1707b9b1
RT
1005 /* Non-root users are allowed to use -t to print_all(),
1006 but not to mount */
1007 if (mnt_context_is_restricted(cxt) && types)
1008 exit_non_root("types");
1009
aedeaa40 1010 if (oper && (types || all || mnt_context_get_source(cxt)))
97073441
KZ
1011 usage(stderr);
1012
9f7472b0
KZ
1013 if (types && (all || strchr(types, ',') ||
1014 strncmp(types, "no", 2) == 0))
97073441
KZ
1015 mnt_context_set_fstype_pattern(cxt, types);
1016 else if (types)
1017 mnt_context_set_fstype(cxt, types);
1018
a9ae3955
KZ
1019 if (all) {
1020 /*
1021 * A) Mount all
1022 */
d2c97887 1023 rc = mount_all(cxt);
11754572 1024 goto done;
a9ae3955 1025
aedeaa40
KZ
1026 } else if (argc == 0 && (mnt_context_get_source(cxt) ||
1027 mnt_context_get_target(cxt))) {
a9ae3955 1028 /*
aedeaa40 1029 * B) mount -L|-U|--source|--target
a9ae3955 1030 */
aedeaa40
KZ
1031 if (mnt_context_is_restricted(cxt) &&
1032 mnt_context_get_source(cxt) &&
1033 mnt_context_get_target(cxt))
1034 exit_non_root(NULL);
97073441
KZ
1035
1036 } else if (argc == 1) {
a9ae3955 1037 /*
aedeaa40 1038 * C) mount [-L|-U|--source] <target>
a9ae3955 1039 * mount <source|target>
aedeaa40
KZ
1040 *
1041 * non-root may specify source *or* target, but not both
a9ae3955 1042 */
aedeaa40
KZ
1043 if (mnt_context_is_restricted(cxt) &&
1044 mnt_context_get_source(cxt))
1045 exit_non_root(NULL);
1046
97073441
KZ
1047 mnt_context_set_target(cxt, argv[0]);
1048
aedeaa40
KZ
1049 } else if (argc == 2 && !mnt_context_get_source(cxt)
1050 && !mnt_context_get_target(cxt)) {
a9ae3955
KZ
1051 /*
1052 * D) mount <source> <target>
1053 */
97073441
KZ
1054 if (mnt_context_is_restricted(cxt))
1055 exit_non_root(NULL);
1056 mnt_context_set_source(cxt, argv[0]);
1057 mnt_context_set_target(cxt, argv[1]);
aedeaa40 1058
97073441
KZ
1059 } else
1060 usage(stderr);
1061
5ebbc386
KZ
1062 if (mnt_context_is_restricted(cxt))
1063 sanitize_paths(cxt);
1064
be6904b9
KZ
1065 if (oper)
1066 /* BIND/MOVE operations, let's set the mount flags */
68164f6c 1067 mnt_context_set_mflags(cxt, oper);
97073441 1068
be6904b9
KZ
1069 if (oper || propa)
1070 /* For --make-* or --bind is fstab unnecessary */
374fd21a 1071 mnt_context_set_optsmode(cxt, MNT_OMODE_NOTAB);
374fd21a 1072
cfb9db30 1073 rc = mnt_context_mount(cxt);
ce433404
KZ
1074 rc = mk_exit_code(cxt, rc);
1075
84600ddc
KZ
1076 if (rc == MOUNT_EX_SUCCESS && mnt_context_is_verbose(cxt))
1077 success_message(cxt);
97073441 1078done:
97073441 1079 mnt_free_context(cxt);
64b6bc4f 1080 mnt_free_table(fstab);
97073441
KZ
1081 return rc;
1082}
1083