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