mount.c revision bc7b0cbe156da639f0cbe17bf89725d87e86512a
1/*
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        int flags;
142
143        flags = (rwflag & MS_RDONLY) ? O_RDONLY : O_RDWR;
144
145        // FIXME - only one loop mount supported at a time
146        file_fd = open(dev, flags);
147        if (file_fd < -1) {
148            perror("open backing file failed");
149            return 1;
150        }
151        device_fd = open(LOOP_DEVICE, flags);
152        if (device_fd < -1) {
153            perror("open loop device failed");
154            close(file_fd);
155            return 1;
156        }
157        if (ioctl(device_fd, LOOP_SET_FD, file_fd) < 0) {
158            perror("ioctl LOOP_SET_FD failed");
159            close(file_fd);
160            close(device_fd);
161            return 1;
162        }
163
164        close(file_fd);
165        close(device_fd);
166        dev = LOOP_DEVICE;
167    }
168
169	while ((s = strsep(&type, ",")) != NULL) {
170retry:
171		if (mount(dev, dir, s, rwflag, data) == -1) {
172			error = errno;
173			/*
174			 * If the filesystem is not found, or the
175			 * superblock is invalid, try the next.
176			 */
177			if (error == ENODEV || error == EINVAL)
178				continue;
179
180			/*
181			 * If we get EACCESS, and we're trying to
182			 * mount readwrite and this isn't a remount,
183			 * try read only.
184			 */
185			if (error == EACCES &&
186			    (rwflag & (MS_REMOUNT|MS_RDONLY)) == 0) {
187				rwflag |= MS_RDONLY;
188				goto retry;
189			}
190			break;
191		}
192	}
193
194	if (error) {
195		errno = error;
196		perror("mount");
197		return 255;
198	}
199
200	return 0;
201}
202
203static int print_mounts()
204{
205    FILE* f;
206    int length;
207    char buffer[100];
208
209    f = fopen("/proc/mounts", "r");
210    if (!f) {
211        fprintf(stdout, "could not open /proc/mounts\n");
212        return -1;
213    }
214
215    do {
216        length = fread(buffer, 1, 100, f);
217        if (length > 0)
218            fwrite(buffer, 1, length, stdout);
219    } while (length > 0);
220
221    fclose(f);
222    return 0;
223}
224
225int mount_main(int argc, char *argv[])
226{
227	char *type = NULL;
228	int c;
229	int loop;
230
231	progname = argv[0];
232	rwflag = MS_VERBOSE;
233
234	// mount with no arguments is equivalent to "cat /proc/mounts"
235	if (argc == 1) return print_mounts();
236
237	do {
238		c = getopt(argc, argv, "o:rt:w");
239		if (c == EOF)
240			break;
241		switch (c) {
242		case 'o':
243			rwflag = parse_mount_options(optarg, rwflag, &extra, &loop);
244			break;
245		case 'r':
246			rwflag |= MS_RDONLY;
247			break;
248		case 't':
249			type = optarg;
250			break;
251		case 'w':
252			rwflag &= ~MS_RDONLY;
253			break;
254		case '?':
255			fprintf(stderr, "%s: invalid option -%c\n",
256				progname, optopt);
257			exit(1);
258		}
259	} while (1);
260
261	/*
262	 * If remount, bind or move was specified, then we don't
263	 * have a "type" as such.  Use the dummy "none" type.
264	 */
265	if (rwflag & MS_TYPE)
266		type = "none";
267
268	if (optind + 2 != argc || type == NULL) {
269		fprintf(stderr, "Usage: %s [-r] [-w] [-o options] [-t type] "
270			"device directory\n", progname);
271		exit(1);
272	}
273
274	return do_mount(argv[optind], argv[optind + 1], type, rwflag,
275		        extra.str, loop);
276}
277