mount.c 63 KB

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