123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- #include <mntent.h>
- #include <sys/mount.h>
- #ifndef MNT_DETACH
- # define MNT_DETACH 0x00000002
- #endif
- #include "libbb.h"
- #include "common_bufsiz.h"
- #if defined(__dietlibc__)
- static struct mntent *getmntent_r(FILE* stream, struct mntent* result,
- char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM)
- {
- struct mntent* ment = getmntent(stream);
- return memcpy(result, ment, sizeof(*ment));
- }
- #endif
- #define OPTION_STRING "fldnrat:" "cvi"
- #define OPT_FORCE (1 << 0)
- #define OPT_LAZY (1 << 1)
- #define OPT_FREELOOP (1 << 2)
- #define OPT_NO_MTAB (1 << 3)
- #define OPT_REMOUNT (1 << 4)
- #define OPT_ALL (ENABLE_FEATURE_UMOUNT_ALL ? (1 << 5) : 0)
- int umount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int umount_main(int argc UNUSED_PARAM, char **argv)
- {
- int doForce;
- struct mntent me;
- FILE *fp;
- char *fstype = NULL;
- exitcode_t status = EXIT_SUCCESS;
- unsigned opt;
- struct mtab_list {
- char *dir;
- char *device;
- struct mtab_list *next;
- } *mtl, *m;
- opt = getopt32(argv, OPTION_STRING, &fstype);
-
- argv += optind;
-
-
- BUILD_BUG_ON(OPT_FORCE != MNT_FORCE || OPT_LAZY != MNT_DETACH);
- doForce = opt & (OPT_FORCE|OPT_LAZY);
-
- m = mtl = NULL;
-
-
- fp = setmntent(bb_path_mtab_file, "r");
- if (!fp) {
- if (opt & OPT_ALL)
- bb_error_msg_and_die("can't open '%s'", bb_path_mtab_file);
- } else {
- setup_common_bufsiz();
- while (getmntent_r(fp, &me, bb_common_bufsiz1, COMMON_BUFSIZE)) {
-
- if (!fstype_matches(me.mnt_type, fstype))
- continue;
- m = xzalloc(sizeof(*m));
- m->next = mtl;
- m->device = xstrdup(me.mnt_fsname);
- m->dir = xstrdup(me.mnt_dir);
- mtl = m;
- }
- endmntent(fp);
- }
-
-
- if (!(opt & OPT_ALL)) {
- if (!argv[0])
- bb_show_usage();
- m = NULL;
- }
-
- for (;;) {
- int curstat;
- char *zapit = *argv;
- char *path;
-
- if (m)
- path = xstrdup(m->dir);
-
- else if (opt & OPT_ALL)
- break;
-
- else {
- if (!zapit)
- break;
- argv++;
- path = xmalloc_realpath(zapit);
- if (path) {
- for (m = mtl; m; m = m->next)
- if (strcmp(path, m->dir) == 0 || strcmp(path, m->device) == 0)
- break;
- }
- }
-
-
-
- if (m) zapit = m->dir;
- #if 0
-
- curstat = umount(zapit);
-
- if (curstat && doForce)
- #endif
- curstat = umount2(zapit, doForce);
-
- if (curstat) {
- if ((opt & OPT_REMOUNT) && errno == EBUSY && m) {
-
-
- const char *msg = "%s busy - remounted read-only";
- curstat = mount(m->device, zapit, NULL, MS_REMOUNT|MS_RDONLY, NULL);
- if (curstat) {
- msg = "can't remount %s read-only";
- status = EXIT_FAILURE;
- }
- bb_error_msg(msg, m->device);
- } else {
- status = EXIT_FAILURE;
- bb_perror_msg("can't unmount %s", zapit);
- }
- } else {
-
-
- if (ENABLE_FEATURE_MOUNT_LOOP && (opt & OPT_FREELOOP) && m)
- del_loop(m->device);
- if (ENABLE_FEATURE_MTAB_SUPPORT && !(opt & OPT_NO_MTAB) && m)
- erase_mtab(m->dir);
- }
-
-
-
- if (m) {
- while ((m = m->next) != NULL)
-
- if ((opt & OPT_ALL) || strcmp(path, m->device) == 0)
- break;
- }
- free(path);
- }
-
- if (ENABLE_FEATURE_CLEAN_UP) {
- while (mtl) {
- m = mtl->next;
- free(mtl->device);
- free(mtl->dir);
- free(mtl);
- mtl = m;
- }
- }
- return status;
- }
|