- Timestamp:
- Nov 8, 2010 12:58:53 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxService/VBoxServiceAutoMount.cpp
r32704 r33834 33 33 #include <grp.h> 34 34 #include <sys/mount.h> 35 #include <mntent.h> 36 #include <paths.h> 35 37 #ifdef RT_OS_SOLARIS 36 38 #include <sys/vfs.h> … … 85 87 86 88 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. */ 89 static 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 122 static 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 142 static 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. */ 92 150 int rc = RTDirCreateFullPath(pszMountPoint, fMode); 93 151 if (RT_SUCCESS(rc)) … … 97 155 { 98 156 rc = RTPathSetMode(pszMountPoint, fMode); 99 VBoxServiceVerbose(3, "RTPathSetMode = rc = %Rrc\n");100 157 if (RT_FAILURE(rc)) 101 VBoxServiceError(" VBoxServiceAutoMountPrepareMountPoint: Could not setmode 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); 103 160 } 104 161 else … … 107 164 } 108 165 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); 111 168 return rc; 112 169 } … … 118 175 AssertPtr(pOpts); 119 176 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); 121 194 if (RT_SUCCESS(rc)) 122 195 { … … 197 270 } 198 271 } 199 else /* r != 0*/272 else /* r == -1, we got some error in errno. */ 200 273 { 201 274 if (errno == EPROTO) 202 275 { 276 VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Messed up share name, re-trying ...\n"); 277 203 278 /* Sometimes the mount utility messes up the share name. Try to 204 279 * un-mangle it again. */ … … 219 294 if (errno == EPROTO) 220 295 { 296 VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Re-trying with old mounting structure ...\n"); 297 221 298 /* New mount tool with old vboxsf module? Try again using the old 222 299 * vbsf_mount_info_old structure. */ … … 229 306 r = mount(NULL, pszMountPoint, "vboxsf", flags, &mntinf_old); 230 307 } 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 } 234 329 } 235 330 #endif /* !RT_OS_SOLARIS */ 236 331 } 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; 241 334 } 242 335
Note:
See TracChangeset
for help on using the changeset viewer.