1 | /* $Id: VBoxServiceAutoMount.cpp 37832 2011-07-08 10:13:18Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Auto-mounting for Shared Folders.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2011 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <iprt/assert.h>
|
---|
23 | #include <iprt/dir.h>
|
---|
24 | #include <iprt/mem.h>
|
---|
25 | #include <iprt/path.h>
|
---|
26 | #include <iprt/string.h>
|
---|
27 | #include <iprt/semaphore.h>
|
---|
28 | #include <VBox/VBoxGuestLib.h>
|
---|
29 | #include "VBoxServiceInternal.h"
|
---|
30 | #include "VBoxServiceUtils.h"
|
---|
31 |
|
---|
32 | #include <errno.h>
|
---|
33 | #include <grp.h>
|
---|
34 | #include <sys/mount.h>
|
---|
35 | #ifdef RT_OS_SOLARIS
|
---|
36 | # include <sys/mntent.h>
|
---|
37 | # include <sys/mnttab.h>
|
---|
38 | # include <sys/vfs.h>
|
---|
39 | #else
|
---|
40 | # include <mntent.h>
|
---|
41 | # include <paths.h>
|
---|
42 | #endif
|
---|
43 | #include <unistd.h>
|
---|
44 |
|
---|
45 | RT_C_DECLS_BEGIN
|
---|
46 | #include "../../linux/sharedfolders/vbsfmount.h"
|
---|
47 | RT_C_DECLS_END
|
---|
48 |
|
---|
49 | #ifdef RT_OS_SOLARIS
|
---|
50 | # define VBOXSERVICE_AUTOMOUNT_DEFAULT_DIR "/mnt"
|
---|
51 | #else
|
---|
52 | # define VBOXSERVICE_AUTOMOUNT_DEFAULT_DIR "/media"
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | #ifndef _PATH_MOUNTED
|
---|
56 | #ifdef RT_OS_SOLARIS
|
---|
57 | #define _PATH_MOUNTED "/etc/mnttab"
|
---|
58 | #else
|
---|
59 | #define _PATH_MOUNTED "/etc/mtab"
|
---|
60 | #endif
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | /*******************************************************************************
|
---|
64 | * Global Variables *
|
---|
65 | *******************************************************************************/
|
---|
66 | /** The semaphore we're blocking on. */
|
---|
67 | static RTSEMEVENTMULTI g_AutoMountEvent = NIL_RTSEMEVENTMULTI;
|
---|
68 | /** The Shared Folders service client ID. */
|
---|
69 | static uint32_t g_SharedFoldersSvcClientID = 0;
|
---|
70 |
|
---|
71 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
72 | static DECLCALLBACK(int) VBoxServiceAutoMountPreInit(void)
|
---|
73 | {
|
---|
74 | return VINF_SUCCESS;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
79 | static DECLCALLBACK(int) VBoxServiceAutoMountOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
80 | {
|
---|
81 | NOREF(ppszShort);
|
---|
82 | NOREF(argc);
|
---|
83 | NOREF(argv);
|
---|
84 | NOREF(pi);
|
---|
85 | return VINF_SUCCESS;
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
90 | static DECLCALLBACK(int) VBoxServiceAutoMountInit(void)
|
---|
91 | {
|
---|
92 | VBoxServiceVerbose(3, "VBoxServiceAutoMountInit\n");
|
---|
93 |
|
---|
94 | int rc = RTSemEventMultiCreate(&g_AutoMountEvent);
|
---|
95 | AssertRCReturn(rc, rc);
|
---|
96 |
|
---|
97 | rc = VbglR3SharedFolderConnect(&g_SharedFoldersSvcClientID);
|
---|
98 | if (RT_SUCCESS(rc))
|
---|
99 | {
|
---|
100 | VBoxServiceVerbose(3, "VBoxServiceAutoMountInit: Service Client ID: %#x\n", g_SharedFoldersSvcClientID);
|
---|
101 | }
|
---|
102 | else
|
---|
103 | {
|
---|
104 | /* If the service was not found, we disable this service without
|
---|
105 | causing VBoxService to fail. */
|
---|
106 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
|
---|
107 | {
|
---|
108 | VBoxServiceVerbose(0, "VBoxServiceAutoMountInit: Shared Folders service is not available\n");
|
---|
109 | rc = VERR_SERVICE_DISABLED;
|
---|
110 | }
|
---|
111 | else
|
---|
112 | VBoxServiceError("Control: Failed to connect to the Shared Folders service! Error: %Rrc\n", rc);
|
---|
113 | RTSemEventMultiDestroy(g_AutoMountEvent);
|
---|
114 | g_AutoMountEvent = NIL_RTSEMEVENTMULTI;
|
---|
115 | }
|
---|
116 |
|
---|
117 | return rc;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /** @todo Integrate into RTFsQueryMountpoint(). */
|
---|
122 | static bool VBoxServiceAutoMountShareIsMounted(const char *pszShare,
|
---|
123 | char *pszMountPoint, size_t cbMountPoint)
|
---|
124 | {
|
---|
125 | AssertPtrReturn(pszShare, VERR_INVALID_PARAMETER);
|
---|
126 | AssertPtrReturn(pszMountPoint, VERR_INVALID_PARAMETER);
|
---|
127 | AssertReturn(cbMountPoint, VERR_INVALID_PARAMETER);
|
---|
128 |
|
---|
129 | bool fMounted = false;
|
---|
130 | /* @todo What to do if we have a relative path in mtab instead
|
---|
131 | * of an absolute one ("temp" vs. "/media/temp")?
|
---|
132 | * procfs contains the full path but not the actual share name ...
|
---|
133 | * FILE *pFh = setmntent("/proc/mounts", "r+t"); */
|
---|
134 | #ifdef RT_OS_SOLARIS
|
---|
135 | FILE *pFh = fopen(_PATH_MOUNTED, "r");
|
---|
136 | if (!pFh)
|
---|
137 | VBoxServiceError("VBoxServiceAutoMountShareIsMounted: Could not open mount tab \"%s\"!\n",
|
---|
138 | _PATH_MOUNTED);
|
---|
139 | else
|
---|
140 | {
|
---|
141 | mnttab mntTab;
|
---|
142 | while ((getmntent(pFh, &mntTab)))
|
---|
143 | {
|
---|
144 | if (!RTStrICmp(mntTab.mnt_special, pszShare))
|
---|
145 | {
|
---|
146 | fMounted = RTStrPrintf(pszMountPoint, cbMountPoint, "%s", mntTab.mnt_mountp)
|
---|
147 | ? true : false;
|
---|
148 | break;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | fclose(pFh);
|
---|
152 | }
|
---|
153 | #else
|
---|
154 | FILE *pFh = setmntent(_PATH_MOUNTED, "r+t");
|
---|
155 | if (pFh == NULL)
|
---|
156 | VBoxServiceError("VBoxServiceAutoMountShareIsMounted: Could not open mount tab \"%s\"!\n",
|
---|
157 | _PATH_MOUNTED);
|
---|
158 | else
|
---|
159 | {
|
---|
160 | mntent *pMntEnt;
|
---|
161 | while ((pMntEnt = getmntent(pFh)))
|
---|
162 | {
|
---|
163 | if (!RTStrICmp(pMntEnt->mnt_fsname, pszShare))
|
---|
164 | {
|
---|
165 | fMounted = RTStrPrintf(pszMountPoint, cbMountPoint, "%s", pMntEnt->mnt_dir)
|
---|
166 | ? true : false;
|
---|
167 | break;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | endmntent(pFh);
|
---|
171 | }
|
---|
172 | #endif
|
---|
173 |
|
---|
174 | VBoxServiceVerbose(4, "VBoxServiceAutoMountShareIsMounted: Share \"%s\" at mount point \"%s\" = %s\n",
|
---|
175 | pszShare, fMounted ? pszMountPoint : "<None>", fMounted ? "Yes" : "No");
|
---|
176 | return fMounted;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | static int VBoxServiceAutoMountUnmount(const char *pszMountPoint)
|
---|
181 | {
|
---|
182 | AssertPtrReturn(pszMountPoint, VERR_INVALID_PARAMETER);
|
---|
183 |
|
---|
184 | int rc = VINF_SUCCESS;
|
---|
185 | uint8_t uTries = 0;
|
---|
186 | int r;
|
---|
187 | while (uTries++ < 3)
|
---|
188 | {
|
---|
189 | r = umount(pszMountPoint);
|
---|
190 | if (r == 0)
|
---|
191 | break;
|
---|
192 | RTThreadSleep(5000); /* Wait a while ... */
|
---|
193 | }
|
---|
194 | if (r == -1)
|
---|
195 | rc = RTErrConvertFromErrno(errno);
|
---|
196 | return rc;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | static int VBoxServiceAutoMountPrepareMountPoint(const char *pszMountPoint, const char *pszShareName,
|
---|
201 | vbsf_mount_opts *pOpts)
|
---|
202 | {
|
---|
203 | AssertPtrReturn(pOpts, VERR_INVALID_PARAMETER);
|
---|
204 | AssertPtrReturn(pszMountPoint, VERR_INVALID_PARAMETER);
|
---|
205 | AssertPtrReturn(pszShareName, VERR_INVALID_PARAMETER);
|
---|
206 |
|
---|
207 | RTFMODE fMode = RTFS_UNIX_IRWXU | RTFS_UNIX_IRWXG; /* Owner (=root) and the group (=vboxsf) have full access. */
|
---|
208 | int rc = RTDirCreateFullPath(pszMountPoint, fMode);
|
---|
209 | if (RT_SUCCESS(rc))
|
---|
210 | {
|
---|
211 | rc = RTPathSetOwnerEx(pszMountPoint, NIL_RTUID /* Owner, unchanged */, pOpts->gid, RTPATH_F_ON_LINK);
|
---|
212 | if (RT_SUCCESS(rc))
|
---|
213 | {
|
---|
214 | rc = RTPathSetMode(pszMountPoint, fMode);
|
---|
215 | if (RT_FAILURE(rc))
|
---|
216 | {
|
---|
217 | if (rc == VERR_WRITE_PROTECT)
|
---|
218 | {
|
---|
219 | VBoxServiceVerbose(3, "VBoxServiceAutoMountPrepareMountPoint: Mount directory \"%s\" already is used/mounted\n", pszMountPoint);
|
---|
220 | rc = VINF_SUCCESS;
|
---|
221 | }
|
---|
222 | else
|
---|
223 | VBoxServiceError("VBoxServiceAutoMountPrepareMountPoint: Could not set mode %RTfmode for mount directory \"%s\", rc = %Rrc\n",
|
---|
224 | fMode, pszMountPoint, rc);
|
---|
225 | }
|
---|
226 | }
|
---|
227 | else
|
---|
228 | VBoxServiceError("VBoxServiceAutoMountPrepareMountPoint: Could not set permissions for mount directory \"%s\", rc = %Rrc\n",
|
---|
229 | pszMountPoint, rc);
|
---|
230 | }
|
---|
231 | else
|
---|
232 | VBoxServiceError("VBoxServiceAutoMountPrepareMountPoint: Could not create mount directory \"%s\" with mode %RTfmode, rc = %Rrc\n",
|
---|
233 | pszMountPoint, fMode, rc);
|
---|
234 | return rc;
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | static int VBoxServiceAutoMountSharedFolder(const char *pszShareName, const char *pszMountPoint,
|
---|
239 | vbsf_mount_opts *pOpts)
|
---|
240 | {
|
---|
241 | AssertPtr(pOpts);
|
---|
242 |
|
---|
243 | int rc = VINF_SUCCESS;
|
---|
244 | char szAlreadyMountedTo[RTPATH_MAX];
|
---|
245 | /* If a Shared Folder already is mounted but not to our desired mount point,
|
---|
246 | * do an unmount first! */
|
---|
247 | if ( VBoxServiceAutoMountShareIsMounted(pszShareName, szAlreadyMountedTo, sizeof(szAlreadyMountedTo))
|
---|
248 | && RTStrICmp(pszMountPoint, szAlreadyMountedTo))
|
---|
249 | {
|
---|
250 | VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Shared folder \"%s\" already mounted to \"%s\", unmounting ...\n",
|
---|
251 | pszShareName, szAlreadyMountedTo);
|
---|
252 | rc = VBoxServiceAutoMountUnmount(szAlreadyMountedTo);
|
---|
253 | if (RT_FAILURE(rc))
|
---|
254 | VBoxServiceError("VBoxServiceAutoMountWorker: Failed to unmount \"%s\", %s (%d)!\n",
|
---|
255 | szAlreadyMountedTo, strerror(errno), errno);
|
---|
256 | }
|
---|
257 |
|
---|
258 | if (RT_SUCCESS(rc))
|
---|
259 | rc = VBoxServiceAutoMountPrepareMountPoint(pszMountPoint, pszShareName, pOpts);
|
---|
260 | if (RT_SUCCESS(rc))
|
---|
261 | {
|
---|
262 | #ifdef RT_OS_SOLARIS
|
---|
263 | char achOptBuf[MAX_MNTOPT_STR] = { '\0', };
|
---|
264 | int flags = 0;
|
---|
265 | if (pOpts->ronly)
|
---|
266 | flags |= MS_RDONLY;
|
---|
267 | RTStrPrintf(achOptBuf, sizeof(achOptBuf), "uid=%d,gid=%d", pOpts->uid, pOpts->gid);
|
---|
268 | int r = mount(pszShareName,
|
---|
269 | pszMountPoint,
|
---|
270 | flags | MS_OPTIONSTR,
|
---|
271 | "vboxfs",
|
---|
272 | NULL, /* char *dataptr */
|
---|
273 | 0, /* int datalen */
|
---|
274 | achOptBuf,
|
---|
275 | sizeof(achOptBuf));
|
---|
276 | if (r == 0)
|
---|
277 | {
|
---|
278 | VBoxServiceVerbose(0, "VBoxServiceAutoMountWorker: Shared folder \"%s\" was mounted to \"%s\"\n", pszShareName, pszMountPoint);
|
---|
279 | }
|
---|
280 | else
|
---|
281 | {
|
---|
282 | if (errno != EBUSY) /* Share is already mounted? Then skip error msg. */
|
---|
283 | VBoxServiceError("VBoxServiceAutoMountWorker: Could not mount shared folder \"%s\" to \"%s\", error = %s\n",
|
---|
284 | pszShareName, pszMountPoint, strerror(errno));
|
---|
285 | }
|
---|
286 | #else /* !RT_OS_SOLARIS */
|
---|
287 | unsigned long flags = MS_NODEV;
|
---|
288 |
|
---|
289 | const char *szOptions = { "rw" };
|
---|
290 | struct vbsf_mount_info_new mntinf;
|
---|
291 |
|
---|
292 | mntinf.nullchar = '\0';
|
---|
293 | mntinf.signature[0] = VBSF_MOUNT_SIGNATURE_BYTE_0;
|
---|
294 | mntinf.signature[1] = VBSF_MOUNT_SIGNATURE_BYTE_1;
|
---|
295 | mntinf.signature[2] = VBSF_MOUNT_SIGNATURE_BYTE_2;
|
---|
296 | mntinf.length = sizeof(mntinf);
|
---|
297 |
|
---|
298 | mntinf.uid = pOpts->uid;
|
---|
299 | mntinf.gid = pOpts->gid;
|
---|
300 | mntinf.ttl = pOpts->ttl;
|
---|
301 | mntinf.dmode = pOpts->dmode;
|
---|
302 | mntinf.fmode = pOpts->fmode;
|
---|
303 | mntinf.dmask = pOpts->dmask;
|
---|
304 | mntinf.fmask = pOpts->fmask;
|
---|
305 |
|
---|
306 | strcpy(mntinf.name, pszShareName);
|
---|
307 | strcpy(mntinf.nls_name, "\0");
|
---|
308 |
|
---|
309 | int r = mount(NULL,
|
---|
310 | pszMountPoint,
|
---|
311 | "vboxsf",
|
---|
312 | flags,
|
---|
313 | &mntinf);
|
---|
314 | if (r == 0)
|
---|
315 | {
|
---|
316 | VBoxServiceVerbose(0, "VBoxServiceAutoMountWorker: Shared folder \"%s\" was mounted to \"%s\"\n", pszShareName, pszMountPoint);
|
---|
317 |
|
---|
318 | r = vbsfmount_complete(pszShareName, pszMountPoint, flags, pOpts);
|
---|
319 | switch (r)
|
---|
320 | {
|
---|
321 | case 0: /* Success. */
|
---|
322 | errno = 0; /* Clear all errors/warnings. */
|
---|
323 | break;
|
---|
324 |
|
---|
325 | case 1:
|
---|
326 | VBoxServiceError("VBoxServiceAutoMountWorker: Could not update mount table (failed to create memstream): %s\n", strerror(errno));
|
---|
327 | break;
|
---|
328 |
|
---|
329 | case 2:
|
---|
330 | VBoxServiceError("VBoxServiceAutoMountWorker: Could not open mount table for update: %s\n", strerror(errno));
|
---|
331 | break;
|
---|
332 |
|
---|
333 | case 3:
|
---|
334 | VBoxServiceError("VBoxServiceAutoMountWorker: Could not add an entry to the mount table: %s\n", strerror(errno));
|
---|
335 | break;
|
---|
336 |
|
---|
337 | default:
|
---|
338 | VBoxServiceError("VBoxServiceAutoMountWorker: Unknown error while completing mount operation: %d\n", r);
|
---|
339 | break;
|
---|
340 | }
|
---|
341 | }
|
---|
342 | else /* r == -1, we got some error in errno. */
|
---|
343 | {
|
---|
344 | if (errno == EPROTO)
|
---|
345 | {
|
---|
346 | VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Messed up share name, re-trying ...\n");
|
---|
347 |
|
---|
348 | /* Sometimes the mount utility messes up the share name. Try to
|
---|
349 | * un-mangle it again. */
|
---|
350 | char szCWD[4096];
|
---|
351 | size_t cchCWD;
|
---|
352 | if (!getcwd(szCWD, sizeof(szCWD)))
|
---|
353 | VBoxServiceError("VBoxServiceAutoMountWorker: Failed to get the current working directory\n");
|
---|
354 | cchCWD = strlen(szCWD);
|
---|
355 | if (!strncmp(pszMountPoint, szCWD, cchCWD))
|
---|
356 | {
|
---|
357 | while (pszMountPoint[cchCWD] == '/')
|
---|
358 | ++cchCWD;
|
---|
359 | /* We checked before that we have enough space */
|
---|
360 | strcpy(mntinf.name, pszMountPoint + cchCWD);
|
---|
361 | }
|
---|
362 | r = mount(NULL, pszMountPoint, "vboxsf", flags, &mntinf);
|
---|
363 | }
|
---|
364 | if (errno == EPROTO)
|
---|
365 | {
|
---|
366 | VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Re-trying with old mounting structure ...\n");
|
---|
367 |
|
---|
368 | /* New mount tool with old vboxsf module? Try again using the old
|
---|
369 | * vbsf_mount_info_old structure. */
|
---|
370 | struct vbsf_mount_info_old mntinf_old;
|
---|
371 | memcpy(&mntinf_old.name, &mntinf.name, MAX_HOST_NAME);
|
---|
372 | memcpy(&mntinf_old.nls_name, mntinf.nls_name, MAX_NLS_NAME);
|
---|
373 | mntinf_old.uid = mntinf.uid;
|
---|
374 | mntinf_old.gid = mntinf.gid;
|
---|
375 | mntinf_old.ttl = mntinf.ttl;
|
---|
376 | r = mount(NULL, pszMountPoint, "vboxsf", flags, &mntinf_old);
|
---|
377 | }
|
---|
378 | if (r == -1) /* Was there some error from one of the tries above? */
|
---|
379 | {
|
---|
380 | switch (errno)
|
---|
381 | {
|
---|
382 | /* If we get EINVAL here, the system already has mounted the Shared Folder to another
|
---|
383 | * mount point. */
|
---|
384 | case EINVAL:
|
---|
385 | VBoxServiceVerbose(0, "VBoxServiceAutoMountWorker: Shared folder \"%s\" already is mounted!\n", pszShareName);
|
---|
386 | /* Ignore this error! */
|
---|
387 | break;
|
---|
388 | case EBUSY:
|
---|
389 | /* Ignore these errors! */
|
---|
390 | break;
|
---|
391 |
|
---|
392 | default:
|
---|
393 | VBoxServiceError("VBoxServiceAutoMountWorker: Could not mount shared folder \"%s\" to \"%s\": %s (%d)\n",
|
---|
394 | pszShareName, pszMountPoint, strerror(errno), errno);
|
---|
395 | rc = RTErrConvertFromErrno(errno);
|
---|
396 | break;
|
---|
397 | }
|
---|
398 | }
|
---|
399 | }
|
---|
400 | #endif /* !RT_OS_SOLARIS */
|
---|
401 | }
|
---|
402 | VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Mounting returned with rc=%Rrc\n", rc);
|
---|
403 | return rc;
|
---|
404 | }
|
---|
405 |
|
---|
406 | static int VBoxServiceAutoMountProcessMappings(PVBGLR3SHAREDFOLDERMAPPING paMappings, uint32_t cMappings,
|
---|
407 | const char *pszMountDir, const char *pszSharePrefix, uint32_t uClientID)
|
---|
408 | {
|
---|
409 | if (cMappings == 0)
|
---|
410 | return VINF_SUCCESS;
|
---|
411 | AssertPtrReturn(paMappings, VERR_INVALID_PARAMETER);
|
---|
412 | AssertPtrReturn(pszMountDir, VERR_INVALID_PARAMETER);
|
---|
413 | AssertPtrReturn(pszSharePrefix, VERR_INVALID_PARAMETER);
|
---|
414 | AssertReturn(uClientID > 0, VERR_INVALID_PARAMETER);
|
---|
415 |
|
---|
416 | int rc = VINF_SUCCESS;
|
---|
417 | for (uint32_t i = 0; i < cMappings && RT_SUCCESS(rc); i++)
|
---|
418 | {
|
---|
419 | char *pszShareName = NULL;
|
---|
420 | rc = VbglR3SharedFolderGetName(uClientID, paMappings[i].u32Root, &pszShareName);
|
---|
421 | if ( RT_SUCCESS(rc)
|
---|
422 | && *pszShareName)
|
---|
423 | {
|
---|
424 | VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Connecting share %u (%s) ...\n", i+1, pszShareName);
|
---|
425 |
|
---|
426 | char *pszShareNameFull = NULL;
|
---|
427 | if (RTStrAPrintf(&pszShareNameFull, "%s%s", pszSharePrefix, pszShareName) > 0)
|
---|
428 | {
|
---|
429 | char szMountPoint[RTPATH_MAX];
|
---|
430 | rc = RTPathJoin(szMountPoint, sizeof(szMountPoint), pszMountDir, pszShareNameFull);
|
---|
431 | if (RT_SUCCESS(rc))
|
---|
432 | {
|
---|
433 | VBoxServiceVerbose(4, "VBoxServiceAutoMountWorker: Processing mount point \"%s\"\n", szMountPoint);
|
---|
434 |
|
---|
435 | struct group *grp_vboxsf = getgrnam("vboxsf");
|
---|
436 | if (grp_vboxsf)
|
---|
437 | {
|
---|
438 | struct vbsf_mount_opts mount_opts =
|
---|
439 | {
|
---|
440 | 0, /* uid */
|
---|
441 | grp_vboxsf->gr_gid, /* gid */
|
---|
442 | 0, /* ttl */
|
---|
443 | 0770, /* dmode, owner and group "vboxsf" have full access */
|
---|
444 | 0770, /* fmode, owner and group "vboxsf" have full access */
|
---|
445 | 0, /* dmask */
|
---|
446 | 0, /* fmask */
|
---|
447 | 0, /* ronly */
|
---|
448 | 0, /* noexec */
|
---|
449 | 0, /* nodev */
|
---|
450 | 0, /* nosuid */
|
---|
451 | 0, /* remount */
|
---|
452 | "\0", /* nls_name */
|
---|
453 | NULL, /* convertcp */
|
---|
454 | };
|
---|
455 |
|
---|
456 | rc = VBoxServiceAutoMountSharedFolder(pszShareName, szMountPoint, &mount_opts);
|
---|
457 | }
|
---|
458 | else
|
---|
459 | VBoxServiceError("VBoxServiceAutoMountWorker: Group \"vboxsf\" does not exist\n");
|
---|
460 | }
|
---|
461 | else
|
---|
462 | VBoxServiceError("VBoxServiceAutoMountWorker: Unable to join mount point/prefix/shrae, rc = %Rrc\n", rc);
|
---|
463 | RTStrFree(pszShareNameFull);
|
---|
464 | }
|
---|
465 | else
|
---|
466 | VBoxServiceError("VBoxServiceAutoMountWorker: Unable to allocate full share name\n");
|
---|
467 | RTStrFree(pszShareName);
|
---|
468 | }
|
---|
469 | else
|
---|
470 | VBoxServiceError("VBoxServiceAutoMountWorker: Error while getting the shared folder name for root node = %u, rc = %Rrc\n",
|
---|
471 | paMappings[i].u32Root, rc);
|
---|
472 | } /* for cMappings. */
|
---|
473 | return rc;
|
---|
474 | }
|
---|
475 |
|
---|
476 |
|
---|
477 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
478 | DECLCALLBACK(int) VBoxServiceAutoMountWorker(bool volatile *pfShutdown)
|
---|
479 | {
|
---|
480 | /*
|
---|
481 | * Tell the control thread that it can continue
|
---|
482 | * spawning services.
|
---|
483 | */
|
---|
484 | RTThreadUserSignal(RTThreadSelf());
|
---|
485 |
|
---|
486 | uint32_t cMappings;
|
---|
487 | PVBGLR3SHAREDFOLDERMAPPING paMappings;
|
---|
488 | int rc = VbglR3SharedFolderGetMappings(g_SharedFoldersSvcClientID, true /* Only process auto-mounted folders */,
|
---|
489 | &paMappings, &cMappings);
|
---|
490 | if ( RT_SUCCESS(rc)
|
---|
491 | && cMappings)
|
---|
492 | {
|
---|
493 | char *pszMountDir;
|
---|
494 | rc = VbglR3SharedFolderGetMountDir(&pszMountDir);
|
---|
495 | if (rc == VERR_NOT_FOUND)
|
---|
496 | rc = RTStrDupEx(&pszMountDir, VBOXSERVICE_AUTOMOUNT_DEFAULT_DIR);
|
---|
497 | if (RT_SUCCESS(rc))
|
---|
498 | {
|
---|
499 | VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Shared folder mount dir set to \"%s\"\n", pszMountDir);
|
---|
500 |
|
---|
501 | char *pszSharePrefix;
|
---|
502 | rc = VbglR3SharedFolderGetMountPrefix(&pszSharePrefix);
|
---|
503 | if (RT_SUCCESS(rc))
|
---|
504 | {
|
---|
505 | VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Shared folder mount prefix set to \"%s\"\n", pszSharePrefix);
|
---|
506 | #ifdef USE_VIRTUAL_SHARES
|
---|
507 | /* Check for a fixed/virtual auto-mount share. */
|
---|
508 | if (VbglR3SharedFolderExists(g_SharedFoldersSvcClientID, "vbsfAutoMount"))
|
---|
509 | {
|
---|
510 | VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Host supports auto-mount root\n");
|
---|
511 | }
|
---|
512 | else
|
---|
513 | {
|
---|
514 | #endif
|
---|
515 | VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Got %u shared folder mappings\n", cMappings);
|
---|
516 | rc = VBoxServiceAutoMountProcessMappings(paMappings, cMappings, pszMountDir, pszSharePrefix, g_SharedFoldersSvcClientID);
|
---|
517 | #ifdef USE_VIRTUAL_SHARES
|
---|
518 | }
|
---|
519 | #endif
|
---|
520 | RTStrFree(pszSharePrefix);
|
---|
521 | } /* Mount share prefix. */
|
---|
522 | else
|
---|
523 | VBoxServiceError("VBoxServiceAutoMountWorker: Error while getting the shared folder mount prefix, rc = %Rrc\n", rc);
|
---|
524 | RTStrFree(pszMountDir);
|
---|
525 | }
|
---|
526 | else
|
---|
527 | VBoxServiceError("VBoxServiceAutoMountWorker: Error while getting the shared folder directory, rc = %Rrc\n", rc);
|
---|
528 | VbglR3SharedFolderFreeMappings(paMappings);
|
---|
529 | }
|
---|
530 | else
|
---|
531 | {
|
---|
532 | if (RT_FAILURE(rc))
|
---|
533 | VBoxServiceError("VBoxServiceAutoMountWorker: Error while getting the shared folder mappings, rc = %Rrc\n", rc);
|
---|
534 | else if (!cMappings)
|
---|
535 | VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: No shared folder mappings fouund\n");
|
---|
536 | }
|
---|
537 |
|
---|
538 | /*
|
---|
539 | * Because this thread is a one-timer at the moment we don't want to break/change
|
---|
540 | * the semantics of the main thread's start/stop sub-threads handling.
|
---|
541 | *
|
---|
542 | * This thread exits so fast while doing its own startup in VBoxServiceStartServices()
|
---|
543 | * that this->fShutdown flag is set to true in VBoxServiceThread() before we have the
|
---|
544 | * chance to check for a service failure in VBoxServiceStartServices() to indicate
|
---|
545 | * a VBoxService startup error.
|
---|
546 | *
|
---|
547 | * Therefore *no* service threads are allowed to quit themselves and need to wait
|
---|
548 | * for the pfShutdown flag to be set by the main thread.
|
---|
549 | */
|
---|
550 | for (;;)
|
---|
551 | {
|
---|
552 | /* Do we need to shutdown? */
|
---|
553 | if (*pfShutdown)
|
---|
554 | break;
|
---|
555 |
|
---|
556 | /* Let's sleep for a bit and let others run ... */
|
---|
557 | RTThreadSleep(500);
|
---|
558 | }
|
---|
559 |
|
---|
560 | RTSemEventMultiDestroy(g_AutoMountEvent);
|
---|
561 | g_AutoMountEvent = NIL_RTSEMEVENTMULTI;
|
---|
562 |
|
---|
563 | VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Finished with rc=%Rrc\n", rc);
|
---|
564 | return VINF_SUCCESS;
|
---|
565 | }
|
---|
566 |
|
---|
567 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
568 | static DECLCALLBACK(void) VBoxServiceAutoMountTerm(void)
|
---|
569 | {
|
---|
570 | VBoxServiceVerbose(3, "VBoxServiceAutoMountTerm\n");
|
---|
571 |
|
---|
572 | VbglR3SharedFolderDisconnect(g_SharedFoldersSvcClientID);
|
---|
573 | g_SharedFoldersSvcClientID = 0;
|
---|
574 |
|
---|
575 | if (g_AutoMountEvent != NIL_RTSEMEVENTMULTI)
|
---|
576 | {
|
---|
577 | RTSemEventMultiDestroy(g_AutoMountEvent);
|
---|
578 | g_AutoMountEvent = NIL_RTSEMEVENTMULTI;
|
---|
579 | }
|
---|
580 | return;
|
---|
581 | }
|
---|
582 |
|
---|
583 |
|
---|
584 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
585 | static DECLCALLBACK(void) VBoxServiceAutoMountStop(void)
|
---|
586 | {
|
---|
587 | /*
|
---|
588 | * We need this check because at the moment our auto-mount
|
---|
589 | * thread really is a one-timer which destroys the event itself
|
---|
590 | * after running.
|
---|
591 | */
|
---|
592 | if (g_AutoMountEvent != NIL_RTSEMEVENTMULTI)
|
---|
593 | RTSemEventMultiSignal(g_AutoMountEvent);
|
---|
594 | }
|
---|
595 |
|
---|
596 |
|
---|
597 | /**
|
---|
598 | * The 'automount' service description.
|
---|
599 | */
|
---|
600 | VBOXSERVICE g_AutoMount =
|
---|
601 | {
|
---|
602 | /* pszName. */
|
---|
603 | "automount",
|
---|
604 | /* pszDescription. */
|
---|
605 | "Auto-mount for Shared Folders",
|
---|
606 | /* pszUsage. */
|
---|
607 | NULL,
|
---|
608 | /* pszOptions. */
|
---|
609 | NULL,
|
---|
610 | /* methods */
|
---|
611 | VBoxServiceAutoMountPreInit,
|
---|
612 | VBoxServiceAutoMountOption,
|
---|
613 | VBoxServiceAutoMountInit,
|
---|
614 | VBoxServiceAutoMountWorker,
|
---|
615 | VBoxServiceAutoMountStop,
|
---|
616 | VBoxServiceAutoMountTerm
|
---|
617 | };
|
---|