1 | /* $Id: vbsfmount.c 31205 2010-07-29 12:48:43Z 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 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 | #include "vbsfmount.h"
|
---|
20 |
|
---|
21 | #ifndef _GNU_SOURCE
|
---|
22 | #define _GNU_SOURCE
|
---|
23 | #endif
|
---|
24 | #include <ctype.h>
|
---|
25 | #include <mntent.h>
|
---|
26 | #include <stdio.h>
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <string.h>
|
---|
29 | #include <sys/mount.h>
|
---|
30 |
|
---|
31 |
|
---|
32 | /** @todo Use defines for return values! */
|
---|
33 | int vbsfmount_complete(const char *host_name, const char *mount_point,
|
---|
34 | unsigned long flags, struct vbsf_mount_opts *opts)
|
---|
35 | {
|
---|
36 | FILE *f, *m;
|
---|
37 | char *buf;
|
---|
38 | size_t size;
|
---|
39 | struct mntent e;
|
---|
40 | int rc = 0;
|
---|
41 |
|
---|
42 | m = open_memstream(&buf, &size);
|
---|
43 | if (!m)
|
---|
44 | return 1; /* Could not update mount table (failed to create memstream). */
|
---|
45 |
|
---|
46 | if (opts->uid)
|
---|
47 | fprintf(m, "uid=%d,", opts->uid);
|
---|
48 | if (opts->gid)
|
---|
49 | fprintf(m, "gid=%d,", opts->gid);
|
---|
50 | if (opts->ttl)
|
---|
51 | fprintf(m, "ttl=%d,", opts->ttl);
|
---|
52 | if (*opts->nls_name)
|
---|
53 | fprintf(m, "iocharset=%s,", opts->nls_name);
|
---|
54 | if (flags & MS_NOSUID)
|
---|
55 | fprintf(m, "%s,", MNTOPT_NOSUID);
|
---|
56 | if (flags & MS_RDONLY)
|
---|
57 | fprintf(m, "%s,", MNTOPT_RO);
|
---|
58 | else
|
---|
59 | fprintf(m, "%s,", MNTOPT_RW);
|
---|
60 |
|
---|
61 | fclose(m);
|
---|
62 |
|
---|
63 | if (size > 0)
|
---|
64 | buf[size - 1] = 0;
|
---|
65 | else
|
---|
66 | buf = "defaults";
|
---|
67 |
|
---|
68 | f = setmntent(MOUNTED, "a+");
|
---|
69 | if (!f)
|
---|
70 | {
|
---|
71 | rc = 2; /* Could not open mount table for update. */
|
---|
72 | }
|
---|
73 | else
|
---|
74 | {
|
---|
75 | e.mnt_fsname = (char*)host_name;
|
---|
76 | e.mnt_dir = (char*)mount_point;
|
---|
77 | e.mnt_type = "vboxsf";
|
---|
78 | e.mnt_opts = buf;
|
---|
79 | e.mnt_freq = 0;
|
---|
80 | e.mnt_passno = 0;
|
---|
81 |
|
---|
82 | if (addmntent(f, &e))
|
---|
83 | rc = 3; /* Could not add an entry to the mount table. */
|
---|
84 |
|
---|
85 | endmntent(f);
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (size > 0)
|
---|
89 | {
|
---|
90 | memset(buf, 0, size);
|
---|
91 | free(buf);
|
---|
92 | }
|
---|
93 |
|
---|
94 | return rc;
|
---|
95 | }
|
---|