3
0

umount.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini umount implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. * Copyright (C) 2005 by Rob Landley <rob@landley.net>
  7. *
  8. * Licensed under GPLv2, see file LICENSE in this source tree.
  9. */
  10. //config:config UMOUNT
  11. //config: bool "umount (5.1 kb)"
  12. //config: default y
  13. //config: help
  14. //config: When you want to remove a mounted filesystem from its current mount
  15. //config: point, for example when you are shutting down the system, the
  16. //config: 'umount' utility is the tool to use. If you enabled the 'mount'
  17. //config: utility, you almost certainly also want to enable 'umount'.
  18. //config:
  19. //config:config FEATURE_UMOUNT_ALL
  20. //config: bool "Support -a (unmount all)"
  21. //config: default y
  22. //config: depends on UMOUNT
  23. //config: help
  24. //config: Support -a option to unmount all currently mounted filesystems.
  25. //applet:IF_UMOUNT(APPLET_NOEXEC(umount, umount, BB_DIR_BIN, BB_SUID_DROP, umount))
  26. /*
  27. * On one hand, in some weird situations you'd want umount
  28. * to not do anything surprising, to behave as a usual fork+execed executable.
  29. *
  30. * OTOH, there can be situations where execing would not succeed, or even hang
  31. * (say, if executable is on a filesystem which is in trouble and accesses to it
  32. * block in kernel).
  33. * In this case, you might be actually happy if your standalone bbox shell
  34. * does not fork+exec, but only forks and calls umount_main() which it already has!
  35. * Let's go with NOEXEC.
  36. *
  37. * bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed.
  38. */
  39. //kbuild:lib-$(CONFIG_UMOUNT) += umount.o
  40. //usage:#define umount_trivial_usage
  41. //usage: "[-rlf"IF_FEATURE_MTAB_SUPPORT("m")IF_FEATURE_MOUNT_LOOP("d")IF_FEATURE_UMOUNT_ALL("a")"] [-t FSTYPE] FILESYSTEM|DIRECTORY"
  42. //usage:#define umount_full_usage "\n\n"
  43. //usage: "Unmount filesystems\n"
  44. //usage: IF_FEATURE_UMOUNT_ALL(
  45. //usage: "\n -a Unmount all filesystems" IF_FEATURE_MTAB_SUPPORT(" in /etc/mtab")
  46. //usage: )
  47. //usage: IF_FEATURE_MTAB_SUPPORT(
  48. //usage: "\n -n Don't erase /etc/mtab entries"
  49. //usage: )
  50. //usage: "\n -r Remount devices read-only if mount is busy"
  51. //usage: "\n -l Lazy umount (detach filesystem)"
  52. //usage: "\n -f Force umount (i.e., unreachable NFS server)"
  53. //usage: IF_FEATURE_MOUNT_LOOP(
  54. //usage: "\n -d Free loop device if it has been used"
  55. //usage: )
  56. //usage: "\n -t FSTYPE[,...] Unmount only these filesystem type(s)"
  57. //usage:
  58. //usage:#define umount_example_usage
  59. //usage: "$ umount /dev/hdc1\n"
  60. #include <mntent.h>
  61. #include <sys/mount.h>
  62. #ifndef MNT_DETACH
  63. # define MNT_DETACH 0x00000002
  64. #endif
  65. #include "libbb.h"
  66. #include "common_bufsiz.h"
  67. #if defined(__dietlibc__)
  68. // TODO: This does not belong here.
  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. /* ignored: -c -v -i */
  79. #define OPTION_STRING "fldnrat:" "cvi"
  80. #define OPT_FORCE (1 << 0) // Same as MNT_FORCE
  81. #define OPT_LAZY (1 << 1) // Same as MNT_DETACH
  82. #define OPT_FREELOOP (1 << 2)
  83. #define OPT_NO_MTAB (1 << 3)
  84. #define OPT_REMOUNT (1 << 4)
  85. #define OPT_ALL (ENABLE_FEATURE_UMOUNT_ALL ? (1 << 5) : 0)
  86. int umount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  87. int umount_main(int argc UNUSED_PARAM, char **argv)
  88. {
  89. int doForce;
  90. struct mntent me;
  91. FILE *fp;
  92. char *fstype = NULL;
  93. exitcode_t status = EXIT_SUCCESS;
  94. unsigned opt;
  95. struct mtab_list {
  96. char *dir;
  97. char *device;
  98. struct mtab_list *next;
  99. } *mtl, *m;
  100. opt = getopt32(argv, OPTION_STRING, &fstype);
  101. //argc -= optind;
  102. argv += optind;
  103. // MNT_FORCE and MNT_DETACH (from linux/fs.h) must match
  104. // OPT_FORCE and OPT_LAZY.
  105. BUILD_BUG_ON(OPT_FORCE != MNT_FORCE || OPT_LAZY != MNT_DETACH);
  106. doForce = opt & (OPT_FORCE|OPT_LAZY);
  107. /* Get a list of mount points from mtab. We read them all in now mostly
  108. * for umount -a (so we don't have to worry about the list changing while
  109. * we iterate over it, or about getting stuck in a loop on the same failing
  110. * entry. Notice that this also naturally reverses the list so that -a
  111. * umounts the most recent entries first. */
  112. m = mtl = NULL;
  113. // If we're umounting all, then m points to the start of the list and
  114. // the argument list should be empty (which will match all).
  115. fp = setmntent(bb_path_mtab_file, "r");
  116. if (!fp) {
  117. if (opt & OPT_ALL)
  118. bb_error_msg_and_die("can't open '%s'", bb_path_mtab_file);
  119. } else {
  120. setup_common_bufsiz();
  121. while (getmntent_r(fp, &me, bb_common_bufsiz1, COMMON_BUFSIZE)) {
  122. /* Match fstype (fstype==NULL matches always) */
  123. if (!fstype_matches(me.mnt_type, fstype))
  124. continue;
  125. m = xzalloc(sizeof(*m));
  126. m->next = mtl;
  127. m->device = xstrdup(me.mnt_fsname);
  128. m->dir = xstrdup(me.mnt_dir);
  129. mtl = m;
  130. }
  131. endmntent(fp);
  132. }
  133. // If we're not umounting all, we need at least one argument.
  134. // Note: "-t FSTYPE" does not imply -a.
  135. if (!(opt & OPT_ALL)) {
  136. if (!argv[0])
  137. bb_show_usage();
  138. m = NULL;
  139. }
  140. // Loop through everything we're supposed to umount, and do so.
  141. for (;;) {
  142. int curstat;
  143. char *zapit = *argv;
  144. char *path;
  145. // Do we already know what to umount this time through the loop?
  146. if (m)
  147. path = xstrdup(m->dir);
  148. // For umount -a, end of mtab means time to exit.
  149. else if (opt & OPT_ALL)
  150. break;
  151. // Use command line argument (and look it up in mtab list)
  152. else {
  153. if (!zapit)
  154. break;
  155. argv++;
  156. path = xmalloc_realpath(zapit);
  157. if (path) {
  158. for (m = mtl; m; m = m->next)
  159. if (strcmp(path, m->dir) == 0 || strcmp(path, m->device) == 0)
  160. break;
  161. }
  162. }
  163. // If we couldn't find this sucker in /etc/mtab, punt by passing our
  164. // command line argument straight to the umount syscall. Otherwise,
  165. // umount the directory even if we were given the block device.
  166. if (m) zapit = m->dir;
  167. // umount from util-linux 2.22.2 does not do this:
  168. // umount -f uses umount2(MNT_FORCE) immediately,
  169. // not trying umount() first.
  170. // (Strangely, umount -fl ignores -f: it is equivalent to umount -l.
  171. // We do pass both flags in this case)
  172. #if 0
  173. // Let's ask the thing nicely to unmount.
  174. curstat = umount(zapit);
  175. // Unmount with force and/or lazy flags, if necessary.
  176. if (curstat && doForce)
  177. #endif
  178. curstat = umount2(zapit, doForce);
  179. // If still can't umount, maybe remount read-only?
  180. if (curstat) {
  181. if ((opt & OPT_REMOUNT) && errno == EBUSY && m) {
  182. // Note! Even if we succeed here, later we should not
  183. // free loop device or erase mtab entry!
  184. const char *msg = "%s busy - remounted read-only";
  185. curstat = mount(m->device, zapit, NULL, MS_REMOUNT|MS_RDONLY, NULL);
  186. if (curstat) {
  187. msg = "can't remount %s read-only";
  188. status = EXIT_FAILURE;
  189. }
  190. bb_error_msg(msg, m->device);
  191. } else {
  192. status = EXIT_FAILURE;
  193. bb_perror_msg("can't unmount %s", zapit);
  194. }
  195. } else {
  196. // De-allocate the loop device. This ioctl should be ignored on
  197. // any non-loop block devices.
  198. if (ENABLE_FEATURE_MOUNT_LOOP && (opt & OPT_FREELOOP) && m)
  199. del_loop(m->device);
  200. if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m)
  201. erase_mtab(m->dir);
  202. }
  203. // Find next matching mtab entry for -a or umount /dev
  204. // Note this means that "umount /dev/blah" will unmount all instances
  205. // of /dev/blah, not just the most recent.
  206. if (m) {
  207. while ((m = m->next) != NULL)
  208. // NB: if m is non-NULL, path is non-NULL as well
  209. if ((opt & OPT_ALL) || strcmp(path, m->device) == 0)
  210. break;
  211. }
  212. free(path);
  213. }
  214. // Free mtab list if necessary
  215. if (ENABLE_FEATURE_CLEAN_UP) {
  216. while (mtl) {
  217. m = mtl->next;
  218. free(mtl->device);
  219. free(mtl->dir);
  220. free(mtl);
  221. mtl = m;
  222. }
  223. }
  224. return status;
  225. }