mount.c 56 KB

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