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