umount.c 5.1 KB

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