umount.c 6.6 KB

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