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