[58143] | 1 | /* $Id: mount.vboxsf.c 100317 2023-06-28 10:34:21Z vboxsync $ */
|
---|
[2614] | 2 | /** @file
|
---|
[58143] | 3 | * VirtualBox Guest Additions for Linux - mount(8) helper.
|
---|
[2614] | 4 | *
|
---|
| 5 | * Parses options provided by mount (or user directly)
|
---|
| 6 | * Packs them into struct vbsfmount and passes to mount(2)
|
---|
| 7 | * Optionally adds entries to mtab
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | /*
|
---|
[98103] | 11 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[2614] | 12 | *
|
---|
[96407] | 13 | * This file is part of VirtualBox base platform packages, as
|
---|
| 14 | * available from https://www.virtualbox.org.
|
---|
| 15 | *
|
---|
| 16 | * This program is free software; you can redistribute it and/or
|
---|
| 17 | * modify it under the terms of the GNU General Public License
|
---|
| 18 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 19 | * License.
|
---|
| 20 | *
|
---|
| 21 | * This program is distributed in the hope that it will be useful, but
|
---|
| 22 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 24 | * General Public License for more details.
|
---|
| 25 | *
|
---|
| 26 | * You should have received a copy of the GNU General Public License
|
---|
| 27 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 28 | *
|
---|
| 29 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[2614] | 30 | */
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | #ifndef _GNU_SOURCE
|
---|
[58143] | 34 | # define _GNU_SOURCE
|
---|
[2614] | 35 | #endif
|
---|
| 36 |
|
---|
| 37 | /* #define DEBUG */
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <fcntl.h>
|
---|
| 40 | #include <ctype.h>
|
---|
| 41 | #include <getopt.h>
|
---|
| 42 | #include <mntent.h>
|
---|
| 43 | #include <pwd.h>
|
---|
| 44 | #include <stdarg.h>
|
---|
| 45 | #include <stdio.h>
|
---|
| 46 | #include <stdlib.h>
|
---|
[77139] | 47 | #include <stdint.h>
|
---|
[2614] | 48 | #include <string.h>
|
---|
| 49 | #include <sys/mount.h>
|
---|
| 50 | #include <sys/stat.h>
|
---|
| 51 | #include <sys/types.h>
|
---|
| 52 | #include <unistd.h>
|
---|
| 53 | #include <mntent.h>
|
---|
| 54 | #include <limits.h>
|
---|
| 55 | #include <iconv.h>
|
---|
[87463] | 56 | #include <sys/utsname.h>
|
---|
| 57 | #include <linux/version.h>
|
---|
[2614] | 58 |
|
---|
| 59 | #include "vbsfmount.h"
|
---|
| 60 |
|
---|
[87463] | 61 | #include <iprt/assertcompile.h>
|
---|
| 62 | #include <iprt/string.h>
|
---|
[2614] | 63 |
|
---|
[58143] | 64 |
|
---|
[87463] | 65 | /*********************************************************************************************************************************
|
---|
| 66 | * Defined Constants And Macros *
|
---|
| 67 | *********************************************************************************************************************************/
|
---|
[2614] | 68 | #define PANIC_ATTR __attribute ((noreturn, __format__ (__printf__, 1, 2)))
|
---|
| 69 |
|
---|
[87463] | 70 |
|
---|
| 71 | /*********************************************************************************************************************************
|
---|
| 72 | * Structures and Typedefs *
|
---|
| 73 | *********************************************************************************************************************************/
|
---|
| 74 | struct vbsf_mount_opts
|
---|
| 75 | {
|
---|
| 76 | unsigned long fFlags; /**< MS_XXX */
|
---|
| 77 |
|
---|
| 78 | /** @name Preformatted option=value or empty if not specified.
|
---|
| 79 | * Helps eliminate duplicate options as well as simplifying concatting.
|
---|
| 80 | * @{ */
|
---|
| 81 | char szTTL[32];
|
---|
| 82 | char szMsDirCacheTTL[32];
|
---|
| 83 | char szMsInodeTTL[32];
|
---|
| 84 | char szMaxIoPages[32];
|
---|
| 85 | char szDirBuf[32];
|
---|
| 86 | char szCacheMode[32];
|
---|
| 87 | char szUid[32];
|
---|
| 88 | char szGid[32];
|
---|
| 89 | char szDMode[32];
|
---|
| 90 | char szFMode[32];
|
---|
| 91 | char szDMask[32];
|
---|
| 92 | char szFMask[32];
|
---|
| 93 | char szIoCharset[32];
|
---|
| 94 | /** @} */
|
---|
| 95 |
|
---|
| 96 | bool fSloppy;
|
---|
| 97 | char *pszConvertCp;
|
---|
| 98 | };
|
---|
| 99 |
|
---|
| 100 |
|
---|
[2614] | 101 | static void PANIC_ATTR
|
---|
[30153] | 102 | panic(const char *fmt, ...)
|
---|
[2614] | 103 | {
|
---|
| 104 | va_list ap;
|
---|
| 105 |
|
---|
[30153] | 106 | va_start(ap, fmt);
|
---|
| 107 | vfprintf(stderr, fmt, ap);
|
---|
| 108 | va_end(ap);
|
---|
| 109 | exit(EXIT_FAILURE);
|
---|
[2614] | 110 | }
|
---|
| 111 |
|
---|
| 112 | static void PANIC_ATTR
|
---|
[30153] | 113 | panic_err(const char *fmt, ...)
|
---|
[2614] | 114 | {
|
---|
| 115 | va_list ap;
|
---|
| 116 | int errno_code = errno;
|
---|
| 117 |
|
---|
[30153] | 118 | va_start(ap, fmt);
|
---|
| 119 | vfprintf(stderr, fmt, ap);
|
---|
| 120 | va_end(ap);
|
---|
| 121 | fprintf(stderr, ": %s\n", strerror(errno_code));
|
---|
| 122 | exit(EXIT_FAILURE);
|
---|
[2614] | 123 | }
|
---|
| 124 |
|
---|
| 125 | static int
|
---|
[30153] | 126 | safe_atoi(const char *s, size_t size, int base)
|
---|
[2614] | 127 | {
|
---|
| 128 | char *endptr;
|
---|
[30153] | 129 | long long int val = strtoll(s, &endptr, base);
|
---|
[2614] | 130 |
|
---|
[77859] | 131 | if ( val < INT_MIN
|
---|
| 132 | || ( val > INT_MAX
|
---|
| 133 | && (base != 8 || val != UINT_MAX) ) /* hack for printf("%o", -1) - 037777777777 */
|
---|
| 134 | || endptr < s + size)
|
---|
[16099] | 135 | {
|
---|
[2614] | 136 | errno = ERANGE;
|
---|
[77859] | 137 | panic_err("could not convert %.*s to integer, result = %lld (%d)",
|
---|
| 138 | (int)size, s, val, (int)val);
|
---|
[2614] | 139 | }
|
---|
[30153] | 140 | return (int)val;
|
---|
[2614] | 141 | }
|
---|
| 142 |
|
---|
[77953] | 143 | static unsigned
|
---|
| 144 | safe_atoiu(const char *s, size_t size, int base)
|
---|
| 145 | {
|
---|
| 146 | char *endptr;
|
---|
| 147 | long long int val = strtoll(s, &endptr, base);
|
---|
| 148 |
|
---|
| 149 | if ( val < 0
|
---|
| 150 | || val > UINT_MAX
|
---|
| 151 | || endptr < s + size)
|
---|
| 152 | {
|
---|
| 153 | errno = ERANGE;
|
---|
| 154 | panic_err("could not convert %.*s to unsigned integer, result = %lld (%#llx)",
|
---|
| 155 | (int)size, s, val, val);
|
---|
| 156 | }
|
---|
| 157 | return (unsigned)val;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[2614] | 160 | static void
|
---|
[31202] | 161 | process_mount_opts(const char *s, struct vbsf_mount_opts *opts)
|
---|
[2614] | 162 | {
|
---|
| 163 | const char *next = s;
|
---|
| 164 | size_t len;
|
---|
| 165 | typedef enum handler_opt
|
---|
| 166 | {
|
---|
[77138] | 167 | HO_RW,
|
---|
| 168 | HO_RO,
|
---|
| 169 | HO_UID,
|
---|
| 170 | HO_GID,
|
---|
| 171 | HO_TTL,
|
---|
[77953] | 172 | HO_DENTRY_TTL,
|
---|
| 173 | HO_INODE_TTL,
|
---|
| 174 | HO_MAX_IO_PAGES,
|
---|
| 175 | HO_DIR_BUF,
|
---|
| 176 | HO_CACHE,
|
---|
[77138] | 177 | HO_DMODE,
|
---|
| 178 | HO_FMODE,
|
---|
| 179 | HO_UMASK,
|
---|
| 180 | HO_DMASK,
|
---|
| 181 | HO_FMASK,
|
---|
| 182 | HO_IOCHARSET,
|
---|
[87463] | 183 | HO_NLS,
|
---|
[77138] | 184 | HO_CONVERTCP,
|
---|
| 185 | HO_NOEXEC,
|
---|
| 186 | HO_EXEC,
|
---|
| 187 | HO_NODEV,
|
---|
| 188 | HO_DEV,
|
---|
| 189 | HO_NOSUID,
|
---|
| 190 | HO_SUID,
|
---|
| 191 | HO_REMOUNT,
|
---|
| 192 | HO_NOAUTO,
|
---|
| 193 | HO_NIGNORE
|
---|
[2614] | 194 | } handler_opt;
|
---|
[16099] | 195 | struct
|
---|
| 196 | {
|
---|
[2614] | 197 | const char *name;
|
---|
| 198 | handler_opt opt;
|
---|
| 199 | int has_arg;
|
---|
| 200 | const char *desc;
|
---|
[58143] | 201 | } handlers[] =
|
---|
[16099] | 202 | {
|
---|
[77138] | 203 | {"rw", HO_RW, 0, "mount read write (default)"},
|
---|
| 204 | {"ro", HO_RO, 0, "mount read only"},
|
---|
| 205 | {"uid", HO_UID, 1, "default file owner user id"},
|
---|
| 206 | {"gid", HO_GID, 1, "default file owner group id"},
|
---|
[77953] | 207 | {"ttl", HO_TTL, 1, "time to live for dentries & inode info"},
|
---|
| 208 | {"dcachettl", HO_DENTRY_TTL, 1, "time to live for dentries"},
|
---|
| 209 | {"inodettl", HO_INODE_TTL, 1, "time to live for inode info"},
|
---|
| 210 | {"maxiopages", HO_MAX_IO_PAGES, 1, "max buffer size for I/O with host"},
|
---|
| 211 | {"dirbuf", HO_DIR_BUF, 1, "directory buffer size (0 for default)"},
|
---|
| 212 | {"cache", HO_CACHE, 1, "cache mode: none, strict (default), read, readwrite"},
|
---|
[77138] | 213 | {"iocharset", HO_IOCHARSET, 1, "i/o charset (default utf8)"},
|
---|
[87463] | 214 | {"nls", HO_NLS, 1, "i/o charset (default utf8)"},
|
---|
[77138] | 215 | {"convertcp", HO_CONVERTCP, 1, "convert share name from given charset to utf8"},
|
---|
| 216 | {"dmode", HO_DMODE, 1, "mode of all directories"},
|
---|
| 217 | {"fmode", HO_FMODE, 1, "mode of all regular files"},
|
---|
| 218 | {"umask", HO_UMASK, 1, "umask of directories and regular files"},
|
---|
| 219 | {"dmask", HO_DMASK, 1, "umask of directories"},
|
---|
| 220 | {"fmask", HO_FMASK, 1, "umask of regular files"},
|
---|
| 221 | {"noexec", HO_NOEXEC, 0, NULL}, /* don't document these options directly here */
|
---|
| 222 | {"exec", HO_EXEC, 0, NULL}, /* as they are well known and described in the */
|
---|
| 223 | {"nodev", HO_NODEV, 0, NULL}, /* usual manpages */
|
---|
| 224 | {"dev", HO_DEV, 0, NULL},
|
---|
| 225 | {"nosuid", HO_NOSUID, 0, NULL},
|
---|
| 226 | {"suid", HO_SUID, 0, NULL},
|
---|
| 227 | {"remount", HO_REMOUNT, 0, NULL},
|
---|
| 228 | {"noauto", HO_NOAUTO, 0, NULL},
|
---|
| 229 | {"_netdev", HO_NIGNORE, 0, NULL},
|
---|
| 230 | {"relatime", HO_NIGNORE, 0, NULL},
|
---|
| 231 | {NULL, 0, 0, NULL}
|
---|
[2614] | 232 | }, *handler;
|
---|
| 233 |
|
---|
[16099] | 234 | while (next)
|
---|
| 235 | {
|
---|
[2614] | 236 | const char *val;
|
---|
| 237 | size_t key_len, val_len;
|
---|
| 238 |
|
---|
| 239 | s = next;
|
---|
[30153] | 240 | next = strchr(s, ',');
|
---|
[16099] | 241 | if (!next)
|
---|
| 242 | {
|
---|
[30153] | 243 | len = strlen(s);
|
---|
[2614] | 244 | }
|
---|
[16099] | 245 | else
|
---|
| 246 | {
|
---|
[2614] | 247 | len = next - s;
|
---|
| 248 | next += 1;
|
---|
[16099] | 249 | if (!*next)
|
---|
[2614] | 250 | next = 0;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | val = NULL;
|
---|
| 254 | val_len = 0;
|
---|
[16099] | 255 | for (key_len = 0; key_len < len; ++key_len)
|
---|
| 256 | {
|
---|
| 257 | if (s[key_len] == '=')
|
---|
| 258 | {
|
---|
| 259 | if (key_len + 1 < len)
|
---|
| 260 | {
|
---|
[2614] | 261 | val = s + key_len + 1;
|
---|
| 262 | val_len = len - key_len - 1;
|
---|
| 263 | }
|
---|
| 264 | break;
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[16099] | 268 | for (handler = handlers; handler->name; ++handler)
|
---|
| 269 | {
|
---|
[2614] | 270 | size_t j;
|
---|
| 271 | for (j = 0; j < key_len && handler->name[j] == s[j]; ++j)
|
---|
| 272 | ;
|
---|
| 273 |
|
---|
[16099] | 274 | if (j == key_len && !handler->name[j])
|
---|
| 275 | {
|
---|
| 276 | if (handler->has_arg)
|
---|
| 277 | {
|
---|
| 278 | if (!(val && *val))
|
---|
| 279 | {
|
---|
[30153] | 280 | panic("%.*s requires an argument (i.e. %.*s=<arg>)\n",
|
---|
[87463] | 281 | (int)len, s, (int)len, s);
|
---|
[2614] | 282 | }
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[77534] | 285 | switch (handler->opt)
|
---|
[2614] | 286 | {
|
---|
[77138] | 287 | case HO_RW:
|
---|
[87463] | 288 | opts->fFlags &= ~MS_RDONLY;
|
---|
[77138] | 289 | break;
|
---|
| 290 | case HO_RO:
|
---|
[87463] | 291 | opts->fFlags |= MS_RDONLY;
|
---|
[77138] | 292 | break;
|
---|
| 293 | case HO_NOEXEC:
|
---|
[87463] | 294 | opts->fFlags |= MS_NOEXEC;
|
---|
[77138] | 295 | break;
|
---|
| 296 | case HO_EXEC:
|
---|
[87463] | 297 | opts->fFlags &= ~MS_NOEXEC;
|
---|
[77138] | 298 | break;
|
---|
| 299 | case HO_NODEV:
|
---|
[87463] | 300 | opts->fFlags |= MS_NODEV;
|
---|
[77138] | 301 | break;
|
---|
| 302 | case HO_DEV:
|
---|
[87463] | 303 | opts->fFlags &= ~MS_NODEV;
|
---|
[77138] | 304 | break;
|
---|
| 305 | case HO_NOSUID:
|
---|
[87463] | 306 | opts->fFlags |= MS_NOSUID;
|
---|
[77138] | 307 | break;
|
---|
| 308 | case HO_SUID:
|
---|
[87463] | 309 | opts->fFlags &= ~MS_NOSUID;
|
---|
[77138] | 310 | break;
|
---|
| 311 | case HO_REMOUNT:
|
---|
[87463] | 312 | opts->fFlags |= MS_REMOUNT;
|
---|
[77138] | 313 | break;
|
---|
[77953] | 314 | case HO_TTL:
|
---|
[87463] | 315 | snprintf(opts->szTTL, sizeof(opts->szTTL),
|
---|
| 316 | "ttl=%d", safe_atoi(val, val_len, 10));
|
---|
[77953] | 317 | break;
|
---|
| 318 | case HO_DENTRY_TTL:
|
---|
[87463] | 319 | snprintf(opts->szMsDirCacheTTL, sizeof(opts->szMsDirCacheTTL),
|
---|
| 320 | "dcachettl=%d", safe_atoi(val, val_len, 10));
|
---|
[77953] | 321 | break;
|
---|
| 322 | case HO_INODE_TTL:
|
---|
[87463] | 323 | snprintf(opts->szMsInodeTTL, sizeof(opts->szMsInodeTTL),
|
---|
| 324 | "inodettl=%d", safe_atoi(val, val_len, 10));
|
---|
[77953] | 325 | break;
|
---|
| 326 | case HO_MAX_IO_PAGES:
|
---|
[87463] | 327 | snprintf(opts->szMaxIoPages, sizeof(opts->szMaxIoPages),
|
---|
| 328 | "maxiopages=%d", safe_atoiu(val, val_len, 10));
|
---|
[77953] | 329 | break;
|
---|
| 330 | case HO_DIR_BUF:
|
---|
[87463] | 331 | snprintf(opts->szDirBuf, sizeof(opts->szDirBuf),
|
---|
| 332 | "dirbuf=%d", safe_atoiu(val, val_len, 10));
|
---|
[77953] | 333 | break;
|
---|
| 334 | case HO_CACHE:
|
---|
| 335 | #define IS_EQUAL(a_sz) (val_len == sizeof(a_sz) - 1U && strncmp(val, a_sz, sizeof(a_sz) - 1U) == 0)
|
---|
| 336 | if (IS_EQUAL("default"))
|
---|
[87463] | 337 | strcpy(opts->szCacheMode, "cache=default");
|
---|
[77953] | 338 | else if (IS_EQUAL("none"))
|
---|
[87463] | 339 | strcpy(opts->szCacheMode, "cache=none");
|
---|
[77953] | 340 | else if (IS_EQUAL("strict"))
|
---|
[87463] | 341 | strcpy(opts->szCacheMode, "cache=strict");
|
---|
[77953] | 342 | else if (IS_EQUAL("read"))
|
---|
[87463] | 343 | strcpy(opts->szCacheMode, "cache=read");
|
---|
[77953] | 344 | else if (IS_EQUAL("readwrite"))
|
---|
[87463] | 345 | strcpy(opts->szCacheMode, "cache=readwrite");
|
---|
[77953] | 346 | else
|
---|
| 347 | panic("invalid cache mode '%.*s'\n"
|
---|
| 348 | "Valid cache modes are: default, none, strict, read, readwrite\n",
|
---|
| 349 | (int)val_len, val);
|
---|
| 350 | break;
|
---|
[77138] | 351 | case HO_UID:
|
---|
| 352 | /** @todo convert string to id. */
|
---|
[87463] | 353 | snprintf(opts->szUid, sizeof(opts->szUid),
|
---|
| 354 | "uid=%d", safe_atoi(val, val_len, 10));
|
---|
[77138] | 355 | break;
|
---|
| 356 | case HO_GID:
|
---|
| 357 | /** @todo convert string to id. */
|
---|
[87463] | 358 | snprintf(opts->szGid, sizeof(opts->szGid),
|
---|
| 359 | "gid=%d", safe_atoi(val, val_len, 10));
|
---|
[77138] | 360 | break;
|
---|
| 361 | case HO_DMODE:
|
---|
[87463] | 362 | snprintf(opts->szDMode, sizeof(opts->szDMode),
|
---|
| 363 | "dmode=0%o", safe_atoi(val, val_len, 8));
|
---|
[77138] | 364 | break;
|
---|
| 365 | case HO_FMODE:
|
---|
[87463] | 366 | snprintf(opts->szFMode, sizeof(opts->szFMode),
|
---|
| 367 | "fmode=0%o", safe_atoi(val, val_len, 8));
|
---|
[77138] | 368 | break;
|
---|
| 369 | case HO_UMASK:
|
---|
[87463] | 370 | {
|
---|
| 371 | int fMask = safe_atoi(val, val_len, 8);
|
---|
| 372 | snprintf(opts->szDMask, sizeof(opts->szDMask), "dmask=0%o", fMask);
|
---|
| 373 | snprintf(opts->szFMask, sizeof(opts->szFMask), "fmask=0%o", fMask);
|
---|
[77138] | 374 | break;
|
---|
[87463] | 375 | }
|
---|
[77138] | 376 | case HO_DMASK:
|
---|
[87463] | 377 | snprintf(opts->szDMask, sizeof(opts->szDMask),
|
---|
| 378 | "dmask=0%o", safe_atoi(val, val_len, 8));
|
---|
[77138] | 379 | break;
|
---|
| 380 | case HO_FMASK:
|
---|
[87463] | 381 | snprintf(opts->szFMask, sizeof(opts->szFMask),
|
---|
| 382 | "fmask=0%o", safe_atoi(val, val_len, 8));
|
---|
[77138] | 383 | break;
|
---|
| 384 | case HO_IOCHARSET:
|
---|
[87463] | 385 | case HO_NLS:
|
---|
| 386 | if (val_len >= MAX_NLS_NAME)
|
---|
| 387 | panic("the character set name for I/O is too long: %*.*s\n", (int)val_len, (int)val_len, val);
|
---|
| 388 | snprintf(opts->szIoCharset, sizeof(opts->szIoCharset),
|
---|
| 389 | "%s=%*.*s", handler->opt == HO_IOCHARSET ? "iocharset" : "nls", (int)val_len, (int)val_len, val);
|
---|
[77138] | 390 | break;
|
---|
| 391 | case HO_CONVERTCP:
|
---|
[87463] | 392 | opts->pszConvertCp = malloc(val_len + 1);
|
---|
| 393 | if (!opts->pszConvertCp)
|
---|
[77138] | 394 | panic_err("could not allocate memory");
|
---|
[87463] | 395 | memcpy(opts->pszConvertCp, val, val_len);
|
---|
| 396 | opts->pszConvertCp[val_len] = '\0';
|
---|
[77138] | 397 | break;
|
---|
| 398 | case HO_NOAUTO:
|
---|
| 399 | case HO_NIGNORE:
|
---|
| 400 | break;
|
---|
[2614] | 401 | }
|
---|
| 402 | break;
|
---|
| 403 | }
|
---|
| 404 | continue;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[56675] | 407 | if ( !handler->name
|
---|
[87463] | 408 | && !opts->fSloppy)
|
---|
[16099] | 409 | {
|
---|
[30153] | 410 | fprintf(stderr, "unknown mount option `%.*s'\n", (int)len, s);
|
---|
| 411 | fprintf(stderr, "valid options:\n");
|
---|
[2614] | 412 |
|
---|
[16099] | 413 | for (handler = handlers; handler->name; ++handler)
|
---|
| 414 | {
|
---|
[2614] | 415 | if (handler->desc)
|
---|
[30153] | 416 | fprintf(stderr, " %-10s%s %s\n", handler->name,
|
---|
[87463] | 417 | handler->has_arg ? "=<arg>" : "", handler->desc);
|
---|
[2614] | 418 | }
|
---|
[30153] | 419 | exit(EXIT_FAILURE);
|
---|
[2614] | 420 | }
|
---|
| 421 | }
|
---|
| 422 | }
|
---|
| 423 |
|
---|
[87463] | 424 | /** Appends @a pszOptVal to pszOpts if not empty. */
|
---|
| 425 | static size_t append_option(char *pszOpts, size_t cbOpts, size_t offOpts, const char *pszOptVal)
|
---|
| 426 | {
|
---|
| 427 | if (*pszOptVal != '\0')
|
---|
| 428 | {
|
---|
| 429 | size_t cchOptVal = strlen(pszOptVal);
|
---|
| 430 | if (offOpts + (offOpts > 0) + cchOptVal < cbOpts)
|
---|
| 431 | {
|
---|
| 432 | if (offOpts)
|
---|
| 433 | pszOpts[offOpts++] = ',';
|
---|
| 434 | memcpy(&pszOpts[offOpts], pszOptVal, cchOptVal);
|
---|
| 435 | offOpts += cchOptVal;
|
---|
| 436 | pszOpts[offOpts] = '\0';
|
---|
| 437 | }
|
---|
| 438 | else
|
---|
| 439 | panic("Too many options!");
|
---|
| 440 | }
|
---|
| 441 | return offOpts;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
[2614] | 444 | static void
|
---|
[87463] | 445 | convertcp(char *in_codeset, char *pszSharedFolder, char *pszDst)
|
---|
[2614] | 446 | {
|
---|
[87463] | 447 | char *i = pszSharedFolder;
|
---|
| 448 | char *o = pszDst;
|
---|
| 449 | size_t ib = strlen(pszSharedFolder);
|
---|
| 450 | size_t ob = MAX_HOST_NAME - 1;
|
---|
[2614] | 451 | iconv_t cd;
|
---|
| 452 |
|
---|
[30153] | 453 | cd = iconv_open("UTF-8", in_codeset);
|
---|
[87463] | 454 | if (cd == (iconv_t)-1)
|
---|
[16099] | 455 | {
|
---|
[30153] | 456 | panic_err("could not convert share name, iconv_open `%s' failed",
|
---|
[2614] | 457 | in_codeset);
|
---|
| 458 | }
|
---|
| 459 |
|
---|
[16099] | 460 | while (ib)
|
---|
| 461 | {
|
---|
[30153] | 462 | size_t c = iconv(cd, &i, &ib, &o, &ob);
|
---|
[87463] | 463 | if (c == (size_t)-1)
|
---|
[16099] | 464 | {
|
---|
[30153] | 465 | panic_err("could not convert share name(%s) at %d",
|
---|
[87463] | 466 | pszSharedFolder, (int)(strlen(pszSharedFolder) - ib));
|
---|
[2614] | 467 | }
|
---|
| 468 | }
|
---|
| 469 | *o = 0;
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 |
|
---|
| 473 | /**
|
---|
| 474 | * Print out a usage message and exit.
|
---|
| 475 | *
|
---|
[58143] | 476 | * @returns 1
|
---|
| 477 | * @param argv0 The name of the application
|
---|
[2614] | 478 | */
|
---|
[58143] | 479 | static int usage(char *argv0)
|
---|
[2614] | 480 | {
|
---|
| 481 | printf("Usage: %s [OPTIONS] NAME MOUNTPOINT\n"
|
---|
| 482 | "Mount the VirtualBox shared folder NAME from the host system to MOUNTPOINT.\n"
|
---|
| 483 | "\n"
|
---|
[33550] | 484 | " -w mount the shared folder writable (the default)\n"
|
---|
[2614] | 485 | " -r mount the shared folder read-only\n"
|
---|
[11507] | 486 | " -n do not create an mtab entry\n"
|
---|
[56675] | 487 | " -s sloppy parsing, ignore unrecognized mount options\n"
|
---|
[2614] | 488 | " -o OPTION[,OPTION...] use the mount options specified\n"
|
---|
[58143] | 489 | "\n", argv0);
|
---|
[2614] | 490 | printf("Available mount options are:\n"
|
---|
[33550] | 491 | " rw mount writable (the default)\n"
|
---|
[2614] | 492 | " ro mount read only\n"
|
---|
| 493 | " uid=UID set the default file owner user id to UID\n"
|
---|
[77953] | 494 | " gid=GID set the default file owner group id to GID\n");
|
---|
| 495 | printf(" ttl=MILLIESECSONDS set the \"time to live\" for both the directory cache\n"
|
---|
| 496 | " and inode info. -1 for kernel default, 0 disables it.\n"
|
---|
| 497 | " dcachettl=MILLIES set the \"time to live\" for the directory cache,\n"
|
---|
| 498 | " overriding the 'ttl' option. Ignored if negative.\n"
|
---|
| 499 | " inodettl=MILLIES set the \"time to live\" for the inode information,\n"
|
---|
| 500 | " overriding the 'ttl' option. Ignored if negative.\n");
|
---|
| 501 | printf(" maxiopages=PAGES set the max host I/O buffers size in pages. Uses\n"
|
---|
| 502 | " default if zero.\n"
|
---|
| 503 | " dirbuf=BYTES set the directory enumeration buffer size in bytes.\n"
|
---|
| 504 | " Uses default size if zero.\n");
|
---|
| 505 | printf(" cache=MODE set the caching mode for the mount. Allowed values:\n"
|
---|
| 506 | " default: use the kernel default (strict)\n"
|
---|
| 507 | " none: no caching; may experience guest side\n"
|
---|
[77955] | 508 | " coherence issues between mmap and read.\n");
|
---|
| 509 | printf(" strict: no caching, except for writably mapped\n"
|
---|
[77953] | 510 | " files (for guest side coherence)\n"
|
---|
| 511 | " read: read via the page cache; host changes\n"
|
---|
[77955] | 512 | " may be completely ignored\n");
|
---|
| 513 | printf(" readwrite: read and write via the page cache; host\n"
|
---|
[77953] | 514 | " changes may be completely ignored and\n"
|
---|
| 515 | " guest changes takes a while to reach the host\n");
|
---|
[11507] | 516 | printf(" dmode=MODE override the mode of all directories to (octal) MODE\n"
|
---|
| 517 | " fmode=MODE override the mode of all regular files to (octal) MODE\n"
|
---|
| 518 | " umask=UMASK set the umask to (octal) UMASK\n");
|
---|
| 519 | printf(" dmask=UMASK set the umask applied to directories only\n"
|
---|
| 520 | " fmask=UMASK set the umask applied to regular files only\n"
|
---|
| 521 | " iocharset CHARSET use the character set CHARSET for I/O operations\n"
|
---|
| 522 | " (default set is utf8)\n"
|
---|
| 523 | " convertcp CHARSET convert the folder name from CHARSET to utf8\n"
|
---|
| 524 | "\n");
|
---|
[2614] | 525 | printf("Less common used options:\n"
|
---|
| 526 | " noexec,exec,nodev,dev,nosuid,suid\n");
|
---|
[58143] | 527 | return EXIT_FAILURE;
|
---|
[2614] | 528 | }
|
---|
| 529 |
|
---|
| 530 | int
|
---|
[58143] | 531 | main(int argc, char **argv)
|
---|
[2614] | 532 | {
|
---|
| 533 | int c;
|
---|
| 534 | int err;
|
---|
[77739] | 535 | int saved_errno;
|
---|
[2614] | 536 | int nomtab = 0;
|
---|
[87463] | 537 | char *pszSharedFolder;
|
---|
| 538 | char *pszMountPoint;
|
---|
| 539 | struct utsname uts;
|
---|
| 540 | int major, minor, patch;
|
---|
| 541 | size_t offOpts;
|
---|
| 542 | static const char s_szSfNameOpt[] = "sf_name=";
|
---|
| 543 | char szSharedFolderIconved[sizeof(s_szSfNameOpt) - 1 + MAX_HOST_NAME];
|
---|
[100317] | 544 | char *pszOpts = NULL;
|
---|
[31202] | 545 | struct vbsf_mount_opts opts =
|
---|
[11507] | 546 | {
|
---|
[87463] | 547 | MS_NODEV,
|
---|
| 548 | "",
|
---|
| 549 | "",
|
---|
| 550 | "",
|
---|
| 551 | "",
|
---|
| 552 | "",
|
---|
| 553 | "",
|
---|
| 554 | "",
|
---|
| 555 | "",
|
---|
| 556 | "",
|
---|
| 557 | "",
|
---|
| 558 | "",
|
---|
| 559 | "",
|
---|
| 560 | "",
|
---|
| 561 | false, /*fSloppy*/
|
---|
| 562 | NULL,
|
---|
[2614] | 563 | };
|
---|
[87463] | 564 |
|
---|
[58143] | 565 | AssertCompile(sizeof(uid_t) == sizeof(int));
|
---|
| 566 | AssertCompile(sizeof(gid_t) == sizeof(int));
|
---|
[2614] | 567 |
|
---|
[30153] | 568 | if (getuid())
|
---|
| 569 | panic("Only root can mount shared folders from the host.\n");
|
---|
[2614] | 570 |
|
---|
| 571 | if (!argv[0])
|
---|
| 572 | argv[0] = "mount.vboxsf";
|
---|
| 573 |
|
---|
[100317] | 574 | long cbOpts = sysconf(_SC_PAGESIZE);
|
---|
| 575 | if (cbOpts == -1)
|
---|
| 576 | panic("Couldn't query system page size errno=%d.\n", errno);
|
---|
| 577 |
|
---|
| 578 | pszOpts = alloca(cbOpts);
|
---|
| 579 | if (!pszOpts)
|
---|
| 580 | panic("Couldn't allocate memory for mount options buffers\n");
|
---|
| 581 |
|
---|
[87463] | 582 | /*
|
---|
| 583 | * Parse options.
|
---|
| 584 | */
|
---|
[56675] | 585 | while ((c = getopt(argc, argv, "rwsno:h")) != -1)
|
---|
[16099] | 586 | {
|
---|
| 587 | switch (c)
|
---|
| 588 | {
|
---|
[2614] | 589 | default:
|
---|
[30153] | 590 | fprintf(stderr, "unknown option `%c:%#x'\n", c, c);
|
---|
[69046] | 591 | RT_FALL_THRU();
|
---|
[2614] | 592 | case '?':
|
---|
| 593 | case 'h':
|
---|
[58143] | 594 | return usage(argv[0]);
|
---|
[2614] | 595 |
|
---|
| 596 | case 'r':
|
---|
[87463] | 597 | opts.fFlags |= MS_RDONLY;
|
---|
[2614] | 598 | break;
|
---|
| 599 |
|
---|
| 600 | case 'w':
|
---|
[87463] | 601 | opts.fFlags &= ~MS_RDONLY;
|
---|
[65641] | 602 | break;
|
---|
[2614] | 603 |
|
---|
[56675] | 604 | case 's':
|
---|
[87463] | 605 | opts.fSloppy = true;
|
---|
[56675] | 606 | break;
|
---|
| 607 |
|
---|
[2614] | 608 | case 'o':
|
---|
[30153] | 609 | process_mount_opts(optarg, &opts);
|
---|
[2614] | 610 | break;
|
---|
| 611 |
|
---|
| 612 | case 'n':
|
---|
| 613 | nomtab = 1;
|
---|
| 614 | break;
|
---|
| 615 | }
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | if (argc - optind < 2)
|
---|
[58143] | 619 | return usage(argv[0]);
|
---|
[2614] | 620 |
|
---|
[87463] | 621 | pszSharedFolder = argv[optind];
|
---|
| 622 | pszMountPoint = argv[optind + 1];
|
---|
| 623 | if (opts.pszConvertCp)
|
---|
[2614] | 624 | {
|
---|
[87463] | 625 | convertcp(opts.pszConvertCp, pszSharedFolder, &szSharedFolderIconved[sizeof(s_szSfNameOpt) - 1]);
|
---|
| 626 | pszSharedFolder = &szSharedFolderIconved[sizeof(s_szSfNameOpt) - 1];
|
---|
[2614] | 627 | }
|
---|
| 628 |
|
---|
[87463] | 629 | /*
|
---|
| 630 | * Concat option strings.
|
---|
| 631 | */
|
---|
| 632 | offOpts = 0;
|
---|
[100317] | 633 | pszOpts[0] = '\0';
|
---|
| 634 | offOpts = append_option(pszOpts, cbOpts, offOpts, opts.szTTL);
|
---|
| 635 | offOpts = append_option(pszOpts, cbOpts, offOpts, opts.szMsDirCacheTTL);
|
---|
| 636 | offOpts = append_option(pszOpts, cbOpts, offOpts, opts.szMsInodeTTL);
|
---|
| 637 | offOpts = append_option(pszOpts, cbOpts, offOpts, opts.szMaxIoPages);
|
---|
| 638 | offOpts = append_option(pszOpts, cbOpts, offOpts, opts.szDirBuf);
|
---|
| 639 | offOpts = append_option(pszOpts, cbOpts, offOpts, opts.szCacheMode);
|
---|
| 640 | offOpts = append_option(pszOpts, cbOpts, offOpts, opts.szUid);
|
---|
| 641 | offOpts = append_option(pszOpts, cbOpts, offOpts, opts.szGid);
|
---|
| 642 | offOpts = append_option(pszOpts, cbOpts, offOpts, opts.szDMode);
|
---|
| 643 | offOpts = append_option(pszOpts, cbOpts, offOpts, opts.szFMode);
|
---|
| 644 | offOpts = append_option(pszOpts, cbOpts, offOpts, opts.szDMask);
|
---|
| 645 | offOpts = append_option(pszOpts, cbOpts, offOpts, opts.szFMask);
|
---|
| 646 | offOpts = append_option(pszOpts, cbOpts, offOpts, opts.szIoCharset);
|
---|
[2614] | 647 |
|
---|
[87463] | 648 | /* For pre-2.6 kernels we have to supply the shared folder name as a
|
---|
| 649 | string option because the kernel hides the device name from us. */
|
---|
| 650 | RT_ZERO(uts);
|
---|
| 651 | if ( uname(&uts) == -1
|
---|
| 652 | || sscanf(uts.release, "%d.%d.%d", &major, &minor, &patch) != 3)
|
---|
| 653 | major = minor = patch = 5;
|
---|
[2614] | 654 |
|
---|
[87463] | 655 | if (KERNEL_VERSION(major, minor, patch) < KERNEL_VERSION(2,6,0))
|
---|
| 656 | {
|
---|
| 657 | memcpy(szSharedFolderIconved, s_szSfNameOpt, sizeof(s_szSfNameOpt) - 1);
|
---|
| 658 | if (!opts.pszConvertCp)
|
---|
| 659 | {
|
---|
| 660 | if (strlen(pszSharedFolder) >= MAX_HOST_NAME)
|
---|
| 661 | panic("%s: shared folder name is too long (max %d)", argv[0], (int)MAX_HOST_NAME - 1);
|
---|
| 662 | strcpy(&szSharedFolderIconved[sizeof(s_szSfNameOpt) - 1], pszSharedFolder);
|
---|
| 663 | }
|
---|
[100317] | 664 | offOpts = append_option(pszOpts, cbOpts, offOpts, szSharedFolderIconved);
|
---|
[87463] | 665 | }
|
---|
[2614] | 666 |
|
---|
[31202] | 667 | /*
|
---|
[87463] | 668 | * Do the actual mounting.
|
---|
[31202] | 669 | */
|
---|
[100317] | 670 | err = mount(pszSharedFolder, pszMountPoint, "vboxsf", opts.fFlags, pszOpts);
|
---|
[77739] | 671 | saved_errno = errno;
|
---|
| 672 |
|
---|
[2614] | 673 | if (err)
|
---|
[77739] | 674 | {
|
---|
| 675 | if (saved_errno == ENXIO)
|
---|
[87463] | 676 | panic("%s: shared folder '%s' was not found (check VM settings / spelling)\n", argv[0], pszSharedFolder);
|
---|
[77739] | 677 | else
|
---|
| 678 | panic_err("%s: mounting failed with the error", argv[0]);
|
---|
| 679 | }
|
---|
[2614] | 680 |
|
---|
| 681 | if (!nomtab)
|
---|
[31202] | 682 | {
|
---|
[100317] | 683 | err = vbsfmount_complete(pszSharedFolder, pszMountPoint, opts.fFlags, pszOpts);
|
---|
[31202] | 684 | switch (err)
|
---|
| 685 | {
|
---|
| 686 | case 0: /* Success. */
|
---|
| 687 | break;
|
---|
[2614] | 688 |
|
---|
[31202] | 689 | case 1:
|
---|
[87463] | 690 | panic_err("%s: Could not update mount table (out of memory).", argv[0]);
|
---|
[31202] | 691 | break;
|
---|
| 692 |
|
---|
| 693 | case 2:
|
---|
| 694 | panic_err("%s: Could not open mount table for update.", argv[0]);
|
---|
| 695 | break;
|
---|
| 696 |
|
---|
| 697 | case 3:
|
---|
[38898] | 698 | /* panic_err("%s: Could not add an entry to the mount table.", argv[0]); */
|
---|
[31202] | 699 | break;
|
---|
| 700 |
|
---|
| 701 | default:
|
---|
| 702 | panic_err("%s: Unknown error while completing mount operation: %d", argv[0], err);
|
---|
| 703 | break;
|
---|
| 704 | }
|
---|
| 705 | }
|
---|
| 706 |
|
---|
[30153] | 707 | exit(EXIT_SUCCESS);
|
---|
[2614] | 708 | }
|
---|
[58143] | 709 |
|
---|