3
0

umount.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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"
  12. //config: default y
  13. //config: select PLATFORM_LINUX
  14. //config: help
  15. //config: When you want to remove a mounted filesystem from its current mount
  16. //config: point, for example when you are shutting down the system, the
  17. //config: 'umount' utility is the tool to use. If you enabled the 'mount'
  18. //config: utility, you almost certainly also want to enable 'umount'.
  19. //config:
  20. //config:config FEATURE_UMOUNT_ALL
  21. //config: bool "Support option -a"
  22. //config: default y
  23. //config: depends on UMOUNT
  24. //config: help
  25. //config: Support -a option to unmount all currently mounted filesystems.
  26. //applet:IF_UMOUNT(APPLET(umount, BB_DIR_BIN, BB_SUID_DROP))
  27. //kbuild:lib-$(CONFIG_UMOUNT) += umount.o
  28. //usage:#define umount_trivial_usage
  29. //usage: "[OPTIONS] FILESYSTEM|DIRECTORY"
  30. //usage:#define umount_full_usage "\n\n"
  31. //usage: "Unmount file systems\n"
  32. //usage: IF_FEATURE_UMOUNT_ALL(
  33. //usage: "\n -a Unmount all file systems" IF_FEATURE_MTAB_SUPPORT(" in /etc/mtab")
  34. //usage: )
  35. //usage: IF_FEATURE_MTAB_SUPPORT(
  36. //usage: "\n -n Don't erase /etc/mtab entries"
  37. //usage: )
  38. //usage: "\n -r Try to remount devices as read-only if mount is busy"
  39. //usage: "\n -l Lazy umount (detach filesystem)"
  40. //usage: "\n -f Force umount (i.e., unreachable NFS server)"
  41. //usage: IF_FEATURE_MOUNT_LOOP(
  42. //usage: "\n -D Don't free loop device even if it has been used"
  43. //usage: )
  44. //usage:
  45. //usage:#define umount_example_usage
  46. //usage: "$ umount /dev/hdc1\n"
  47. #include <mntent.h>
  48. #include <sys/mount.h>
  49. #ifndef MNT_DETACH
  50. # define MNT_DETACH 0x00000002
  51. #endif
  52. #include "libbb.h"
  53. #include "common_bufsiz.h"
  54. #if defined(__dietlibc__)
  55. // TODO: This does not belong here.
  56. /* 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
  57. * dietlibc-0.30 does not have implementation of getmntent_r() */
  58. static struct mntent *getmntent_r(FILE* stream, struct mntent* result,
  59. char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM)
  60. {
  61. struct mntent* ment = getmntent(stream);
  62. return memcpy(result, ment, sizeof(*ment));
  63. }
  64. #endif
  65. /* Ignored: -v -t -i
  66. * bbox always acts as if -d is present.
  67. * -D can be used to suppress it (bbox extension).
  68. * Rationale:
  69. * (1) util-linux's umount does it if "loop=..." is seen in /etc/mtab:
  70. * thus, on many systems, bare umount _does_ drop loop devices.
  71. * (2) many users request this feature.
  72. */
  73. #define OPTION_STRING "fldDnra" "vt:i"
  74. #define OPT_FORCE (1 << 0) // Same as MNT_FORCE
  75. #define OPT_LAZY (1 << 1) // Same as MNT_DETACH
  76. //#define OPT_FREE_LOOP (1 << 2) // -d is assumed always present
  77. #define OPT_DONT_FREE_LOOP (1 << 3)
  78. #define OPT_NO_MTAB (1 << 4)
  79. #define OPT_REMOUNT (1 << 5)
  80. #define OPT_ALL (ENABLE_FEATURE_UMOUNT_ALL ? (1 << 6) : 0)
  81. int umount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  82. int umount_main(int argc UNUSED_PARAM, char **argv)
  83. {
  84. int doForce;
  85. struct mntent me;
  86. FILE *fp;
  87. char *fstype = NULL;
  88. int status = EXIT_SUCCESS;
  89. unsigned opt;
  90. struct mtab_list {
  91. char *dir;
  92. char *device;
  93. struct mtab_list *next;
  94. } *mtl, *m;
  95. opt = getopt32(argv, OPTION_STRING, &fstype);
  96. //argc -= optind;
  97. argv += optind;
  98. // MNT_FORCE and MNT_DETACH (from linux/fs.h) must match
  99. // OPT_FORCE and OPT_LAZY.
  100. BUILD_BUG_ON(OPT_FORCE != MNT_FORCE || OPT_LAZY != MNT_DETACH);
  101. doForce = opt & (OPT_FORCE|OPT_LAZY);
  102. /* Get a list of mount points from mtab. We read them all in now mostly
  103. * for umount -a (so we don't have to worry about the list changing while
  104. * we iterate over it, or about getting stuck in a loop on the same failing
  105. * entry. Notice that this also naturally reverses the list so that -a
  106. * umounts the most recent entries first. */
  107. m = mtl = NULL;
  108. // If we're umounting all, then m points to the start of the list and
  109. // the argument list should be empty (which will match all).
  110. fp = setmntent(bb_path_mtab_file, "r");
  111. if (!fp) {
  112. if (opt & OPT_ALL)
  113. bb_error_msg_and_die("can't open '%s'", bb_path_mtab_file);
  114. } else {
  115. setup_common_bufsiz();
  116. while (getmntent_r(fp, &me, bb_common_bufsiz1, COMMON_BUFSIZE)) {
  117. /* Match fstype if passed */
  118. if (!match_fstype(&me, fstype))
  119. continue;
  120. m = xzalloc(sizeof(*m));
  121. m->next = mtl;
  122. m->device = xstrdup(me.mnt_fsname);
  123. m->dir = xstrdup(me.mnt_dir);
  124. mtl = m;
  125. }
  126. endmntent(fp);
  127. }
  128. // If we're not umounting all, we need at least one argument.
  129. if (!(opt & OPT_ALL) && !fstype) {
  130. if (!argv[0])
  131. bb_show_usage();
  132. m = NULL;
  133. }
  134. // Loop through everything we're supposed to umount, and do so.
  135. for (;;) {
  136. int curstat;
  137. char *zapit = *argv;
  138. char *path;
  139. // Do we already know what to umount this time through the loop?
  140. if (m)
  141. path = xstrdup(m->dir);
  142. // For umount -a, end of mtab means time to exit.
  143. else if (opt & OPT_ALL)
  144. break;
  145. // Use command line argument (and look it up in mtab list)
  146. else {
  147. if (!zapit)
  148. break;
  149. argv++;
  150. path = xmalloc_realpath(zapit);
  151. if (path) {
  152. for (m = mtl; m; m = m->next)
  153. if (strcmp(path, m->dir) == 0 || strcmp(path, m->device) == 0)
  154. break;
  155. }
  156. }
  157. // If we couldn't find this sucker in /etc/mtab, punt by passing our
  158. // command line argument straight to the umount syscall. Otherwise,
  159. // umount the directory even if we were given the block device.
  160. if (m) zapit = m->dir;
  161. // umount from util-linux 2.22.2 does not do this:
  162. // umount -f uses umount2(MNT_FORCE) immediately,
  163. // not trying umount() first.
  164. // (Strangely, umount -fl ignores -f: it is equivalent to umount -l.
  165. // We do pass both flags in this case)
  166. #if 0
  167. // Let's ask the thing nicely to unmount.
  168. curstat = umount(zapit);
  169. // Unmount with force and/or lazy flags, if necessary.
  170. if (curstat && doForce)
  171. #endif
  172. curstat = umount2(zapit, doForce);
  173. // If still can't umount, maybe remount read-only?
  174. if (curstat) {
  175. if ((opt & OPT_REMOUNT) && errno == EBUSY && m) {
  176. // Note! Even if we succeed here, later we should not
  177. // free loop device or erase mtab entry!
  178. const char *msg = "%s busy - remounted read-only";
  179. curstat = mount(m->device, zapit, NULL, MS_REMOUNT|MS_RDONLY, NULL);
  180. if (curstat) {
  181. msg = "can't remount %s read-only";
  182. status = EXIT_FAILURE;
  183. }
  184. bb_error_msg(msg, m->device);
  185. } else {
  186. status = EXIT_FAILURE;
  187. bb_perror_msg("can't unmount %s", zapit);
  188. }
  189. } else {
  190. // De-allocate the loop device. This ioctl should be ignored on
  191. // any non-loop block devices.
  192. if (ENABLE_FEATURE_MOUNT_LOOP && !(opt & OPT_DONT_FREE_LOOP) && m)
  193. del_loop(m->device);
  194. if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m)
  195. erase_mtab(m->dir);
  196. }
  197. // Find next matching mtab entry for -a or umount /dev
  198. // Note this means that "umount /dev/blah" will unmount all instances
  199. // of /dev/blah, not just the most recent.
  200. if (m) {
  201. while ((m = m->next) != NULL)
  202. // NB: if m is non-NULL, path is non-NULL as well
  203. if ((opt & OPT_ALL) || strcmp(path, m->device) == 0)
  204. break;
  205. }
  206. free(path);
  207. }
  208. // Free mtab list if necessary
  209. if (ENABLE_FEATURE_CLEAN_UP) {
  210. while (mtl) {
  211. m = mtl->next;
  212. free(mtl->device);
  213. free(mtl->dir);
  214. free(mtl);
  215. mtl = m;
  216. }
  217. }
  218. return status;
  219. }