forked from rmyorston/busybox-w32
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmount.c
More file actions
2556 lines (2320 loc) · 67.8 KB
/
Copy pathmount.c
File metadata and controls
2556 lines (2320 loc) · 67.8 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi: set sw=4 ts=4: */
/*
* Mini mount implementation for busybox
*
* Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
* Copyright (C) 2005-2006 by Rob Landley <rob@landley.net>
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
// Design notes: There is no spec for mount. Remind me to write one.
//
// mount_main() calls singlemount() which calls mount_it_now().
//
// mount_main() can loop through /etc/fstab for mount -a
// singlemount() can loop through /etc/filesystems for fstype detection.
// mount_it_now() does the actual mount.
//
//config:config MOUNT
//config: bool "mount (24 kb)"
//config: default y
//config: help
//config: All files and filesystems in Unix are arranged into one big directory
//config: tree. The 'mount' utility is used to graft a filesystem onto a
//config: particular part of the tree. A filesystem can either live on a block
//config: device, or it can be accessible over the network, as is the case with
//config: NFS filesystems.
//config:
//config:config FEATURE_MOUNT_FAKE
//config: bool "Support -f (fake mount)"
//config: default y
//config: depends on MOUNT
//config: help
//config: Enable support for faking a file system mount.
//config:
//config:config FEATURE_MOUNT_VERBOSE
//config: bool "Support -v (verbose)"
//config: default y
//config: depends on MOUNT
//config: help
//config: Enable multi-level -v[vv...] verbose messages. Useful if you
//config: debug mount problems and want to see what is exactly passed
//config: to the kernel.
//config:
//config:config FEATURE_MOUNT_HELPERS
//config: bool "Support mount helpers"
//config: default n
//config: depends on MOUNT
//config: help
//config: Enable mounting of virtual file systems via external helpers.
//config: E.g. "mount obexfs#-b00.11.22.33.44.55 /mnt" will in effect call
//config: "obexfs -b00.11.22.33.44.55 /mnt"
//config: Also "mount -t sometype [-o opts] fs /mnt" will try
//config: "sometype [-o opts] fs /mnt" if simple mount syscall fails.
//config: The idea is to use such virtual filesystems in /etc/fstab.
//config:
//config:config FEATURE_MOUNT_LABEL
//config: bool "Support specifying devices by label or UUID"
//config: default y
//config: depends on MOUNT
//config: select VOLUMEID
//config: help
//config: This allows for specifying a device by label or uuid, rather than by
//config: name. This feature utilizes the same functionality as blkid/findfs.
//config:
//config:config FEATURE_MOUNT_NFS
//config: bool "Support mounting NFS file systems on Linux < 2.6.23"
//config: default n
//config: depends on MOUNT
//config: select FEATURE_SYSLOG
//config: help
//config: Enable mounting of NFS file systems on Linux kernels prior
//config: to version 2.6.23. Note that in this case mounting of NFS
//config: over IPv6 will not be possible.
//config:
//config: Note that this option links in RPC support from libc,
//config: which is rather large (~10 kbytes on uclibc).
//config:
//config:config FEATURE_MOUNT_CIFS
//config: bool "Support mounting CIFS/SMB file systems"
//config: default y
//config: depends on MOUNT
//config: help
//config: Enable support for samba mounts.
//config:
//config:config FEATURE_MOUNT_FLAGS
//config: depends on MOUNT
//config: bool "Support lots of -o flags"
//config: default y
//config: help
//config: Without this, mount only supports ro/rw/remount. With this, it
//config: supports nosuid, suid, dev, nodev, exec, noexec, sync, async, atime,
//config: noatime, diratime, nodiratime, loud, bind, move, shared, slave,
//config: private, unbindable, rshared, rslave, rprivate, and runbindable.
//config:
//config:config FEATURE_MOUNT_FSTAB
//config: depends on MOUNT
//config: bool "Support /etc/fstab and -a (mount all)"
//config: default y
//config: help
//config: Support mount all and looking for files in /etc/fstab.
//config:
//config:config FEATURE_MOUNT_OTHERTAB
//config: depends on FEATURE_MOUNT_FSTAB
//config: bool "Support -T <alt_fstab>"
//config: default y
//config: help
//config: Support mount -T (specifying an alternate fstab)
/* On full-blown systems, requires suid for user mounts.
* But it's not unthinkable to have it available in non-suid flavor on some systems,
* for viewing mount table.
* Therefore we use BB_SUID_MAYBE instead of BB_SUID_REQUIRE: */
//applet:IF_MOUNT(APPLET(mount, BB_DIR_BIN, IF_DESKTOP(BB_SUID_MAYBE) IF_NOT_DESKTOP(BB_SUID_DROP)))
//kbuild:lib-$(CONFIG_MOUNT) += mount.o
//usage:#define mount_trivial_usage
//usage: "[OPTIONS] [-o OPT] DEVICE NODE"
//usage:#define mount_full_usage "\n\n"
//usage: "Mount a filesystem. Filesystem autodetection requires /proc.\n"
//usage: "\n -a Mount all filesystems in fstab"
//usage: IF_FEATURE_MOUNT_FAKE(
//usage: IF_FEATURE_MTAB_SUPPORT(
//usage: "\n -f Update /etc/mtab, but don't mount"
//usage: )
//usage: IF_NOT_FEATURE_MTAB_SUPPORT(
//usage: "\n -f Dry run"
//usage: )
//usage: )
//usage: IF_FEATURE_MOUNT_HELPERS(
//usage: "\n -i Don't run mount helper"
//usage: )
//usage: IF_FEATURE_MTAB_SUPPORT(
//usage: "\n -n Don't update /etc/mtab"
//usage: )
//usage: IF_FEATURE_MOUNT_VERBOSE(
//usage: "\n -v Verbose"
//usage: )
////usage: "\n -s Sloppy (ignored)"
//usage: "\n -r Read-only mount"
////usage: "\n -w Read-write mount (default)"
//usage: "\n -t FSTYPE[,...] Filesystem type(s)"
//usage: IF_FEATURE_MOUNT_OTHERTAB(
//usage: "\n -T FILE Read FILE instead of /etc/fstab"
//usage: )
//usage: "\n -O OPT Mount only filesystems with option OPT (-a only)"
//usage: "\n-o OPT:"
//usage: IF_FEATURE_MOUNT_LOOP(
//usage: "\n loop Ignored (loop devices are autodetected)"
//usage: )
//usage: IF_FEATURE_MOUNT_FLAGS(
//usage: "\n [a]sync Writes are [a]synchronous"
//usage: "\n [no]atime Disable/enable updates to inode access times"
//usage: "\n [no]diratime Disable/enable atime updates to directories"
//usage: "\n [no]relatime Disable/enable atime updates relative to modification time"
//usage: "\n [no]dev (Dis)allow use of special device files"
//usage: "\n [no]exec (Dis)allow use of executable files"
//usage: "\n [no]suid (Dis)allow set-user-id-root programs"
//usage: "\n [r]shared Convert [recursively] to a shared subtree"
//usage: "\n [r]slave Convert [recursively] to a slave subtree"
//usage: "\n [r]private Convert [recursively] to a private subtree"
//usage: "\n [un]bindable Make mount point [un]able to be bind mounted"
//usage: "\n [r]bind Bind a file or directory [recursively] to another location"
//usage: "\n move Relocate an existing mount point"
//usage: )
//usage: "\n remount Remount a mounted filesystem, changing flags"
//usage: "\n ro Same as -r"
//usage: "\n"
//usage: "\nThere are filesystem-specific -o flags."
//usage:
//usage:#define mount_example_usage
//usage: "$ mount\n"
//usage: "/dev/hda3 on / type minix (rw)\n"
//usage: "proc on /proc type proc (rw)\n"
//usage: "devpts on /dev/pts type devpts (rw)\n"
//usage: "$ mount /dev/fd0 /mnt -t msdos -o ro\n"
//usage: "$ mount /tmp/diskimage /opt -t ext2 -o loop\n"
//usage: "$ mount cd_image.iso mydir\n"
//usage:#define mount_notes_usage
//usage: "Returns 0 for success, number of failed mounts for -a, or errno for one mount."
#include <mntent.h>
#if ENABLE_FEATURE_SYSLOG
#include <syslog.h>
#endif
#include <sys/mount.h>
// Grab more as needed from util-linux's mount/mount_constants.h
#ifndef MS_DIRSYNC
# define MS_DIRSYNC (1 << 7) // Directory modifications are synchronous
#endif
#ifndef MS_NOSYMFOLLOW
# define MS_NOSYMFOLLOW (1 << 8)
#endif
#ifndef MS_BIND
# define MS_BIND (1 << 12)
#endif
#ifndef MS_MOVE
# define MS_MOVE (1 << 13)
#endif
#ifndef MS_RECURSIVE
# define MS_RECURSIVE (1 << 14)
#endif
#ifndef MS_SILENT
# define MS_SILENT (1 << 15)
#endif
// The shared subtree stuff, which went in around 2.6.15
#ifndef MS_UNBINDABLE
# define MS_UNBINDABLE (1 << 17)
#endif
#ifndef MS_PRIVATE
# define MS_PRIVATE (1 << 18)
#endif
#ifndef MS_SLAVE
# define MS_SLAVE (1 << 19)
#endif
#ifndef MS_SHARED
# define MS_SHARED (1 << 20)
#endif
#ifndef MS_RELATIME
# define MS_RELATIME (1 << 21)
#endif
#ifndef MS_STRICTATIME
# define MS_STRICTATIME (1 << 24)
#endif
#ifndef MS_LAZYTIME
# define MS_LAZYTIME (1 << 25)
#endif
/* Any ~MS_FOO value has this bit set: */
#define BB_MS_INVERTED_VALUE (1u << 31)
#include "libbb.h"
#include "common_bufsiz.h"
#if ENABLE_FEATURE_MOUNT_LABEL
# include "volume_id.h"
#else
# define resolve_mount_spec(fsname) ((void)0)
#endif
// Needed for nfs support only
#include <sys/utsname.h>
#undef TRUE
#undef FALSE
#if ENABLE_FEATURE_MOUNT_NFS
/* This is just a warning of a common mistake. Possibly this should be a
* uclibc faq entry rather than in busybox... */
# if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
# warning "You probably need to build uClibc with UCLIBC_HAS_RPC for NFS support"
/* not #error, since user may be using e.g. libtirpc instead.
* This might work:
* CONFIG_EXTRA_CFLAGS="-I/usr/include/tirpc"
* CONFIG_EXTRA_LDLIBS="tirpc"
*/
# endif
# include <rpc/rpc.h>
# include <rpc/pmap_prot.h>
# include <rpc/pmap_clnt.h>
#endif
#if defined(__dietlibc__)
// 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
// dietlibc-0.30 does not have implementation of getmntent_r()
static struct mntent *getmntent_r(FILE* stream, struct mntent* result,
char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM)
{
struct mntent* ment = getmntent(stream);
return memcpy(result, ment, sizeof(*ment));
}
#endif
// Not real flags, but we want to be able to check for this.
enum {
MOUNT_USERS = (1 << 27) * ENABLE_DESKTOP,
MOUNT_NOFAIL = (1 << 28) * ENABLE_DESKTOP,
MOUNT_NOAUTO = (1 << 29),
MOUNT_SWAP = (1 << 30),
MOUNT_FAKEFLAGS = MOUNT_USERS | MOUNT_NOFAIL | MOUNT_NOAUTO | MOUNT_SWAP
};
#define OPTION_STR "o:*t:rwanfvsiO:" IF_FEATURE_MOUNT_OTHERTAB("T:")
enum {
OPT_o = (1 << 0),
OPT_t = (1 << 1),
OPT_r = (1 << 2),
OPT_w = (1 << 3),
OPT_a = (1 << 4),
OPT_n = (1 << 5),
OPT_f = (1 << 6),
OPT_v = (1 << 7),
OPT_s = (1 << 8),
OPT_i = (1 << 9),
OPT_O = (1 << 10),
OPT_T = (1 << 11),
};
#if ENABLE_FEATURE_MTAB_SUPPORT
#define USE_MTAB (!(option_mask32 & OPT_n))
#else
#define USE_MTAB 0
#endif
#if ENABLE_FEATURE_MOUNT_FAKE
#define FAKE_IT (option_mask32 & OPT_f)
#else
#define FAKE_IT 0
#endif
#if ENABLE_FEATURE_MOUNT_HELPERS
#define HELPERS_ALLOWED (!(option_mask32 & OPT_i))
#else
#define HELPERS_ALLOWED 0
#endif
// TODO: more "user" flag compatibility.
// "user" option (from mount manpage):
// Only the user that mounted a filesystem can unmount it again.
// If any user should be able to unmount, then use users instead of user
// in the fstab line. The owner option is similar to the user option,
// with the restriction that the user must be the owner of the special file.
// This may be useful e.g. for /dev/fd if a login script makes
// the console user owner of this device.
// Standard mount options (from -o options or --options),
// with corresponding flags
static const int32_t mount_options[] ALIGN4 = {
// MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs.
IF_FEATURE_MOUNT_LOOP(
/* "loop" */ 0,
)
IF_FEATURE_MOUNT_FSTAB(
/* "defaults" */ 0,
/* "quiet" 0 - do not filter out, vfat wants to see it */
/* "noauto" */ MOUNT_NOAUTO,
/* "sw" */ MOUNT_SWAP,
/* "swap" */ MOUNT_SWAP,
IF_DESKTOP(/* "user" */ MOUNT_USERS,)
IF_DESKTOP(/* "users" */ MOUNT_USERS,)
IF_DESKTOP(/* "nofail" */ MOUNT_NOFAIL,)
/* "_netdev" */ 0,
IF_DESKTOP(/* "comment=" */ 0,) /* systemd uses this in fstab */
)
IF_FEATURE_MOUNT_FLAGS(
// vfs flags
/* "nosuid" */ MS_NOSUID,
/* "suid" */ ~MS_NOSUID,
/* "dev" */ ~MS_NODEV,
/* "nodev" */ MS_NODEV,
/* "exec" */ ~MS_NOEXEC,
/* "noexec" */ MS_NOEXEC,
/* "sync" */ MS_SYNCHRONOUS,
/* "dirsync" */ MS_DIRSYNC,
/* "async" */ ~MS_SYNCHRONOUS,
/* "atime" */ ~MS_NOATIME,
/* "noatime" */ MS_NOATIME,
/* "diratime" */ ~MS_NODIRATIME,
/* "nodiratime" */ MS_NODIRATIME,
/* "relatime" */ MS_RELATIME,
/* "norelatime" */ ~MS_RELATIME,
/* "strictatime" */ MS_STRICTATIME,
/* "nostrictatime"*/ ~MS_STRICTATIME,
/* "lazytime" */ MS_LAZYTIME,
/* "nolazytime" */ ~MS_LAZYTIME,
/* "nosymfollow" */ MS_NOSYMFOLLOW,
/* "mand" */ MS_MANDLOCK,
/* "nomand" */ ~MS_MANDLOCK,
/* "loud" */ ~MS_SILENT,
// action flags
/* "rbind" */ MS_BIND|MS_RECURSIVE,
/* "bind" */ MS_BIND,
/* "move" */ MS_MOVE,
/* "shared" */ MS_SHARED,
/* "slave" */ MS_SLAVE,
/* "private" */ MS_PRIVATE,
/* "unbindable" */ MS_UNBINDABLE,
/* "rshared" */ MS_SHARED|MS_RECURSIVE,
/* "rslave" */ MS_SLAVE|MS_RECURSIVE,
/* "rprivate" */ MS_PRIVATE|MS_RECURSIVE,
/* "runbindable" */ MS_UNBINDABLE|MS_RECURSIVE,
)
// Always understood.
/* "ro" */ MS_RDONLY, // vfs flag
/* "rw" */ ~MS_RDONLY, // vfs flag
/* "remount" */ MS_REMOUNT // action flag
};
static const char mount_option_str[] ALIGN1 =
IF_FEATURE_MOUNT_LOOP(
"loop\0"
)
IF_FEATURE_MOUNT_FSTAB(
"defaults\0"
// "quiet\0" - do not filter out, vfat wants to see it
"noauto\0"
"sw\0"
"swap\0"
IF_DESKTOP("user\0")
IF_DESKTOP("users\0")
IF_DESKTOP("nofail\0")
"_netdev\0"
IF_DESKTOP("comment=\0") /* systemd uses this in fstab */
)
IF_FEATURE_MOUNT_FLAGS(
// vfs flags
"nosuid" "\0"
"suid" "\0"
"dev" "\0"
"nodev" "\0"
"exec" "\0"
"noexec" "\0"
"sync" "\0"
"dirsync" "\0"
"async" "\0"
"atime" "\0"
"noatime" "\0"
"diratime" "\0"
"nodiratime" "\0"
"relatime" "\0"
"norelatime" "\0"
"strictatime" "\0"
"nostrictatime""\0"
"lazytime" "\0"
"nolazytime" "\0"
"nosymfollow" "\0"
"mand" "\0"
"nomand" "\0"
"loud" "\0"
// action flags
"rbind\0"
"bind\0"
"move\0"
"make-shared\0"
"make-slave\0"
"make-private\0"
"make-unbindable\0"
"make-rshared\0"
"make-rslave\0"
"make-rprivate\0"
"make-runbindable\0"
)
// Always understood.
"ro\0" // vfs flag
"rw\0" // vfs flag
"remount\0" // action flag
;
struct globals {
#if ENABLE_FEATURE_MOUNT_NFS
smalluint nfs_mount_version;
#endif
#if ENABLE_FEATURE_MOUNT_VERBOSE
unsigned verbose;
#endif
llist_t *fslist;
char getmntent_buf[1];
} FIX_ALIASING;
enum { GETMNTENT_BUFSIZE = COMMON_BUFSIZE - offsetof(struct globals, getmntent_buf) };
#define G (*(struct globals*)bb_common_bufsiz1)
#define nfs_mount_version (G.nfs_mount_version)
#if ENABLE_FEATURE_MOUNT_VERBOSE
#define verbose (G.verbose )
#else
#define verbose 0
#endif
#define fslist (G.fslist )
#define getmntent_buf (G.getmntent_buf )
#define INIT_G() do { setup_common_bufsiz(); } while (0)
#if ENABLE_FEATURE_MTAB_SUPPORT
/*
* update_mtab_entry_on_move() is used to update entry in case of mount --move.
* we are looking for existing entries mnt_dir which is equal to mnt_fsname of
* input mntent and replace it by new one.
*/
static void FAST_FUNC update_mtab_entry_on_move(const struct mntent *mp)
{
struct mntent *entries, *m;
int i, count;
FILE *mountTable;
mountTable = setmntent(bb_path_mtab_file, "r");
if (!mountTable) {
bb_simple_perror_msg(bb_path_mtab_file);
return;
}
entries = NULL;
count = 0;
while ((m = getmntent(mountTable)) != NULL) {
entries = xrealloc_vector(entries, 3, count);
entries[count].mnt_fsname = xstrdup(m->mnt_fsname);
entries[count].mnt_dir = xstrdup(m->mnt_dir);
entries[count].mnt_type = xstrdup(m->mnt_type);
entries[count].mnt_opts = xstrdup(m->mnt_opts);
entries[count].mnt_freq = m->mnt_freq;
entries[count].mnt_passno = m->mnt_passno;
count++;
}
endmntent(mountTable);
mountTable = setmntent(bb_path_mtab_file, "w");
if (mountTable) {
for (i = 0; i < count; i++) {
if (strcmp(entries[i].mnt_dir, mp->mnt_fsname) != 0)
addmntent(mountTable, &entries[i]);
else
addmntent(mountTable, mp);
}
endmntent(mountTable);
} else if (errno != EROFS)
bb_simple_perror_msg(bb_path_mtab_file);
if (ENABLE_FEATURE_CLEAN_UP) {
for (i = 0; i < count; i++) {
free(entries[i].mnt_fsname);
free(entries[i].mnt_dir);
free(entries[i].mnt_type);
free(entries[i].mnt_opts);
}
free(entries);
}
}
#endif
#if ENABLE_FEATURE_MOUNT_VERBOSE
static int verbose_mount(const char *source, const char *target,
const char *filesystemtype,
unsigned long mountflags, const void *data)
{
int rc;
errno = 0;
rc = mount(source, target, filesystemtype, mountflags, data);
if (verbose >= 2)
bb_perror_msg("mount('%s','%s','%s',0x%08lx,'%s'):%d",
source, target, filesystemtype,
mountflags, (char*)data, rc);
return rc;
}
#else
#define verbose_mount(...) mount(__VA_ARGS__)
#endif
// Append mount options to string
// ("merge two comma-separated lists" is a good candidate for libbb!)
static void append_mount_options(char **oldopts, const char *newopts)
{
if (*oldopts && **oldopts) {
//TODO: do this unconditionally?
//this way, newopts of "opt1,opt2,opt1"
//will be de-duped into "opt1,opt2" in _both_ cases
//(whether or now old opts are empty)
//the only modification needed is to not prepend extra comma
//when old opts is "".
// Do not insert options which are already there
while (*newopts) {
char *p;
int len;
//if (*newopts == ',') { newopts++; continue; }
len = strchrnul(newopts, ',') - newopts;
p = *oldopts;
while (1) {
if (strncmp(p, newopts, len) == 0
&& (p[len] == ',' || p[len] == '\0'))
goto skip;
p = strchr(p, ',');
if (!p) break;
p++;
}
xasprintf_inplace(*oldopts, "%s,%.*s", *oldopts, len, newopts);
skip:
newopts += len;
while (*newopts == ',') newopts++;
}
} else {
if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
*oldopts = xstrdup(newopts);
}
}
// Use the mount_options list to parse options into flags.
// Also update list of unrecognized options if unrecognized != NULL
static unsigned long parse_mount_options(char *options, char **unrecognized, uint32_t *opt)
{
unsigned long flags = MS_SILENT;
// Loop through options
for (;;) {
unsigned i;
char *comma = strchr(options, ',');
const char *option_str = mount_option_str;
if (comma) *comma = '\0';
// FIXME: use hasmntopt()
// Find this option in mount_options
for (i = 0; i < ARRAY_SIZE(mount_options); i++) {
unsigned opt_len = strlen(option_str);
if (strncasecmp(option_str, options, opt_len) == 0
&& (options[opt_len] == '\0'
/* or is it "comment=" thingy in fstab? */
IF_FEATURE_MOUNT_FSTAB(IF_DESKTOP( || option_str[opt_len-1] == '=' ))
)
) {
unsigned long fl = mount_options[i];
if (fl & BB_MS_INVERTED_VALUE)
flags &= fl;
else
flags |= fl;
/* If we see "-o rw" on command line, it's the same as -w:
* "do not try to fall back to RO mounts"
*/
if (fl == ~MS_RDONLY && opt)
(*opt) |= OPT_w;
goto found;
}
option_str += opt_len + 1;
}
// We did not recognize this option.
// If "unrecognized" is not NULL, append option there.
// Note that we should not append *empty* option -
// in this case we want to pass NULL, not "", to "data"
// parameter of mount(2) syscall.
// This is crucial for filesystems that don't accept
// any arbitrary mount options, like cgroup fs:
// "mount -t cgroup none /mnt"
if (options[0] && unrecognized) {
// Add it to strflags, to pass on to kernel
char *p = *unrecognized;
unsigned len = p ? strlen(p) : 0;
*unrecognized = p = xrealloc(p, len + strlen(options) + 2);
// Comma separated if it's not the first one
if (len) p[len++] = ',';
strcpy(p + len, options);
}
found:
if (!comma)
break;
// Advance to next option
*comma = ',';
options = ++comma;
}
return flags;
}
// Return a list of all block device backed filesystems
static llist_t *get_block_backed_filesystems(void)
{
static const char filesystems[2][sizeof("/proc/filesystems")] ALIGN1 = {
"/etc/filesystems",
"/proc/filesystems",
};
char *fs, *buf;
llist_t *list = NULL;
int i;
FILE *f;
for (i = 0; i < 2; i++) {
f = fopen_for_read(filesystems[i]);
if (!f) continue;
while ((buf = xmalloc_fgetline(f)) != NULL) {
if (is_prefixed_with(buf, "nodev") && isspace(buf[5]))
goto next;
fs = skip_whitespace(buf);
if (*fs == '#' || *fs == '*' || !*fs)
goto next;
llist_add_to_end(&list, xstrdup(fs));
next:
free(buf);
}
if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
}
return list;
}
#if ENABLE_FEATURE_CLEAN_UP
static void delete_block_backed_filesystems(void)
{
llist_free(fslist, free);
}
#else
void delete_block_backed_filesystems(void);
#endif
// Perform actual mount of specific filesystem at specific location.
// NB: mp->xxx fields may be trashed on exit
static int mount_it_now(struct mntent *mp, unsigned long vfsflags, char *filteropts)
{
int rc = 0;
vfsflags &= ~(unsigned long)MOUNT_FAKEFLAGS;
if (FAKE_IT) {
if (verbose >= 2)
bb_error_msg("would do mount('%s','%s','%s',0x%08lx,'%s')",
mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
vfsflags, filteropts);
goto mtab;
}
// Mount, with fallback to read-only if necessary.
for (;;) {
errno = 0;
rc = verbose_mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
vfsflags, filteropts);
if (rc == 0)
goto mtab; // success
// mount failed, try helper program
// mount.<mnt_type>
if (HELPERS_ALLOWED && mp->mnt_type) {
char *args[8];
int errno_save = errno;
args[0] = xasprintf("mount.%s", mp->mnt_type);
rc = 1;
if (FAKE_IT)
args[rc++] = (char *)"-f";
if (ENABLE_FEATURE_MTAB_SUPPORT && !USE_MTAB)
args[rc++] = (char *)"-n";
args[rc++] = mp->mnt_fsname;
args[rc++] = mp->mnt_dir;
if (filteropts) {
args[rc++] = (char *)"-o";
args[rc++] = filteropts;
}
args[rc] = NULL;
rc = spawn_and_wait(args);
free(args[0]);
if (rc == 0)
goto mtab; // success
errno = errno_save;
}
// Should we retry read-only mount?
if (vfsflags & MS_RDONLY)
break; // no, already was tried
if (option_mask32 & OPT_w)
break; // no, "mount -w" never falls back to RO
if (errno != EACCES && errno != EROFS)
break; // no, error isn't hinting that RO may work
if (!(vfsflags & MS_SILENT))
bb_error_msg("%s is write-protected, mounting read-only",
mp->mnt_fsname);
vfsflags |= MS_RDONLY;
}
// Abort entirely if permission denied.
if (rc && errno == EPERM)
bb_simple_error_msg_and_die(bb_msg_perm_denied_are_you_root);
// If the mount was successful, and we're maintaining an old-style
// mtab file by hand, add the new entry to it now.
mtab:
if (USE_MTAB && !rc && !(vfsflags & MS_REMOUNT)) {
char *fsname;
FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
const char *option_str = mount_option_str;
int i;
if (!mountTable) {
bb_simple_perror_msg(bb_path_mtab_file);
goto ret;
}
// Add vfs string flags
for (i = 0; mount_options[i] != MS_REMOUNT; i++) {
if (mount_options[i] > 0 && (mount_options[i] & vfsflags))
append_mount_options(&(mp->mnt_opts), option_str);
option_str += strlen(option_str) + 1;
}
// Remove trailing / (if any) from directory we mounted on
i = strlen(mp->mnt_dir) - 1;
while (i > 0 && mp->mnt_dir[i] == '/')
mp->mnt_dir[i--] = '\0';
// Convert to canonical pathnames as needed
mp->mnt_dir = bb_simplify_path(mp->mnt_dir);
fsname = NULL;
if (!mp->mnt_type || !*mp->mnt_type) { // bind mount
mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname);
mp->mnt_type = (char*)"bind";
}
mp->mnt_freq = mp->mnt_passno = 0;
// Write and close
#if ENABLE_FEATURE_MTAB_SUPPORT
if (vfsflags & MS_MOVE)
update_mtab_entry_on_move(mp);
else
#endif
addmntent(mountTable, mp);
endmntent(mountTable);
if (ENABLE_FEATURE_CLEAN_UP) {
free(mp->mnt_dir);
free(fsname);
}
}
ret:
return rc;
}
#if ENABLE_FEATURE_MOUNT_NFS
/*
* Linux NFS mount
* Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
*
* Licensed under GPLv2, see file LICENSE in this source tree.
*
* Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
* numbers to be specified on the command line.
*
* Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
* Omit the call to connect() for Linux version 1.3.11 or later.
*
* Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
* Implemented the "bg", "fg" and "retry" mount options for NFS.
*
* 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org>
* - added Native Language Support
*
* Modified by Olaf Kirch and Trond Myklebust for new NFS code,
* plus NFSv3 stuff.
*/
#define MOUNTPORT 635
#define MNTPATHLEN 1024
#define MNTNAMLEN 255
#define FHSIZE 32
#define FHSIZE3 64
typedef char fhandle[FHSIZE];
typedef struct {
unsigned int fhandle3_len;
char *fhandle3_val;
} fhandle3;
enum mountstat3 {
MNT_OK = 0,
MNT3ERR_PERM = 1,
MNT3ERR_NOENT = 2,
MNT3ERR_IO = 5,
MNT3ERR_ACCES = 13,
MNT3ERR_NOTDIR = 20,
MNT3ERR_INVAL = 22,
MNT3ERR_NAMETOOLONG = 63,
MNT3ERR_NOTSUPP = 10004,
MNT3ERR_SERVERFAULT = 10006,
};
typedef enum mountstat3 mountstat3;
struct fhstatus {
unsigned int fhs_status;
union {
fhandle fhs_fhandle;
} fhstatus_u;
};
typedef struct fhstatus fhstatus;
struct mountres3_ok {
fhandle3 fhandle;
struct {
unsigned int auth_flavours_len;
char *auth_flavours_val;
} auth_flavours;
};
typedef struct mountres3_ok mountres3_ok;
struct mountres3 {
mountstat3 fhs_status;
union {
mountres3_ok mountinfo;
} mountres3_u;
};
typedef struct mountres3 mountres3;
typedef char *dirpath;
typedef char *name;
typedef struct mountbody *mountlist;
struct mountbody {
name ml_hostname;
dirpath ml_directory;
mountlist ml_next;
};
typedef struct mountbody mountbody;
typedef struct groupnode *groups;
struct groupnode {
name gr_name;
groups gr_next;
};
typedef struct groupnode groupnode;
typedef struct exportnode *exports;
struct exportnode {
dirpath ex_dir;
groups ex_groups;
exports ex_next;
};
typedef struct exportnode exportnode;
struct ppathcnf {
int pc_link_max;
short pc_max_canon;
short pc_max_input;
short pc_name_max;
short pc_path_max;
short pc_pipe_buf;
uint8_t pc_vdisable;
char pc_xxx;
short pc_mask[2];
};
typedef struct ppathcnf ppathcnf;
#define MOUNTPROG 100005
#define MOUNTVERS 1
#define MOUNTPROC_NULL 0
#define MOUNTPROC_MNT 1
#define MOUNTPROC_DUMP 2
#define MOUNTPROC_UMNT 3
#define MOUNTPROC_UMNTALL 4
#define MOUNTPROC_EXPORT 5
#define MOUNTPROC_EXPORTALL 6
#define MOUNTVERS_POSIX 2
#define MOUNTPROC_PATHCONF 7
#define MOUNT_V3 3
#define MOUNTPROC3_NULL 0
#define MOUNTPROC3_MNT 1
#define MOUNTPROC3_DUMP 2
#define MOUNTPROC3_UMNT 3
#define MOUNTPROC3_UMNTALL 4
#define MOUNTPROC3_EXPORT 5
enum {
#ifndef NFS_FHSIZE
NFS_FHSIZE = 32,
#endif
#ifndef NFS_PORT
NFS_PORT = 2049
#endif
};
/*
* We want to be able to compile mount on old kernels in such a way
* that the binary will work well on more recent kernels.
* Thus, if necessary we teach nfsmount.c the structure of new fields
* that will come later.
*
* Moreover, the new kernel includes conflict with glibc includes
* so it is easiest to ignore the kernel altogether (at compile time).
*/
struct nfs2_fh {
char data[32];
};
struct nfs3_fh {
unsigned short size;
unsigned char data[64];
};
struct nfs_mount_data {
int version; /* 1 */
int fd; /* 1 */
struct nfs2_fh old_root; /* 1 */