mount.c 50 KB

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