blob: ef13e1fd5337366e3b7ada71df888db429ebd5ab [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * mount.c, by rmk
3 */
4
5#include <sys/mount.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <errno.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <unistd.h>
13
14#include <linux/loop.h>
15
16#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
17
18// FIXME - only one loop mount is supported at a time
19#define LOOP_DEVICE "/dev/block/loop0"
20
21struct mount_opts {
22 const char str[8];
23 unsigned long rwmask;
24 unsigned long rwset;
25 unsigned long rwnoset;
26};
27
28struct extra_opts {
29 char *str;
30 char *end;
31 int used_size;
32 int alloc_size;
33};
34
35/*
36 * These options define the function of "mount(2)".
37 */
38#define MS_TYPE (MS_REMOUNT|MS_BIND|MS_MOVE)
39
40
41static const struct mount_opts options[] = {
42 /* name mask set noset */
43 { "async", MS_SYNCHRONOUS, 0, MS_SYNCHRONOUS },
44 { "atime", MS_NOATIME, 0, MS_NOATIME },
45 { "bind", MS_TYPE, MS_BIND, 0, },
46 { "dev", MS_NODEV, 0, MS_NODEV },
47 { "diratime", MS_NODIRATIME, 0, MS_NODIRATIME },
48 { "dirsync", MS_DIRSYNC, MS_DIRSYNC, 0 },
49 { "exec", MS_NOEXEC, 0, MS_NOEXEC },
50 { "move", MS_TYPE, MS_MOVE, 0 },
51 { "recurse", MS_REC, MS_REC, 0 },
52 { "remount", MS_TYPE, MS_REMOUNT, 0 },
53 { "ro", MS_RDONLY, MS_RDONLY, 0 },
54 { "rw", MS_RDONLY, 0, MS_RDONLY },
55 { "suid", MS_NOSUID, 0, MS_NOSUID },
56 { "sync", MS_SYNCHRONOUS, MS_SYNCHRONOUS, 0 },
57 { "verbose", MS_VERBOSE, MS_VERBOSE, 0 },
58};
59
60static void add_extra_option(struct extra_opts *extra, char *s)
61{
62 int len = strlen(s);
63 int newlen = extra->used_size + len;
64
65 if (extra->str)
66 len++; /* +1 for ',' */
67
68 if (newlen >= extra->alloc_size) {
69 char *new;
70
71 new = realloc(extra->str, newlen + 1); /* +1 for NUL */
72 if (!new)
73 return;
74
75 extra->str = new;
76 extra->end = extra->str + extra->used_size;
77 extra->alloc_size = newlen;
78 }
79
80 if (extra->used_size) {
81 *extra->end = ',';
82 extra->end++;
83 }
84 strcpy(extra->end, s);
85 extra->used_size += len;
86
87}
88
89static unsigned long
90parse_mount_options(char *arg, unsigned long rwflag, struct extra_opts *extra, int* loop)
91{
92 char *s;
93
94 *loop = 0;
95 while ((s = strsep(&arg, ",")) != NULL) {
96 char *opt = s;
97 unsigned int i;
98 int res, no = s[0] == 'n' && s[1] == 'o';
99
100 if (no)
101 s += 2;
102
103 if (strcmp(s, "loop") == 0) {
104 *loop = 1;
105 continue;
106 }
107 for (i = 0, res = 1; i < ARRAY_SIZE(options); i++) {
108 res = strcmp(s, options[i].str);
109
110 if (res == 0) {
111 rwflag &= ~options[i].rwmask;
112 if (no)
113 rwflag |= options[i].rwnoset;
114 else
115 rwflag |= options[i].rwset;
116 }
117 if (res <= 0)
118 break;
119 }
120
121 if (res != 0 && s[0])
122 add_extra_option(extra, opt);
123 }
124
125 return rwflag;
126}
127
128static char *progname;
129
130static struct extra_opts extra;
131static unsigned long rwflag;
132
133static int
134do_mount(char *dev, char *dir, char *type, unsigned long rwflag, void *data, int loop)
135{
136 char *s;
137 int error = 0;
138
139 if (loop) {
140 int file_fd, device_fd;
141
142 // FIXME - only one loop mount supported at a time
143 file_fd = open(dev, O_RDWR);
144 if (file_fd < -1) {
145 perror("open backing file failed");
146 return 1;
147 }
148 device_fd = open(LOOP_DEVICE, O_RDWR);
149 if (device_fd < -1) {
150 perror("open loop device failed");
151 close(file_fd);
152 return 1;
153 }
154 if (ioctl(device_fd, LOOP_SET_FD, file_fd) < 0) {
155 perror("ioctl LOOP_SET_FD failed");
156 close(file_fd);
157 close(device_fd);
158 return 1;
159 }
160
161 close(file_fd);
162 close(device_fd);
163 dev = LOOP_DEVICE;
164 }
165
166 while ((s = strsep(&type, ",")) != NULL) {
167retry:
168 if (mount(dev, dir, s, rwflag, data) == -1) {
169 error = errno;
170 /*
171 * If the filesystem is not found, or the
172 * superblock is invalid, try the next.
173 */
174 if (error == ENODEV || error == EINVAL)
175 continue;
176
177 /*
178 * If we get EACCESS, and we're trying to
179 * mount readwrite and this isn't a remount,
180 * try read only.
181 */
182 if (error == EACCES &&
183 (rwflag & (MS_REMOUNT|MS_RDONLY)) == 0) {
184 rwflag |= MS_RDONLY;
185 goto retry;
186 }
187 break;
188 }
189 }
190
191 if (error) {
192 errno = error;
193 perror("mount");
194 return 255;
195 }
196
197 return 0;
198}
199
200static int print_mounts()
201{
202 FILE* f;
203 int length;
204 char buffer[100];
205
206 f = fopen("/proc/mounts", "r");
207 if (!f) {
208 fprintf(stdout, "could not open /proc/mounts\n");
209 return -1;
210 }
211
212 do {
213 length = fread(buffer, 1, 100, f);
214 if (length > 0)
215 fwrite(buffer, 1, length, stdout);
216 } while (length > 0);
217
218 fclose(f);
219 return 0;
220}
221
222int mount_main(int argc, char *argv[])
223{
224 char *type = NULL;
225 int c;
226 int loop;
227
228 progname = argv[0];
229 rwflag = MS_VERBOSE;
230
231 // mount with no arguments is equivalent to "cat /proc/mounts"
232 if (argc == 1) return print_mounts();
233
234 do {
235 c = getopt(argc, argv, "o:rt:w");
236 if (c == EOF)
237 break;
238 switch (c) {
239 case 'o':
240 rwflag = parse_mount_options(optarg, rwflag, &extra, &loop);
241 break;
242 case 'r':
243 rwflag |= MS_RDONLY;
244 break;
245 case 't':
246 type = optarg;
247 break;
248 case 'w':
249 rwflag &= ~MS_RDONLY;
250 break;
251 case '?':
252 fprintf(stderr, "%s: invalid option -%c\n",
253 progname, optopt);
254 exit(1);
255 }
256 } while (1);
257
258 /*
259 * If remount, bind or move was specified, then we don't
260 * have a "type" as such. Use the dummy "none" type.
261 */
262 if (rwflag & MS_TYPE)
263 type = "none";
264
265 if (optind + 2 != argc || type == NULL) {
266 fprintf(stderr, "Usage: %s [-r] [-w] [-o options] [-t type] "
267 "device directory\n", progname);
268 exit(1);
269 }
270
271 return do_mount(argv[optind], argv[optind + 1], type, rwflag,
272 extra.str, loop);
273}