3
0

mount.c 65 KB

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