1 | /* $Id: vbsfmount.c 77139 2019-02-01 19:35:23Z 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->uid)
|
---|
48 | fprintf(m, "uid=%d,", opts->uid);
|
---|
49 | if (opts->gid)
|
---|
50 | fprintf(m, "gid=%d,", opts->gid);
|
---|
51 | if (opts->ttl)
|
---|
52 | fprintf(m, "ttl=%d,", opts->ttl);
|
---|
53 | if (*opts->nls_name)
|
---|
54 | fprintf(m, "iocharset=%s,", opts->nls_name);
|
---|
55 | if (flags & MS_NOSUID)
|
---|
56 | fprintf(m, "%s,", MNTOPT_NOSUID);
|
---|
57 | if (flags & MS_RDONLY)
|
---|
58 | fprintf(m, "%s,", MNTOPT_RO);
|
---|
59 | else
|
---|
60 | fprintf(m, "%s,", MNTOPT_RW);
|
---|
61 | if (opts->cMaxIoPages)
|
---|
62 | fprintf(m, "maxiopages=%u,", opts->cMaxIoPages);
|
---|
63 |
|
---|
64 | fclose(m);
|
---|
65 |
|
---|
66 | if (size > 0)
|
---|
67 | buf[size - 1] = 0;
|
---|
68 | else
|
---|
69 | buf = "defaults";
|
---|
70 |
|
---|
71 | f = setmntent(MOUNTED, "a+");
|
---|
72 | if (!f)
|
---|
73 | {
|
---|
74 | rc = 2; /* Could not open mount table for update. */
|
---|
75 | }
|
---|
76 | else
|
---|
77 | {
|
---|
78 | e.mnt_fsname = (char*)host_name;
|
---|
79 | e.mnt_dir = (char*)mount_point;
|
---|
80 | e.mnt_type = "vboxsf";
|
---|
81 | e.mnt_opts = buf;
|
---|
82 | e.mnt_freq = 0;
|
---|
83 | e.mnt_passno = 0;
|
---|
84 |
|
---|
85 | if (addmntent(f, &e))
|
---|
86 | rc = 3; /* Could not add an entry to the mount table. */
|
---|
87 |
|
---|
88 | endmntent(f);
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (size > 0)
|
---|
92 | {
|
---|
93 | memset(buf, 0, size);
|
---|
94 | free(buf);
|
---|
95 | }
|
---|
96 |
|
---|
97 | return rc;
|
---|
98 | }
|
---|