1 | /* $Id: vbsfmount.c 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * vbsfmount - Commonly used code to mount shared folders on Linux-based
|
---|
4 | * systems. Currently used by mount.vboxsf and VBoxService.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox base platform packages, as
|
---|
11 | * available from https://www.virtualbox.org.
|
---|
12 | *
|
---|
13 | * This program is free software; you can redistribute it and/or
|
---|
14 | * modify it under the terms of the GNU General Public License
|
---|
15 | * as published by the Free Software Foundation, in version 3 of the
|
---|
16 | * License.
|
---|
17 | *
|
---|
18 | * This program is distributed in the hope that it will be useful, but
|
---|
19 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
21 | * General Public License for more details.
|
---|
22 | *
|
---|
23 | * You should have received a copy of the GNU General Public License
|
---|
24 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
25 | *
|
---|
26 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
27 | */
|
---|
28 |
|
---|
29 |
|
---|
30 | /*********************************************************************************************************************************
|
---|
31 | * Header Files *
|
---|
32 | *********************************************************************************************************************************/
|
---|
33 | #ifndef _GNU_SOURCE
|
---|
34 | # define _GNU_SOURCE
|
---|
35 | #endif
|
---|
36 | #include <assert.h>
|
---|
37 | #include <ctype.h>
|
---|
38 | #include <mntent.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <stdint.h>
|
---|
42 | #include <string.h>
|
---|
43 | #include <sys/mount.h>
|
---|
44 |
|
---|
45 | #include "vbsfmount.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | /** @todo Use defines for return values! */
|
---|
49 | int vbsfmount_complete(const char *pszSharedFolder, const char *pszMountPoint,
|
---|
50 | unsigned long fFlags, const char *pszOpts)
|
---|
51 | {
|
---|
52 | /*
|
---|
53 | * Combine pszOpts and fFlags.
|
---|
54 | */
|
---|
55 | int rc;
|
---|
56 | size_t const cchFlags = (fFlags & MS_NOSUID ? strlen(MNTOPT_NOSUID) + 1 : 0)
|
---|
57 | + (fFlags & MS_RDONLY ? strlen(MNTOPT_RO) : strlen(MNTOPT_RW));
|
---|
58 | size_t const cchOpts = pszOpts ? 1 + strlen(pszOpts) : 0;
|
---|
59 | char *pszBuf = (char *)malloc(cchFlags + cchOpts + 8);
|
---|
60 | if (pszBuf)
|
---|
61 | {
|
---|
62 | char *psz = pszBuf;
|
---|
63 | FILE *pMTab;
|
---|
64 |
|
---|
65 | strcpy(psz, fFlags & MS_RDONLY ? MNTOPT_RO : MNTOPT_RW);
|
---|
66 | psz += strlen(psz);
|
---|
67 |
|
---|
68 | if (fFlags & MS_NOSUID)
|
---|
69 | {
|
---|
70 | *psz++ = ',';
|
---|
71 | strcpy(psz, MNTOPT_NOSUID);
|
---|
72 | psz += strlen(psz);
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (cchOpts)
|
---|
76 | {
|
---|
77 | *psz++ = ',';
|
---|
78 | strcpy(psz, pszOpts);
|
---|
79 | }
|
---|
80 |
|
---|
81 | assert(strlen(pszBuf) <= cchFlags + cchOpts);
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * Open the mtab and update it:
|
---|
85 | */
|
---|
86 | pMTab = setmntent(MOUNTED, "a+");
|
---|
87 | if (pMTab)
|
---|
88 | {
|
---|
89 | struct mntent Entry;
|
---|
90 | Entry.mnt_fsname = (char*)pszSharedFolder;
|
---|
91 | Entry.mnt_dir = (char *)pszMountPoint;
|
---|
92 | Entry.mnt_type = "vboxsf";
|
---|
93 | Entry.mnt_opts = pszBuf;
|
---|
94 | Entry.mnt_freq = 0;
|
---|
95 | Entry.mnt_passno = 0;
|
---|
96 |
|
---|
97 | if (!addmntent(pMTab, &Entry))
|
---|
98 | rc = 0; /* success. */
|
---|
99 | else
|
---|
100 | rc = 3; /* Could not add an entry to the mount table. */
|
---|
101 |
|
---|
102 | endmntent(pMTab);
|
---|
103 | }
|
---|
104 | else
|
---|
105 | rc = 2; /* Could not open mount table for update. */
|
---|
106 |
|
---|
107 | free(pszBuf);
|
---|
108 | }
|
---|
109 | else
|
---|
110 | rc = 1; /* allocation error */
|
---|
111 | return rc;
|
---|
112 | }
|
---|
113 |
|
---|