1 | /* $Id: vbsfmount.c 77953 2019-03-29 17:07:16Z 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-2019 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #ifndef _GNU_SOURCE
|
---|
20 | #define _GNU_SOURCE
|
---|
21 | #endif
|
---|
22 | #include <ctype.h>
|
---|
23 | #include <mntent.h>
|
---|
24 | #include <stdio.h>
|
---|
25 | #include <stdlib.h>
|
---|
26 | #include <stdint.h>
|
---|
27 | #include <string.h>
|
---|
28 | #include <sys/mount.h>
|
---|
29 |
|
---|
30 | #include "vbsfmount.h"
|
---|
31 |
|
---|
32 |
|
---|
33 | /** @todo Use defines for return values! */
|
---|
34 | int vbsfmount_complete(const char *host_name, const char *mount_point,
|
---|
35 | unsigned long flags, struct vbsf_mount_opts *opts)
|
---|
36 | {
|
---|
37 | FILE *f, *m;
|
---|
38 | char *buf;
|
---|
39 | size_t size;
|
---|
40 | struct mntent e;
|
---|
41 | int rc = 0;
|
---|
42 |
|
---|
43 | m = open_memstream(&buf, &size);
|
---|
44 | if (!m)
|
---|
45 | return 1; /* Could not update mount table (failed to create memstream). */
|
---|
46 |
|
---|
47 | if (opts->ttl != -1)
|
---|
48 | fprintf(m, "ttl=%d,", opts->ttl);
|
---|
49 | if (opts->msDirCacheTTL >= 0)
|
---|
50 | fprintf(m, "dcachettl=%d,", opts->msDirCacheTTL);
|
---|
51 | if (opts->msInodeTTL >= 0)
|
---|
52 | fprintf(m, "inodettl=%d,", opts->msInodeTTL);
|
---|
53 | if (opts->cMaxIoPages)
|
---|
54 | fprintf(m, "maxiopages=%u,", opts->cMaxIoPages);
|
---|
55 | if (opts->cbDirBuf)
|
---|
56 | fprintf(m, "dirbuf=%u,", opts->cbDirBuf);
|
---|
57 | switch (opts->enmCacheMode)
|
---|
58 | {
|
---|
59 | default:
|
---|
60 | case kVbsfCacheMode_Default:
|
---|
61 | break;
|
---|
62 | case kVbsfCacheMode_None: fprintf(m, "cache=none,"); break;
|
---|
63 | case kVbsfCacheMode_Strict: fprintf(m, "cache=strict,"); break;
|
---|
64 | case kVbsfCacheMode_Read: fprintf(m, "cache=read,"); break;
|
---|
65 | case kVbsfCacheMode_ReadWrite: fprintf(m, "cache=readwrite,"); break;
|
---|
66 | }
|
---|
67 | if (opts->uid)
|
---|
68 | fprintf(m, "uid=%d,", opts->uid);
|
---|
69 | if (opts->gid)
|
---|
70 | fprintf(m, "gid=%d,", opts->gid);
|
---|
71 | if (*opts->nls_name)
|
---|
72 | fprintf(m, "iocharset=%s,", opts->nls_name);
|
---|
73 | if (flags & MS_NOSUID)
|
---|
74 | fprintf(m, "%s,", MNTOPT_NOSUID);
|
---|
75 | if (flags & MS_RDONLY)
|
---|
76 | fprintf(m, "%s,", MNTOPT_RO);
|
---|
77 | else
|
---|
78 | fprintf(m, "%s,", MNTOPT_RW);
|
---|
79 |
|
---|
80 | fclose(m);
|
---|
81 |
|
---|
82 | if (size > 0)
|
---|
83 | buf[size - 1] = 0;
|
---|
84 | else
|
---|
85 | buf = "defaults";
|
---|
86 |
|
---|
87 | f = setmntent(MOUNTED, "a+");
|
---|
88 | if (!f)
|
---|
89 | {
|
---|
90 | rc = 2; /* Could not open mount table for update. */
|
---|
91 | }
|
---|
92 | else
|
---|
93 | {
|
---|
94 | e.mnt_fsname = (char*)host_name;
|
---|
95 | e.mnt_dir = (char*)mount_point;
|
---|
96 | e.mnt_type = "vboxsf";
|
---|
97 | e.mnt_opts = buf;
|
---|
98 | e.mnt_freq = 0;
|
---|
99 | e.mnt_passno = 0;
|
---|
100 |
|
---|
101 | if (addmntent(f, &e))
|
---|
102 | rc = 3; /* Could not add an entry to the mount table. */
|
---|
103 |
|
---|
104 | endmntent(f);
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (size > 0)
|
---|
108 | {
|
---|
109 | memset(buf, 0, size);
|
---|
110 | free(buf);
|
---|
111 | }
|
---|
112 |
|
---|
113 | return rc;
|
---|
114 | }
|
---|