mount.c 63 KB

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