1 | /* $Id: VBoxServiceAutoMount.cpp 62882 2016-08-02 15:31:02Z 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 (errno == EPROTO)
|
---|
408 | {
|
---|
409 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Re-trying with old mounting structure ...\n");
|
---|
410 |
|
---|
411 | /* New mount tool with old vboxsf module? Try again using the old
|
---|
412 | * vbsf_mount_info_old structure. */
|
---|
413 | struct vbsf_mount_info_old mntinf_old;
|
---|
414 | memcpy(&mntinf_old.name, &mntinf.name, MAX_HOST_NAME);
|
---|
415 | memcpy(&mntinf_old.nls_name, mntinf.nls_name, MAX_NLS_NAME);
|
---|
416 | mntinf_old.uid = mntinf.uid;
|
---|
417 | mntinf_old.gid = mntinf.gid;
|
---|
418 | mntinf_old.ttl = mntinf.ttl;
|
---|
419 | r = mount(mntinf_old.name, pszMountPoint, "vboxsf", fFlags, &mntinf_old);
|
---|
420 | }
|
---|
421 | if (r == -1) /* Was there some error from one of the tries above? */
|
---|
422 | {
|
---|
423 | switch (errno)
|
---|
424 | {
|
---|
425 | /* If we get EINVAL here, the system already has mounted the Shared Folder to another
|
---|
426 | * mount point. */
|
---|
427 | case EINVAL:
|
---|
428 | VGSvcVerbose(0, "vbsvcAutoMountWorker: Shared folder '%s' already is mounted!\n", pszShareName);
|
---|
429 | /* Ignore this error! */
|
---|
430 | break;
|
---|
431 | case EBUSY:
|
---|
432 | /* Ignore these errors! */
|
---|
433 | break;
|
---|
434 |
|
---|
435 | default:
|
---|
436 | VGSvcError("vbsvcAutoMountWorker: Could not mount shared folder '%s' to '%s': %s (%d)\n",
|
---|
437 | pszShareName, pszMountPoint, strerror(errno), errno);
|
---|
438 | rc = RTErrConvertFromErrno(errno);
|
---|
439 | break;
|
---|
440 | }
|
---|
441 | }
|
---|
442 | }
|
---|
443 | #else
|
---|
444 | # error "PORTME"
|
---|
445 | #endif
|
---|
446 | }
|
---|
447 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Mounting returned with rc=%Rrc\n", rc);
|
---|
448 | return rc;
|
---|
449 | }
|
---|
450 |
|
---|
451 |
|
---|
452 | /**
|
---|
453 | * Processes shared folder mappings retrieved from the host.
|
---|
454 | *
|
---|
455 | * @returns VBox status code.
|
---|
456 | * @param paMappings The mappings.
|
---|
457 | * @param cMappings The number of mappings.
|
---|
458 | * @param pszMountDir The mount directory.
|
---|
459 | * @param pszSharePrefix The share prefix.
|
---|
460 | * @param uClientID The shared folder service (HGCM) client ID.
|
---|
461 | */
|
---|
462 | static int vbsvcAutoMountProcessMappings(PCVBGLR3SHAREDFOLDERMAPPING paMappings, uint32_t cMappings,
|
---|
463 | const char *pszMountDir, const char *pszSharePrefix, uint32_t uClientID)
|
---|
464 | {
|
---|
465 | if (cMappings == 0)
|
---|
466 | return VINF_SUCCESS;
|
---|
467 | AssertPtrReturn(paMappings, VERR_INVALID_PARAMETER);
|
---|
468 | AssertPtrReturn(pszMountDir, VERR_INVALID_PARAMETER);
|
---|
469 | AssertPtrReturn(pszSharePrefix, VERR_INVALID_PARAMETER);
|
---|
470 | AssertReturn(uClientID > 0, VERR_INVALID_PARAMETER);
|
---|
471 |
|
---|
472 | /** @todo r=bird: Why is this loop schitzoid about status codes? It quits if
|
---|
473 | * RTPathJoin fails (i.e. if the user specifies a very long name), but happily
|
---|
474 | * continues if RTStrAPrintf failes (mem alloc).
|
---|
475 | *
|
---|
476 | * It also happily continues if the 'vboxsf' group is missing, which is a waste
|
---|
477 | * of effort... In fact, retrieving the group ID could probably be done up
|
---|
478 | * front, outside the loop. */
|
---|
479 | int rc = VINF_SUCCESS;
|
---|
480 | for (uint32_t i = 0; i < cMappings && RT_SUCCESS(rc); i++)
|
---|
481 | {
|
---|
482 | char *pszShareName = NULL;
|
---|
483 | rc = VbglR3SharedFolderGetName(uClientID, paMappings[i].u32Root, &pszShareName);
|
---|
484 | if ( RT_SUCCESS(rc)
|
---|
485 | && *pszShareName)
|
---|
486 | {
|
---|
487 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Connecting share %u (%s) ...\n", i+1, pszShareName);
|
---|
488 |
|
---|
489 | /** @todo r=bird: why do you copy things twice here and waste heap space?
|
---|
490 | * szMountPoint has a fixed size.
|
---|
491 | * @code
|
---|
492 | * char szMountPoint[RTPATH_MAX];
|
---|
493 | * rc = RTPathJoin(szMountPoint, sizeof(szMountPoint), pszMountDir, *pszSharePrefix ? pszSharePrefix : pszShareName);
|
---|
494 | * if (RT_SUCCESS(rc) && *pszSharePrefix)
|
---|
495 | * rc = RTStrCat(szMountPoint, sizeof(szMountPoint), pszShareName);
|
---|
496 | * @endcode */
|
---|
497 | char *pszShareNameFull = NULL;
|
---|
498 | if (RTStrAPrintf(&pszShareNameFull, "%s%s", pszSharePrefix, pszShareName) > 0)
|
---|
499 | {
|
---|
500 | char szMountPoint[RTPATH_MAX];
|
---|
501 | rc = RTPathJoin(szMountPoint, sizeof(szMountPoint), pszMountDir, pszShareNameFull);
|
---|
502 | if (RT_SUCCESS(rc))
|
---|
503 | {
|
---|
504 | VGSvcVerbose(4, "vbsvcAutoMountWorker: Processing mount point '%s'\n", szMountPoint);
|
---|
505 |
|
---|
506 | struct group *grp_vboxsf = getgrnam("vboxsf");
|
---|
507 | if (grp_vboxsf)
|
---|
508 | {
|
---|
509 | struct vbsf_mount_opts mount_opts =
|
---|
510 | {
|
---|
511 | 0, /* uid */
|
---|
512 | (int)grp_vboxsf->gr_gid, /* gid */
|
---|
513 | 0, /* ttl */
|
---|
514 | 0770, /* dmode, owner and group "vboxsf" have full access */
|
---|
515 | 0770, /* fmode, owner and group "vboxsf" have full access */
|
---|
516 | 0, /* dmask */
|
---|
517 | 0, /* fmask */
|
---|
518 | 0, /* ronly */
|
---|
519 | 0, /* sloppy */
|
---|
520 | 0, /* noexec */
|
---|
521 | 0, /* nodev */
|
---|
522 | 0, /* nosuid */
|
---|
523 | 0, /* remount */
|
---|
524 | "\0", /* nls_name */
|
---|
525 | NULL, /* convertcp */
|
---|
526 | };
|
---|
527 |
|
---|
528 | rc = vbsvcAutoMountSharedFolder(pszShareName, szMountPoint, &mount_opts);
|
---|
529 | }
|
---|
530 | else
|
---|
531 | VGSvcError("vbsvcAutoMountWorker: Group 'vboxsf' does not exist\n");
|
---|
532 | }
|
---|
533 | else
|
---|
534 | VGSvcError("vbsvcAutoMountWorker: Unable to join mount point/prefix/shrae, rc = %Rrc\n", rc);
|
---|
535 | RTStrFree(pszShareNameFull);
|
---|
536 | }
|
---|
537 | else
|
---|
538 | VGSvcError("vbsvcAutoMountWorker: Unable to allocate full share name\n");
|
---|
539 | RTStrFree(pszShareName);
|
---|
540 | }
|
---|
541 | else
|
---|
542 | VGSvcError("vbsvcAutoMountWorker: Error while getting the shared folder name for root node = %u, rc = %Rrc\n",
|
---|
543 | paMappings[i].u32Root, rc);
|
---|
544 | } /* for cMappings. */
|
---|
545 | return rc;
|
---|
546 | }
|
---|
547 |
|
---|
548 |
|
---|
549 | /**
|
---|
550 | * @interface_method_impl{VBOXSERVICE,pfnWorker}
|
---|
551 | */
|
---|
552 | static DECLCALLBACK(int) vbsvcAutoMountWorker(bool volatile *pfShutdown)
|
---|
553 | {
|
---|
554 | /*
|
---|
555 | * Tell the control thread that it can continue
|
---|
556 | * spawning services.
|
---|
557 | */
|
---|
558 | RTThreadUserSignal(RTThreadSelf());
|
---|
559 |
|
---|
560 | uint32_t cMappings;
|
---|
561 | PVBGLR3SHAREDFOLDERMAPPING paMappings;
|
---|
562 | int rc = VbglR3SharedFolderGetMappings(g_SharedFoldersSvcClientID, true /* Only process auto-mounted folders */,
|
---|
563 | &paMappings, &cMappings);
|
---|
564 | if ( RT_SUCCESS(rc)
|
---|
565 | && cMappings)
|
---|
566 | {
|
---|
567 | char *pszMountDir;
|
---|
568 | rc = VbglR3SharedFolderGetMountDir(&pszMountDir);
|
---|
569 | if (rc == VERR_NOT_FOUND)
|
---|
570 | rc = RTStrDupEx(&pszMountDir, VBOXSERVICE_AUTOMOUNT_DEFAULT_DIR);
|
---|
571 | if (RT_SUCCESS(rc))
|
---|
572 | {
|
---|
573 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Shared folder mount dir set to '%s'\n", pszMountDir);
|
---|
574 |
|
---|
575 | char *pszSharePrefix;
|
---|
576 | rc = VbglR3SharedFolderGetMountPrefix(&pszSharePrefix);
|
---|
577 | if (RT_SUCCESS(rc))
|
---|
578 | {
|
---|
579 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Shared folder mount prefix set to '%s'\n", pszSharePrefix);
|
---|
580 | #ifdef USE_VIRTUAL_SHARES
|
---|
581 | /* Check for a fixed/virtual auto-mount share. */
|
---|
582 | if (VbglR3SharedFolderExists(g_SharedFoldersSvcClientID, "vbsfAutoMount"))
|
---|
583 | {
|
---|
584 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Host supports auto-mount root\n");
|
---|
585 | }
|
---|
586 | else
|
---|
587 | {
|
---|
588 | #endif
|
---|
589 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Got %u shared folder mappings\n", cMappings);
|
---|
590 | rc = vbsvcAutoMountProcessMappings(paMappings, cMappings, pszMountDir, pszSharePrefix, g_SharedFoldersSvcClientID);
|
---|
591 | #ifdef USE_VIRTUAL_SHARES
|
---|
592 | }
|
---|
593 | #endif
|
---|
594 | RTStrFree(pszSharePrefix);
|
---|
595 | } /* Mount share prefix. */
|
---|
596 | else
|
---|
597 | VGSvcError("vbsvcAutoMountWorker: Error while getting the shared folder mount prefix, rc = %Rrc\n", rc);
|
---|
598 | RTStrFree(pszMountDir);
|
---|
599 | }
|
---|
600 | else
|
---|
601 | VGSvcError("vbsvcAutoMountWorker: Error while getting the shared folder directory, rc = %Rrc\n", rc);
|
---|
602 | VbglR3SharedFolderFreeMappings(paMappings);
|
---|
603 | }
|
---|
604 | else if (RT_FAILURE(rc))
|
---|
605 | VGSvcError("vbsvcAutoMountWorker: Error while getting the shared folder mappings, rc = %Rrc\n", rc);
|
---|
606 | else
|
---|
607 | VGSvcVerbose(3, "vbsvcAutoMountWorker: No shared folder mappings found\n");
|
---|
608 |
|
---|
609 | /*
|
---|
610 | * Because this thread is a one-timer at the moment we don't want to break/change
|
---|
611 | * the semantics of the main thread's start/stop sub-threads handling.
|
---|
612 | *
|
---|
613 | * This thread exits so fast while doing its own startup in VGSvcStartServices()
|
---|
614 | * that this->fShutdown flag is set to true in VGSvcThread() before we have the
|
---|
615 | * chance to check for a service failure in VGSvcStartServices() to indicate
|
---|
616 | * a VBoxService startup error.
|
---|
617 | *
|
---|
618 | * Therefore *no* service threads are allowed to quit themselves and need to wait
|
---|
619 | * for the pfShutdown flag to be set by the main thread.
|
---|
620 | */
|
---|
621 | /** @todo r=bird: Shared folders have always been configurable at run time, so
|
---|
622 | * this service must be changed to check for changes and execute those changes!
|
---|
623 | *
|
---|
624 | * The 0.5sec sleep here is just soo crude and must go!
|
---|
625 | */
|
---|
626 | for (;;)
|
---|
627 | {
|
---|
628 | /* Do we need to shutdown? */
|
---|
629 | if (*pfShutdown)
|
---|
630 | break;
|
---|
631 |
|
---|
632 | /* Let's sleep for a bit and let others run ... */
|
---|
633 | RTThreadSleep(500);
|
---|
634 | }
|
---|
635 |
|
---|
636 | VGSvcVerbose(3, "vbsvcAutoMountWorker: Finished with rc=%Rrc\n", rc);
|
---|
637 | return VINF_SUCCESS;
|
---|
638 | }
|
---|
639 |
|
---|
640 |
|
---|
641 | /**
|
---|
642 | * @interface_method_impl{VBOXSERVICE,pfnTerm}
|
---|
643 | */
|
---|
644 | static DECLCALLBACK(void) vbsvcAutoMountTerm(void)
|
---|
645 | {
|
---|
646 | VGSvcVerbose(3, "vbsvcAutoMountTerm\n");
|
---|
647 |
|
---|
648 | VbglR3SharedFolderDisconnect(g_SharedFoldersSvcClientID);
|
---|
649 | g_SharedFoldersSvcClientID = 0;
|
---|
650 |
|
---|
651 | if (g_AutoMountEvent != NIL_RTSEMEVENTMULTI)
|
---|
652 | {
|
---|
653 | RTSemEventMultiDestroy(g_AutoMountEvent);
|
---|
654 | g_AutoMountEvent = NIL_RTSEMEVENTMULTI;
|
---|
655 | }
|
---|
656 | return;
|
---|
657 | }
|
---|
658 |
|
---|
659 |
|
---|
660 | /**
|
---|
661 | * @interface_method_impl{VBOXSERVICE,pfnStop}
|
---|
662 | */
|
---|
663 | static DECLCALLBACK(void) vbsvcAutoMountStop(void)
|
---|
664 | {
|
---|
665 | RTSemEventMultiSignal(g_AutoMountEvent);
|
---|
666 | }
|
---|
667 |
|
---|
668 |
|
---|
669 | /**
|
---|
670 | * The 'automount' service description.
|
---|
671 | */
|
---|
672 | VBOXSERVICE g_AutoMount =
|
---|
673 | {
|
---|
674 | /* pszName. */
|
---|
675 | "automount",
|
---|
676 | /* pszDescription. */
|
---|
677 | "Auto-mount for Shared Folders",
|
---|
678 | /* pszUsage. */
|
---|
679 | NULL,
|
---|
680 | /* pszOptions. */
|
---|
681 | NULL,
|
---|
682 | /* methods */
|
---|
683 | VGSvcDefaultPreInit,
|
---|
684 | VGSvcDefaultOption,
|
---|
685 | vbsvcAutoMountInit,
|
---|
686 | vbsvcAutoMountWorker,
|
---|
687 | vbsvcAutoMountStop,
|
---|
688 | vbsvcAutoMountTerm
|
---|
689 | };
|
---|