1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59#define FOR_mount
60#include "toys.h"
61
62GLOBALS(
63 struct arg_list *o;
64 char *t, *O;
65
66 unsigned long flags;
67 char *opts;
68 int okuser;
69)
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92static long flag_opts(char *new, long flags, char **more)
93{
94 struct {
95 char *name;
96 long flags;
97 } opts[] = {
98 {"loop", 0}, {"defaults", 0}, {"quiet", 0},
99 {"user", 0}, {"nouser", 0},
100 {"bind", MS_REC}, {"rbind", ~MS_REC},
101 {"ro", MS_RDONLY}, {"rw", ~MS_RDONLY},
102 {"nosuid", MS_NOSUID}, {"suid", ~MS_NOSUID},
103 {"nodev", MS_NODEV}, {"dev", ~MS_NODEV},
104 {"noexec", MS_NOEXEC}, {"exec", ~MS_NOEXEC},
105 {"sync", MS_SYNCHRONOUS}, {"async", ~MS_SYNCHRONOUS},
106 {"noatime", MS_NOATIME}, {"atime", ~MS_NOATIME},
107 {"norelatime", ~MS_RELATIME}, {"relatime", MS_RELATIME},
108 {"nodiratime", MS_NODIRATIME}, {"diratime", ~MS_NODIRATIME},
109 {"loud", ~MS_SILENT},
110 {"shared", MS_SHARED}, {"rshared", MS_SHARED|MS_REC},
111 {"slave", MS_SLAVE}, {"rslave", MS_SLAVE|MS_REC},
112 {"private", MS_PRIVATE}, {"rprivate", MS_SLAVE|MS_REC},
113 {"unbindable", MS_UNBINDABLE}, {"runbindable", MS_UNBINDABLE|MS_REC},
114 {"remount", MS_REMOUNT}, {"move", MS_MOVE},
115
116 };
117
118 if (new) for (;;) {
119 char *comma = strchr(new, ',');
120 int i;
121
122 if (comma) *comma = 0;
123
124
125 for (i = 0; i < ARRAY_LEN(opts); i++) if (!strcasecmp(opts[i].name, new)) {
126 long ll = opts[i].flags;
127
128 if (ll < 0) flags &= ll;
129 else flags |= ll;
130
131 break;
132 }
133
134
135 if (more && i == ARRAY_LEN(opts)) {
136 i = *more ? strlen(*more) : 0;
137 *more = xrealloc(*more, i + strlen(new) + 2);
138 if (i) (*more)[i++] = ',';
139 strcpy(i+*more, new);
140 }
141
142 if (!comma) break;
143 *comma = ',';
144 new = comma + 1;
145 }
146
147 return flags;
148}
149
150static void mount_filesystem(char *dev, char *dir, char *type,
151 unsigned long flags, char *opts)
152{
153 FILE *fp = 0;
154 int rc = EINVAL;
155 char *buf = 0;
156
157 if (FLAG(f)) return;
158
159 if (getuid()) {
160 if (TT.okuser) TT.okuser = 0;
161 else {
162 error_msg("'%s' not user mountable in fstab", dev);
163
164 return;
165 }
166 }
167
168 if (strstart(&dev, "UUID=")) {
169 char *s = xrunread((char *[]){"blkid", "-U", dev, 0}, 0);
170
171 if (!s || strlen(s)>=sizeof(toybuf)) return error_msg("No uuid %s", dev);
172 strcpy(dev = toybuf, s);
173 free(s);
174 }
175
176
177
178 if (type && !strcmp(type, "auto")) type = 0;
179 if (flags & MS_MOVE) {
180 if (type) error_exit("--move with -t");
181 } else if (!type) {
182 struct stat stdev, stdir;
183
184
185 if (!stat(dev, &stdev) && !stat(dir, &stdir)
186 && ((S_ISREG(stdev.st_mode) && S_ISREG(stdir.st_mode))
187 || (S_ISDIR(stdev.st_mode) && S_ISDIR(stdir.st_mode))))
188 {
189 flags ^= MS_REC;
190 flags |= MS_BIND;
191 } else fp = xfopen("/proc/filesystems", "r");
192 } else if (!strcmp(type, "ignore")) return;
193 else if (!strcmp(type, "swap"))
194 toys.exitval |= xrun((char *[]){"swapon", "--", dev, 0});
195
196 for (;;) {
197 int fd = -1, ro = 0;
198
199
200 if (fp && !buf) {
201 size_t i;
202
203 if (getline(&buf, &i, fp)<1) {
204 error_msg("%s: need -t", dev);
205 break;
206 }
207 type = buf;
208
209 if (!isspace(*type)) {
210 free(buf);
211 buf = 0;
212
213 continue;
214 }
215
216 while (isspace(*type)) type++;
217 i = strlen(type);
218 if (i) type[i-1] = 0;
219 }
220 if (FLAG(v)) printf("try '%s' type '%s' on '%s'\n", dev, type, dir);
221 for (;;) {
222 rc = mount(dev, dir, type, flags, opts);
223
224 if (!rc || (errno != EACCES && errno != EROFS) || (flags&MS_RDONLY))
225 break;
226
227
228 if (fd == -1) {
229 if (FLAG(v))
230 printf("trying BLKROSET ioctl on '%s'\n", dev);
231 if (-1 != (fd = open(dev, O_RDONLY))) {
232 rc = ioctl(fd, BLKROSET, &ro);
233 close(fd);
234 if (!rc) continue;
235 }
236 }
237 fprintf(stderr, "'%s' is read-only\n", dev);
238 flags |= MS_RDONLY;
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253 if (rc && errno == ENOTBLK) {
254 char *losetup[] = {"losetup", (flags&MS_RDONLY)?"-fsr":"-fs", dev, 0};
255
256 if ((dev = xrunread(losetup, 0))) continue;
257 error_msg("%s failed", *losetup);
258 break;
259 }
260
261 free(buf);
262 buf = 0;
263 if (!rc) break;
264 if (fp && (errno == EINVAL || errno == EBUSY)) continue;
265
266 perror_msg("'%s'->'%s'", dev, dir);
267
268 break;
269 }
270 if (fp) fclose(fp);
271}
272
273void mount_main(void)
274{
275 char *opts = 0, *dev = 0, *dir = 0, **ss;
276 long flags = MS_SILENT;
277 struct arg_list *o;
278 struct mtab_list *mtl, *mtl2 = 0, *mm, *remount;
279
280
281
282
283
284
285
286
287
288
289 for (o = TT.o; o; o = o->next) comma_collate(&opts, o->arg);
290 if (FLAG(r)) comma_collate(&opts, "ro");
291 if (FLAG(w)) comma_collate(&opts, "rw");
292 if (FLAG(R)) comma_collate(&opts, "rbind");
293
294
295 for (ss = toys.optargs; *ss; ss++) {
296 char *sss = *ss;
297
298
299 if (sss[0]=='-' && sss[1]=='-' && sss[2]) comma_collate(&opts, sss+2);
300 else if (!dev) dev = sss;
301 else if (!dir) dir = sss;
302
303 else error_exit("Max 2 arguments\n");
304 }
305
306 if (FLAG(a) && dir) error_exit("-a with >1 arg");
307
308
309
310
311 remount = (void *)(long)comma_scan(opts, "remount", 0);
312 if ((FLAG(a) && !access("/proc/mounts", R_OK)) || remount) {
313 mm = dlist_terminate(mtl = mtl2 = xgetmountlist(0));
314 if (remount) remount = mm;
315 }
316
317
318
319 if (FLAG(a) || (dev && (!dir || getuid() || remount))) {
320 if (!remount) dlist_terminate(mtl = xgetmountlist("/etc/fstab"));
321
322 for (mm = remount ? remount : mtl; mm; mm = (remount ? mm->prev : mm->next))
323 {
324 char *aopts = 0;
325 struct mtab_list *mmm = 0;
326 int aflags, noauto, len;
327
328
329
330 noauto = comma_scan(mm->opts, "noauto", 1);
331
332 if (FLAG(a)) {
333
334 if (dev) {
335 len = strlen(dev);
336 if (strncmp(dev, mm->dir, len)
337 || (mm->dir[len] && mm->dir[len] != '/')) continue;
338 } else if (noauto) continue;
339 if (!mountlist_istype(mm, TT.t) || !comma_scanall(mm->opts, TT.O))
340 continue;
341 } else {
342 if (dir && strcmp(dir, mm->dir)) continue;
343 if (strcmp(dev, mm->device) && (dir || strcmp(dev, mm->dir))) continue;
344 }
345
346
347
348 if (mtl2 && !remount)
349 for (mmm = mtl2; mmm; mmm = mmm->next)
350 if (!strcmp(mm->dir, mmm->dir) && !strcmp(mm->device, mmm->device))
351 break;
352
353
354 if (!mmm) {
355 TT.okuser = comma_scan(mm->opts, "user", 1);
356 aflags = flag_opts(mm->opts, flags, &aopts);
357 aflags = flag_opts(opts, aflags, &aopts);
358
359 mount_filesystem(mm->device, mm->dir, mm->type, aflags, aopts);
360 }
361 free(aopts);
362
363 if (!FLAG(a)) break;
364 }
365 if (CFG_TOYBOX_FREE) {
366 llist_traverse(mtl, free);
367 llist_traverse(mtl2, free);
368 }
369 if (!mm && !FLAG(a))
370 error_exit("'%s' not in %s", dir ? dir : dev,
371 remount ? "/proc/mounts" : "fstab");
372
373
374 } else if (!dev) {
375 for (mtl = xgetmountlist(0); mtl && (mm = dlist_pop(&mtl)); free(mm)) {
376 char *s = 0;
377
378 if (TT.t && strcmp(TT.t, mm->type)) continue;
379 if (*mm->device == '/') s = xabspath(mm->device, 0);
380 xprintf("%s on %s type %s (%s)\n",
381 s ? s : mm->device, mm->dir, mm->type, mm->opts);
382 free(s);
383 }
384
385
386 } else {
387 char *more = 0;
388
389 flags = flag_opts(opts, flags, &more);
390 mount_filesystem(dev, dir, TT.t, flags, more);
391 if (CFG_TOYBOX_FREE) free(more);
392 }
393}
394