mount.c 52 KB

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