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