VBoxServiceAutoMount.cpp revision b6517c5cc3b7a38889416706905a3cf2fd010785
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * VBoxService - Auto-mounting for Shared Folders.
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * Copyright (C) 2010 Oracle Corporation
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * available from http://www.virtualbox.org. This file is free software;
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * you can redistribute it and/or modify it under the terms of the GNU
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * General Public License (GPL) as published by the Free Software
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync/*******************************************************************************
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync* Header Files *
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync*******************************************************************************/
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync #include "../../linux/sharedfolders/vbsfmount.h"
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync/*******************************************************************************
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync* Global Variables *
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync*******************************************************************************/
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync/** The semaphore we're blocking on. */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsyncstatic RTSEMEVENTMULTI g_AutoMountEvent = NIL_RTSEMEVENTMULTI;
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync/** @copydoc VBOXSERVICE::pfnPreInit */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsyncstatic DECLCALLBACK(int) VBoxServiceAutoMountPreInit(void)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync/** @copydoc VBOXSERVICE::pfnOption */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsyncstatic DECLCALLBACK(int) VBoxServiceAutoMountOption(const char **ppszShort, int argc, char **argv, int *pi)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync/** @copydoc VBOXSERVICE::pfnInit */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsyncstatic DECLCALLBACK(int) VBoxServiceAutoMountInit(void)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceVerbose(3, "VBoxServiceAutoMountInit\n");
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsyncstatic int VBoxServiceAutoMountSharedFolder(const char *pszShareName, const char *pszMountPoint)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync 0, /* uid */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync 0, /* gid */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync 0, /* ttl */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync ~0, /* dmode */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync ~0, /* fmode*/
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync 0, /* dmask */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync 0, /* fmask */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync 0, /* ronly */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync 0, /* noexec */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync 0, /* nodev */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync 0, /* nosuid */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync 0, /* remount */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync 0, /* int datalen */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync 0); /* int optlen */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (r == 0)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceVerbose(0, "VBoxServiceAutoMountWorker: Shared folder \"%s\" was mounted to \"%s\"\n", pszShareName, pszMountPoint);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (errno != EBUSY) /* Share is already mounted? Then skip error msg. */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceError("VBoxServiceAutoMountWorker: Could not mount shared folder \"%s\" to \"%s\", error = %s\n",
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync#else /* !RT_OS_SOLARIS */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (r == 0)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceVerbose(0, "VBoxServiceAutoMountWorker: Shared folder \"%s\" was mounted to \"%s\"\n", pszShareName, pszMountPoint);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync r = vbsfmount_complete(pszShareName, pszMountPoint, flags, &opts);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync case 0: /* Success. */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceError("VBoxServiceAutoMountWorker: Could not update mount table (failed to create memstream): %s\n", strerror(errno));
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceError("VBoxServiceAutoMountWorker: Could not open mount table for update: %s\n", strerror(errno));
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceError("VBoxServiceAutoMountWorker: Could not add an entry to the mount table: %s\n", strerror(errno));
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceError("VBoxServiceAutoMountWorker: Unknown error while completing mount operation: %d\n", r);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync else /* r != 0 */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync /* Sometimes the mount utility messes up the share name. Try to
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * un-mangle it again. */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceError("VBoxServiceAutoMountWorker: Failed to get the current working directory\n");
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync /* We checked before that we have enough space */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync r = mount(NULL, pszMountPoint, "vboxsf", flags, &mntinf);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync /* New mount tool with old vboxsf module? Try again using the old
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * vbsf_mount_info_old structure. */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync memcpy(&mntinf_old.name, &mntinf.name, MAX_HOST_NAME);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync memcpy(&mntinf_old.nls_name, mntinf.nls_name, MAX_NLS_NAME);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync r = mount(NULL, pszMountPoint, "vboxsf", flags, &mntinf_old);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (errno != EBUSY) /* Share is already mounted? Then skip error msg. */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceError("VBoxServiceAutoMountWorker: Could not mount shared folder \"%s\" to \"%s\", error = %s\n",
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync#endif /* !RT_OS_SOLARIS */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Mounting returned with errno=%d, error=%s\n",
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync/** @copydoc VBOXSERVICE::pfnWorker */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsyncDECLCALLBACK(int) VBoxServiceAutoMountWorker(bool volatile *pfShutdown)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * Tell the control thread that it can continue
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * spawning services.
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Failed to connect to the shared folder service, error %Rrc\n", rc);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync rc = VbglR3SharedFolderGetMappings(u32ClientId, true /* Only process auto-mounted folders */,
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync /* Check for a fixed/virtual auto-mount share. */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (VbglR3SharedFolderExists(u32ClientId, "vbsfAutoMount"))
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Host supports auto-mount root\n");
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Got %u shared folder mappings\n", cMappings);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync for (uint32_t i = 0; i < cMappings && RT_SUCCESS(rc); i++)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync rc = VbglR3SharedFolderGetName(u32ClientId, paMappings[i].u32Root, &pszShareName);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Connecting share %u (%s) ...\n", i+1, pszShareName);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if ( RTStrAPrintf(&pszMountPoint, "/media/sf_%s", pszShareName) > 0
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync /* We always use "/media" as our root mounting directory. */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync /** @todo Detect the correct "media" directory, based on the current guest (?). */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync rc = VBoxServiceAutoMountSharedFolder(pszShareName, pszMountPoint);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceError("VBoxServiceAutoMountWorker: Could not create mount directory \"%s\", rc = %Rrc\n",
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceError("VBoxServiceAutoMountWorker: Error while getting the shared folder name for root node = %u, rc = %Rrc\n",
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceError("VBoxServiceAutoMountWorker: Error while getting the shared folder mappings, rc = %Rrc\n", rc);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Finished\n");
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync/** @copydoc VBOXSERVICE::pfnTerm */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsyncstatic DECLCALLBACK(void) VBoxServiceAutoMountTerm(void)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync VBoxServiceVerbose(3, "VBoxServiceAutoMountTerm\n");
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync/** @copydoc VBOXSERVICE::pfnStop */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsyncstatic DECLCALLBACK(void) VBoxServiceAutoMountStop(void)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * The 'automount' service description.
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync /* pszName. */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync "automount",
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync /* pszDescription. */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync "Auto-mount for Shared Folders",
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync /* pszUsage. */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync /* pszOptions. */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync /* methods */