3
0

umount.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #if defined(__dietlibc__)
  43. /* 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
  44. * dietlibc-0.30 does not have implementation of getmntent_r() */
  45. static struct mntent *getmntent_r(FILE* stream, struct mntent* result,
  46. char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM)
  47. {
  48. struct mntent* ment = getmntent(stream);
  49. return memcpy(result, ment, sizeof(*ment));
  50. }
  51. #endif
  52. /* ignored: -v -d -t -i */
  53. #define OPTION_STRING "fldnra" "vdt:i"
  54. #define OPT_FORCE (1 << 0)
  55. #define OPT_LAZY (1 << 1)
  56. #define OPT_FREELOOP (1 << 2)
  57. #define OPT_NO_MTAB (1 << 3)
  58. #define OPT_REMOUNT (1 << 4)
  59. #define OPT_ALL (ENABLE_FEATURE_UMOUNT_ALL ? (1 << 5) : 0)
  60. // These constants from linux/fs.h must match OPT_FORCE and OPT_LAZY,
  61. // otherwise "doForce" trick below won't work!
  62. //#define MNT_FORCE 0x00000001 /* Attempt to forcibly umount */
  63. //#define MNT_DETACH 0x00000002 /* Just detach from the tree */
  64. int umount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  65. int umount_main(int argc UNUSED_PARAM, char **argv)
  66. {
  67. int doForce;
  68. char *const path = xmalloc(PATH_MAX + 2); /* to save stack */
  69. struct mntent me;
  70. FILE *fp;
  71. char *fstype = NULL;
  72. int status = EXIT_SUCCESS;
  73. unsigned opt;
  74. struct mtab_list {
  75. char *dir;
  76. char *device;
  77. struct mtab_list *next;
  78. } *mtl, *m;
  79. opt = getopt32(argv, OPTION_STRING, &fstype);
  80. //argc -= optind;
  81. argv += optind;
  82. doForce = MAX((opt & OPT_FORCE), (opt & 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, path, PATH_MAX)) {
  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. // Do we already know what to umount this time through the loop?
  119. if (m)
  120. safe_strncpy(path, m->dir, PATH_MAX);
  121. // For umount -a, end of mtab means time to exit.
  122. else if (opt & OPT_ALL)
  123. break;
  124. // Use command line argument (and look it up in mtab list)
  125. else {
  126. if (!zapit)
  127. break;
  128. argv++;
  129. realpath(zapit, path);
  130. for (m = mtl; m; m = m->next)
  131. if (!strcmp(path, m->dir) || !strcmp(path, m->device))
  132. break;
  133. }
  134. // If we couldn't find this sucker in /etc/mtab, punt by passing our
  135. // command line argument straight to the umount syscall. Otherwise,
  136. // umount the directory even if we were given the block device.
  137. if (m) zapit = m->dir;
  138. // Let's ask the thing nicely to unmount.
  139. curstat = umount(zapit);
  140. // Force the unmount, if necessary.
  141. if (curstat && doForce)
  142. curstat = umount2(zapit, doForce);
  143. // If still can't umount, maybe remount read-only?
  144. if (curstat) {
  145. if ((opt & OPT_REMOUNT) && errno == EBUSY && m) {
  146. // Note! Even if we succeed here, later we should not
  147. // free loop device or erase mtab entry!
  148. const char *msg = "%s busy - remounted read-only";
  149. curstat = mount(m->device, zapit, NULL, MS_REMOUNT|MS_RDONLY, NULL);
  150. if (curstat) {
  151. msg = "can't remount %s read-only";
  152. status = EXIT_FAILURE;
  153. }
  154. bb_error_msg(msg, m->device);
  155. } else {
  156. status = EXIT_FAILURE;
  157. bb_perror_msg("can't %sumount %s", (doForce ? "forcibly " : ""), zapit);
  158. }
  159. } else {
  160. // De-allocate the loop device. This ioctl should be ignored on
  161. // any non-loop block devices.
  162. if (ENABLE_FEATURE_MOUNT_LOOP && (opt & OPT_FREELOOP) && m)
  163. del_loop(m->device);
  164. if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m)
  165. erase_mtab(m->dir);
  166. }
  167. // Find next matching mtab entry for -a or umount /dev
  168. // Note this means that "umount /dev/blah" will unmount all instances
  169. // of /dev/blah, not just the most recent.
  170. if (m) while ((m = m->next) != NULL)
  171. if ((opt & OPT_ALL) || !strcmp(path, m->device))
  172. break;
  173. }
  174. // Free mtab list if necessary
  175. if (ENABLE_FEATURE_CLEAN_UP) {
  176. while (mtl) {
  177. m = mtl->next;
  178. free(mtl->device);
  179. free(mtl->dir);
  180. free(mtl);
  181. mtl = m;
  182. }
  183. free(path);
  184. }
  185. return status;
  186. }