VBoxServiceAutoMount.cpp revision a2410ed6c0ce63f29ac7132882e4e46d921f239d
f4e8ab454689875a442f0259087dcdb96527351fvboxsync * VBoxService - Auto-mounting for Shared Folders.
f4e8ab454689875a442f0259087dcdb96527351fvboxsync * Copyright (C) 2010 Oracle Corporation
f4e8ab454689875a442f0259087dcdb96527351fvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
f4e8ab454689875a442f0259087dcdb96527351fvboxsync * available from http://www.virtualbox.org. This file is free software;
f4e8ab454689875a442f0259087dcdb96527351fvboxsync * you can redistribute it and/or modify it under the terms of the GNU
f4e8ab454689875a442f0259087dcdb96527351fvboxsync * General Public License (GPL) as published by the Free Software
f4e8ab454689875a442f0259087dcdb96527351fvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
f4e8ab454689875a442f0259087dcdb96527351fvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
f4e8ab454689875a442f0259087dcdb96527351fvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
f4e8ab454689875a442f0259087dcdb96527351fvboxsync/*******************************************************************************
f4e8ab454689875a442f0259087dcdb96527351fvboxsync* Header Files *
f4e8ab454689875a442f0259087dcdb96527351fvboxsync*******************************************************************************/
f4e8ab454689875a442f0259087dcdb96527351fvboxsync/*******************************************************************************
f4e8ab454689875a442f0259087dcdb96527351fvboxsync* Global Variables *
f4e8ab454689875a442f0259087dcdb96527351fvboxsync*******************************************************************************/
f4e8ab454689875a442f0259087dcdb96527351fvboxsync/** The semaphore we're blocking on. */
f4e8ab454689875a442f0259087dcdb96527351fvboxsyncstatic RTSEMEVENTMULTI g_AutoMountEvent = NIL_RTSEMEVENTMULTI;
f4e8ab454689875a442f0259087dcdb96527351fvboxsync/** @copydoc VBOXSERVICE::pfnPreInit */
f4e8ab454689875a442f0259087dcdb96527351fvboxsyncstatic DECLCALLBACK(int) VBoxServiceAutoMountPreInit(void)
static DECLCALLBACK(int) VBoxServiceAutoMountOption(const char **ppszShort, int argc, char **argv, int *pi)
return VINF_SUCCESS;
return 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);
VBoxServiceError("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;
VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Failed to connect to the shared folder service, error %Rrc\n", rc);
&& 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);
rc = VBoxServiceAutoMountProcessMappings(paMappings, cMappings, pszMountDir, pszSharePrefix, u32ClientId);
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)
return VINF_SUCCESS;
NULL,
NULL,