summaryrefslogtreecommitdiffstats
path: root/src/VBox/Additions/linux/sharedfolders/vbsfmount.c
blob: 72d9210e2786ba78bb756ebb33f45114e4c8d045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* $Id: vbsfmount.c $ */
/** @file
 * vbsfmount - Commonly used code to mount shared folders on Linux-based
 *             systems.  Currently used by mount.vboxsf and VBoxService.
 */

/*
 * Copyright (C) 2010-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <assert.h>
#include <ctype.h>
#include <mntent.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/mount.h>

#include "vbsfmount.h"


/** @todo Use defines for return values! */
int vbsfmount_complete(const char *pszSharedFolder, const char *pszMountPoint,
                       unsigned long fFlags, const char *pszOpts)
{
    /*
     * Combine pszOpts and fFlags.
     */
    int          rc;
    size_t const cchFlags = (fFlags & MS_NOSUID ? strlen(MNTOPT_NOSUID) + 1 : 0)
                          + (fFlags & MS_RDONLY ? strlen(MNTOPT_RO) : strlen(MNTOPT_RW));
    size_t const cchOpts  = pszOpts ? 1 + strlen(pszOpts) : 0;
    char        *pszBuf   = (char *)malloc(cchFlags + cchOpts + 8);
    if (pszBuf)
    {
        char         *psz = pszBuf;
        FILE         *pMTab;

        strcpy(psz, fFlags & MS_RDONLY ? MNTOPT_RO : MNTOPT_RW);
        psz += strlen(psz);

        if (fFlags & MS_NOSUID)
        {
            *psz++ = ',';
            strcpy(psz, MNTOPT_NOSUID);
            psz += strlen(psz);
        }

        if (cchOpts)
        {
            *psz++ = ',';
            strcpy(psz, pszOpts);
        }

        assert(strlen(pszBuf) <= cchFlags + cchOpts);

        /*
         * Open the mtab and update it:
         */
        pMTab = setmntent(MOUNTED, "a+");
        if (pMTab)
        {
            struct mntent Entry;
            Entry.mnt_fsname = (char*)pszSharedFolder;
            Entry.mnt_dir = (char *)pszMountPoint;
            Entry.mnt_type = "vboxsf";
            Entry.mnt_opts = pszBuf;
            Entry.mnt_freq = 0;
            Entry.mnt_passno = 0;

            if (!addmntent(pMTab, &Entry))
                rc = 0; /* success. */
            else
                rc = 3;  /* Could not add an entry to the mount table. */

            endmntent(pMTab);
        }
        else
            rc = 2; /* Could not open mount table for update. */

        free(pszBuf);
    }
    else
        rc = 1; /* allocation error */
    return rc;
}