3
0

mount.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini mount implementation for busybox
  4. *
  5. * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
  6. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  7. * Copyright (C) 2005-2006 by Rob Landley <rob@landley.net>
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  10. */
  11. // Design notes: There is no spec for mount. Remind me to write one.
  12. //
  13. // mount_main() calls singlemount() which calls mount_it_now().
  14. //
  15. // mount_main() can loop through /etc/fstab for mount -a
  16. // singlemount() can loop through /etc/filesystems for fstype detection.
  17. // mount_it_now() does the actual mount.
  18. //
  19. //config:config MOUNT
  20. //config: bool "mount (23 kb)"
  21. //config: default y
  22. //config: help
  23. //config: All files and filesystems in Unix are arranged into one big directory
  24. //config: tree. The 'mount' utility is used to graft a filesystem onto a
  25. //config: particular part of the tree. A filesystem can either live on a block
  26. //config: device, or it can be accessible over the network, as is the case with
  27. //config: NFS filesystems.
  28. //config:
  29. //config:config FEATURE_MOUNT_FAKE
  30. //config: bool "Support -f (fake mount)"
  31. //config: default y
  32. //config: depends on MOUNT
  33. //config: help
  34. //config: Enable support for faking a file system mount.
  35. //config:
  36. //config:config FEATURE_MOUNT_VERBOSE
  37. //config: bool "Support -v (verbose)"
  38. //config: default y
  39. //config: depends on MOUNT
  40. //config: help
  41. //config: Enable multi-level -v[vv...] verbose messages. Useful if you
  42. //config: debug mount problems and want to see what is exactly passed
  43. //config: to the kernel.
  44. //config:
  45. //config:config FEATURE_MOUNT_HELPERS
  46. //config: bool "Support mount helpers"
  47. //config: default n
  48. //config: depends on MOUNT
  49. //config: help
  50. //config: Enable mounting of virtual file systems via external helpers.
  51. //config: E.g. "mount obexfs#-b00.11.22.33.44.55 /mnt" will in effect call
  52. //config: "obexfs -b00.11.22.33.44.55 /mnt"
  53. //config: Also "mount -t sometype [-o opts] fs /mnt" will try
  54. //config: "sometype [-o opts] fs /mnt" if simple mount syscall fails.
  55. //config: The idea is to use such virtual filesystems in /etc/fstab.
  56. //config:
  57. //config:config FEATURE_MOUNT_LABEL
  58. //config: bool "Support specifying devices by label or UUID"
  59. //config: default y
  60. //config: depends on MOUNT
  61. //config: select VOLUMEID
  62. //config: help
  63. //config: This allows for specifying a device by label or uuid, rather than by
  64. //config: name. This feature utilizes the same functionality as blkid/findfs.
  65. //config:
  66. //config:config FEATURE_MOUNT_NFS
  67. //config: bool "Support mounting NFS file systems on Linux < 2.6.23"
  68. //config: default n
  69. //config: depends on MOUNT
  70. //config: select FEATURE_SYSLOG
  71. //config: help
  72. //config: Enable mounting of NFS file systems on Linux kernels prior
  73. //config: to version 2.6.23. Note that in this case mounting of NFS
  74. //config: over IPv6 will not be possible.
  75. //config:
  76. //config: Note that this option links in RPC support from libc,
  77. //config: which is rather large (~10 kbytes on uclibc).
  78. //config:
  79. //config:config FEATURE_MOUNT_CIFS
  80. //config: bool "Support mounting CIFS/SMB file systems"
  81. //config: default y
  82. //config: depends on MOUNT
  83. //config: help
  84. //config: Enable support for samba mounts.
  85. //config:
  86. //config:config FEATURE_MOUNT_FLAGS
  87. //config: depends on MOUNT
  88. //config: bool "Support lots of -o flags"
  89. //config: default y
  90. //config: help
  91. //config: Without this, mount only supports ro/rw/remount. With this, it
  92. //config: supports nosuid, suid, dev, nodev, exec, noexec, sync, async, atime,
  93. //config: noatime, diratime, nodiratime, loud, bind, move, shared, slave,
  94. //config: private, unbindable, rshared, rslave, rprivate, and runbindable.
  95. //config:
  96. //config:config FEATURE_MOUNT_FSTAB
  97. //config: depends on MOUNT
  98. //config: bool "Support /etc/fstab and -a (mount all)"
  99. //config: default y
  100. //config: help
  101. //config: Support mount all and looking for files in /etc/fstab.
  102. //config:
  103. //config:config FEATURE_MOUNT_OTHERTAB
  104. //config: depends on FEATURE_MOUNT_FSTAB
  105. //config: bool "Support -T <alt_fstab>"
  106. //config: default y
  107. //config: help
  108. //config: Support mount -T (specifying an alternate fstab)
  109. /* On full-blown systems, requires suid for user mounts.
  110. * But it's not unthinkable to have it available in non-suid flavor on some systems,
  111. * for viewing mount table.
  112. * Therefore we use BB_SUID_MAYBE instead of BB_SUID_REQUIRE: */
  113. //applet:IF_MOUNT(APPLET(mount, BB_DIR_BIN, IF_DESKTOP(BB_SUID_MAYBE) IF_NOT_DESKTOP(BB_SUID_DROP)))
  114. //kbuild:lib-$(CONFIG_MOUNT) += mount.o
  115. //usage:#define mount_trivial_usage
  116. //usage: "[OPTIONS] [-o OPT] DEVICE NODE"
  117. //usage:#define mount_full_usage "\n\n"
  118. //usage: "Mount a filesystem. Filesystem autodetection requires /proc.\n"
  119. //usage: "\n -a Mount all filesystems in fstab"
  120. //usage: IF_FEATURE_MOUNT_FAKE(
  121. //usage: IF_FEATURE_MTAB_SUPPORT(
  122. //usage: "\n -f Update /etc/mtab, but don't mount"
  123. //usage: )
  124. //usage: IF_NOT_FEATURE_MTAB_SUPPORT(
  125. //usage: "\n -f Dry run"
  126. //usage: )
  127. //usage: )
  128. //usage: IF_FEATURE_MOUNT_HELPERS(
  129. //usage: "\n -i Don't run mount helper"
  130. //usage: )
  131. //usage: IF_FEATURE_MTAB_SUPPORT(
  132. //usage: "\n -n Don't update /etc/mtab"
  133. //usage: )
  134. //usage: IF_FEATURE_MOUNT_VERBOSE(
  135. //usage: "\n -v Verbose"
  136. //usage: )
  137. ////usage: "\n -s Sloppy (ignored)"
  138. //usage: "\n -r Read-only mount"
  139. ////usage: "\n -w Read-write mount (default)"
  140. //usage: "\n -t FSTYPE[,...] Filesystem type(s)"
  141. //usage: IF_FEATURE_MOUNT_OTHERTAB(
  142. //usage: "\n -T FILE Read FILE instead of /etc/fstab"
  143. //usage: )
  144. //usage: "\n -O OPT Mount only filesystems with option OPT (-a only)"
  145. //usage: "\n-o OPT:"
  146. //usage: IF_FEATURE_MOUNT_LOOP(
  147. //usage: "\n loop Ignored (loop devices are autodetected)"
  148. //usage: )
  149. //usage: IF_FEATURE_MOUNT_FLAGS(
  150. //usage: "\n [a]sync Writes are [a]synchronous"
  151. //usage: "\n [no]atime Disable/enable updates to inode access times"
  152. //usage: "\n [no]diratime Disable/enable atime updates to directories"
  153. //usage: "\n [no]relatime Disable/enable atime updates relative to modification time"
  154. //usage: "\n [no]dev (Dis)allow use of special device files"
  155. //usage: "\n [no]exec (Dis)allow use of executable files"
  156. //usage: "\n [no]suid (Dis)allow set-user-id-root programs"
  157. //usage: "\n [r]shared Convert [recursively] to a shared subtree"
  158. //usage: "\n [r]slave Convert [recursively] to a slave subtree"
  159. //usage: "\n [r]private Convert [recursively] to a private subtree"
  160. //usage: "\n [un]bindable Make mount point [un]able to be bind mounted"
  161. //usage: "\n [r]bind Bind a file or directory [recursively] to another location"
  162. //usage: "\n move Relocate an existing mount point"
  163. //usage: )
  164. //usage: "\n remount Remount a mounted filesystem, changing flags"
  165. //usage: "\n ro Same as -r"
  166. //usage: "\n"
  167. //usage: "\nThere are filesystem-specific -o flags."
  168. //usage:
  169. //usage:#define mount_example_usage
  170. //usage: "$ mount\n"
  171. //usage: "/dev/hda3 on / type minix (rw)\n"
  172. //usage: "proc on /proc type proc (rw)\n"
  173. //usage: "devpts on /dev/pts type devpts (rw)\n"
  174. //usage: "$ mount /dev/fd0 /mnt -t msdos -o ro\n"
  175. //usage: "$ mount /tmp/diskimage /opt -t ext2 -o loop\n"
  176. //usage: "$ mount cd_image.iso mydir\n"
  177. //usage:#define mount_notes_usage
  178. //usage: "Returns 0 for success, number of failed mounts for -a, or errno for one mount."
  179. #include <mntent.h>
  180. #if ENABLE_FEATURE_SYSLOG
  181. #include <syslog.h>
  182. #endif
  183. #include <sys/mount.h>
  184. // Grab more as needed from util-linux's mount/mount_constants.h
  185. #ifndef MS_DIRSYNC
  186. # define MS_DIRSYNC (1 << 7) // Directory modifications are synchronous
  187. #endif
  188. #ifndef MS_NOSYMFOLLOW
  189. # define MS_NOSYMFOLLOW (1 << 8)
  190. #endif
  191. #ifndef MS_BIND
  192. # define MS_BIND (1 << 12)
  193. #endif
  194. #ifndef MS_MOVE
  195. # define MS_MOVE (1 << 13)
  196. #endif
  197. #ifndef MS_RECURSIVE
  198. # define MS_RECURSIVE (1 << 14)
  199. #endif
  200. #ifndef MS_SILENT
  201. # define MS_SILENT (1 << 15)
  202. #endif
  203. // The shared subtree stuff, which went in around 2.6.15
  204. #ifndef MS_UNBINDABLE
  205. # define MS_UNBINDABLE (1 << 17)
  206. #endif
  207. #ifndef MS_PRIVATE
  208. # define MS_PRIVATE (1 << 18)
  209. #endif
  210. #ifndef MS_SLAVE
  211. # define MS_SLAVE (1 << 19)
  212. #endif
  213. #ifndef MS_SHARED
  214. # define MS_SHARED (1 << 20)
  215. #endif
  216. #ifndef MS_RELATIME
  217. # define MS_RELATIME (1 << 21)
  218. #endif
  219. #ifndef MS_STRICTATIME
  220. # define MS_STRICTATIME (1 << 24)
  221. #endif
  222. #ifndef MS_LAZYTIME
  223. # define MS_LAZYTIME (1 << 25)
  224. #endif
  225. /* Any ~MS_FOO value has this bit set: */
  226. #define BB_MS_INVERTED_VALUE (1u << 31)
  227. #include "libbb.h"
  228. #include "common_bufsiz.h"
  229. #if ENABLE_FEATURE_MOUNT_LABEL
  230. # include "volume_id.h"
  231. #else
  232. # define resolve_mount_spec(fsname) ((void)0)
  233. #endif
  234. // Needed for nfs support only
  235. #include <sys/utsname.h>
  236. #undef TRUE
  237. #undef FALSE
  238. #if ENABLE_FEATURE_MOUNT_NFS
  239. /* This is just a warning of a common mistake. Possibly this should be a
  240. * uclibc faq entry rather than in busybox... */
  241. # if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
  242. # warning "You probably need to build uClibc with UCLIBC_HAS_RPC for NFS support"
  243. /* not #error, since user may be using e.g. libtirpc instead.
  244. * This might work:
  245. * CONFIG_EXTRA_CFLAGS="-I/usr/include/tirpc"
  246. * CONFIG_EXTRA_LDLIBS="tirpc"
  247. */
  248. # endif
  249. # include <rpc/rpc.h>
  250. # include <rpc/pmap_prot.h>
  251. # include <rpc/pmap_clnt.h>
  252. #endif
  253. #if defined(__dietlibc__)
  254. // 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
  255. // dietlibc-0.30 does not have implementation of getmntent_r()
  256. static struct mntent *getmntent_r(FILE* stream, struct mntent* result,
  257. char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM)
  258. {
  259. struct mntent* ment = getmntent(stream);
  260. return memcpy(result, ment, sizeof(*ment));
  261. }
  262. #endif
  263. // Not real flags, but we want to be able to check for this.
  264. enum {
  265. MOUNT_USERS = (1 << 27) * ENABLE_DESKTOP,
  266. MOUNT_NOFAIL = (1 << 28) * ENABLE_DESKTOP,
  267. MOUNT_NOAUTO = (1 << 29),
  268. MOUNT_SWAP = (1 << 30),
  269. MOUNT_FAKEFLAGS = MOUNT_USERS | MOUNT_NOFAIL | MOUNT_NOAUTO | MOUNT_SWAP
  270. };
  271. #define OPTION_STR "o:*t:rwanfvsiO:" IF_FEATURE_MOUNT_OTHERTAB("T:")
  272. enum {
  273. OPT_o = (1 << 0),
  274. OPT_t = (1 << 1),
  275. OPT_r = (1 << 2),
  276. OPT_w = (1 << 3),
  277. OPT_a = (1 << 4),
  278. OPT_n = (1 << 5),
  279. OPT_f = (1 << 6),
  280. OPT_v = (1 << 7),
  281. OPT_s = (1 << 8),
  282. OPT_i = (1 << 9),
  283. OPT_O = (1 << 10),
  284. OPT_T = (1 << 11),
  285. };
  286. #if ENABLE_FEATURE_MTAB_SUPPORT
  287. #define USE_MTAB (!(option_mask32 & OPT_n))
  288. #else
  289. #define USE_MTAB 0
  290. #endif
  291. #if ENABLE_FEATURE_MOUNT_FAKE
  292. #define FAKE_IT (option_mask32 & OPT_f)
  293. #else
  294. #define FAKE_IT 0
  295. #endif
  296. #if ENABLE_FEATURE_MOUNT_HELPERS
  297. #define HELPERS_ALLOWED (!(option_mask32 & OPT_i))
  298. #else
  299. #define HELPERS_ALLOWED 0
  300. #endif
  301. // TODO: more "user" flag compatibility.
  302. // "user" option (from mount manpage):
  303. // Only the user that mounted a filesystem can unmount it again.
  304. // If any user should be able to unmount, then use users instead of user
  305. // in the fstab line. The owner option is similar to the user option,
  306. // with the restriction that the user must be the owner of the special file.
  307. // This may be useful e.g. for /dev/fd if a login script makes
  308. // the console user owner of this device.
  309. // Standard mount options (from -o options or --options),
  310. // with corresponding flags
  311. static const int32_t mount_options[] ALIGN4 = {
  312. // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs.
  313. IF_FEATURE_MOUNT_LOOP(
  314. /* "loop" */ 0,
  315. )
  316. IF_FEATURE_MOUNT_FSTAB(
  317. /* "defaults" */ 0,
  318. /* "quiet" 0 - do not filter out, vfat wants to see it */
  319. /* "noauto" */ MOUNT_NOAUTO,
  320. /* "sw" */ MOUNT_SWAP,
  321. /* "swap" */ MOUNT_SWAP,
  322. IF_DESKTOP(/* "user" */ MOUNT_USERS,)
  323. IF_DESKTOP(/* "users" */ MOUNT_USERS,)
  324. IF_DESKTOP(/* "nofail" */ MOUNT_NOFAIL,)
  325. /* "_netdev" */ 0,
  326. IF_DESKTOP(/* "comment=" */ 0,) /* systemd uses this in fstab */
  327. )
  328. IF_FEATURE_MOUNT_FLAGS(
  329. // vfs flags
  330. /* "nosuid" */ MS_NOSUID,
  331. /* "suid" */ ~MS_NOSUID,
  332. /* "dev" */ ~MS_NODEV,
  333. /* "nodev" */ MS_NODEV,
  334. /* "exec" */ ~MS_NOEXEC,
  335. /* "noexec" */ MS_NOEXEC,
  336. /* "sync" */ MS_SYNCHRONOUS,
  337. /* "dirsync" */ MS_DIRSYNC,
  338. /* "async" */ ~MS_SYNCHRONOUS,
  339. /* "atime" */ ~MS_NOATIME,
  340. /* "noatime" */ MS_NOATIME,
  341. /* "diratime" */ ~MS_NODIRATIME,
  342. /* "nodiratime" */ MS_NODIRATIME,
  343. /* "relatime" */ MS_RELATIME,
  344. /* "norelatime" */ ~MS_RELATIME,
  345. /* "strictatime" */ MS_STRICTATIME,
  346. /* "nostrictatime"*/ ~MS_STRICTATIME,
  347. /* "lazytime" */ MS_LAZYTIME,
  348. /* "nolazytime" */ ~MS_LAZYTIME,
  349. /* "nosymfollow" */ MS_NOSYMFOLLOW,
  350. /* "mand" */ MS_MANDLOCK,
  351. /* "nomand" */ ~MS_MANDLOCK,
  352. /* "loud" */ ~MS_SILENT,
  353. // action flags
  354. /* "rbind" */ MS_BIND|MS_RECURSIVE,
  355. /* "bind" */ MS_BIND,
  356. /* "move" */ MS_MOVE,
  357. /* "shared" */ MS_SHARED,
  358. /* "slave" */ MS_SLAVE,
  359. /* "private" */ MS_PRIVATE,
  360. /* "unbindable" */ MS_UNBINDABLE,
  361. /* "rshared" */ MS_SHARED|MS_RECURSIVE,
  362. /* "rslave" */ MS_SLAVE|MS_RECURSIVE,
  363. /* "rprivate" */ MS_PRIVATE|MS_RECURSIVE,
  364. /* "runbindable" */ MS_UNBINDABLE|MS_RECURSIVE,
  365. )
  366. // Always understood.
  367. /* "ro" */ MS_RDONLY, // vfs flag
  368. /* "rw" */ ~MS_RDONLY, // vfs flag
  369. /* "remount" */ MS_REMOUNT // action flag
  370. };
  371. static const char mount_option_str[] ALIGN1 =
  372. IF_FEATURE_MOUNT_LOOP(
  373. "loop\0"
  374. )
  375. IF_FEATURE_MOUNT_FSTAB(
  376. "defaults\0"
  377. // "quiet\0" - do not filter out, vfat wants to see it
  378. "noauto\0"
  379. "sw\0"
  380. "swap\0"
  381. IF_DESKTOP("user\0")
  382. IF_DESKTOP("users\0")
  383. IF_DESKTOP("nofail\0")
  384. "_netdev\0"
  385. IF_DESKTOP("comment=\0") /* systemd uses this in fstab */
  386. )
  387. IF_FEATURE_MOUNT_FLAGS(
  388. // vfs flags
  389. "nosuid" "\0"
  390. "suid" "\0"
  391. "dev" "\0"
  392. "nodev" "\0"
  393. "exec" "\0"
  394. "noexec" "\0"
  395. "sync" "\0"
  396. "dirsync" "\0"
  397. "async" "\0"
  398. "atime" "\0"
  399. "noatime" "\0"
  400. "diratime" "\0"
  401. "nodiratime" "\0"
  402. "relatime" "\0"
  403. "norelatime" "\0"
  404. "strictatime" "\0"
  405. "nostrictatime""\0"
  406. "lazytime" "\0"
  407. "nolazytime" "\0"
  408. "nosymfollow" "\0"
  409. "mand" "\0"
  410. "nomand" "\0"
  411. "loud" "\0"
  412. // action flags
  413. "rbind\0"
  414. "bind\0"
  415. "move\0"
  416. "make-shared\0"
  417. "make-slave\0"
  418. "make-private\0"
  419. "make-unbindable\0"
  420. "make-rshared\0"
  421. "make-rslave\0"
  422. "make-rprivate\0"
  423. "make-runbindable\0"
  424. )
  425. // Always understood.
  426. "ro\0" // vfs flag
  427. "rw\0" // vfs flag
  428. "remount\0" // action flag
  429. ;
  430. struct globals {
  431. #if ENABLE_FEATURE_MOUNT_NFS
  432. smalluint nfs_mount_version;
  433. #endif
  434. #if ENABLE_FEATURE_MOUNT_VERBOSE
  435. unsigned verbose;
  436. #endif
  437. llist_t *fslist;
  438. char getmntent_buf[1];
  439. } FIX_ALIASING;
  440. enum { GETMNTENT_BUFSIZE = COMMON_BUFSIZE - offsetof(struct globals, getmntent_buf) };
  441. #define G (*(struct globals*)bb_common_bufsiz1)
  442. #define nfs_mount_version (G.nfs_mount_version)
  443. #if ENABLE_FEATURE_MOUNT_VERBOSE
  444. #define verbose (G.verbose )
  445. #else
  446. #define verbose 0
  447. #endif
  448. #define fslist (G.fslist )
  449. #define getmntent_buf (G.getmntent_buf )
  450. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  451. #if ENABLE_FEATURE_MTAB_SUPPORT
  452. /*
  453. * update_mtab_entry_on_move() is used to update entry in case of mount --move.
  454. * we are looking for existing entries mnt_dir which is equal to mnt_fsname of
  455. * input mntent and replace it by new one.
  456. */
  457. static void FAST_FUNC update_mtab_entry_on_move(const struct mntent *mp)
  458. {
  459. struct mntent *entries, *m;
  460. int i, count;
  461. FILE *mountTable;
  462. mountTable = setmntent(bb_path_mtab_file, "r");
  463. if (!mountTable) {
  464. bb_simple_perror_msg(bb_path_mtab_file);
  465. return;
  466. }
  467. entries = NULL;
  468. count = 0;
  469. while ((m = getmntent(mountTable)) != NULL) {
  470. entries = xrealloc_vector(entries, 3, count);
  471. entries[count].mnt_fsname = xstrdup(m->mnt_fsname);
  472. entries[count].mnt_dir = xstrdup(m->mnt_dir);
  473. entries[count].mnt_type = xstrdup(m->mnt_type);
  474. entries[count].mnt_opts = xstrdup(m->mnt_opts);
  475. entries[count].mnt_freq = m->mnt_freq;
  476. entries[count].mnt_passno = m->mnt_passno;
  477. count++;
  478. }
  479. endmntent(mountTable);
  480. mountTable = setmntent(bb_path_mtab_file, "w");
  481. if (mountTable) {
  482. for (i = 0; i < count; i++) {
  483. if (strcmp(entries[i].mnt_dir, mp->mnt_fsname) != 0)
  484. addmntent(mountTable, &entries[i]);
  485. else
  486. addmntent(mountTable, mp);
  487. }
  488. endmntent(mountTable);
  489. } else if (errno != EROFS)
  490. bb_simple_perror_msg(bb_path_mtab_file);
  491. if (ENABLE_FEATURE_CLEAN_UP) {
  492. for (i = 0; i < count; i++) {
  493. free(entries[i].mnt_fsname);
  494. free(entries[i].mnt_dir);
  495. free(entries[i].mnt_type);
  496. free(entries[i].mnt_opts);
  497. }
  498. free(entries);
  499. }
  500. }
  501. #endif
  502. #if ENABLE_FEATURE_MOUNT_VERBOSE
  503. static int verbose_mount(const char *source, const char *target,
  504. const char *filesystemtype,
  505. unsigned long mountflags, const void *data)
  506. {
  507. int rc;
  508. errno = 0;
  509. rc = mount(source, target, filesystemtype, mountflags, data);
  510. if (verbose >= 2)
  511. bb_perror_msg("mount('%s','%s','%s',0x%08lx,'%s'):%d",
  512. source, target, filesystemtype,
  513. mountflags, (char*)data, rc);
  514. return rc;
  515. }
  516. #else
  517. #define verbose_mount(...) mount(__VA_ARGS__)
  518. #endif
  519. // Append mount options to string
  520. static void append_mount_options(char **oldopts, const char *newopts)
  521. {
  522. if (*oldopts && **oldopts) {
  523. // Do not insert options which are already there
  524. while (newopts[0]) {
  525. char *p;
  526. int len = strlen(newopts);
  527. p = strchr(newopts, ',');
  528. if (p) len = p - newopts;
  529. p = *oldopts;
  530. while (1) {
  531. if (!strncmp(p, newopts, len)
  532. && (p[len] == ',' || p[len] == '\0'))
  533. goto skip;
  534. p = strchr(p,',');
  535. if (!p) break;
  536. p++;
  537. }
  538. p = xasprintf("%s,%.*s", *oldopts, len, newopts);
  539. free(*oldopts);
  540. *oldopts = p;
  541. skip:
  542. newopts += len;
  543. while (newopts[0] == ',') newopts++;
  544. }
  545. } else {
  546. if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
  547. *oldopts = xstrdup(newopts);
  548. }
  549. }
  550. // Use the mount_options list to parse options into flags.
  551. // Also update list of unrecognized options if unrecognized != NULL
  552. static unsigned long parse_mount_options(char *options, char **unrecognized)
  553. {
  554. unsigned long flags = MS_SILENT;
  555. // Loop through options
  556. for (;;) {
  557. unsigned i;
  558. char *comma = strchr(options, ',');
  559. const char *option_str = mount_option_str;
  560. if (comma) *comma = '\0';
  561. // FIXME: use hasmntopt()
  562. // Find this option in mount_options
  563. for (i = 0; i < ARRAY_SIZE(mount_options); i++) {
  564. unsigned opt_len = strlen(option_str);
  565. if (strncasecmp(option_str, options, opt_len) == 0
  566. && (options[opt_len] == '\0'
  567. /* or is it "comment=" thingy in fstab? */
  568. IF_FEATURE_MOUNT_FSTAB(IF_DESKTOP( || option_str[opt_len-1] == '=' ))
  569. )
  570. ) {
  571. unsigned long fl = mount_options[i];
  572. if (fl & BB_MS_INVERTED_VALUE)
  573. flags &= fl;
  574. else
  575. flags |= fl;
  576. goto found;
  577. }
  578. option_str += opt_len + 1;
  579. }
  580. // We did not recognize this option.
  581. // If "unrecognized" is not NULL, append option there.
  582. // Note that we should not append *empty* option -
  583. // in this case we want to pass NULL, not "", to "data"
  584. // parameter of mount(2) syscall.
  585. // This is crucial for filesystems that don't accept
  586. // any arbitrary mount options, like cgroup fs:
  587. // "mount -t cgroup none /mnt"
  588. if (options[0] && unrecognized) {
  589. // Add it to strflags, to pass on to kernel
  590. char *p = *unrecognized;
  591. unsigned len = p ? strlen(p) : 0;
  592. *unrecognized = p = xrealloc(p, len + strlen(options) + 2);
  593. // Comma separated if it's not the first one
  594. if (len) p[len++] = ',';
  595. strcpy(p + len, options);
  596. }
  597. found:
  598. if (!comma)
  599. break;
  600. // Advance to next option
  601. *comma = ',';
  602. options = ++comma;
  603. }
  604. return flags;
  605. }
  606. // Return a list of all block device backed filesystems
  607. static llist_t *get_block_backed_filesystems(void)
  608. {
  609. static const char filesystems[2][sizeof("/proc/filesystems")] ALIGN1 = {
  610. "/etc/filesystems",
  611. "/proc/filesystems",
  612. };
  613. char *fs, *buf;
  614. llist_t *list = NULL;
  615. int i;
  616. FILE *f;
  617. for (i = 0; i < 2; i++) {
  618. f = fopen_for_read(filesystems[i]);
  619. if (!f) continue;
  620. while ((buf = xmalloc_fgetline(f)) != NULL) {
  621. if (is_prefixed_with(buf, "nodev") && isspace(buf[5]))
  622. goto next;
  623. fs = skip_whitespace(buf);
  624. if (*fs == '#' || *fs == '*' || !*fs)
  625. goto next;
  626. llist_add_to_end(&list, xstrdup(fs));
  627. next:
  628. free(buf);
  629. }
  630. if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
  631. }
  632. return list;
  633. }
  634. #if ENABLE_FEATURE_CLEAN_UP
  635. static void delete_block_backed_filesystems(void)
  636. {
  637. llist_free(fslist, free);
  638. }
  639. #else
  640. void delete_block_backed_filesystems(void);
  641. #endif
  642. // Perform actual mount of specific filesystem at specific location.
  643. // NB: mp->xxx fields may be trashed on exit
  644. static int mount_it_now(struct mntent *mp, unsigned long vfsflags, char *filteropts)
  645. {
  646. int rc = 0;
  647. vfsflags &= ~(unsigned long)MOUNT_FAKEFLAGS;
  648. if (FAKE_IT) {
  649. if (verbose >= 2)
  650. bb_error_msg("would do mount('%s','%s','%s',0x%08lx,'%s')",
  651. mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
  652. vfsflags, filteropts);
  653. goto mtab;
  654. }
  655. // Mount, with fallback to read-only if necessary.
  656. for (;;) {
  657. errno = 0;
  658. rc = verbose_mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
  659. vfsflags, filteropts);
  660. // If mount failed, try
  661. // helper program mount.<mnt_type>
  662. if (HELPERS_ALLOWED && rc && mp->mnt_type) {
  663. char *args[8];
  664. int errno_save = errno;
  665. args[0] = xasprintf("mount.%s", mp->mnt_type);
  666. rc = 1;
  667. if (FAKE_IT)
  668. args[rc++] = (char *)"-f";
  669. if (ENABLE_FEATURE_MTAB_SUPPORT && !USE_MTAB)
  670. args[rc++] = (char *)"-n";
  671. args[rc++] = mp->mnt_fsname;
  672. args[rc++] = mp->mnt_dir;
  673. if (filteropts) {
  674. args[rc++] = (char *)"-o";
  675. args[rc++] = filteropts;
  676. }
  677. args[rc] = NULL;
  678. rc = spawn_and_wait(args);
  679. free(args[0]);
  680. if (!rc)
  681. break;
  682. errno = errno_save;
  683. }
  684. if (!rc || (vfsflags & MS_RDONLY) || (errno != EACCES && errno != EROFS))
  685. break;
  686. if (!(vfsflags & MS_SILENT))
  687. bb_error_msg("%s is write-protected, mounting read-only",
  688. mp->mnt_fsname);
  689. vfsflags |= MS_RDONLY;
  690. }
  691. // Abort entirely if permission denied.
  692. if (rc && errno == EPERM)
  693. bb_simple_error_msg_and_die(bb_msg_perm_denied_are_you_root);
  694. // If the mount was successful, and we're maintaining an old-style
  695. // mtab file by hand, add the new entry to it now.
  696. mtab:
  697. if (USE_MTAB && !rc && !(vfsflags & MS_REMOUNT)) {
  698. char *fsname;
  699. FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
  700. const char *option_str = mount_option_str;
  701. int i;
  702. if (!mountTable) {
  703. bb_simple_perror_msg(bb_path_mtab_file);
  704. goto ret;
  705. }
  706. // Add vfs string flags
  707. for (i = 0; mount_options[i] != MS_REMOUNT; i++) {
  708. if (mount_options[i] > 0 && (mount_options[i] & vfsflags))
  709. append_mount_options(&(mp->mnt_opts), option_str);
  710. option_str += strlen(option_str) + 1;
  711. }
  712. // Remove trailing / (if any) from directory we mounted on
  713. i = strlen(mp->mnt_dir) - 1;
  714. while (i > 0 && mp->mnt_dir[i] == '/')
  715. mp->mnt_dir[i--] = '\0';
  716. // Convert to canonical pathnames as needed
  717. mp->mnt_dir = bb_simplify_path(mp->mnt_dir);
  718. fsname = NULL;
  719. if (!mp->mnt_type || !*mp->mnt_type) { // bind mount
  720. mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname);
  721. mp->mnt_type = (char*)"bind";
  722. }
  723. mp->mnt_freq = mp->mnt_passno = 0;
  724. // Write and close
  725. #if ENABLE_FEATURE_MTAB_SUPPORT
  726. if (vfsflags & MS_MOVE)
  727. update_mtab_entry_on_move(mp);
  728. else
  729. #endif
  730. addmntent(mountTable, mp);
  731. endmntent(mountTable);
  732. if (ENABLE_FEATURE_CLEAN_UP) {
  733. free(mp->mnt_dir);
  734. free(fsname);
  735. }
  736. }
  737. ret:
  738. return rc;
  739. }
  740. #if ENABLE_FEATURE_MOUNT_NFS
  741. /*
  742. * Linux NFS mount
  743. * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
  744. *
  745. * Licensed under GPLv2, see file LICENSE in this source tree.
  746. *
  747. * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
  748. * numbers to be specified on the command line.
  749. *
  750. * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
  751. * Omit the call to connect() for Linux version 1.3.11 or later.
  752. *
  753. * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
  754. * Implemented the "bg", "fg" and "retry" mount options for NFS.
  755. *
  756. * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org>
  757. * - added Native Language Support
  758. *
  759. * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
  760. * plus NFSv3 stuff.
  761. */
  762. #define MOUNTPORT 635
  763. #define MNTPATHLEN 1024
  764. #define MNTNAMLEN 255
  765. #define FHSIZE 32
  766. #define FHSIZE3 64
  767. typedef char fhandle[FHSIZE];
  768. typedef struct {
  769. unsigned int fhandle3_len;
  770. char *fhandle3_val;
  771. } fhandle3;
  772. enum mountstat3 {
  773. MNT_OK = 0,
  774. MNT3ERR_PERM = 1,
  775. MNT3ERR_NOENT = 2,
  776. MNT3ERR_IO = 5,
  777. MNT3ERR_ACCES = 13,
  778. MNT3ERR_NOTDIR = 20,
  779. MNT3ERR_INVAL = 22,
  780. MNT3ERR_NAMETOOLONG = 63,
  781. MNT3ERR_NOTSUPP = 10004,
  782. MNT3ERR_SERVERFAULT = 10006,
  783. };
  784. typedef enum mountstat3 mountstat3;
  785. struct fhstatus {
  786. unsigned int fhs_status;
  787. union {
  788. fhandle fhs_fhandle;
  789. } fhstatus_u;
  790. };
  791. typedef struct fhstatus fhstatus;
  792. struct mountres3_ok {
  793. fhandle3 fhandle;
  794. struct {
  795. unsigned int auth_flavours_len;
  796. char *auth_flavours_val;
  797. } auth_flavours;
  798. };
  799. typedef struct mountres3_ok mountres3_ok;
  800. struct mountres3 {
  801. mountstat3 fhs_status;
  802. union {
  803. mountres3_ok mountinfo;
  804. } mountres3_u;
  805. };
  806. typedef struct mountres3 mountres3;
  807. typedef char *dirpath;
  808. typedef char *name;
  809. typedef struct mountbody *mountlist;
  810. struct mountbody {
  811. name ml_hostname;
  812. dirpath ml_directory;
  813. mountlist ml_next;
  814. };
  815. typedef struct mountbody mountbody;
  816. typedef struct groupnode *groups;
  817. struct groupnode {
  818. name gr_name;
  819. groups gr_next;
  820. };
  821. typedef struct groupnode groupnode;
  822. typedef struct exportnode *exports;
  823. struct exportnode {
  824. dirpath ex_dir;
  825. groups ex_groups;
  826. exports ex_next;
  827. };
  828. typedef struct exportnode exportnode;
  829. struct ppathcnf {
  830. int pc_link_max;
  831. short pc_max_canon;
  832. short pc_max_input;
  833. short pc_name_max;
  834. short pc_path_max;
  835. short pc_pipe_buf;
  836. uint8_t pc_vdisable;
  837. char pc_xxx;
  838. short pc_mask[2];
  839. };
  840. typedef struct ppathcnf ppathcnf;
  841. #define MOUNTPROG 100005
  842. #define MOUNTVERS 1
  843. #define MOUNTPROC_NULL 0
  844. #define MOUNTPROC_MNT 1
  845. #define MOUNTPROC_DUMP 2
  846. #define MOUNTPROC_UMNT 3
  847. #define MOUNTPROC_UMNTALL 4
  848. #define MOUNTPROC_EXPORT 5
  849. #define MOUNTPROC_EXPORTALL 6
  850. #define MOUNTVERS_POSIX 2
  851. #define MOUNTPROC_PATHCONF 7
  852. #define MOUNT_V3 3
  853. #define MOUNTPROC3_NULL 0
  854. #define MOUNTPROC3_MNT 1
  855. #define MOUNTPROC3_DUMP 2
  856. #define MOUNTPROC3_UMNT 3
  857. #define MOUNTPROC3_UMNTALL 4
  858. #define MOUNTPROC3_EXPORT 5
  859. enum {
  860. #ifndef NFS_FHSIZE
  861. NFS_FHSIZE = 32,
  862. #endif
  863. #ifndef NFS_PORT
  864. NFS_PORT = 2049
  865. #endif
  866. };
  867. /*
  868. * We want to be able to compile mount on old kernels in such a way
  869. * that the binary will work well on more recent kernels.
  870. * Thus, if necessary we teach nfsmount.c the structure of new fields
  871. * that will come later.
  872. *
  873. * Moreover, the new kernel includes conflict with glibc includes
  874. * so it is easiest to ignore the kernel altogether (at compile time).
  875. */
  876. struct nfs2_fh {
  877. char data[32];
  878. };
  879. struct nfs3_fh {
  880. unsigned short size;
  881. unsigned char data[64];
  882. };
  883. struct nfs_mount_data {
  884. int version; /* 1 */
  885. int fd; /* 1 */
  886. struct nfs2_fh old_root; /* 1 */
  887. int flags; /* 1 */
  888. int rsize; /* 1 */
  889. int wsize; /* 1 */
  890. int timeo; /* 1 */
  891. int retrans; /* 1 */
  892. int acregmin; /* 1 */
  893. int acregmax; /* 1 */
  894. int acdirmin; /* 1 */
  895. int acdirmax; /* 1 */
  896. struct sockaddr_in addr; /* 1 */
  897. char hostname[256]; /* 1 */
  898. int namlen; /* 2 */
  899. unsigned int bsize; /* 3 */
  900. struct nfs3_fh root; /* 4 */
  901. };
  902. /* bits in the flags field */
  903. enum {
  904. NFS_MOUNT_SOFT = 0x0001, /* 1 */
  905. NFS_MOUNT_INTR = 0x0002, /* 1 */
  906. NFS_MOUNT_SECURE = 0x0004, /* 1 */
  907. NFS_MOUNT_POSIX = 0x0008, /* 1 */
  908. NFS_MOUNT_NOCTO = 0x0010, /* 1 */
  909. NFS_MOUNT_NOAC = 0x0020, /* 1 */
  910. NFS_MOUNT_TCP = 0x0040, /* 2 */
  911. NFS_MOUNT_VER3 = 0x0080, /* 3 */
  912. NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
  913. NFS_MOUNT_NONLM = 0x0200, /* 3 */
  914. NFS_MOUNT_NOACL = 0x0800, /* 4 */
  915. NFS_MOUNT_NORDIRPLUS = 0x4000
  916. };
  917. /*
  918. * We need to translate between nfs status return values and
  919. * the local errno values which may not be the same.
  920. *
  921. * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
  922. * "after #include <errno.h> the symbol errno is reserved for any use,
  923. * it cannot even be used as a struct tag or field name".
  924. */
  925. #ifndef EDQUOT
  926. # define EDQUOT ENOSPC
  927. #endif
  928. /* Convert each NFSERR_BLAH into EBLAH */
  929. static const uint8_t nfs_err_stat[] ALIGN1 = {
  930. 1, 2, 5, 6, 13, 17,
  931. 19, 20, 21, 22, 27, 28,
  932. 30, 63, 66, 69, 70, 71
  933. };
  934. #if ( \
  935. EPERM | ENOENT | EIO | ENXIO | EACCES| EEXIST | \
  936. ENODEV| ENOTDIR | EISDIR | EINVAL| EFBIG | ENOSPC | \
  937. EROFS | ENAMETOOLONG| ENOTEMPTY| EDQUOT| ESTALE| EREMOTE) < 256
  938. typedef uint8_t nfs_err_type;
  939. #else
  940. typedef uint16_t nfs_err_type;
  941. #endif
  942. static const nfs_err_type nfs_err_errnum[] ALIGN2 = {
  943. EPERM , ENOENT , EIO , ENXIO , EACCES, EEXIST,
  944. ENODEV, ENOTDIR , EISDIR , EINVAL, EFBIG , ENOSPC,
  945. EROFS , ENAMETOOLONG, ENOTEMPTY, EDQUOT, ESTALE, EREMOTE
  946. };
  947. static char *nfs_strerror(int status)
  948. {
  949. int i;
  950. for (i = 0; i < ARRAY_SIZE(nfs_err_stat); i++) {
  951. if (nfs_err_stat[i] == status)
  952. return strerror(nfs_err_errnum[i]);
  953. }
  954. return xasprintf("unknown nfs status return value: %d", status);
  955. }
  956. static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
  957. {
  958. return xdr_opaque(xdrs, objp, FHSIZE);
  959. }
  960. static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
  961. {
  962. if (!xdr_u_int(xdrs, &objp->fhs_status))
  963. return FALSE;
  964. if (objp->fhs_status == 0)
  965. return xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle);
  966. return TRUE;
  967. }
  968. static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp)
  969. {
  970. return xdr_string(xdrs, objp, MNTPATHLEN);
  971. }
  972. static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
  973. {
  974. return xdr_bytes(xdrs, (char **)&objp->fhandle3_val,
  975. (unsigned int *) &objp->fhandle3_len,
  976. FHSIZE3);
  977. }
  978. static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
  979. {
  980. if (!xdr_fhandle3(xdrs, &objp->fhandle))
  981. return FALSE;
  982. return xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val),
  983. &(objp->auth_flavours.auth_flavours_len),
  984. ~0,
  985. sizeof(int),
  986. (xdrproc_t) xdr_int);
  987. }
  988. static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
  989. {
  990. return xdr_enum(xdrs, (enum_t *) objp);
  991. }
  992. static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
  993. {
  994. if (!xdr_mountstat3(xdrs, &objp->fhs_status))
  995. return FALSE;
  996. if (objp->fhs_status == MNT_OK)
  997. return xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo);
  998. return TRUE;
  999. }
  1000. #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
  1001. /*
  1002. * Unfortunately, the kernel prints annoying console messages
  1003. * in case of an unexpected nfs mount version (instead of
  1004. * just returning some error). Therefore we'll have to try
  1005. * and figure out what version the kernel expects.
  1006. *
  1007. * Variables:
  1008. * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
  1009. * NFS_MOUNT_VERSION: these nfsmount sources at compile time
  1010. * nfs_mount_version: version this source and running kernel can handle
  1011. */
  1012. static void
  1013. find_kernel_nfs_mount_version(void)
  1014. {
  1015. int kernel_version;
  1016. if (nfs_mount_version)
  1017. return;
  1018. nfs_mount_version = 4; /* default */
  1019. kernel_version = get_linux_version_code();
  1020. if (kernel_version) {
  1021. if (kernel_version < KERNEL_VERSION(2,2,18))
  1022. nfs_mount_version = 3;
  1023. /* else v4 since 2.3.99pre4 */
  1024. }
  1025. }
  1026. static void
  1027. get_mountport(struct pmap *pm_mnt,
  1028. struct sockaddr_in *server_addr,
  1029. long unsigned prog,
  1030. long unsigned version,
  1031. long unsigned proto,
  1032. long unsigned port)
  1033. {
  1034. struct pmaplist *pmap;
  1035. server_addr->sin_port = PMAPPORT;
  1036. /* glibc 2.4 (still) has pmap_getmaps(struct sockaddr_in *).
  1037. * I understand it like "IPv6 for this is not 100% ready" */
  1038. pmap = pmap_getmaps(server_addr);
  1039. if (version > MAX_NFSPROT)
  1040. version = MAX_NFSPROT;
  1041. if (!prog)
  1042. prog = MOUNTPROG;
  1043. pm_mnt->pm_prog = prog;
  1044. pm_mnt->pm_vers = version;
  1045. pm_mnt->pm_prot = proto;
  1046. pm_mnt->pm_port = port;
  1047. while (pmap) {
  1048. if (pmap->pml_map.pm_prog != prog)
  1049. goto next;
  1050. if (!version && pm_mnt->pm_vers > pmap->pml_map.pm_vers)
  1051. goto next;
  1052. if (version > 2 && pmap->pml_map.pm_vers != version)
  1053. goto next;
  1054. if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
  1055. goto next;
  1056. if (pmap->pml_map.pm_vers > MAX_NFSPROT
  1057. || (proto && pm_mnt->pm_prot && pmap->pml_map.pm_prot != proto)
  1058. || (port && pmap->pml_map.pm_port != port)
  1059. ) {
  1060. goto next;
  1061. }
  1062. memcpy(pm_mnt, &pmap->pml_map, sizeof(*pm_mnt));
  1063. next:
  1064. pmap = pmap->pml_next;
  1065. }
  1066. if (!pm_mnt->pm_vers)
  1067. pm_mnt->pm_vers = MOUNTVERS;
  1068. if (!pm_mnt->pm_port)
  1069. pm_mnt->pm_port = MOUNTPORT;
  1070. if (!pm_mnt->pm_prot)
  1071. pm_mnt->pm_prot = IPPROTO_TCP;
  1072. }
  1073. #if BB_MMU
  1074. static int daemonize(void)
  1075. {
  1076. int pid = fork();
  1077. if (pid < 0) /* error */
  1078. return -errno;
  1079. if (pid > 0) /* parent */
  1080. return 0;
  1081. /* child */
  1082. close(0);
  1083. xopen(bb_dev_null, O_RDWR);
  1084. xdup2(0, 1);
  1085. xdup2(0, 2);
  1086. setsid();
  1087. openlog(applet_name, LOG_PID, LOG_DAEMON);
  1088. logmode = LOGMODE_SYSLOG;
  1089. return 1;
  1090. }
  1091. #else
  1092. static inline int daemonize(void)
  1093. {
  1094. return -ENOSYS;
  1095. }
  1096. #endif
  1097. /* TODO */
  1098. static inline int we_saw_this_host_before(const char *hostname UNUSED_PARAM)
  1099. {
  1100. return 0;
  1101. }
  1102. /* RPC strerror analogs are terminally idiotic:
  1103. * *mandatory* prefix and \n at end.
  1104. * This hopefully helps. Usage:
  1105. * error_msg_rpc(clnt_*error*(" ")) */
  1106. static void error_msg_rpc(const char *msg)
  1107. {
  1108. int len;
  1109. while (msg[0] == ' ' || msg[0] == ':') msg++;
  1110. len = strlen(msg);
  1111. while (len && msg[len-1] == '\n') len--;
  1112. bb_error_msg("%.*s", len, msg);
  1113. }
  1114. /* NB: mp->xxx fields may be trashed on exit */
  1115. static NOINLINE int nfsmount(struct mntent *mp, unsigned long vfsflags, char *filteropts)
  1116. {
  1117. CLIENT *mclient;
  1118. char *hostname;
  1119. char *pathname;
  1120. char *mounthost;
  1121. /* prior to 2.6.23, kernel took NFS options in a form of this struct
  1122. * only. 2.6.23+ looks at data->version, and if it's not 1..6,
  1123. * then data pointer is interpreted as a string. */
  1124. struct nfs_mount_data data;
  1125. char *opt;
  1126. char *tokstate;
  1127. struct hostent *hp;
  1128. struct sockaddr_in server_addr;
  1129. struct sockaddr_in mount_server_addr;
  1130. int msock, fsock;
  1131. union {
  1132. struct fhstatus nfsv2;
  1133. struct mountres3 nfsv3;
  1134. } status;
  1135. int daemonized;
  1136. char *s;
  1137. int port;
  1138. int mountport;
  1139. int proto;
  1140. #if BB_MMU
  1141. smallint bg = 0;
  1142. #else
  1143. enum { bg = 0 };
  1144. #endif
  1145. int retry;
  1146. int mountprog;
  1147. int mountvers;
  1148. int nfsprog;
  1149. int nfsvers;
  1150. int retval;
  1151. /* these all are one-bit really. gcc 4.3.1 likes this combination: */
  1152. smallint tcp;
  1153. smallint soft;
  1154. int intr;
  1155. int posix;
  1156. int nocto;
  1157. int noac;
  1158. int nordirplus;
  1159. int nolock;
  1160. int noacl;
  1161. find_kernel_nfs_mount_version();
  1162. daemonized = 0;
  1163. mounthost = NULL;
  1164. retval = ETIMEDOUT;
  1165. msock = fsock = -1;
  1166. mclient = NULL;
  1167. /* NB: hostname, mounthost, filteropts must be free()d prior to return */
  1168. filteropts = xstrdup(filteropts); /* going to trash it later... */
  1169. hostname = xstrdup(mp->mnt_fsname);
  1170. /* mount_main() guarantees that ':' is there */
  1171. s = strchr(hostname, ':');
  1172. pathname = s + 1;
  1173. *s = '\0';
  1174. /* Ignore all but first hostname in replicated mounts
  1175. * until they can be fully supported. (mack@sgi.com) */
  1176. s = strchr(hostname, ',');
  1177. if (s) {
  1178. *s = '\0';
  1179. bb_simple_error_msg("warning: multiple hostnames not supported");
  1180. }
  1181. server_addr.sin_family = AF_INET;
  1182. if (!inet_aton(hostname, &server_addr.sin_addr)) {
  1183. hp = gethostbyname(hostname);
  1184. if (hp == NULL) {
  1185. bb_simple_herror_msg(hostname);
  1186. goto fail;
  1187. }
  1188. if (hp->h_length != (int)sizeof(struct in_addr)) {
  1189. bb_simple_error_msg_and_die("only IPv4 is supported");
  1190. }
  1191. memcpy(&server_addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
  1192. }
  1193. memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr));
  1194. /* add IP address to mtab options for use when unmounting */
  1195. if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */
  1196. mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr));
  1197. } else {
  1198. char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts,
  1199. mp->mnt_opts[0] ? "," : "",
  1200. inet_ntoa(server_addr.sin_addr));
  1201. free(mp->mnt_opts);
  1202. mp->mnt_opts = tmp;
  1203. }
  1204. /* Set default options.
  1205. * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
  1206. * let the kernel decide.
  1207. * timeo is filled in after we know whether it'll be TCP or UDP. */
  1208. memset(&data, 0, sizeof(data));
  1209. data.retrans = 3;
  1210. data.acregmin = 3;
  1211. data.acregmax = 60;
  1212. data.acdirmin = 30;
  1213. data.acdirmax = 60;
  1214. data.namlen = NAME_MAX;
  1215. soft = 0;
  1216. intr = 0;
  1217. posix = 0;
  1218. nocto = 0;
  1219. nolock = 0;
  1220. noac = 0;
  1221. nordirplus = 0;
  1222. noacl = 0;
  1223. retry = 10000; /* 10000 minutes ~ 1 week */
  1224. tcp = 1; /* nfs-utils uses tcp per default */
  1225. mountprog = MOUNTPROG;
  1226. mountvers = 0;
  1227. port = 0;
  1228. mountport = 0;
  1229. nfsprog = 100003;
  1230. nfsvers = 0;
  1231. /* parse options */
  1232. if (filteropts) for (opt = strtok_r(filteropts, ",", &tokstate); opt; opt = strtok_r(NULL, ",", &tokstate)) {
  1233. char *opteq = strchr(opt, '=');
  1234. if (opteq) {
  1235. int val, idx;
  1236. static const char options[] ALIGN1 =
  1237. /* 0 */ "rsize\0"
  1238. /* 1 */ "wsize\0"
  1239. /* 2 */ "timeo\0"
  1240. /* 3 */ "retrans\0"
  1241. /* 4 */ "acregmin\0"
  1242. /* 5 */ "acregmax\0"
  1243. /* 6 */ "acdirmin\0"
  1244. /* 7 */ "acdirmax\0"
  1245. /* 8 */ "actimeo\0"
  1246. /* 9 */ "retry\0"
  1247. /* 10 */ "port\0"
  1248. /* 11 */ "mountport\0"
  1249. /* 12 */ "mounthost\0"
  1250. /* 13 */ "mountprog\0"
  1251. /* 14 */ "mountvers\0"
  1252. /* 15 */ "nfsprog\0"
  1253. /* 16 */ "nfsvers\0"
  1254. /* 17 */ "vers\0"
  1255. /* 18 */ "proto\0"
  1256. /* 19 */ "namlen\0"
  1257. /* 20 */ "addr\0";
  1258. *opteq++ = '\0';
  1259. idx = index_in_strings(options, opt);
  1260. switch (idx) {
  1261. case 12: // "mounthost"
  1262. mounthost = xstrndup(opteq,
  1263. strcspn(opteq, " \t\n\r,"));
  1264. continue;
  1265. case 18: // "proto"
  1266. if (is_prefixed_with(opteq, "tcp"))
  1267. tcp = 1;
  1268. else if (is_prefixed_with(opteq, "udp"))
  1269. tcp = 0;
  1270. else
  1271. bb_simple_error_msg("warning: unrecognized proto= option");
  1272. continue;
  1273. case 20: // "addr" - ignore
  1274. continue;
  1275. case -1: // unknown
  1276. if (vfsflags & MS_REMOUNT)
  1277. continue;
  1278. }
  1279. val = xatoi_positive(opteq);
  1280. switch (idx) {
  1281. case 0: // "rsize"
  1282. data.rsize = val;
  1283. continue;
  1284. case 1: // "wsize"
  1285. data.wsize = val;
  1286. continue;
  1287. case 2: // "timeo"
  1288. data.timeo = val;
  1289. continue;
  1290. case 3: // "retrans"
  1291. data.retrans = val;
  1292. continue;
  1293. case 4: // "acregmin"
  1294. data.acregmin = val;
  1295. continue;
  1296. case 5: // "acregmax"
  1297. data.acregmax = val;
  1298. continue;
  1299. case 6: // "acdirmin"
  1300. data.acdirmin = val;
  1301. continue;
  1302. case 7: // "acdirmax"
  1303. data.acdirmax = val;
  1304. continue;
  1305. case 8: // "actimeo"
  1306. data.acregmin = val;
  1307. data.acregmax = val;
  1308. data.acdirmin = val;
  1309. data.acdirmax = val;
  1310. continue;
  1311. case 9: // "retry"
  1312. retry = val;
  1313. continue;
  1314. case 10: // "port"
  1315. port = val;
  1316. continue;
  1317. case 11: // "mountport"
  1318. mountport = val;
  1319. continue;
  1320. case 13: // "mountprog"
  1321. mountprog = val;
  1322. continue;
  1323. case 14: // "mountvers"
  1324. mountvers = val;
  1325. continue;
  1326. case 15: // "nfsprog"
  1327. nfsprog = val;
  1328. continue;
  1329. case 16: // "nfsvers"
  1330. case 17: // "vers"
  1331. nfsvers = val;
  1332. continue;
  1333. case 19: // "namlen"
  1334. //if (nfs_mount_version >= 2)
  1335. data.namlen = val;
  1336. //else
  1337. // bb_error_msg("warning: option namlen is not supported\n");
  1338. continue;
  1339. default:
  1340. bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
  1341. goto fail;
  1342. }
  1343. }
  1344. else { /* not of the form opt=val */
  1345. static const char options[] ALIGN1 =
  1346. "bg\0"
  1347. "fg\0"
  1348. "soft\0"
  1349. "hard\0"
  1350. "intr\0"
  1351. "posix\0"
  1352. "cto\0"
  1353. "ac\0"
  1354. "tcp\0"
  1355. "udp\0"
  1356. "lock\0"
  1357. "rdirplus\0"
  1358. "acl\0";
  1359. int val = 1;
  1360. if (is_prefixed_with(opt, "no")) {
  1361. val = 0;
  1362. opt += 2;
  1363. }
  1364. switch (index_in_strings(options, opt)) {
  1365. case 0: // "bg"
  1366. #if BB_MMU
  1367. bg = val;
  1368. #endif
  1369. break;
  1370. case 1: // "fg"
  1371. #if BB_MMU
  1372. bg = !val;
  1373. #endif
  1374. break;
  1375. case 2: // "soft"
  1376. soft = val;
  1377. break;
  1378. case 3: // "hard"
  1379. soft = !val;
  1380. break;
  1381. case 4: // "intr"
  1382. intr = val;
  1383. break;
  1384. case 5: // "posix"
  1385. posix = val;
  1386. break;
  1387. case 6: // "cto"
  1388. nocto = !val;
  1389. break;
  1390. case 7: // "ac"
  1391. noac = !val;
  1392. break;
  1393. case 8: // "tcp"
  1394. tcp = val;
  1395. break;
  1396. case 9: // "udp"
  1397. tcp = !val;
  1398. break;
  1399. case 10: // "lock"
  1400. if (nfs_mount_version >= 3)
  1401. nolock = !val;
  1402. else
  1403. bb_simple_error_msg("warning: option nolock is not supported");
  1404. break;
  1405. case 11: //rdirplus
  1406. nordirplus = !val;
  1407. break;
  1408. case 12: // acl
  1409. noacl = !val;
  1410. break;
  1411. default:
  1412. bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
  1413. goto fail;
  1414. }
  1415. }
  1416. }
  1417. proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
  1418. data.flags = (soft ? NFS_MOUNT_SOFT : 0)
  1419. | (intr ? NFS_MOUNT_INTR : 0)
  1420. | (posix ? NFS_MOUNT_POSIX : 0)
  1421. | (nocto ? NFS_MOUNT_NOCTO : 0)
  1422. | (noac ? NFS_MOUNT_NOAC : 0)
  1423. | (nordirplus ? NFS_MOUNT_NORDIRPLUS : 0)
  1424. | (noacl ? NFS_MOUNT_NOACL : 0);
  1425. if (nfs_mount_version >= 2)
  1426. data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
  1427. if (nfs_mount_version >= 3)
  1428. data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
  1429. if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
  1430. bb_error_msg("NFSv%d not supported", nfsvers);
  1431. goto fail;
  1432. }
  1433. if (nfsvers && !mountvers)
  1434. mountvers = (nfsvers < 3) ? 1 : nfsvers;
  1435. if (nfsvers && nfsvers < mountvers) {
  1436. mountvers = nfsvers;
  1437. }
  1438. /* Adjust options if none specified */
  1439. if (!data.timeo)
  1440. data.timeo = tcp ? 70 : 7;
  1441. data.version = nfs_mount_version;
  1442. if (vfsflags & MS_REMOUNT)
  1443. goto do_mount;
  1444. /*
  1445. * If the previous mount operation on the same host was
  1446. * backgrounded, and the "bg" for this mount is also set,
  1447. * give up immediately, to avoid the initial timeout.
  1448. */
  1449. if (bg && we_saw_this_host_before(hostname)) {
  1450. daemonized = daemonize();
  1451. if (daemonized <= 0) { /* parent or error */
  1452. retval = -daemonized;
  1453. goto ret;
  1454. }
  1455. }
  1456. /* Create mount daemon client */
  1457. /* See if the nfs host = mount host. */
  1458. if (mounthost) {
  1459. if (mounthost[0] >= '0' && mounthost[0] <= '9') {
  1460. mount_server_addr.sin_family = AF_INET;
  1461. mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
  1462. } else {
  1463. hp = gethostbyname(mounthost);
  1464. if (hp == NULL) {
  1465. bb_simple_herror_msg(mounthost);
  1466. goto fail;
  1467. }
  1468. if (hp->h_length != (int)sizeof(struct in_addr)) {
  1469. bb_simple_error_msg_and_die("only IPv4 is supported");
  1470. }
  1471. mount_server_addr.sin_family = AF_INET;
  1472. memcpy(&mount_server_addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
  1473. }
  1474. }
  1475. /*
  1476. * The following loop implements the mount retries. When the mount
  1477. * times out, and the "bg" option is set, we background ourself
  1478. * and continue trying.
  1479. *
  1480. * The case where the mount point is not present and the "bg"
  1481. * option is set, is treated as a timeout. This is done to
  1482. * support nested mounts.
  1483. *
  1484. * The "retry" count specified by the user is the number of
  1485. * minutes to retry before giving up.
  1486. */
  1487. {
  1488. struct timeval total_timeout;
  1489. struct timeval retry_timeout;
  1490. struct pmap pm_mnt;
  1491. time_t t;
  1492. time_t prevt;
  1493. time_t timeout;
  1494. retry_timeout.tv_sec = 3;
  1495. retry_timeout.tv_usec = 0;
  1496. total_timeout.tv_sec = 20;
  1497. total_timeout.tv_usec = 0;
  1498. /* FIXME: use monotonic()? */
  1499. timeout = time(NULL) + 60 * retry;
  1500. prevt = 0;
  1501. t = 30;
  1502. retry:
  1503. /* Be careful not to use too many CPU cycles */
  1504. if (t - prevt < 30)
  1505. sleep(30);
  1506. get_mountport(&pm_mnt, &mount_server_addr,
  1507. mountprog,
  1508. mountvers,
  1509. proto,
  1510. mountport);
  1511. nfsvers = (pm_mnt.pm_vers < 2) ? 2 : pm_mnt.pm_vers;
  1512. /* contact the mount daemon via TCP */
  1513. mount_server_addr.sin_port = htons(pm_mnt.pm_port);
  1514. msock = RPC_ANYSOCK;
  1515. switch (pm_mnt.pm_prot) {
  1516. case IPPROTO_UDP:
  1517. mclient = clntudp_create(&mount_server_addr,
  1518. pm_mnt.pm_prog,
  1519. pm_mnt.pm_vers,
  1520. retry_timeout,
  1521. &msock);
  1522. if (mclient)
  1523. break;
  1524. mount_server_addr.sin_port = htons(pm_mnt.pm_port);
  1525. msock = RPC_ANYSOCK;
  1526. case IPPROTO_TCP:
  1527. mclient = clnttcp_create(&mount_server_addr,
  1528. pm_mnt.pm_prog,
  1529. pm_mnt.pm_vers,
  1530. &msock, 0, 0);
  1531. break;
  1532. default:
  1533. mclient = NULL;
  1534. }
  1535. if (!mclient) {
  1536. if (!daemonized && prevt == 0)
  1537. error_msg_rpc(clnt_spcreateerror(" "));
  1538. } else {
  1539. enum clnt_stat clnt_stat;
  1540. /* Try to mount hostname:pathname */
  1541. mclient->cl_auth = authunix_create_default();
  1542. /* Make pointers in xdr_mountres3 NULL so
  1543. * that xdr_array allocates memory for us
  1544. */
  1545. memset(&status, 0, sizeof(status));
  1546. if (pm_mnt.pm_vers == 3)
  1547. clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
  1548. (xdrproc_t) xdr_dirpath,
  1549. (caddr_t) &pathname,
  1550. (xdrproc_t) xdr_mountres3,
  1551. (caddr_t) &status,
  1552. total_timeout);
  1553. else
  1554. clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
  1555. (xdrproc_t) xdr_dirpath,
  1556. (caddr_t) &pathname,
  1557. (xdrproc_t) xdr_fhstatus,
  1558. (caddr_t) &status,
  1559. total_timeout);
  1560. if (clnt_stat == RPC_SUCCESS)
  1561. goto prepare_kernel_data; /* we're done */
  1562. if (errno != ECONNREFUSED) {
  1563. error_msg_rpc(clnt_sperror(mclient, " "));
  1564. goto fail; /* don't retry */
  1565. }
  1566. /* Connection refused */
  1567. if (!daemonized && prevt == 0) /* print just once */
  1568. error_msg_rpc(clnt_sperror(mclient, " "));
  1569. auth_destroy(mclient->cl_auth);
  1570. clnt_destroy(mclient);
  1571. mclient = NULL;
  1572. close(msock);
  1573. msock = -1;
  1574. }
  1575. /* Timeout. We are going to retry... maybe */
  1576. if (!bg)
  1577. goto fail;
  1578. if (!daemonized) {
  1579. daemonized = daemonize();
  1580. if (daemonized <= 0) { /* parent or error */
  1581. retval = -daemonized;
  1582. goto ret;
  1583. }
  1584. }
  1585. prevt = t;
  1586. t = time(NULL);
  1587. if (t >= timeout)
  1588. /* TODO error message */
  1589. goto fail;
  1590. goto retry;
  1591. }
  1592. prepare_kernel_data:
  1593. if (nfsvers == 2) {
  1594. if (status.nfsv2.fhs_status != 0) {
  1595. bb_error_msg("%s:%s failed, reason given by server: %s",
  1596. hostname, pathname,
  1597. nfs_strerror(status.nfsv2.fhs_status));
  1598. goto fail;
  1599. }
  1600. memcpy(data.root.data,
  1601. (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
  1602. NFS_FHSIZE);
  1603. data.root.size = NFS_FHSIZE;
  1604. memcpy(data.old_root.data,
  1605. (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
  1606. NFS_FHSIZE);
  1607. } else {
  1608. fhandle3 *my_fhandle;
  1609. if (status.nfsv3.fhs_status != 0) {
  1610. bb_error_msg("%s:%s failed, reason given by server: %s",
  1611. hostname, pathname,
  1612. nfs_strerror(status.nfsv3.fhs_status));
  1613. goto fail;
  1614. }
  1615. my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
  1616. memset(data.old_root.data, 0, NFS_FHSIZE);
  1617. memset(&data.root, 0, sizeof(data.root));
  1618. data.root.size = my_fhandle->fhandle3_len;
  1619. memcpy(data.root.data,
  1620. (char *) my_fhandle->fhandle3_val,
  1621. my_fhandle->fhandle3_len);
  1622. data.flags |= NFS_MOUNT_VER3;
  1623. }
  1624. /* Create nfs socket for kernel */
  1625. if (tcp) {
  1626. if (nfs_mount_version < 3) {
  1627. bb_simple_error_msg("NFS over TCP is not supported");
  1628. goto fail;
  1629. }
  1630. fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  1631. } else
  1632. fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  1633. if (fsock < 0) {
  1634. bb_simple_perror_msg("nfs socket");
  1635. goto fail;
  1636. }
  1637. if (bindresvport(fsock, 0) < 0) {
  1638. bb_simple_perror_msg("nfs bindresvport");
  1639. goto fail;
  1640. }
  1641. if (port == 0) {
  1642. server_addr.sin_port = PMAPPORT;
  1643. port = pmap_getport(&server_addr, nfsprog, nfsvers,
  1644. tcp ? IPPROTO_TCP : IPPROTO_UDP);
  1645. if (port == 0)
  1646. port = NFS_PORT;
  1647. }
  1648. server_addr.sin_port = htons(port);
  1649. /* Prepare data structure for kernel */
  1650. data.fd = fsock;
  1651. memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
  1652. strncpy(data.hostname, hostname, sizeof(data.hostname));
  1653. /* Clean up */
  1654. auth_destroy(mclient->cl_auth);
  1655. clnt_destroy(mclient);
  1656. close(msock);
  1657. msock = -1;
  1658. if (bg) {
  1659. /* We must wait until mount directory is available */
  1660. struct stat statbuf;
  1661. int delay = 1;
  1662. while (stat(mp->mnt_dir, &statbuf) == -1) {
  1663. if (!daemonized) {
  1664. daemonized = daemonize();
  1665. if (daemonized <= 0) { /* parent or error */
  1666. /* FIXME: parent doesn't close fsock - ??! */
  1667. retval = -daemonized;
  1668. goto ret;
  1669. }
  1670. }
  1671. sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */
  1672. delay *= 2;
  1673. if (delay > 30)
  1674. delay = 30;
  1675. }
  1676. }
  1677. /* Perform actual mount */
  1678. do_mount:
  1679. retval = mount_it_now(mp, vfsflags, (char*)&data);
  1680. goto ret;
  1681. /* Abort */
  1682. fail:
  1683. if (msock >= 0) {
  1684. if (mclient) {
  1685. auth_destroy(mclient->cl_auth);
  1686. clnt_destroy(mclient);
  1687. }
  1688. close(msock);
  1689. }
  1690. if (fsock >= 0)
  1691. close(fsock);
  1692. ret:
  1693. free(hostname);
  1694. free(mounthost);
  1695. free(filteropts);
  1696. return retval;
  1697. }
  1698. #else // !ENABLE_FEATURE_MOUNT_NFS
  1699. /* Linux 2.6.23+ supports nfs mounts with options passed as a string.
  1700. * For older kernels, you must build busybox with ENABLE_FEATURE_MOUNT_NFS.
  1701. * (However, note that then you lose any chances that NFS over IPv6 would work).
  1702. */
  1703. static int nfsmount(struct mntent *mp, unsigned long vfsflags, char *filteropts)
  1704. {
  1705. len_and_sockaddr *lsa;
  1706. char *opts;
  1707. char *end;
  1708. char *dotted;
  1709. int ret;
  1710. # if ENABLE_FEATURE_IPV6
  1711. end = strchr(mp->mnt_fsname, ']');
  1712. if (end && end[1] == ':')
  1713. end++;
  1714. else
  1715. # endif
  1716. /* mount_main() guarantees that ':' is there */
  1717. end = strchr(mp->mnt_fsname, ':');
  1718. *end = '\0';
  1719. lsa = xhost2sockaddr(mp->mnt_fsname, /*port:*/ 0);
  1720. *end = ':';
  1721. dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
  1722. if (ENABLE_FEATURE_CLEAN_UP) free(lsa);
  1723. opts = xasprintf("%s%saddr=%s",
  1724. filteropts ? filteropts : "",
  1725. filteropts ? "," : "",
  1726. dotted
  1727. );
  1728. if (ENABLE_FEATURE_CLEAN_UP) free(dotted);
  1729. ret = mount_it_now(mp, vfsflags, opts);
  1730. if (ENABLE_FEATURE_CLEAN_UP) free(opts);
  1731. return ret;
  1732. }
  1733. #endif // !ENABLE_FEATURE_MOUNT_NFS
  1734. // Find "...,NAME=NUM,..." in the option string, remove "NAME=NUM" option
  1735. // and return NUM.
  1736. // Return 0 if not found.
  1737. // All instances must be parsed and removed (for example, since kernel 5.4
  1738. // squashfs: Unknown parameter 'sizelimit'
  1739. // will result if loopback mount option "sizelimit=NNN" is not removed
  1740. // and squashfs sees it in option string).
  1741. static unsigned long long cut_out_ull_opt(char *opts, const char *name_eq)
  1742. {
  1743. unsigned long long ret = 0;
  1744. if (!opts) // allow NULL opts (simplifies callers' work)
  1745. return ret;
  1746. for (;;) {
  1747. char *end;
  1748. char *opt;
  1749. // Find comma-delimited "NAME="
  1750. for (;;) {
  1751. opt = strstr(opts, name_eq);
  1752. if (!opt)
  1753. return ret;
  1754. if (opt == opts)
  1755. break; // found it (it's first opt)
  1756. if (opt[-1] == ',') {
  1757. opts = opt - 1;
  1758. break; // found it (it's not a first opt)
  1759. }
  1760. // False positive like "VNAME=", we are at "N".
  1761. // - skip it, loop back to searching
  1762. opts = opt + 1;
  1763. }
  1764. ret = bb_strtoull(opt + strlen(name_eq), &end, 0);
  1765. if (errno && errno != EINVAL) {
  1766. err:
  1767. bb_error_msg_and_die("bad option '%s'", opt);
  1768. }
  1769. if (*end == '\0') {
  1770. // It is "[,]NAME=NUM\0" - truncate it and return
  1771. *opts = '\0';
  1772. return ret;
  1773. }
  1774. if (*end != ',')
  1775. goto err;
  1776. // We are at trailing comma
  1777. // Remove "NAME=NUM," and loop back to check for duplicate opts
  1778. overlapping_strcpy(opt, end + 1);
  1779. }
  1780. }
  1781. // Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
  1782. // type detection. Returns 0 for success, nonzero for failure.
  1783. // NB: mp->xxx fields may be trashed on exit
  1784. static int singlemount(struct mntent *mp, int ignore_busy)
  1785. {
  1786. int loopfd = -1;
  1787. int rc = -1;
  1788. unsigned long vfsflags;
  1789. char *loopFile = NULL, *filteropts = NULL;
  1790. llist_t *fl = NULL;
  1791. struct stat st;
  1792. errno = 0;
  1793. vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
  1794. // Treat fstype "auto" as unspecified
  1795. if (mp->mnt_type && strcmp(mp->mnt_type, "auto") == 0)
  1796. mp->mnt_type = NULL;
  1797. // Might this be a virtual filesystem?
  1798. if (ENABLE_FEATURE_MOUNT_HELPERS && strchr(mp->mnt_fsname, '#')) {
  1799. char *args[35];
  1800. char *s;
  1801. int n;
  1802. // fsname: "cmd#arg1#arg2..."
  1803. // WARNING: allows execution of arbitrary commands!
  1804. // Try "mount 'sh#-c#sh' bogus_dir".
  1805. // It is safe ONLY because non-root
  1806. // cannot use two-argument mount command
  1807. // and using one-argument "mount 'sh#-c#sh'" doesn't work:
  1808. // "mount: can't find sh#-c#sh in /etc/fstab"
  1809. // (if /etc/fstab has it, it's ok: root sets up /etc/fstab).
  1810. s = mp->mnt_fsname;
  1811. n = 0;
  1812. args[n++] = s;
  1813. while (*s && n < 35 - 2) {
  1814. if (*s++ == '#' && *s != '#') {
  1815. s[-1] = '\0';
  1816. args[n++] = s;
  1817. }
  1818. }
  1819. args[n++] = mp->mnt_dir;
  1820. args[n] = NULL;
  1821. rc = spawn_and_wait(args);
  1822. goto report_error;
  1823. }
  1824. // Might this be an CIFS filesystem?
  1825. if (ENABLE_FEATURE_MOUNT_CIFS
  1826. && (!mp->mnt_type || strcmp(mp->mnt_type, "cifs") == 0)
  1827. && (mp->mnt_fsname[0] == '/' || mp->mnt_fsname[0] == '\\')
  1828. && mp->mnt_fsname[0] == mp->mnt_fsname[1]
  1829. ) {
  1830. int len;
  1831. char c;
  1832. char *hostname, *share;
  1833. len_and_sockaddr *lsa;
  1834. // Parse mp->mnt_fsname of the form "//hostname/share[/dir1/dir2]"
  1835. hostname = mp->mnt_fsname + 2;
  1836. len = strcspn(hostname, "/\\");
  1837. share = hostname + len + 1;
  1838. if (len == 0 // 3rd char is a [back]slash (IOW: empty hostname)
  1839. || share[-1] == '\0' // no [back]slash after hostname
  1840. || share[0] == '\0' // empty share name
  1841. ) {
  1842. goto report_error;
  1843. }
  1844. c = share[-1];
  1845. share[-1] = '\0';
  1846. len = strcspn(share, "/\\");
  1847. // "unc=\\hostname\share" option is mandatory
  1848. // after CIFS option parsing was rewritten in Linux 3.4.
  1849. // Must use backslashes.
  1850. // If /dir1/dir2 is present, also add "prefixpath=dir1/dir2"
  1851. {
  1852. char *unc = xasprintf(
  1853. share[len] != '\0' /* "/dir1/dir2" exists? */
  1854. ? "unc=\\\\%s\\%.*s,prefixpath=%s"
  1855. : "unc=\\\\%s\\%.*s",
  1856. hostname,
  1857. len, share,
  1858. share + len + 1 /* "dir1/dir2" */
  1859. );
  1860. parse_mount_options(unc, &filteropts);
  1861. if (ENABLE_FEATURE_CLEAN_UP) free(unc);
  1862. }
  1863. lsa = host2sockaddr(hostname, 0);
  1864. share[-1] = c;
  1865. if (!lsa)
  1866. goto report_error;
  1867. // If there is no "ip=..." option yet
  1868. if (!is_prefixed_with(filteropts, ",ip="+1)
  1869. && !strstr(filteropts, ",ip=")
  1870. ) {
  1871. char *dotted, *ip;
  1872. // Insert "ip=..." option into options
  1873. dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
  1874. if (ENABLE_FEATURE_CLEAN_UP) free(lsa);
  1875. ip = xasprintf("ip=%s", dotted);
  1876. if (ENABLE_FEATURE_CLEAN_UP) free(dotted);
  1877. // Note: IPv6 scoped addresses ("host%iface", see RFC 4007) should be
  1878. // handled by libc in getnameinfo() (inside xmalloc_sockaddr2dotted_noport()).
  1879. // Currently, glibc does not support that (has no NI_NUMERICSCOPE),
  1880. // musl apparently does. This results in "ip=numericIPv6%iface_name"
  1881. // (instead of _numeric_ iface_id) with glibc.
  1882. // This probably should be fixed in glibc, not here.
  1883. // The workaround is to manually specify correct "ip=ADDR%n" option.
  1884. parse_mount_options(ip, &filteropts);
  1885. if (ENABLE_FEATURE_CLEAN_UP) free(ip);
  1886. }
  1887. mp->mnt_type = (char*)"cifs";
  1888. rc = mount_it_now(mp, vfsflags, filteropts);
  1889. goto report_error;
  1890. }
  1891. // Might this be an NFS filesystem?
  1892. if (!(vfsflags & (MS_BIND | MS_MOVE))
  1893. && (!mp->mnt_type || is_prefixed_with(mp->mnt_type, "nfs"))
  1894. ) {
  1895. char *colon = strchr(mp->mnt_fsname, ':');
  1896. if (colon // looks like "hostname:..."
  1897. && strchrnul(mp->mnt_fsname, '/') > colon // "hostname:" has no slashes
  1898. ) {
  1899. if (!mp->mnt_type)
  1900. mp->mnt_type = (char*)"nfs";
  1901. rc = nfsmount(mp, vfsflags, filteropts);
  1902. goto report_error;
  1903. }
  1904. }
  1905. // Look at the file. (Not found isn't a failure for remount, or for
  1906. // a synthetic filesystem like proc or sysfs.)
  1907. // (We use stat, not lstat, in order to allow
  1908. // mount symlink_to_file_or_blkdev dir)
  1909. if (!stat(mp->mnt_fsname, &st)
  1910. && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))
  1911. ) {
  1912. // Do we need to allocate a loopback device for it?
  1913. if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
  1914. unsigned long long offset;
  1915. unsigned long long sizelimit;
  1916. loopFile = bb_simplify_path(mp->mnt_fsname);
  1917. mp->mnt_fsname = NULL; // will receive malloced loop dev name
  1918. // Parse and remove loopback options
  1919. offset = cut_out_ull_opt(filteropts, "offset=");
  1920. sizelimit = cut_out_ull_opt(filteropts, "sizelimit=");
  1921. // mount always creates AUTOCLEARed loopdevs, so that umounting
  1922. // drops them without any code in the userspace.
  1923. // This happens since circa linux-2.6.25:
  1924. // commit 96c5865559cee0f9cbc5173f3c949f6ce3525581
  1925. // Date: Wed Feb 6 01:36:27 2008 -0800
  1926. // Subject: Allow auto-destruction of loop devices
  1927. loopfd = set_loop(&mp->mnt_fsname,
  1928. loopFile,
  1929. offset,
  1930. sizelimit,
  1931. ((vfsflags & MS_RDONLY) ? BB_LO_FLAGS_READ_ONLY : 0)
  1932. | BB_LO_FLAGS_AUTOCLEAR
  1933. );
  1934. if (loopfd < 0) {
  1935. if (errno == EPERM || errno == EACCES)
  1936. bb_simple_error_msg(bb_msg_perm_denied_are_you_root);
  1937. else
  1938. bb_simple_perror_msg("can't setup loop device");
  1939. return loopfd; // was "return errno", but it can be 0 here
  1940. }
  1941. // Autodetect bind mounts
  1942. } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
  1943. vfsflags |= MS_BIND;
  1944. }
  1945. // If we know the fstype (or don't need to), jump straight
  1946. // to the actual mount.
  1947. if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) {
  1948. char *next;
  1949. for (;;) {
  1950. next = mp->mnt_type ? strchr(mp->mnt_type, ',') : NULL;
  1951. if (next)
  1952. *next = '\0';
  1953. rc = mount_it_now(mp, vfsflags, filteropts);
  1954. if (rc == 0 || !next)
  1955. break;
  1956. mp->mnt_type = next + 1;
  1957. }
  1958. } else {
  1959. // Loop through filesystem types until mount succeeds
  1960. // or we run out
  1961. // Initialize list of block backed filesystems.
  1962. // This has to be done here so that during "mount -a",
  1963. // mounts after /proc shows up can autodetect.
  1964. if (!fslist) {
  1965. fslist = get_block_backed_filesystems();
  1966. if (ENABLE_FEATURE_CLEAN_UP && fslist)
  1967. atexit(delete_block_backed_filesystems);
  1968. }
  1969. for (fl = fslist; fl; fl = fl->link) {
  1970. mp->mnt_type = fl->data;
  1971. rc = mount_it_now(mp, vfsflags, filteropts);
  1972. if (rc == 0)
  1973. break;
  1974. }
  1975. }
  1976. // If mount failed, clean up loop file (if any).
  1977. // (Newer kernels which support LO_FLAGS_AUTOCLEAR should not need this,
  1978. // merely "close(loopfd)" should do it?)
  1979. if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
  1980. del_loop(mp->mnt_fsname);
  1981. if (ENABLE_FEATURE_CLEAN_UP) {
  1982. free(loopFile);
  1983. /* No, "rc != 0" needs it: free(mp->mnt_fsname); */
  1984. }
  1985. }
  1986. report_error:
  1987. if (ENABLE_FEATURE_CLEAN_UP)
  1988. free(filteropts);
  1989. if (loopfd >= 0)
  1990. close(loopfd);
  1991. if (errno == EBUSY && ignore_busy)
  1992. return 0;
  1993. if (errno == ENOENT && (vfsflags & MOUNT_NOFAIL))
  1994. return 0;
  1995. if (rc != 0)
  1996. bb_perror_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
  1997. return rc;
  1998. }
  1999. // -O support
  2000. // -O interprets a list of filter options which select whether a mount
  2001. // point will be mounted: only mounts with options matching *all* filtering
  2002. // options will be selected.
  2003. // By default each -O filter option must be present in the list of mount
  2004. // options, but if it is prefixed by "no" then it must be absent.
  2005. // For example,
  2006. // -O a,nob,c matches -o a,c but fails to match -o a,b,c
  2007. // (and also fails to match -o a because -o c is absent).
  2008. //
  2009. // It is different from -t in that each option is matched exactly; a leading
  2010. // "no" at the beginning of one option does not negate the rest.
  2011. static int match_opt(const char *fs_opt_in, const char *O_opt)
  2012. {
  2013. if (!O_opt)
  2014. return 1;
  2015. while (*O_opt) {
  2016. const char *fs_opt = fs_opt_in;
  2017. int O_len;
  2018. int match;
  2019. // If option begins with "no" then treat as an inverted match:
  2020. // matching is a failure
  2021. match = 0;
  2022. if (O_opt[0] == 'n' && O_opt[1] == 'o') {
  2023. match = 1;
  2024. O_opt += 2;
  2025. }
  2026. // Isolate the current O option
  2027. O_len = strchrnul(O_opt, ',') - O_opt;
  2028. // Check for a match against existing options
  2029. while (1) {
  2030. if (strncmp(fs_opt, O_opt, O_len) == 0
  2031. && (fs_opt[O_len] == '\0' || fs_opt[O_len] == ',')
  2032. ) {
  2033. if (match)
  2034. return 0; // "no" prefix, but option found
  2035. match = 1; // current O option found, go check next one
  2036. break;
  2037. }
  2038. fs_opt = strchr(fs_opt, ',');
  2039. if (!fs_opt)
  2040. break;
  2041. fs_opt++;
  2042. }
  2043. if (match == 0)
  2044. return 0; // match wanted but not found
  2045. if (O_opt[O_len] == '\0') // end?
  2046. break;
  2047. // Step to the next O option
  2048. O_opt += O_len + 1;
  2049. }
  2050. // If we get here then everything matched
  2051. return 1;
  2052. }
  2053. // Parse options, if necessary parse fstab/mtab, and call singlemount for
  2054. // each directory to be mounted.
  2055. int mount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  2056. int mount_main(int argc UNUSED_PARAM, char **argv)
  2057. {
  2058. char *cmdopts = xzalloc(1);
  2059. char *fstype = NULL;
  2060. char *O_optmatch = NULL;
  2061. char *storage_path;
  2062. llist_t *lst_o = NULL;
  2063. const char *fstabname = "/etc/fstab";
  2064. FILE *fstab;
  2065. int i, j;
  2066. int rc = EXIT_SUCCESS;
  2067. unsigned long cmdopt_flags;
  2068. unsigned opt;
  2069. struct mntent mtpair[2], *mtcur = mtpair;
  2070. IF_NOT_DESKTOP(const int nonroot = 0;)
  2071. IF_DESKTOP(int nonroot = ) sanitize_env_if_suid();
  2072. INIT_G();
  2073. // Parse long options, like --bind and --move. Note that -o option
  2074. // and --option are synonymous. Yes, this means --remount,rw works.
  2075. for (i = j = 1; argv[i]; i++) {
  2076. if (argv[i][0] == '-' && argv[i][1] == '-')
  2077. append_mount_options(&cmdopts, argv[i] + 2);
  2078. else
  2079. argv[j++] = argv[i];
  2080. }
  2081. argv[j] = NULL;
  2082. // Parse remaining options
  2083. // Max 2 params; -o is a list, -v is a counter
  2084. opt = getopt32(argv, "^"
  2085. OPTION_STR
  2086. "\0" "?2"IF_FEATURE_MOUNT_VERBOSE("vv"),
  2087. &lst_o, &fstype, &O_optmatch
  2088. IF_FEATURE_MOUNT_OTHERTAB(, &fstabname)
  2089. IF_FEATURE_MOUNT_VERBOSE(, &verbose)
  2090. );
  2091. while (lst_o) append_mount_options(&cmdopts, llist_pop(&lst_o)); // -o
  2092. if (opt & OPT_r) append_mount_options(&cmdopts, "ro"); // -r
  2093. if (opt & OPT_w) append_mount_options(&cmdopts, "rw"); // -w
  2094. argv += optind;
  2095. // If we have no arguments, show currently mounted filesystems
  2096. if (!argv[0]) {
  2097. if (!(opt & OPT_a)) {
  2098. FILE *mountTable = setmntent(bb_path_mtab_file, "r");
  2099. if (!mountTable)
  2100. bb_error_msg_and_die("no %s", bb_path_mtab_file);
  2101. while (getmntent_r(mountTable, &mtpair[0], getmntent_buf,
  2102. GETMNTENT_BUFSIZE))
  2103. {
  2104. // Don't show rootfs. FIXME: why??
  2105. // util-linux 2.12a happily shows rootfs...
  2106. //if (strcmp(mtpair->mnt_fsname, "rootfs") == 0) continue;
  2107. if (!fstype || strcmp(mtpair->mnt_type, fstype) == 0)
  2108. printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
  2109. mtpair->mnt_dir, mtpair->mnt_type,
  2110. mtpair->mnt_opts);
  2111. }
  2112. if (ENABLE_FEATURE_CLEAN_UP)
  2113. endmntent(mountTable);
  2114. return EXIT_SUCCESS;
  2115. }
  2116. storage_path = NULL;
  2117. } else {
  2118. // When we have two arguments, the second is the directory and we can
  2119. // skip looking at fstab entirely. We can always abspath() the directory
  2120. // argument when we get it.
  2121. if (argv[1]) {
  2122. if (nonroot)
  2123. bb_simple_error_msg_and_die(bb_msg_you_must_be_root);
  2124. mtpair->mnt_fsname = argv[0];
  2125. mtpair->mnt_dir = argv[1];
  2126. mtpair->mnt_type = fstype;
  2127. mtpair->mnt_opts = cmdopts;
  2128. resolve_mount_spec(&mtpair->mnt_fsname);
  2129. rc = singlemount(mtpair, /*ignore_busy:*/ 0);
  2130. return rc;
  2131. }
  2132. storage_path = bb_simplify_path(argv[0]); // malloced
  2133. }
  2134. // Past this point, we are handling either "mount -a [opts]"
  2135. // or "mount [opts] single_param"
  2136. cmdopt_flags = parse_mount_options(cmdopts, NULL);
  2137. if (nonroot && (cmdopt_flags & ~MS_SILENT)) // Non-root users cannot specify flags
  2138. bb_simple_error_msg_and_die(bb_msg_you_must_be_root);
  2139. // If we have a shared subtree flag, don't worry about fstab or mtab.
  2140. if (ENABLE_FEATURE_MOUNT_FLAGS
  2141. && (cmdopt_flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  2142. ) {
  2143. // verbose_mount(source, target, type, flags, data)
  2144. rc = verbose_mount("", argv[0], "", cmdopt_flags, "");
  2145. if (rc)
  2146. bb_simple_perror_msg_and_die(argv[0]);
  2147. return rc;
  2148. }
  2149. // A malicious user could overmount /usr without this.
  2150. if (ENABLE_FEATURE_MOUNT_OTHERTAB && nonroot)
  2151. fstabname = "/etc/fstab";
  2152. // Open either fstab or mtab
  2153. if (cmdopt_flags & MS_REMOUNT) {
  2154. // WARNING. I am not sure this matches util-linux's
  2155. // behavior. It's possible util-linux does not
  2156. // take -o opts from mtab (takes only mount source).
  2157. fstabname = bb_path_mtab_file;
  2158. }
  2159. fstab = setmntent(fstabname, "r");
  2160. if (!fstab)
  2161. bb_perror_msg_and_die("can't read '%s'", fstabname);
  2162. // Loop through entries until we find what we're looking for
  2163. memset(mtpair, 0, sizeof(mtpair));
  2164. for (;;) {
  2165. struct mntent *mtother = (mtcur==mtpair ? mtpair+1 : mtpair);
  2166. // Get next fstab entry
  2167. if (!getmntent_r(fstab, mtcur, getmntent_buf
  2168. + (mtcur==mtpair ? GETMNTENT_BUFSIZE/2 : 0),
  2169. GETMNTENT_BUFSIZE/2)
  2170. ) { // End of fstab/mtab is reached
  2171. mtcur = mtother; // the thing we found last time
  2172. break;
  2173. }
  2174. // If we're trying to mount something specific and this isn't it,
  2175. // skip it. Note we must match the exact text in fstab (ala
  2176. // "proc") or a full path from root
  2177. if (argv[0]) {
  2178. // Is this what we're looking for?
  2179. if (strcmp(argv[0], mtcur->mnt_fsname) != 0
  2180. && strcmp(storage_path, mtcur->mnt_fsname) != 0
  2181. && strcmp(argv[0], mtcur->mnt_dir) != 0
  2182. && strcmp(storage_path, mtcur->mnt_dir) != 0
  2183. ) {
  2184. continue; // no
  2185. }
  2186. // Remember this entry. Something later may have
  2187. // overmounted it, and we want the _last_ match.
  2188. mtcur = mtother;
  2189. // If we're mounting all
  2190. } else {
  2191. struct mntent *mp;
  2192. // No, mount -a won't mount anything,
  2193. // even user mounts, for mere humans
  2194. if (nonroot)
  2195. bb_simple_error_msg_and_die(bb_msg_you_must_be_root);
  2196. // Does type match? (NULL matches always)
  2197. if (!fstype_matches(mtcur->mnt_type, fstype))
  2198. continue;
  2199. // Skip noauto and swap anyway
  2200. if ((parse_mount_options(mtcur->mnt_opts, NULL) & (MOUNT_NOAUTO | MOUNT_SWAP))
  2201. // swap is bogus "fstype", parse_mount_options can't check fstypes
  2202. || strcasecmp(mtcur->mnt_type, "swap") == 0
  2203. ) {
  2204. continue;
  2205. }
  2206. // Does (at least one) option match?
  2207. // (NULL matches always)
  2208. if (!match_opt(mtcur->mnt_opts, O_optmatch))
  2209. continue;
  2210. resolve_mount_spec(&mtcur->mnt_fsname);
  2211. // NFS mounts want this to be xrealloc-able
  2212. mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
  2213. // If nothing is mounted on this directory...
  2214. // (otherwise repeated "mount -a" mounts everything again)
  2215. mp = find_mount_point(mtcur->mnt_dir, /*subdir_too:*/ 0);
  2216. // We do not check fsname match of found mount point -
  2217. // "/" may have fsname of "/dev/root" while fstab
  2218. // says "/dev/something_else".
  2219. if (mp) {
  2220. if (verbose) {
  2221. bb_error_msg("according to %s, "
  2222. "%s is already mounted on %s",
  2223. bb_path_mtab_file,
  2224. mp->mnt_fsname, mp->mnt_dir);
  2225. }
  2226. } else {
  2227. // ...mount this thing
  2228. if (singlemount(mtcur, /*ignore_busy:*/ 1)) {
  2229. // Count number of failed mounts
  2230. rc++;
  2231. }
  2232. }
  2233. free(mtcur->mnt_opts);
  2234. }
  2235. }
  2236. // End of fstab/mtab is reached.
  2237. // Were we looking for something specific?
  2238. if (argv[0]) { // yes
  2239. unsigned long l;
  2240. // If we didn't find anything, complain
  2241. if (!mtcur->mnt_fsname)
  2242. bb_error_msg_and_die("can't find %s in %s",
  2243. argv[0], fstabname);
  2244. // What happens when we try to "mount swap_partition"?
  2245. // (fstab containts "swap_partition swap swap defaults 0 0")
  2246. // util-linux-ng 2.13.1 does this:
  2247. // stat("/sbin/mount.swap", 0x7fff62a3a350) = -1 ENOENT (No such file or directory)
  2248. // mount("swap_partition", "swap", "swap", MS_MGC_VAL, NULL) = -1 ENOENT (No such file or directory)
  2249. // lstat("swap", 0x7fff62a3a640) = -1 ENOENT (No such file or directory)
  2250. // write(2, "mount: mount point swap does not exist\n", 39) = 39
  2251. // exit_group(32) = ?
  2252. #if 0
  2253. // In case we want to simply skip swap partitions:
  2254. l = parse_mount_options(mtcur->mnt_opts, NULL);
  2255. if ((l & MOUNT_SWAP)
  2256. // swap is bogus "fstype", parse_mount_options can't check fstypes
  2257. || strcasecmp(mtcur->mnt_type, "swap") == 0
  2258. ) {
  2259. goto ret;
  2260. }
  2261. #endif
  2262. if (nonroot) {
  2263. // fstab must have "users" or "user"
  2264. l = parse_mount_options(mtcur->mnt_opts, NULL);
  2265. if (!(l & MOUNT_USERS))
  2266. bb_simple_error_msg_and_die(bb_msg_you_must_be_root);
  2267. }
  2268. //util-linux-2.12 does not do this check.
  2269. //// If nothing is mounted on this directory...
  2270. //// (otherwise repeated "mount FOO" mounts FOO again)
  2271. //mp = find_mount_point(mtcur->mnt_dir, /*subdir_too:*/ 0);
  2272. //if (mp) {
  2273. // bb_error_msg("according to %s, "
  2274. // "%s is already mounted on %s",
  2275. // bb_path_mtab_file,
  2276. // mp->mnt_fsname, mp->mnt_dir);
  2277. //} else {
  2278. // ...mount the last thing we found
  2279. mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
  2280. append_mount_options(&(mtcur->mnt_opts), cmdopts);
  2281. resolve_mount_spec(&mtpair->mnt_fsname);
  2282. rc = singlemount(mtcur, /*ignore_busy:*/ 0);
  2283. if (ENABLE_FEATURE_CLEAN_UP)
  2284. free(mtcur->mnt_opts);
  2285. //}
  2286. }
  2287. //ret:
  2288. if (ENABLE_FEATURE_CLEAN_UP)
  2289. endmntent(fstab);
  2290. if (ENABLE_FEATURE_CLEAN_UP) {
  2291. free(storage_path);
  2292. free(cmdopts);
  2293. }
  2294. //TODO: exitcode should be ORed mask of (from "man mount"):
  2295. // 0 success
  2296. // 1 incorrect invocation or permissions
  2297. // 2 system error (out of memory, cannot fork, no more loop devices)
  2298. // 4 internal mount bug or missing nfs support in mount
  2299. // 8 user interrupt
  2300. //16 problems writing or locking /etc/mtab
  2301. //32 mount failure
  2302. //64 some mount succeeded
  2303. return rc;
  2304. }