3
0

mount.c 47 KB

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