1 | /* $Id: VBoxServiceAutoMount.cpp 58029 2015-10-05 20:50:18Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Auto-mounting for Shared Folders, only Linux & Solaris atm.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2015 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 | /*********************************************************************************************************************************
|
---|
65 | * Global Variables *
|
---|
66 | *********************************************************************************************************************************/
|
---|
67 | /** The semaphore we're blocking on. */
|
---|
68 | static RTSEMEVENTMULTI g_AutoMountEvent = NIL_RTSEMEVENTMULTI;
|
---|
69 | /** The Shared Folders service client ID. */
|
---|
70 | static uint32_t g_SharedFoldersSvcClientID = 0;
|
---|
71 |
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * @interface_method_impl{VBOXSERVICE,pfnInit}
|
---|
75 | */
|
---|
76 | static DECLCALLBACK(int) vbsvcAutoMountInit(void)
|
---|
77 | {
|
---|
78 | VGSvcVerbose(3, "vbsvcAutoMountInit\n");
|
---|
79 |
|
---|
80 | int rc = RTSemEventMultiCreate(&g_AutoMountEvent);
|
---|
81 | AssertRCReturn(rc, rc);
|
---|
82 |
|
---|
83 | rc = VbglR3SharedFolderConnect(&g_SharedFoldersSvcClientID);
|
---|
84 | if (RT_SUCCESS(rc))
|
---|
85 | {
|
---|
86 | VGSvcVerbose(3, "vbsvcAutoMountInit: Service Client ID: %#x\n", g_SharedFoldersSvcClientID);
|
---|
87 | }
|
---|
88 | else
|
---|
89 | {
|
---|
90 | /* If the service was not found, we disable this service without
|
---|
91 | causing VBoxService to fail. */
|
---|
92 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
|
---|
93 | {
|
---|
94 | VGSvcVerbose(0, "vbsvcAutoMountInit: Shared Folders service is not available\n");
|
---|
95 | rc = VERR_SERVICE_DISABLED;
|
---|
96 | }
|
---|
97 | else
|
---|
98 | VGSvcError("Control: Failed to connect to the Shared Folders service! Error: %Rrc\n", rc);
|
---|
99 | RTSemEventMultiDestroy(g_AutoMountEvent);
|
---|
100 | g_AutoMountEvent = NIL_RTSEMEVENTMULTI;
|
---|
101 | }
|
---|
102 |
|
---|
103 | return rc;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * @todo Integrate into RTFsQueryMountpoint()?
|
---|
109 | */
|
---|
110 | static bool vbsvcAutoMountShareIsMounted(const char *pszShare, char *pszMountPoint, size_t cbMountPoint)
|
---|
111 | {
|
---|
112 | AssertPtrReturn(pszShare, VERR_INVALID_PARAMETER);
|
---|
113 | AssertPtrReturn(pszMountPoint, VERR_INVALID_PARAMETER);
|
---|
114 | AssertReturn(cbMountPoint, VERR_INVALID_PARAMETER);
|
---|
115 |
|
---|
116 | bool fMounted = false;
|
---|
117 | /** @todo What to do if we have a relative path in mtab instead
|
---|
118 | * of an absolute one ("temp" vs. "/media/temp")?
|
---|
119 | * procfs contains the full path but not the actual share name ...
|
---|
120 | * FILE *pFh = setmntent("/proc/mounts", "r+t"); */
|
---|
121 | #ifdef RT_OS_SOLARIS
|
---|
122 | FILE *pFh = fopen(_PATH_MOUNTED, "r");
|
---|
123 | if (!pFh)
|
---|
124 | VGSvcError("vbsvcAutoMountShareIsMounted: Could not open mount tab '%s'!\n", _PATH_MOUNTED);
|
---|
125 | else
|
---|
126 | {
|
---|
127 | mnttab mntTab;
|
---|
128 | while ((getmntent(pFh, &mntTab)))
|
---|
129 | {
|
---|
130 | if (!RTStrICmp(mntTab.mnt_special, pszShare))
|
---|
131 | {
|
---|
132 | fMounted = RTStrPrintf(pszMountPoint, cbMountPoint, "%s", mntTab.mnt_mountp)
|
---|
133 | ? true : false;
|
---|
134 | break;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | fclose(pFh);
|
---|
138 | }
|
---|
139 | #else
|
---|
140 | FILE *pFh = setmntent(_PATH_MOUNTED, "r+t"); /** @todo r=bird: why open it for writing? (the '+') */
|
---|
141 | if (pFh == NULL)
|
---|
142 | VGSvcError("vbsvcAutoMountShareIsMounted: Could not open mount tab '%s'!\n", _PATH_MOUNTED);
|
---|
143 | else
|
---|
144 | {
|
---|
145 | mntent *pMntEnt;
|
---|
146 | while ((pMntEnt = getmntent(pFh)))
|
---|
147 | {
|
---|
148 | if (!RTStrICmp(pMntEnt->mnt_fsname, pszShare))
|
---|
149 | {
|
---|
150 | fMounted = RTStrPrintf(pszMountPoint, cbMountPoint, "%s", pMntEnt->mnt_dir)
|
---|
151 | ? true : false;
|
---|
152 | break;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | endmntent(pFh);
|
---|
156 | }
|
---|
157 | #endif
|
---|
158 |
|
---|
159 | VGSvcVerbose(4, "vbsvcAutoMountShareIsMounted: Share '%s' at mount point '%s' = %s\n",
|
---|
160 | pszShare, fMounted ? pszMountPoint : "<None>", fMounted ? "Yes" : "No");
|
---|
161 | return fMounted;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Unmounts a shared folder.
|
---|
167 | *
|
---|
168 | * @returns VBox status code
|
---|
169 | * @param pszMountPoint The shared folder mount point.
|
---|
170 | */
|
---|
171 | static int vbsvcAutoMountUnmount(const char *pszMountPoint)
|
---|
172 | {
|
---|
173 | AssertPtrReturn(pszMountPoint, VERR_INVALID_PARAMETER);
|
---|
174 |
|
---|
175 | int rc = VINF_SUCCESS;
|
---|
176 | uint8_t uTries = 0;
|
---|
177 | int r;
|
---|
178 | while (uTries++ < 3)
|
---|
179 | {
|
---|
180 | r = umount(pszMountPoint);
|
---|
181 | if (r == 0)
|
---|
182 | break;
|
---|
183 | /** @todo r=bird: Why do sleep 5 seconds after the final retry?
|
---|
184 | * May also be a good idea to check for EINVAL or other signs that someone
|
---|
185 | * else have already unmounted the share. */
|
---|
186 | RTThreadSleep(5000); /* Wait a while ... */
|
---|
187 | }
|
---|
188 | if (r == -1) /** @todo r=bird: RTThreadSleep set errno. */
|
---|
189 | rc = RTErrConvertFromErrno(errno);
|
---|
190 | return rc;
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Prepares a mount point (create it, set group and mode).
|
---|
196 | *
|
---|
197 | * @returns VBox status code
|
---|
198 | * @param pszMountPoint The mount point.
|
---|
199 | * @param pszShareName Unused.
|
---|
200 | * @param pOpts For getting the group ID.
|
---|
201 | */
|
---|
202 | static int vbsvcAutoMountPrepareMountPoint(const char *pszMountPoint, const char *pszShareName, vbsf_mount_opts *pOpts)
|
---|
203 | {
|
---|
204 | AssertPtrReturn(pOpts, VERR_INVALID_PARAMETER);
|
---|
205 | AssertPtrReturn(pszMountPoint, VERR_INVALID_PARAMETER);
|
---|
206 | AssertPtrReturn(pszShareName, VERR_INVALID_PARAMETER);
|
---|
207 |
|
---|
208 | RTFMODE fMode = RTFS_UNIX_IRWXU | RTFS_UNIX_IRWXG; /* Owner (=root) and the group (=vboxsf) have full access. */
|
---|
209 | int rc = RTDirCreateFullPath(pszMountPoint, fMode);
|
---|
210 | if (RT_SUCCESS(rc))
|
---|
211 | {
|
---|
212 | rc = RTPathSetOwnerEx(pszMountPoint, NIL_RTUID /* Owner, unchanged */, pOpts->gid, RTPATH_F_ON_LINK);
|
---|
213 | if (RT_SUCCESS(rc))
|
---|
214 | {
|
---|
215 | rc = RTPathSetMode(pszMountPoint, fMode);
|
---|
216 | if (RT_FAILURE(rc))
|
---|
217 | {
|
---|
218 | if (rc == VERR_WRITE_PROTECT)
|
---|
219 | {
|
---|
220 | VGSvcVerbose(3, "vbsvcAutoMountPrepareMountPoint: Mount directory '%s' already is used/mounted\n",
|
---|
221 | pszMountPoint);
|
---|
222 | rc = VINF_SUCCESS;
|
---|
223 | }
|
---|
224 | else
|
---|
225 | VGSvcError("vbsvcAutoMountPrepareMountPoint: Could not set mode %RTfmode for mount directory '%s', rc = %Rrc\n",
|
---|
226 | fMode, pszMountPoint, rc);
|
---|
227 | }
|
---|
228 | }
|
---|
229 | else
|
---|
230 | VGSvcError("vbsvcAutoMountPrepareMountPoint: Could not set permissions for mount directory '%s', rc = %Rrc\n",
|
---|
231 | pszMountPoint, rc);
|
---|
232 | }
|
---|
233 | else
|
---|
234 | VGSvcError("vbsvcAutoMountPrepareMountPoint: Could not create mount directory '%s' with mode %RTfmode, rc = %Rrc\n",
|
---|
235 | pszMountPoint, fMode, rc);
|
---|
236 | return rc;
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Mounts a shared folder.
|
---|
242 | *
|
---|
243 | * @returns VBox status code reflecting unmount and mount point preparation
|
---|
244 | * results, but not actual mounting
|
---|
245 | *
|
---|
246 | * @param pszShareName The shared folder name.
|
---|
247 | * @param pszMountPoint The mount point.
|
---|
248 | * @param pOpts The mount options.
|
---|
249 | */
|
---|
250 | static int vbsvcAutoMountSharedFolder(const char *pszShareName, const char *pszMountPoint, struct vbsf_mount_opts *pOpts)
|
---|
251 | {
|
---|
252 | AssertPtr(pOpts);
|
---|
253 |
|
---|
254 | int rc = VINF_SUCCESS;
|
---|
255 | bool fSkip = false;
|
---|
256 |
|
---|
257 | /* Already mounted? */
|
---|
258 | char szAlreadyMountedTo[RTPATH_MAX];
|
---|
259 | if (vbsvcAutoMountShareIsMounted(pszShareName, szAlreadyMountedTo, sizeof(szAlreadyMountedTo)))
|
---|
260 | {
|
---|
261 | fSkip = true;
|
---|
262 | /* Do if it not mounted to our desired mount point */
|
---|
263 | if (RTStrICmp(pszMountPoint, szAlreadyMountedTo))
|
---|
264 | {
|
---|
265 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Shared folder '%s' already mounted to '%s', unmounting ...\n",
|
---|
266 | pszShareName, szAlreadyMountedTo);
|
---|
267 | rc = vbsvcAutoMountUnmount(szAlreadyMountedTo);
|
---|
268 | if (RT_SUCCESS(rc))
|
---|
269 | fSkip = false;
|
---|
270 | else
|
---|
271 | VGSvcError("vbsvcAutoMountWorker: Failed to unmount '%s', %s (%d)! (rc=%Rrc)\n",
|
---|
272 | szAlreadyMountedTo, strerror(errno), errno, rc); /** @todo errno isn't reliable at this point */
|
---|
273 | }
|
---|
274 | if (fSkip)
|
---|
275 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Shared folder '%s' already mounted to '%s', skipping\n",
|
---|
276 | pszShareName, szAlreadyMountedTo);
|
---|
277 | }
|
---|
278 |
|
---|
279 | if (!fSkip && RT_SUCCESS(rc))
|
---|
280 | rc = vbsvcAutoMountPrepareMountPoint(pszMountPoint, pszShareName, pOpts);
|
---|
281 | if (!fSkip && RT_SUCCESS(rc))
|
---|
282 | {
|
---|
283 | #ifdef RT_OS_SOLARIS
|
---|
284 | char szOptBuf[MAX_MNTOPT_STR] = { '\0', };
|
---|
285 | int fFlags = 0;
|
---|
286 | if (pOpts->ronly)
|
---|
287 | fFlags |= MS_RDONLY;
|
---|
288 | RTStrPrintf(szOptBuf, sizeof(szOptBuf), "uid=%d,gid=%d,dmode=%0o,fmode=%0o,dmask=%0o,fmask=%0o",
|
---|
289 | pOpts->uid, pOpts->gid, pOpts->dmode, pOpts->fmode, pOpts->dmask, pOpts->fmask);
|
---|
290 | int r = mount(pszShareName,
|
---|
291 | pszMountPoint,
|
---|
292 | fFlags | MS_OPTIONSTR,
|
---|
293 | "vboxfs",
|
---|
294 | NULL, /* char *dataptr */
|
---|
295 | 0, /* int datalen */
|
---|
296 | szOptBuf,
|
---|
297 | sizeof(szOptBuf));
|
---|
298 | if (r == 0)
|
---|
299 | VGSvcVerbose(0, "vbsvcAutoMountWorker: Shared folder '%s' was mounted to '%s'\n", pszShareName, pszMountPoint);
|
---|
300 | else if (errno != EBUSY) /* Share is already mounted? Then skip error msg. */
|
---|
301 | VGSvcError("vbsvcAutoMountWorker: Could not mount shared folder '%s' to '%s', error = %s\n",
|
---|
302 | pszShareName, pszMountPoint, strerror(errno));
|
---|
303 |
|
---|
304 | #elif defined(RT_OS_LINUX)
|
---|
305 | unsigned long fFlags = MS_NODEV;
|
---|
306 |
|
---|
307 | const char *szOptions = { "rw" };
|
---|
308 | struct vbsf_mount_info_new mntinf;
|
---|
309 |
|
---|
310 | mntinf.nullchar = '\0';
|
---|
311 | mntinf.signature[0] = VBSF_MOUNT_SIGNATURE_BYTE_0;
|
---|
312 | mntinf.signature[1] = VBSF_MOUNT_SIGNATURE_BYTE_1;
|
---|
313 | mntinf.signature[2] = VBSF_MOUNT_SIGNATURE_BYTE_2;
|
---|
314 | mntinf.length = sizeof(mntinf);
|
---|
315 |
|
---|
316 | mntinf.uid = pOpts->uid;
|
---|
317 | mntinf.gid = pOpts->gid;
|
---|
318 | mntinf.ttl = pOpts->ttl;
|
---|
319 | mntinf.dmode = pOpts->dmode;
|
---|
320 | mntinf.fmode = pOpts->fmode;
|
---|
321 | mntinf.dmask = pOpts->dmask;
|
---|
322 | mntinf.fmask = pOpts->fmask;
|
---|
323 |
|
---|
324 | strcpy(mntinf.name, pszShareName);
|
---|
325 | strcpy(mntinf.nls_name, "\0");
|
---|
326 |
|
---|
327 | int r = mount(NULL,
|
---|
328 | pszMountPoint,
|
---|
329 | "vboxsf",
|
---|
330 | fFlags,
|
---|
331 | &mntinf);
|
---|
332 | if (r == 0)
|
---|
333 | {
|
---|
334 | VGSvcVerbose(0, "vbsvcAutoMountWorker: Shared folder '%s' was mounted to '%s'\n",
|
---|
335 | pszShareName, pszMountPoint);
|
---|
336 |
|
---|
337 | r = vbsfmount_complete(pszShareName, pszMountPoint, flags, pOpts);
|
---|
338 | switch (r)
|
---|
339 | {
|
---|
340 | case 0: /* Success. */
|
---|
341 | errno = 0; /* Clear all errors/warnings. */
|
---|
342 | break;
|
---|
343 |
|
---|
344 | case 1:
|
---|
345 | VGSvcError("vbsvcAutoMountWorker: Could not update mount table (failed to create memstream): %s\n",
|
---|
346 | strerror(errno));
|
---|
347 | break;
|
---|
348 |
|
---|
349 | case 2:
|
---|
350 | VGSvcError("vbsvcAutoMountWorker: Could not open mount table for update: %s\n", strerror(errno));
|
---|
351 | break;
|
---|
352 |
|
---|
353 | case 3:
|
---|
354 | /* VGSvcError("vbsvcAutoMountWorker: Could not add an entry to the mount table: %s\n", strerror(errno)); */
|
---|
355 | errno = 0;
|
---|
356 | break;
|
---|
357 |
|
---|
358 | default:
|
---|
359 | VGSvcError("vbsvcAutoMountWorker: Unknown error while completing mount operation: %d\n", r);
|
---|
360 | break;
|
---|
361 | }
|
---|
362 | }
|
---|
363 | else /* r == -1, we got some error in errno. */
|
---|
364 | {
|
---|
365 | if (errno == EPROTO)
|
---|
366 | {
|
---|
367 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Messed up share name, re-trying ...\n");
|
---|
368 |
|
---|
369 | /** @todo r=bird: What on earth is going on here????? Why can't you
|
---|
370 | * strcpy(mntinf.name, pszShareName) to fix it again? */
|
---|
371 |
|
---|
372 | /* Sometimes the mount utility messes up the share name. Try to
|
---|
373 | * un-mangle it again. */
|
---|
374 | char szCWD[RTPATH_MAX];
|
---|
375 | size_t cchCWD;
|
---|
376 | if (!getcwd(szCWD, sizeof(szCWD)))
|
---|
377 | {
|
---|
378 | VGSvcError("vbsvcAutoMountWorker: Failed to get the current working directory\n");
|
---|
379 | szCWD[0] = '\0';
|
---|
380 | }
|
---|
381 | cchCWD = strlen(szCWD);
|
---|
382 | if (!strncmp(pszMountPoint, szCWD, cchCWD))
|
---|
383 | {
|
---|
384 | while (pszMountPoint[cchCWD] == '/')
|
---|
385 | ++cchCWD;
|
---|
386 | /* We checked before that we have enough space */
|
---|
387 | strcpy(mntinf.name, pszMountPoint + cchCWD);
|
---|
388 | }
|
---|
389 | r = mount(NULL, pszMountPoint, "vboxsf", flags, &mntinf);
|
---|
390 | }
|
---|
391 | if (errno == EPROTO)
|
---|
392 | {
|
---|
393 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Re-trying with old mounting structure ...\n");
|
---|
394 |
|
---|
395 | /* New mount tool with old vboxsf module? Try again using the old
|
---|
396 | * vbsf_mount_info_old structure. */
|
---|
397 | struct vbsf_mount_info_old mntinf_old;
|
---|
398 | memcpy(&mntinf_old.name, &mntinf.name, MAX_HOST_NAME);
|
---|
399 | memcpy(&mntinf_old.nls_name, mntinf.nls_name, MAX_NLS_NAME);
|
---|
400 | mntinf_old.uid = mntinf.uid;
|
---|
401 | mntinf_old.gid = mntinf.gid;
|
---|
402 | mntinf_old.ttl = mntinf.ttl;
|
---|
403 | r = mount(NULL, pszMountPoint, "vboxsf", flags, &mntinf_old);
|
---|
404 | }
|
---|
405 | if (r == -1) /* Was there some error from one of the tries above? */
|
---|
406 | {
|
---|
407 | switch (errno)
|
---|
408 | {
|
---|
409 | /* If we get EINVAL here, the system already has mounted the Shared Folder to another
|
---|
410 | * mount point. */
|
---|
411 | case EINVAL:
|
---|
412 | VGSvcVerbose(0, "vbsvcAutoMountWorker: Shared folder '%s' already is mounted!\n", pszShareName);
|
---|
413 | /* Ignore this error! */
|
---|
414 | break;
|
---|
415 | case EBUSY:
|
---|
416 | /* Ignore these errors! */
|
---|
417 | break;
|
---|
418 |
|
---|
419 | default:
|
---|
420 | VGSvcError("vbsvcAutoMountWorker: Could not mount shared folder '%s' to '%s': %s (%d)\n",
|
---|
421 | pszShareName, pszMountPoint, strerror(errno), errno);
|
---|
422 | rc = RTErrConvertFromErrno(errno);
|
---|
423 | break;
|
---|
424 | }
|
---|
425 | }
|
---|
426 | }
|
---|
427 | #else
|
---|
428 | # error "PORTME"
|
---|
429 | #endif
|
---|
430 | }
|
---|
431 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Mounting returned with rc=%Rrc\n", rc);
|
---|
432 | return rc;
|
---|
433 | }
|
---|
434 |
|
---|
435 |
|
---|
436 | /**
|
---|
437 | * Processes shared folder mappings retrieved from the host.
|
---|
438 | *
|
---|
439 | * @returns VBox status code.
|
---|
440 | * @param paMappings The mappings.
|
---|
441 | * @param cMappings The number of mappings.
|
---|
442 | * @param pszMountDir The mount directory.
|
---|
443 | * @param pszSharePrefix The share prefix.
|
---|
444 | * @param uClientID The shared folder service (HGCM) client ID.
|
---|
445 | */
|
---|
446 | static int vbsvcAutoMountProcessMappings(PCVBGLR3SHAREDFOLDERMAPPING paMappings, uint32_t cMappings,
|
---|
447 | const char *pszMountDir, const char *pszSharePrefix, uint32_t uClientID)
|
---|
448 | {
|
---|
449 | if (cMappings == 0)
|
---|
450 | return VINF_SUCCESS;
|
---|
451 | AssertPtrReturn(paMappings, VERR_INVALID_PARAMETER);
|
---|
452 | AssertPtrReturn(pszMountDir, VERR_INVALID_PARAMETER);
|
---|
453 | AssertPtrReturn(pszSharePrefix, VERR_INVALID_PARAMETER);
|
---|
454 | AssertReturn(uClientID > 0, VERR_INVALID_PARAMETER);
|
---|
455 |
|
---|
456 | /** @todo r=bird: Why is this loop schitzoid about status codes? It quits if
|
---|
457 | * RTPathJoin fails (i.e. if the user specifies a very long name), but happily
|
---|
458 | * continues if RTStrAPrintf failes (mem alloc).
|
---|
459 | *
|
---|
460 | * It also happily continues if the 'vboxsf' group is missing, which is a waste
|
---|
461 | * of effort... In fact, retrieving the group ID could probably be done up
|
---|
462 | * front, outside the loop. */
|
---|
463 | int rc = VINF_SUCCESS;
|
---|
464 | for (uint32_t i = 0; i < cMappings && RT_SUCCESS(rc); i++)
|
---|
465 | {
|
---|
466 | char *pszShareName = NULL;
|
---|
467 | rc = VbglR3SharedFolderGetName(uClientID, paMappings[i].u32Root, &pszShareName);
|
---|
468 | if ( RT_SUCCESS(rc)
|
---|
469 | && *pszShareName)
|
---|
470 | {
|
---|
471 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Connecting share %u (%s) ...\n", i+1, pszShareName);
|
---|
472 |
|
---|
473 | /** @todo r=bird: why do you copy things twice here and waste heap space?
|
---|
474 | * szMountPoint has a fixed size.
|
---|
475 | * @code
|
---|
476 | * char szMountPoint[RTPATH_MAX];
|
---|
477 | * rc = RTPathJoin(szMountPoint, sizeof(szMountPoint), pszMountDir, *pszSharePrefix ? pszSharePrefix : pszShareName);
|
---|
478 | * if (RT_SUCCESS(rc) && *pszSharePrefix)
|
---|
479 | * rc = RTStrCat(szMountPoint, sizeof(szMountPoint), pszShareName);
|
---|
480 | * @endcode */
|
---|
481 | char *pszShareNameFull = NULL;
|
---|
482 | if (RTStrAPrintf(&pszShareNameFull, "%s%s", pszSharePrefix, pszShareName) > 0)
|
---|
483 | {
|
---|
484 | char szMountPoint[RTPATH_MAX];
|
---|
485 | rc = RTPathJoin(szMountPoint, sizeof(szMountPoint), pszMountDir, pszShareNameFull);
|
---|
486 | if (RT_SUCCESS(rc))
|
---|
487 | {
|
---|
488 | VGSvcVerbose(4, "vbsvcAutoMountWorker: Processing mount point '%s'\n", szMountPoint);
|
---|
489 |
|
---|
490 | struct group *grp_vboxsf = getgrnam("vboxsf");
|
---|
491 | if (grp_vboxsf)
|
---|
492 | {
|
---|
493 | struct vbsf_mount_opts mount_opts =
|
---|
494 | {
|
---|
495 | 0, /* uid */
|
---|
496 | (int)grp_vboxsf->gr_gid, /* gid */
|
---|
497 | 0, /* ttl */
|
---|
498 | 0770, /* dmode, owner and group "vboxsf" have full access */
|
---|
499 | 0770, /* fmode, owner and group "vboxsf" have full access */
|
---|
500 | 0, /* dmask */
|
---|
501 | 0, /* fmask */
|
---|
502 | 0, /* ronly */
|
---|
503 | 0, /* sloppy */
|
---|
504 | 0, /* noexec */
|
---|
505 | 0, /* nodev */
|
---|
506 | 0, /* nosuid */
|
---|
507 | 0, /* remount */
|
---|
508 | "\0", /* nls_name */
|
---|
509 | NULL, /* convertcp */
|
---|
510 | };
|
---|
511 |
|
---|
512 | rc = vbsvcAutoMountSharedFolder(pszShareName, szMountPoint, &mount_opts);
|
---|
513 | }
|
---|
514 | else
|
---|
515 | VGSvcError("vbsvcAutoMountWorker: Group 'vboxsf' does not exist\n");
|
---|
516 | }
|
---|
517 | else
|
---|
518 | VGSvcError("vbsvcAutoMountWorker: Unable to join mount point/prefix/shrae, rc = %Rrc\n", rc);
|
---|
519 | RTStrFree(pszShareNameFull);
|
---|
520 | }
|
---|
521 | else
|
---|
522 | VGSvcError("vbsvcAutoMountWorker: Unable to allocate full share name\n");
|
---|
523 | RTStrFree(pszShareName);
|
---|
524 | }
|
---|
525 | else
|
---|
526 | VGSvcError("vbsvcAutoMountWorker: Error while getting the shared folder name for root node = %u, rc = %Rrc\n",
|
---|
527 | paMappings[i].u32Root, rc);
|
---|
528 | } /* for cMappings. */
|
---|
529 | return rc;
|
---|
530 | }
|
---|
531 |
|
---|
532 |
|
---|
533 | /**
|
---|
534 | * @interface_method_impl{VBOXSERVICE,pfnWorker}
|
---|
535 | */
|
---|
536 | static DECLCALLBACK(int) vbsvcAutoMountWorker(bool volatile *pfShutdown)
|
---|
537 | {
|
---|
538 | /*
|
---|
539 | * Tell the control thread that it can continue
|
---|
540 | * spawning services.
|
---|
541 | */
|
---|
542 | RTThreadUserSignal(RTThreadSelf());
|
---|
543 |
|
---|
544 | uint32_t cMappings;
|
---|
545 | PVBGLR3SHAREDFOLDERMAPPING paMappings;
|
---|
546 | int rc = VbglR3SharedFolderGetMappings(g_SharedFoldersSvcClientID, true /* Only process auto-mounted folders */,
|
---|
547 | &paMappings, &cMappings);
|
---|
548 | if ( RT_SUCCESS(rc)
|
---|
549 | && cMappings)
|
---|
550 | {
|
---|
551 | char *pszMountDir;
|
---|
552 | rc = VbglR3SharedFolderGetMountDir(&pszMountDir);
|
---|
553 | if (rc == VERR_NOT_FOUND)
|
---|
554 | rc = RTStrDupEx(&pszMountDir, VBOXSERVICE_AUTOMOUNT_DEFAULT_DIR);
|
---|
555 | if (RT_SUCCESS(rc))
|
---|
556 | {
|
---|
557 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Shared folder mount dir set to '%s'\n", pszMountDir);
|
---|
558 |
|
---|
559 | char *pszSharePrefix;
|
---|
560 | rc = VbglR3SharedFolderGetMountPrefix(&pszSharePrefix);
|
---|
561 | if (RT_SUCCESS(rc))
|
---|
562 | {
|
---|
563 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Shared folder mount prefix set to '%s'\n", pszSharePrefix);
|
---|
564 | #ifdef USE_VIRTUAL_SHARES
|
---|
565 | /* Check for a fixed/virtual auto-mount share. */
|
---|
566 | if (VbglR3SharedFolderExists(g_SharedFoldersSvcClientID, "vbsfAutoMount"))
|
---|
567 | {
|
---|
568 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Host supports auto-mount root\n");
|
---|
569 | }
|
---|
570 | else
|
---|
571 | {
|
---|
572 | #endif
|
---|
573 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Got %u shared folder mappings\n", cMappings);
|
---|
574 | rc = vbsvcAutoMountProcessMappings(paMappings, cMappings, pszMountDir, pszSharePrefix, g_SharedFoldersSvcClientID);
|
---|
575 | #ifdef USE_VIRTUAL_SHARES
|
---|
576 | }
|
---|
577 | #endif
|
---|
578 | RTStrFree(pszSharePrefix);
|
---|
579 | } /* Mount share prefix. */
|
---|
580 | else
|
---|
581 | VGSvcError("vbsvcAutoMountWorker: Error while getting the shared folder mount prefix, rc = %Rrc\n", rc);
|
---|
582 | RTStrFree(pszMountDir);
|
---|
583 | }
|
---|
584 | else
|
---|
585 | VGSvcError("vbsvcAutoMountWorker: Error while getting the shared folder directory, rc = %Rrc\n", rc);
|
---|
586 | VbglR3SharedFolderFreeMappings(paMappings);
|
---|
587 | }
|
---|
588 | else if (RT_FAILURE(rc))
|
---|
589 | VGSvcError("vbsvcAutoMountWorker: Error while getting the shared folder mappings, rc = %Rrc\n", rc);
|
---|
590 | else
|
---|
591 | VGSvcVerbose(3, "vbsvcAutoMountWorker: No shared folder mappings found\n");
|
---|
592 |
|
---|
593 | /*
|
---|
594 | * Because this thread is a one-timer at the moment we don't want to break/change
|
---|
595 | * the semantics of the main thread's start/stop sub-threads handling.
|
---|
596 | *
|
---|
597 | * This thread exits so fast while doing its own startup in VGSvcStartServices()
|
---|
598 | * that this->fShutdown flag is set to true in VGSvcThread() before we have the
|
---|
599 | * chance to check for a service failure in VGSvcStartServices() to indicate
|
---|
600 | * a VBoxService startup error.
|
---|
601 | *
|
---|
602 | * Therefore *no* service threads are allowed to quit themselves and need to wait
|
---|
603 | * for the pfShutdown flag to be set by the main thread.
|
---|
604 | */
|
---|
605 | for (;;)
|
---|
606 | {
|
---|
607 | /* Do we need to shutdown? */
|
---|
608 | if (*pfShutdown)
|
---|
609 | break;
|
---|
610 |
|
---|
611 | /* Let's sleep for a bit and let others run ... */
|
---|
612 | RTThreadSleep(500);
|
---|
613 | }
|
---|
614 |
|
---|
615 | RTSemEventMultiDestroy(g_AutoMountEvent);
|
---|
616 | g_AutoMountEvent = NIL_RTSEMEVENTMULTI;
|
---|
617 |
|
---|
618 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Finished with rc=%Rrc\n", rc);
|
---|
619 | return VINF_SUCCESS;
|
---|
620 | }
|
---|
621 |
|
---|
622 |
|
---|
623 | /**
|
---|
624 | * @interface_method_impl{VBOXSERVICE,pfnTerm}
|
---|
625 | */
|
---|
626 | static DECLCALLBACK(void) vbsvcAutoMountTerm(void)
|
---|
627 | {
|
---|
628 | VGSvcVerbose(3, "vbsvcAutoMountTerm\n");
|
---|
629 |
|
---|
630 | VbglR3SharedFolderDisconnect(g_SharedFoldersSvcClientID);
|
---|
631 | g_SharedFoldersSvcClientID = 0;
|
---|
632 |
|
---|
633 | if (g_AutoMountEvent != NIL_RTSEMEVENTMULTI)
|
---|
634 | {
|
---|
635 | RTSemEventMultiDestroy(g_AutoMountEvent);
|
---|
636 | g_AutoMountEvent = NIL_RTSEMEVENTMULTI;
|
---|
637 | }
|
---|
638 | return;
|
---|
639 | }
|
---|
640 |
|
---|
641 |
|
---|
642 | /**
|
---|
643 | * @interface_method_impl{VBOXSERVICE,pfnStop}
|
---|
644 | */
|
---|
645 | static DECLCALLBACK(void) vbsvcAutoMountStop(void)
|
---|
646 | {
|
---|
647 | /*
|
---|
648 | * We need this check because at the moment our auto-mount
|
---|
649 | * thread really is a one-timer which destroys the event itself
|
---|
650 | * after running.
|
---|
651 | */
|
---|
652 | if (g_AutoMountEvent != NIL_RTSEMEVENTMULTI)
|
---|
653 | RTSemEventMultiSignal(g_AutoMountEvent);
|
---|
654 | }
|
---|
655 |
|
---|
656 |
|
---|
657 | /**
|
---|
658 | * The 'automount' service description.
|
---|
659 | */
|
---|
660 | VBOXSERVICE g_AutoMount =
|
---|
661 | {
|
---|
662 | /* pszName. */
|
---|
663 | "automount",
|
---|
664 | /* pszDescription. */
|
---|
665 | "Auto-mount for Shared Folders",
|
---|
666 | /* pszUsage. */
|
---|
667 | NULL,
|
---|
668 | /* pszOptions. */
|
---|
669 | NULL,
|
---|
670 | /* methods */
|
---|
671 | VGSvcDefaultPreInit,
|
---|
672 | VGSvcDefaultOption,
|
---|
673 | vbsvcAutoMountInit,
|
---|
674 | vbsvcAutoMountWorker,
|
---|
675 | vbsvcAutoMountStop,
|
---|
676 | vbsvcAutoMountTerm
|
---|
677 | };
|
---|