VBoxServiceAutoMount.cpp revision 1313e5cbe01a41e7ccb1499d2d73c455a2495955
c1c9242f0046150566414e1f222cb667e03e605evboxsync * VBoxService - Auto-mounting for Shared Folders.
c1c9242f0046150566414e1f222cb667e03e605evboxsync * Copyright (C) 2010 Oracle Corporation
c1c9242f0046150566414e1f222cb667e03e605evboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
c1c9242f0046150566414e1f222cb667e03e605evboxsync * available from http://www.virtualbox.org. This file is free software;
c1c9242f0046150566414e1f222cb667e03e605evboxsync * you can redistribute it and/or modify it under the terms of the GNU
c1c9242f0046150566414e1f222cb667e03e605evboxsync * General Public License (GPL) as published by the Free Software
c1c9242f0046150566414e1f222cb667e03e605evboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
c1c9242f0046150566414e1f222cb667e03e605evboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
c1c9242f0046150566414e1f222cb667e03e605evboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
c1c9242f0046150566414e1f222cb667e03e605evboxsync/*******************************************************************************
c1c9242f0046150566414e1f222cb667e03e605evboxsync* Header Files *
c1c9242f0046150566414e1f222cb667e03e605evboxsync*******************************************************************************/
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync/*******************************************************************************
c1c9242f0046150566414e1f222cb667e03e605evboxsync* Global Variables *
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync*******************************************************************************/
c1c9242f0046150566414e1f222cb667e03e605evboxsync/** The semaphore we're blocking on. */
c1c9242f0046150566414e1f222cb667e03e605evboxsyncstatic RTSEMEVENTMULTI g_AutoMountEvent = NIL_RTSEMEVENTMULTI;
c1c9242f0046150566414e1f222cb667e03e605evboxsync/** The Shared Folders service client ID. */
c1c9242f0046150566414e1f222cb667e03e605evboxsync/** @copydoc VBOXSERVICE::pfnPreInit */
c1c9242f0046150566414e1f222cb667e03e605evboxsyncstatic DECLCALLBACK(int) VBoxServiceAutoMountPreInit(void)
c1c9242f0046150566414e1f222cb667e03e605evboxsync/** @copydoc VBOXSERVICE::pfnOption */
c1c9242f0046150566414e1f222cb667e03e605evboxsyncstatic DECLCALLBACK(int) VBoxServiceAutoMountOption(const char **ppszShort, int argc, char **argv, int *pi)
c1c9242f0046150566414e1f222cb667e03e605evboxsync/** @copydoc VBOXSERVICE::pfnInit */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsyncstatic DECLCALLBACK(int) VBoxServiceAutoMountInit(void)
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync VBoxServiceVerbose(3, "VBoxServiceAutoMountInit\n");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync rc = VbglR3SharedFolderConnect(&g_SharedFoldersSvcClientID);
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync VBoxServiceVerbose(3, "VBoxServiceAutoMountInit: Service Client ID: %#x\n", g_SharedFoldersSvcClientID);
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync /* If the service was not found, we disable this service without
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync causing VBoxService to fail. */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync VBoxServiceVerbose(0, "VBoxServiceAutoMountInit: Shared Folders service is not available\n");
54d2d2606d7c83a456819cd038a73e0f9a600ca4vboxsync VBoxServiceError("Control: Failed to connect to the Shared Folders service! Error: %Rrc\n", rc);
bool fMounted = false;
#ifdef RT_OS_SOLARIS
if (!pFh)
VBoxServiceVerbose(4, "VBoxServiceAutoMountShareIsMounted: Share \"%s\" at mount point \"%s\" = %s\n",
return fMounted;
return rc;
static int VBoxServiceAutoMountPrepareMountPoint(const char *pszMountPoint, const char *pszShareName,
RTFMODE fMode = RTFS_UNIX_IRWXU | RTFS_UNIX_IRWXG; /* Owner (=root) and the group (=vboxsf) have full access. */
rc = RTPathSetOwnerEx(pszMountPoint, NIL_RTUID /* Owner, unchanged */, pOpts->gid, RTPATH_F_ON_LINK);
VBoxServiceVerbose(3, "VBoxServiceAutoMountPrepareMountPoint: Mount directory \"%s\" already is used/mounted\n", pszMountPoint);
VBoxServiceError("VBoxServiceAutoMountPrepareMountPoint: Could not set mode %RTfmode for mount directory \"%s\", rc = %Rrc\n",
VBoxServiceError("VBoxServiceAutoMountPrepareMountPoint: Could not set permissions for mount directory \"%s\", rc = %Rrc\n",
VBoxServiceError("VBoxServiceAutoMountPrepareMountPoint: Could not create mount directory \"%s\" with mode %RTfmode, rc = %Rrc\n",
return rc;
if ( VBoxServiceAutoMountShareIsMounted(pszShareName, szAlreadyMountedTo, sizeof(szAlreadyMountedTo))
VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Shared folder \"%s\" already mounted to \"%s\", unmounting ...\n",
#ifdef RT_OS_SOLARIS
VBoxServiceVerbose(0, "VBoxServiceAutoMountWorker: Shared folder \"%s\" was mounted to \"%s\"\n", pszShareName, pszMountPoint);
VBoxServiceError("VBoxServiceAutoMountWorker: Could not mount shared folder \"%s\" to \"%s\", error = %s\n",
&mntinf);
VBoxServiceVerbose(0, "VBoxServiceAutoMountWorker: Shared folder \"%s\" was mounted to \"%s\"\n", pszShareName, pszMountPoint);
VBoxServiceError("VBoxServiceAutoMountWorker: Could not update mount table (failed to create memstream): %s\n", strerror(errno));
VBoxServiceError("VBoxServiceAutoMountWorker: Could not open mount table for update: %s\n", strerror(errno));
VBoxServiceError("VBoxServiceAutoMountWorker: Could not add an entry to the mount table: %s\n", strerror(errno));
VBoxServiceError("VBoxServiceAutoMountWorker: Unknown error while completing mount operation: %d\n", r);
++cchCWD;
switch (errno)
case EINVAL:
VBoxServiceVerbose(0, "VBoxServiceAutoMountWorker: Shared folder \"%s\" already is mounted!\n", pszShareName);
case EBUSY:
VBoxServiceError("VBoxServiceAutoMountWorker: Could not mount shared folder \"%s\" to \"%s\": %s (%d)\n",
return rc;
static int VBoxServiceAutoMountProcessMappings(PVBGLR3SHAREDFOLDERMAPPING paMappings, uint32_t cMappings,
if (cMappings == 0)
return VINF_SUCCESS;
&& *pszShareName)
VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Connecting share %u (%s) ...\n", i+1, pszShareName);
if (grp_vboxsf)
VBoxServiceError("VBoxServiceAutoMountWorker: Unable to join mount point/prefix/shrae, rc = %Rrc\n", rc);
VBoxServiceError("VBoxServiceAutoMountWorker: Error while getting the shared folder name for root node = %u, rc = %Rrc\n",
return rc;
int rc = VbglR3SharedFolderGetMappings(g_SharedFoldersSvcClientID, true /* Only process auto-mounted folders */,
&& cMappings)
char *pszMountDir;
VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Shared folder mount dir set to \"%s\"\n", pszMountDir);
char *pszSharePrefix;
VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Shared folder mount prefix set to \"%s\"\n", pszSharePrefix);
#ifdef USE_VIRTUAL_SHARES
rc = VBoxServiceAutoMountProcessMappings(paMappings, cMappings, pszMountDir, pszSharePrefix, g_SharedFoldersSvcClientID);
#ifdef USE_VIRTUAL_SHARES
VBoxServiceError("VBoxServiceAutoMountWorker: Error while getting the shared folder mount prefix, rc = %Rrc\n", rc);
VBoxServiceError("VBoxServiceAutoMountWorker: Error while getting the shared folder directory, rc = %Rrc\n", rc);
VBoxServiceError("VBoxServiceAutoMountWorker: Error while getting the shared folder mappings, rc = %Rrc\n", rc);
else if (!cMappings)
if (*pfShutdown)
return VINF_SUCCESS;
NULL,
NULL,