3
0

mount.c 46 KB

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