umount.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. //config:config UMOUNT
  11. //config: bool "umount (5.1 kb)"
  12. //config: default y
  13. //config: select PLATFORM_LINUX
  14. //config: help
  15. //config: When you want to remove a mounted filesystem from its current mount
  16. //config: point, for example when you are shutting down the system, the
  17. //config: 'umount' utility is the tool to use. If you enabled the 'mount'
  18. //config: utility, you almost certainly also want to enable 'umount'.
  19. //config:
  20. //config:config FEATURE_UMOUNT_ALL
  21. //config: bool "Support -a (unmount all)"
  22. //config: default y
  23. //config: depends on UMOUNT
  24. //config: help
  25. //config: Support -a option to unmount all currently mounted filesystems.
  26. //applet:IF_UMOUNT(APPLET_NOEXEC(umount, umount, BB_DIR_BIN, BB_SUID_DROP, umount))
  27. /*
  28. * On one hand, in some weird situations you'd want umount
  29. * to not do anything surprising, to behave as a usual fork+execed executable.
  30. *
  31. * OTOH, there can be situations where execing would not succeed, or even hang
  32. * (say, if executable is on a filesystem which is in trouble and accesses to it
  33. * block in kernel).
  34. * In this case, you might be actually happy if your standalone bbox shell
  35. * does not fork+exec, but only forks and calls umount_main() which it already has!
  36. * Let's go with NOEXEC.
  37. *
  38. * bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed.
  39. */
  40. //kbuild:lib-$(CONFIG_UMOUNT) += umount.o
  41. //usage:#define umount_trivial_usage
  42. //usage: "[OPTIONS] FILESYSTEM|DIRECTORY"
  43. //usage:#define umount_full_usage "\n\n"
  44. //usage: "Unmount file systems\n"
  45. //usage: IF_FEATURE_UMOUNT_ALL(
  46. //usage: "\n -a Unmount all file systems" IF_FEATURE_MTAB_SUPPORT(" in /etc/mtab")
  47. //usage: )
  48. //usage: IF_FEATURE_MTAB_SUPPORT(
  49. //usage: "\n -n Don't erase /etc/mtab entries"
  50. //usage: )
  51. //usage: "\n -r Try to remount devices as read-only if mount is busy"
  52. //usage: "\n -l Lazy umount (detach filesystem)"
  53. //usage: "\n -f Force umount (i.e., unreachable NFS server)"
  54. //usage: IF_FEATURE_MOUNT_LOOP(
  55. //usage: "\n -d Free loop device if it has been used"
  56. //usage: )
  57. //usage: "\n -t FSTYPE[,...] Unmount only these filesystem type(s)"
  58. //usage:
  59. //usage:#define umount_example_usage
  60. //usage: "$ umount /dev/hdc1\n"
  61. #include <mntent.h>
  62. #include <sys/mount.h>
  63. #ifndef MNT_DETACH
  64. # define MNT_DETACH 0x00000002
  65. #endif
  66. #include "libbb.h"
  67. #include "common_bufsiz.h"
  68. #if defined(__dietlibc__)
  69. // TODO: This does not belong here.
  70. /* 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
  71. * dietlibc-0.30 does not have implementation of getmntent_r() */
  72. static struct mntent *getmntent_r(FILE* stream, struct mntent* result,
  73. char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM)
  74. {
  75. struct mntent* ment = getmntent(stream);
  76. return memcpy(result, ment, sizeof(*ment));
  77. }
  78. #endif
  79. /* ignored: -c -v -i */
  80. #define OPTION_STRING "fldnrat:" "cvi"
  81. #define OPT_FORCE (1 << 0) // Same as MNT_FORCE
  82. #define OPT_LAZY (1 << 1) // Same as MNT_DETACH
  83. #define OPT_FREELOOP (1 << 2)
  84. #define OPT_NO_MTAB (1 << 3)
  85. #define OPT_REMOUNT (1 << 4)
  86. #define OPT_ALL (ENABLE_FEATURE_UMOUNT_ALL ? (1 << 5) : 0)
  87. int umount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  88. int umount_main(int argc UNUSED_PARAM, char **argv)
  89. {
  90. int doForce;
  91. struct mntent me;
  92. FILE *fp;
  93. char *fstype = NULL;
  94. int status = EXIT_SUCCESS;
  95. unsigned opt;
  96. struct mtab_list {
  97. char *dir;
  98. char *device;
  99. struct mtab_list *next;
  100. } *mtl, *m;
  101. opt = getopt32(argv, OPTION_STRING, &fstype);
  102. //argc -= optind;
  103. argv += optind;
  104. // MNT_FORCE and MNT_DETACH (from linux/fs.h) must match
  105. // OPT_FORCE and OPT_LAZY.
  106. BUILD_BUG_ON(OPT_FORCE != MNT_FORCE || OPT_LAZY != MNT_DETACH);
  107. doForce = opt & (OPT_FORCE|OPT_LAZY);
  108. /* Get a list of mount points from mtab. We read them all in now mostly
  109. * for umount -a (so we don't have to worry about the list changing while
  110. * we iterate over it, or about getting stuck in a loop on the same failing
  111. * entry. Notice that this also naturally reverses the list so that -a
  112. * umounts the most recent entries first. */
  113. m = mtl = NULL;
  114. // If we're umounting all, then m points to the start of the list and
  115. // the argument list should be empty (which will match all).
  116. fp = setmntent(bb_path_mtab_file, "r");
  117. if (!fp) {
  118. if (opt & OPT_ALL)
  119. bb_error_msg_and_die("can't open '%s'", bb_path_mtab_file);
  120. } else {
  121. setup_common_bufsiz();
  122. while (getmntent_r(fp, &me, bb_common_bufsiz1, COMMON_BUFSIZE)) {
  123. /* Match fstype (fstype==NULL matches always) */
  124. if (!fstype_matches(me.mnt_type, fstype))
  125. continue;
  126. m = xzalloc(sizeof(*m));
  127. m->next = mtl;
  128. m->device = xstrdup(me.mnt_fsname);
  129. m->dir = xstrdup(me.mnt_dir);
  130. mtl = m;
  131. }
  132. endmntent(fp);
  133. }
  134. // If we're not umounting all, we need at least one argument.
  135. // Note: "-t FSTYPE" does not imply -a.
  136. if (!(opt & OPT_ALL)) {
  137. if (!argv[0])
  138. bb_show_usage();
  139. m = NULL;
  140. }
  141. // Loop through everything we're supposed to umount, and do so.
  142. for (;;) {
  143. int curstat;
  144. char *zapit = *argv;
  145. char *path;
  146. // Do we already know what to umount this time through the loop?
  147. if (m)
  148. path = xstrdup(m->dir);
  149. // For umount -a, end of mtab means time to exit.
  150. else if (opt & OPT_ALL)
  151. break;
  152. // Use command line argument (and look it up in mtab list)
  153. else {
  154. if (!zapit)
  155. break;
  156. argv++;
  157. path = xmalloc_realpath(zapit);
  158. if (path) {
  159. for (m = mtl; m; m = m->next)
  160. if (strcmp(path, m->dir) == 0 || strcmp(path, m->device) == 0)
  161. break;
  162. }
  163. }
  164. // If we couldn't find this sucker in /etc/mtab, punt by passing our
  165. // command line argument straight to the umount syscall. Otherwise,
  166. // umount the directory even if we were given the block device.
  167. if (m) zapit = m->dir;
  168. // umount from util-linux 2.22.2 does not do this:
  169. // umount -f uses umount2(MNT_FORCE) immediately,
  170. // not trying umount() first.
  171. // (Strangely, umount -fl ignores -f: it is equivalent to umount -l.
  172. // We do pass both flags in this case)
  173. #if 0
  174. // Let's ask the thing nicely to unmount.
  175. curstat = umount(zapit);
  176. // Unmount with force and/or lazy flags, if necessary.
  177. if (curstat && doForce)
  178. #endif
  179. curstat = umount2(zapit, doForce);
  180. // If still can't umount, maybe remount read-only?
  181. if (curstat) {
  182. if ((opt & OPT_REMOUNT) && errno == EBUSY && m) {
  183. // Note! Even if we succeed here, later we should not
  184. // free loop device or erase mtab entry!
  185. const char *msg = "%s busy - remounted read-only";
  186. curstat = mount(m->device, zapit, NULL, MS_REMOUNT|MS_RDONLY, NULL);
  187. if (curstat) {
  188. msg = "can't remount %s read-only";
  189. status = EXIT_FAILURE;
  190. }
  191. bb_error_msg(msg, m->device);
  192. } else {
  193. status = EXIT_FAILURE;
  194. bb_perror_msg("can't unmount %s", zapit);
  195. }
  196. } else {
  197. // De-allocate the loop device. This ioctl should be ignored on
  198. // any non-loop block devices.
  199. if (ENABLE_FEATURE_MOUNT_LOOP && (opt & OPT_FREELOOP) && m)
  200. del_loop(m->device);
  201. if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m)
  202. erase_mtab(m->dir);
  203. }
  204. // Find next matching mtab entry for -a or umount /dev
  205. // Note this means that "umount /dev/blah" will unmount all instances
  206. // of /dev/blah, not just the most recent.
  207. if (m) {
  208. while ((m = m->next) != NULL)
  209. // NB: if m is non-NULL, path is non-NULL as well
  210. if ((opt & OPT_ALL) || strcmp(path, m->device) == 0)
  211. break;
  212. }
  213. free(path);
  214. }
  215. // Free mtab list if necessary
  216. if (ENABLE_FEATURE_CLEAN_UP) {
  217. while (mtl) {
  218. m = mtl->next;
  219. free(mtl->device);
  220. free(mtl->dir);
  221. free(mtl);
  222. mtl = m;
  223. }
  224. }
  225. return status;
  226. }