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