VirtualBox

Changeset 33834 in vbox for trunk/src/VBox/Additions


Ignore:
Timestamp:
Nov 8, 2010 12:58:53 PM (14 years ago)
Author:
vboxsync
Message:

VBoxService/AutoMount: Update, bugfixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxService/VBoxServiceAutoMount.cpp

    r32704 r33834  
    3333#include <grp.h>
    3434#include <sys/mount.h>
     35#include <mntent.h>
     36#include <paths.h>
    3537#ifdef RT_OS_SOLARIS
    3638#include <sys/vfs.h>
     
    8587
    8688
    87 static int VBoxServiceAutoMountPrepareMountPoint(const char *pszMountPoint, vbsf_mount_opts *pOpts)
    88 {
    89     AssertPtr(pOpts);
    90 
    91     RTFMODE fMode = 0770; /* Owner (=root) and the group (=vboxsf) have full access. */
     89static bool VBoxServiceAutoMountShareIsMounted(const char *pszShare,
     90                                               char *pszMountPoint, size_t cbMountPoint)
     91{
     92    AssertPtrReturn(pszShare, VERR_INVALID_PARAMETER);
     93    AssertPtrReturn(pszMountPoint, VERR_INVALID_PARAMETER);
     94    AssertReturn(cbMountPoint, VERR_INVALID_PARAMETER);
     95
     96    bool fMounted = false;
     97    /* @todo What to do if we have a relative path in mtab instead
     98     *       of an absolute one ("temp" vs. "/media/temp")?
     99     * procfs contains the full path but not the actual share name ...
     100     * FILE *pFh = setmntent("/proc/mounts", "r+t"); */
     101    FILE *pFh = setmntent(_PATH_MOUNTED, "r+t");
     102    if (pFh == NULL)
     103        VBoxServiceError("VBoxServiceAutoMountShareIsMounted: Could not open mtab!\n");
     104    else
     105    {
     106        mntent *pMntEnt;
     107        while ((pMntEnt = getmntent(pFh)))
     108        {
     109            if (!RTStrICmp(pMntEnt->mnt_fsname, pszShare))
     110            {
     111                fMounted = RTStrPrintf(pszMountPoint, cbMountPoint, "%s", pMntEnt->mnt_dir)
     112                         ? true : false;               
     113                break;
     114            }               
     115        }       
     116        endmntent(pFh);
     117    }
     118    return fMounted;
     119}
     120
     121
     122static int VBoxServiceAutoMountUnmount(const char *pszMountPoint)
     123{
     124    AssertPtrReturn(pszMountPoint, VERR_INVALID_PARAMETER);
     125
     126    int rc = VINF_SUCCESS;
     127    uint8_t uTries = 0;
     128    int r;
     129    while (uTries++ < 3)
     130    {
     131        r = umount(pszMountPoint);
     132        if (r == 0)
     133            break;               
     134        RTThreadSleep(5000); /* Wait a while ... */
     135    }
     136    if (r == -1)
     137        rc = RTErrConvertFromErrno(errno);
     138    return rc;       
     139}
     140
     141
     142static int VBoxServiceAutoMountPrepareMountPoint(const char *pszMountPoint, const char *pszShareName,
     143                                                 vbsf_mount_opts *pOpts)
     144{
     145    AssertPtrReturn(pOpts, VERR_INVALID_PARAMETER);
     146    AssertPtrReturn(pszMountPoint, VERR_INVALID_PARAMETER);
     147    AssertPtrReturn(pszShareName, VERR_INVALID_PARAMETER);
     148
     149    RTFMODE fMode = RTFS_UNIX_IRWXU | RTFS_UNIX_IRWXG; /* Owner (=root) and the group (=vboxsf) have full access. */
    92150    int rc = RTDirCreateFullPath(pszMountPoint, fMode);
    93151    if (RT_SUCCESS(rc))
     
    97155        {
    98156            rc = RTPathSetMode(pszMountPoint, fMode);
    99             VBoxServiceVerbose(3, "RTPathSetMode = rc = %Rrc\n");
    100157            if (RT_FAILURE(rc))
    101                 VBoxServiceError("VBoxServiceAutoMountPrepareMountPoint: Could not set mode for mount directory \"%s\", rc = %Rrc\n",
    102                                  pszMountPoint, rc);
     158                VBoxServiceError(": Could not set mode %RTfmode for mount directory \"%s\", rc = %Rrc\n",
     159                                 fMode, pszMountPoint, rc);
    103160        }
    104161        else
     
    107164    }
    108165    else
    109         VBoxServiceError("VBoxServiceAutoMountPrepareMountPoint: Could not create mount directory \"%s\", rc = %Rrc\n",
    110                          pszMountPoint, rc);
     166        VBoxServiceError("VBoxServiceAutoMountPrepareMountPoint: Could not create mount directory \"%s\" with mode %RTfmode, rc = %Rrc\n",
     167                         pszMountPoint, fMode, rc);
    111168    return rc;
    112169}
     
    118175    AssertPtr(pOpts);
    119176
    120     int rc = VBoxServiceAutoMountPrepareMountPoint(pszMountPoint, pOpts);
     177    int rc;
     178    char szAlreadyMountedTo[RTPATH_MAX];
     179    /* If a Shared Folder already is mounted but not to our desired mount point,
     180     * do an unmount first! */
     181    if (   VBoxServiceAutoMountShareIsMounted(pszShareName, szAlreadyMountedTo, sizeof(szAlreadyMountedTo))
     182        && RTStrICmp(pszMountPoint, szAlreadyMountedTo))
     183    {
     184        VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Shared folder \"%s\" already mounted to \"%s\", unmounting ...\n",
     185                           pszShareName, szAlreadyMountedTo);
     186        rc = VBoxServiceAutoMountUnmount(szAlreadyMountedTo);
     187        if (RT_FAILURE(rc))
     188            VBoxServiceError("VBoxServiceAutoMountWorker: Failed to unmount \"%s\", %s (%d)!\n",
     189                             szAlreadyMountedTo, strerror(errno), errno);
     190    }
     191
     192    if (RT_SUCCESS(rc))
     193        rc = VBoxServiceAutoMountPrepareMountPoint(pszMountPoint, pszShareName, pOpts);
    121194    if (RT_SUCCESS(rc))
    122195    {
     
    197270            }
    198271        }
    199         else /* r != 0 */
     272        else /* r == -1, we got some error in errno. */
    200273        {
    201274            if (errno == EPROTO)
    202275            {
     276                VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Messed up share name, re-trying ...\n");
     277
    203278                /* Sometimes the mount utility messes up the share name.  Try to
    204279                 * un-mangle it again. */
     
    219294            if (errno == EPROTO)
    220295            {
     296                VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Re-trying with old mounting structure ...\n");
     297
    221298                /* New mount tool with old vboxsf module? Try again using the old
    222299                 * vbsf_mount_info_old structure. */
     
    229306                r = mount(NULL, pszMountPoint, "vboxsf", flags, &mntinf_old);
    230307            }
    231             if (errno != EBUSY) /* Share is already mounted? Then skip error msg. */
    232                 VBoxServiceError("VBoxServiceAutoMountWorker: Could not mount shared folder \"%s\" to \"%s\", error = %s\n",
    233                                  pszShareName, pszMountPoint, strerror(errno));
     308            if (r == -1) /* Was there some error from one of the tries above? */
     309            {
     310                switch (errno)
     311                {
     312                    /* If we get EINVAL here, the system already has mounted the Shared Folder to another
     313                     * mount point. */
     314                    case EINVAL:
     315                        VBoxServiceVerbose(0, "VBoxServiceAutoMountWorker: Shared folder \"%s\" already is mounted!\n", pszShareName);
     316                        /* Ignore this error! */
     317                        break;                                         
     318                    case EBUSY:
     319                        /* Ignore these errors! */
     320                        break;
     321
     322                    default:
     323                        VBoxServiceError("VBoxServiceAutoMountWorker: Could not mount shared folder \"%s\" to \"%s\": %s (%d)\n",
     324                                         pszShareName, pszMountPoint, strerror(errno), errno);
     325                        rc = RTErrConvertFromErrno(errno);
     326                        break;
     327                }               
     328            }
    234329        }
    235330#endif /* !RT_OS_SOLARIS */
    236331    }
    237 
    238     VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Mounting returned with rc=%Rrc, errno=%d, error=%s\n",
    239                        rc, errno, strerror(errno));
    240     return RTErrConvertFromErrno(errno);
     332    VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Mounting returned with rc=%Rrc\n", rc);
     333    return rc;
    241334}
    242335
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette