umount.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 GPL version 2, see file LICENSE in this tarball for details.
  9. */
  10. #include <mntent.h>
  11. #include <sys/mount.h>
  12. /* Make sure we have all the new mount flags we actually try to use. */
  13. #ifndef MS_BIND
  14. # define MS_BIND (1 << 12)
  15. #endif
  16. #ifndef MS_MOVE
  17. # define MS_MOVE (1 << 13)
  18. #endif
  19. #ifndef MS_RECURSIVE
  20. # define MS_RECURSIVE (1 << 14)
  21. #endif
  22. #ifndef MS_SILENT
  23. # define MS_SILENT (1 << 15)
  24. #endif
  25. /* The shared subtree stuff, which went in around 2.6.15. */
  26. #ifndef MS_UNBINDABLE
  27. # define MS_UNBINDABLE (1 << 17)
  28. #endif
  29. #ifndef MS_PRIVATE
  30. # define MS_PRIVATE (1 << 18)
  31. #endif
  32. #ifndef MS_SLAVE
  33. # define MS_SLAVE (1 << 19)
  34. #endif
  35. #ifndef MS_SHARED
  36. # define MS_SHARED (1 << 20)
  37. #endif
  38. #ifndef MS_RELATIME
  39. # define MS_RELATIME (1 << 21)
  40. #endif
  41. #include "libbb.h"
  42. #ifndef PATH_MAX
  43. # define PATH_MAX (4*1024)
  44. #endif
  45. #if defined(__dietlibc__)
  46. /* 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
  47. * dietlibc-0.30 does not have implementation of getmntent_r() */
  48. static struct mntent *getmntent_r(FILE* stream, struct mntent* result,
  49. char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM)
  50. {
  51. struct mntent* ment = getmntent(stream);
  52. return memcpy(result, ment, sizeof(*ment));
  53. }
  54. #endif
  55. /* ignored: -v -d -t -i */
  56. #define OPTION_STRING "fldnra" "vdt:i"
  57. #define OPT_FORCE (1 << 0)
  58. #define OPT_LAZY (1 << 1)
  59. #define OPT_FREELOOP (1 << 2)
  60. #define OPT_NO_MTAB (1 << 3)
  61. #define OPT_REMOUNT (1 << 4)
  62. #define OPT_ALL (ENABLE_FEATURE_UMOUNT_ALL ? (1 << 5) : 0)
  63. // These constants from linux/fs.h must match OPT_FORCE and OPT_LAZY,
  64. // otherwise "doForce" trick below won't work!
  65. //#define MNT_FORCE 0x00000001 /* Attempt to forcibly umount */
  66. //#define MNT_DETACH 0x00000002 /* Just detach from the tree */
  67. int umount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  68. int umount_main(int argc UNUSED_PARAM, char **argv)
  69. {
  70. int doForce;
  71. char *const buf = xmalloc(PATH_MAX * 2 + 128); /* to save stack */
  72. struct mntent me;
  73. FILE *fp;
  74. char *fstype = NULL;
  75. int status = EXIT_SUCCESS;
  76. unsigned opt;
  77. struct mtab_list {
  78. char *dir;
  79. char *device;
  80. struct mtab_list *next;
  81. } *mtl, *m;
  82. opt = getopt32(argv, OPTION_STRING, &fstype);
  83. //argc -= optind;
  84. argv += optind;
  85. doForce = MAX((opt & OPT_FORCE), (opt & OPT_LAZY));
  86. /* Get a list of mount points from mtab. We read them all in now mostly
  87. * for umount -a (so we don't have to worry about the list changing while
  88. * we iterate over it, or about getting stuck in a loop on the same failing
  89. * entry. Notice that this also naturally reverses the list so that -a
  90. * umounts the most recent entries first. */
  91. m = mtl = NULL;
  92. // If we're umounting all, then m points to the start of the list and
  93. // the argument list should be empty (which will match all).
  94. fp = setmntent(bb_path_mtab_file, "r");
  95. if (!fp) {
  96. if (opt & OPT_ALL)
  97. bb_error_msg_and_die("can't open '%s'", bb_path_mtab_file);
  98. } else {
  99. while (getmntent_r(fp, &me, buf, PATH_MAX * 2 + 128)) {
  100. /* Match fstype if passed */
  101. if (!match_fstype(&me, fstype))
  102. continue;
  103. m = xzalloc(sizeof(*m));
  104. m->next = mtl;
  105. m->device = xstrdup(me.mnt_fsname);
  106. m->dir = xstrdup(me.mnt_dir);
  107. mtl = m;
  108. }
  109. endmntent(fp);
  110. }
  111. // If we're not umounting all, we need at least one argument.
  112. if (!(opt & OPT_ALL) && !fstype) {
  113. if (!argv[0])
  114. bb_show_usage();
  115. m = NULL;
  116. }
  117. // Loop through everything we're supposed to umount, and do so.
  118. for (;;) {
  119. int curstat;
  120. char *zapit = *argv;
  121. char *path;
  122. // Do we already know what to umount this time through the loop?
  123. if (m)
  124. path = xstrdup(m->dir);
  125. // For umount -a, end of mtab means time to exit.
  126. else if (opt & OPT_ALL)
  127. break;
  128. // Use command line argument (and look it up in mtab list)
  129. else {
  130. if (!zapit)
  131. break;
  132. argv++;
  133. path = xmalloc_realpath(zapit);
  134. if (path) {
  135. for (m = mtl; m; m = m->next)
  136. if (strcmp(path, m->dir) == 0 || strcmp(path, m->device) == 0)
  137. break;
  138. }
  139. }
  140. // If we couldn't find this sucker in /etc/mtab, punt by passing our
  141. // command line argument straight to the umount syscall. Otherwise,
  142. // umount the directory even if we were given the block device.
  143. if (m) zapit = m->dir;
  144. // Let's ask the thing nicely to unmount.
  145. curstat = umount(zapit);
  146. // Force the unmount, if necessary.
  147. if (curstat && doForce)
  148. curstat = umount2(zapit, doForce);
  149. // If still can't umount, maybe remount read-only?
  150. if (curstat) {
  151. if ((opt & OPT_REMOUNT) && errno == EBUSY && m) {
  152. // Note! Even if we succeed here, later we should not
  153. // free loop device or erase mtab entry!
  154. const char *msg = "%s busy - remounted read-only";
  155. curstat = mount(m->device, zapit, NULL, MS_REMOUNT|MS_RDONLY, NULL);
  156. if (curstat) {
  157. msg = "can't remount %s read-only";
  158. status = EXIT_FAILURE;
  159. }
  160. bb_error_msg(msg, m->device);
  161. } else {
  162. status = EXIT_FAILURE;
  163. bb_perror_msg("can't %sumount %s", (doForce ? "forcibly " : ""), zapit);
  164. }
  165. } else {
  166. // De-allocate the loop device. This ioctl should be ignored on
  167. // any non-loop block devices.
  168. if (ENABLE_FEATURE_MOUNT_LOOP && (opt & OPT_FREELOOP) && m)
  169. del_loop(m->device);
  170. if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m)
  171. erase_mtab(m->dir);
  172. }
  173. // Find next matching mtab entry for -a or umount /dev
  174. // Note this means that "umount /dev/blah" will unmount all instances
  175. // of /dev/blah, not just the most recent.
  176. if (m) {
  177. while ((m = m->next) != NULL)
  178. // NB: if m is non-NULL, path is non-NULL as well
  179. if ((opt & OPT_ALL) || strcmp(path, m->device) == 0)
  180. break;
  181. }
  182. free(path);
  183. }
  184. // Free mtab list if necessary
  185. if (ENABLE_FEATURE_CLEAN_UP) {
  186. while (mtl) {
  187. m = mtl->next;
  188. free(mtl->device);
  189. free(mtl->dir);
  190. free(mtl);
  191. mtl = m;
  192. }
  193. free(buf);
  194. }
  195. return status;
  196. }