[31202] | 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 | /*
|
---|
[98103] | 8 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
[31202] | 9 | *
|
---|
[96407] | 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
|
---|
[31202] | 27 | */
|
---|
| 28 |
|
---|
[87463] | 29 |
|
---|
| 30 | /*********************************************************************************************************************************
|
---|
| 31 | * Header Files *
|
---|
| 32 | *********************************************************************************************************************************/
|
---|
[31205] | 33 | #ifndef _GNU_SOURCE
|
---|
[87463] | 34 | # define _GNU_SOURCE
|
---|
[31205] | 35 | #endif
|
---|
[87463] | 36 | #include <assert.h>
|
---|
[31202] | 37 | #include <ctype.h>
|
---|
| 38 | #include <mntent.h>
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
[77139] | 41 | #include <stdint.h>
|
---|
[31202] | 42 | #include <string.h>
|
---|
| 43 | #include <sys/mount.h>
|
---|
| 44 |
|
---|
[77139] | 45 | #include "vbsfmount.h"
|
---|
[31202] | 46 |
|
---|
[77139] | 47 |
|
---|
[31202] | 48 | /** @todo Use defines for return values! */
|
---|
[87463] | 49 | int vbsfmount_complete(const char *pszSharedFolder, const char *pszMountPoint,
|
---|
| 50 | unsigned long fFlags, const char *pszOpts)
|
---|
[31202] | 51 | {
|
---|
[87463] | 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;
|
---|
[31202] | 64 |
|
---|
[87463] | 65 | strcpy(psz, fFlags & MS_RDONLY ? MNTOPT_RO : MNTOPT_RW);
|
---|
| 66 | psz += strlen(psz);
|
---|
[31202] | 67 |
|
---|
[87463] | 68 | if (fFlags & MS_NOSUID)
|
---|
| 69 | {
|
---|
| 70 | *psz++ = ',';
|
---|
| 71 | strcpy(psz, MNTOPT_NOSUID);
|
---|
| 72 | psz += strlen(psz);
|
---|
| 73 | }
|
---|
[31202] | 74 |
|
---|
[87463] | 75 | if (cchOpts)
|
---|
| 76 | {
|
---|
| 77 | *psz++ = ',';
|
---|
| 78 | strcpy(psz, pszOpts);
|
---|
| 79 | }
|
---|
[31202] | 80 |
|
---|
[87463] | 81 | assert(strlen(pszBuf) <= cchFlags + cchOpts);
|
---|
[31202] | 82 |
|
---|
[87463] | 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;
|
---|
[31202] | 96 |
|
---|
[87463] | 97 | if (!addmntent(pMTab, &Entry))
|
---|
| 98 | rc = 0; /* success. */
|
---|
| 99 | else
|
---|
| 100 | rc = 3; /* Could not add an entry to the mount table. */
|
---|
[31202] | 101 |
|
---|
[87463] | 102 | endmntent(pMTab);
|
---|
| 103 | }
|
---|
| 104 | else
|
---|
| 105 | rc = 2; /* Could not open mount table for update. */
|
---|
[31202] | 106 |
|
---|
[87463] | 107 | free(pszBuf);
|
---|
[31202] | 108 | }
|
---|
[87463] | 109 | else
|
---|
| 110 | rc = 1; /* allocation error */
|
---|
[31203] | 111 | return rc;
|
---|
[31202] | 112 | }
|
---|
[87463] | 113 |
|
---|