umount.c 5.2 KB

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