umount.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <limits.h>
  23. #include <stdio.h>
  24. #include <mntent.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include "busybox.h"
  29. /* Teach libc5 about realpath -- it includes it but the
  30. * prototype is missing... */
  31. #if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
  32. extern char *realpath(const char *path, char *resolved_path);
  33. #endif
  34. static const int MNT_FORCE = 1;
  35. static const int MS_MGC_VAL = 0xc0ed0000; /* Magic number indicatng "new" flags */
  36. static const int MS_REMOUNT = 32; /* Alter flags of a mounted FS. */
  37. static const int MS_RDONLY = 1; /* Mount read-only. */
  38. extern int mount (__const char *__special_file, __const char *__dir,
  39. __const char *__fstype, unsigned long int __rwflag,
  40. __const void *__data);
  41. extern int umount (__const char *__special_file);
  42. extern int umount2 (__const char *__special_file, int __flags);
  43. struct _mtab_entry_t {
  44. char *device;
  45. char *mountpt;
  46. struct _mtab_entry_t *next;
  47. };
  48. static struct _mtab_entry_t *mtab_cache = NULL;
  49. #if defined CONFIG_FEATURE_MOUNT_FORCE
  50. static int doForce = FALSE;
  51. #endif
  52. #if defined CONFIG_FEATURE_MOUNT_LOOP
  53. static int freeLoop = TRUE;
  54. #endif
  55. #if defined CONFIG_FEATURE_MTAB_SUPPORT
  56. static int useMtab = TRUE;
  57. #endif
  58. static int umountAll = FALSE;
  59. static int doRemount = FALSE;
  60. /* These functions are here because the getmntent functions do not appear
  61. * to be re-entrant, which leads to all sorts of problems when we try to
  62. * use them recursively - randolph
  63. *
  64. * TODO: Perhaps switch to using Glibc's getmntent_r
  65. * -Erik
  66. */
  67. static void mtab_read(void)
  68. {
  69. struct _mtab_entry_t *entry = NULL;
  70. struct mntent *e;
  71. FILE *fp;
  72. if (mtab_cache != NULL)
  73. return;
  74. if ((fp = setmntent(bb_path_mtab_file, "r")) == NULL) {
  75. bb_error_msg("Cannot open %s", bb_path_mtab_file);
  76. return;
  77. }
  78. while ((e = getmntent(fp))) {
  79. entry = xmalloc(sizeof(struct _mtab_entry_t));
  80. entry->device = strdup(e->mnt_fsname);
  81. entry->mountpt = strdup(e->mnt_dir);
  82. entry->next = mtab_cache;
  83. mtab_cache = entry;
  84. }
  85. endmntent(fp);
  86. }
  87. static char *mtab_getinfo(const char *match, const char which)
  88. {
  89. struct _mtab_entry_t *cur = mtab_cache;
  90. while (cur) {
  91. if (strcmp(cur->mountpt, match) == 0 ||
  92. strcmp(cur->device, match) == 0) {
  93. if (which == MTAB_GETMOUNTPT) {
  94. return cur->mountpt;
  95. } else {
  96. #if !defined CONFIG_FEATURE_MTAB_SUPPORT
  97. if (strcmp(cur->device, "rootfs") == 0) {
  98. continue;
  99. } else if (strcmp(cur->device, "/dev/root") == 0) {
  100. /* Adjusts device to be the real root device,
  101. * or leaves device alone if it can't find it */
  102. cur->device = find_real_root_device_name();
  103. }
  104. #endif
  105. return cur->device;
  106. }
  107. }
  108. cur = cur->next;
  109. }
  110. return NULL;
  111. }
  112. static char *mtab_next(void **iter)
  113. {
  114. char *mp;
  115. if (iter == NULL || *iter == NULL)
  116. return NULL;
  117. mp = ((struct _mtab_entry_t *) (*iter))->mountpt;
  118. *iter = (void *) ((struct _mtab_entry_t *) (*iter))->next;
  119. return mp;
  120. }
  121. static char *mtab_first(void **iter)
  122. {
  123. struct _mtab_entry_t *mtab_iter;
  124. if (!iter)
  125. return NULL;
  126. mtab_iter = mtab_cache;
  127. *iter = (void *) mtab_iter;
  128. return mtab_next(iter);
  129. }
  130. /* Don't bother to clean up, since exit() does that
  131. * automagically, so we can save a few bytes */
  132. #ifdef CONFIG_FEATURE_CLEAN_UP
  133. static void mtab_free(void)
  134. {
  135. struct _mtab_entry_t *this, *next;
  136. this = mtab_cache;
  137. while (this) {
  138. next = this->next;
  139. free(this->device);
  140. free(this->mountpt);
  141. free(this);
  142. this = next;
  143. }
  144. }
  145. #endif
  146. static int do_umount(const char *name)
  147. {
  148. int status;
  149. char *blockDevice = mtab_getinfo(name, MTAB_GETDEVICE);
  150. if (blockDevice && strcmp(blockDevice, name) == 0)
  151. name = mtab_getinfo(blockDevice, MTAB_GETMOUNTPT);
  152. status = umount(name);
  153. #if defined CONFIG_FEATURE_MOUNT_LOOP
  154. if (freeLoop && blockDevice != NULL && !strncmp("/dev/loop", blockDevice, 9))
  155. /* this was a loop device, delete it */
  156. del_loop(blockDevice);
  157. #endif
  158. #if defined CONFIG_FEATURE_MOUNT_FORCE
  159. if (status != 0 && doForce) {
  160. status = umount2(blockDevice, MNT_FORCE);
  161. if (status != 0) {
  162. bb_error_msg_and_die("forced umount of %s failed!", blockDevice);
  163. }
  164. }
  165. #endif
  166. if (status != 0 && doRemount && errno == EBUSY) {
  167. status = mount(blockDevice, name, NULL,
  168. MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
  169. if (status == 0) {
  170. bb_error_msg("%s busy - remounted read-only", blockDevice);
  171. } else {
  172. bb_error_msg("Cannot remount %s read-only", blockDevice);
  173. }
  174. }
  175. if (status == 0) {
  176. #if defined CONFIG_FEATURE_MTAB_SUPPORT
  177. if (useMtab)
  178. erase_mtab(name);
  179. #endif
  180. return (TRUE);
  181. }
  182. return (FALSE);
  183. }
  184. static int umount_all(void)
  185. {
  186. int status = TRUE;
  187. char *mountpt;
  188. void *iter;
  189. for (mountpt = mtab_first(&iter); mountpt; mountpt = mtab_next(&iter)) {
  190. /* Never umount /proc on a umount -a */
  191. if (strstr(mountpt, "proc")!= NULL)
  192. continue;
  193. if (!do_umount(mountpt)) {
  194. /* Don't bother retrying the umount on busy devices */
  195. if (errno == EBUSY) {
  196. bb_perror_msg("%s", mountpt);
  197. status = FALSE;
  198. continue;
  199. }
  200. if (!do_umount(mountpt)) {
  201. printf("Couldn't umount %s on %s: %s\n",
  202. mountpt, mtab_getinfo(mountpt, MTAB_GETDEVICE),
  203. strerror(errno));
  204. status = FALSE;
  205. }
  206. }
  207. }
  208. return (status);
  209. }
  210. extern int umount_main(int argc, char **argv)
  211. {
  212. char path[PATH_MAX], result = 0;
  213. if (argc < 2) {
  214. bb_show_usage();
  215. }
  216. #ifdef CONFIG_FEATURE_CLEAN_UP
  217. atexit(mtab_free);
  218. #endif
  219. /* Parse any options */
  220. while (--argc > 0 && **(++argv) == '-') {
  221. while (*++(*argv))
  222. switch (**argv) {
  223. case 'a':
  224. umountAll = TRUE;
  225. break;
  226. #if defined CONFIG_FEATURE_MOUNT_LOOP
  227. case 'l':
  228. freeLoop = FALSE;
  229. break;
  230. #endif
  231. #ifdef CONFIG_FEATURE_MTAB_SUPPORT
  232. case 'n':
  233. useMtab = FALSE;
  234. break;
  235. #endif
  236. #ifdef CONFIG_FEATURE_MOUNT_FORCE
  237. case 'f':
  238. doForce = TRUE;
  239. break;
  240. #endif
  241. case 'r':
  242. doRemount = TRUE;
  243. break;
  244. case 'v':
  245. break; /* ignore -v */
  246. default:
  247. bb_show_usage();
  248. }
  249. }
  250. mtab_read();
  251. if (umountAll) {
  252. if (umount_all())
  253. return EXIT_SUCCESS;
  254. else
  255. return EXIT_FAILURE;
  256. }
  257. do {
  258. if (realpath(*argv, path) != NULL)
  259. if (do_umount(path))
  260. continue;
  261. bb_perror_msg("%s", path);
  262. result++;
  263. } while (--argc > 0 && ++argv);
  264. return result;
  265. }