umount.c 6.3 KB

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