123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- #include "busybox.h"
- #include <mntent.h>
- #include <getopt.h>
- #define OPTION_STRING "flDnravd"
- #define OPT_FORCE 1
- #define OPT_LAZY 2
- #define OPT_DONTFREELOOP 4
- #define OPT_NO_MTAB 8
- #define OPT_REMOUNT 16
- #define OPT_ALL (ENABLE_FEATURE_UMOUNT_ALL ? 32 : 0)
- int umount_main(int argc, char **argv)
- {
- int doForce;
- char path[2*PATH_MAX];
- struct mntent me;
- FILE *fp;
- int status = EXIT_SUCCESS;
- unsigned opt;
- struct mtab_list {
- char *dir;
- char *device;
- struct mtab_list *next;
- } *mtl, *m;
-
- opt = getopt32(argc, argv, OPTION_STRING);
- argc -= optind;
- argv += optind;
- doForce = MAX((opt & OPT_FORCE), (opt & OPT_LAZY));
-
- m = mtl = 0;
-
- fp = setmntent(bb_path_mtab_file, "r");
- if (!fp) {
- if (opt & OPT_ALL)
- bb_error_msg_and_die("cannot open %s", bb_path_mtab_file);
- } else {
- while (getmntent_r(fp, &me, path, sizeof(path))) {
- m = xmalloc(sizeof(struct mtab_list));
- m->next = mtl;
- m->device = xstrdup(me.mnt_fsname);
- m->dir = xstrdup(me.mnt_dir);
- mtl = m;
- }
- endmntent(fp);
- }
-
- if (!(opt & OPT_ALL)) {
- m = 0;
- if (!argc) bb_show_usage();
- }
-
- for (;;) {
- int curstat;
- char *zapit = *argv;
-
- if (m) safe_strncpy(path, m->dir, PATH_MAX);
-
- else if (opt & OPT_ALL) break;
-
- else if (!argc--) break;
- else {
- argv++;
- realpath(zapit, path);
- for (m = mtl; m; m = m->next)
- if (!strcmp(path, m->dir) || !strcmp(path, m->device))
- break;
- }
-
-
-
- if (m) zapit = m->dir;
-
- curstat = umount(zapit);
-
- if (curstat && doForce) {
- curstat = umount2(zapit, doForce);
- if (curstat)
- bb_error_msg("forced umount of %s failed!", zapit);
- }
-
- if (curstat && (opt & OPT_REMOUNT) && errno == EBUSY && m) {
- curstat = mount(m->device, zapit, NULL, MS_REMOUNT|MS_RDONLY, NULL);
- bb_error_msg(curstat ? "cannot remount %s read-only" :
- "%s busy - remounted read-only", m->device);
- }
- if (curstat) {
- status = EXIT_FAILURE;
- bb_perror_msg("cannot umount %s", zapit);
- } else {
-
- if (ENABLE_FEATURE_MOUNT_LOOP && !(opt & OPT_DONTFREELOOP) && m)
- del_loop(m->device);
- if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m)
- erase_mtab(m->dir);
- }
-
-
-
- while (m && (m = m->next))
- if ((opt & OPT_ALL) || !strcmp(path, m->device))
- break;
- }
-
- if (ENABLE_FEATURE_CLEAN_UP) {
- while (mtl) {
- m = mtl->next;
- free(mtl->device);
- free(mtl->dir);
- free(mtl);
- mtl = m;
- }
- }
- return status;
- }
|