3
0

mount.c 52 KB

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