2000-02-17 20:38:33 +01:00
|
|
|
/* umount.cc
|
|
|
|
|
|
|
|
Copyright 1996, 1998, 1999 Cygnus Solutions.
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <mntent.h>
|
2000-06-05 20:43:54 +02:00
|
|
|
#include <stdlib.h>
|
2000-06-08 14:54:12 +02:00
|
|
|
#include <errno.h>
|
2001-04-03 04:41:54 +02:00
|
|
|
#include <getopt.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
static void remove_all_mounts ();
|
|
|
|
static void remove_all_user_mounts ();
|
|
|
|
static void remove_all_system_mounts ();
|
2000-07-29 00:34:24 +02:00
|
|
|
static void remove_cygdrive_prefix (int flags);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
static const char *progname;
|
|
|
|
|
2001-04-03 04:41:54 +02:00
|
|
|
struct option longopts[] =
|
|
|
|
{
|
2001-04-16 04:51:03 +02:00
|
|
|
{"help", no_argument, NULL, 'h' },
|
2001-04-03 04:41:54 +02:00
|
|
|
{"remove-all-mounts", no_argument, NULL, 'A'},
|
|
|
|
{"remove-cygdrive-prefix", no_argument, NULL, 'c'},
|
|
|
|
{"remove-system-mounts", no_argument, NULL, 'S'},
|
|
|
|
{"remove-user-mounts", no_argument, NULL, 'U'},
|
|
|
|
{"system", no_argument, NULL, 's'},
|
|
|
|
{"user", no_argument, NULL, 'u'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2001-04-16 04:51:03 +02:00
|
|
|
char opts[] = "hASUsuc";
|
2001-04-03 04:41:54 +02:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
static void
|
|
|
|
usage (void)
|
|
|
|
{
|
2001-04-03 04:41:54 +02:00
|
|
|
fprintf (stderr, "Usage %s [OPTION] [<posixpath>]\n\
|
|
|
|
-A, --remove-all-mounts remove all mounts\n\
|
|
|
|
-c, --remove-cygdrive-prefix remove cygdrive prefix\n\
|
|
|
|
-s, --system remove system mount\n\
|
|
|
|
-S, --remove-system-mounts remove all system mounts\n\
|
|
|
|
-u, --user remove user mount\n\
|
|
|
|
-U, --remove-user-mounts remove all user mounts\n\
|
|
|
|
", progname);
|
2000-02-17 20:38:33 +01:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2000-06-08 14:54:12 +02:00
|
|
|
static void
|
2000-07-29 00:34:24 +02:00
|
|
|
error (const char *path)
|
2000-06-08 14:54:12 +02:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: %s: %s\n", progname, path, strerror (errno));
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int flags = 0;
|
|
|
|
progname = argv[0];
|
2001-04-03 04:41:54 +02:00
|
|
|
enum do_what
|
|
|
|
{
|
|
|
|
nada,
|
|
|
|
saw_remove_all_mounts,
|
|
|
|
saw_remove_cygdrive_prefix,
|
|
|
|
saw_remove_all_system_mounts,
|
|
|
|
saw_remove_all_user_mounts
|
|
|
|
} do_what = nada;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
usage ();
|
|
|
|
|
2001-04-03 04:41:54 +02:00
|
|
|
while ((i = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
|
|
|
|
switch (i)
|
|
|
|
{
|
|
|
|
case 'A':
|
|
|
|
if (do_what != nada)
|
|
|
|
usage ();
|
|
|
|
do_what = saw_remove_all_mounts;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (do_what != nada)
|
|
|
|
usage ();
|
|
|
|
do_what = saw_remove_cygdrive_prefix;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
flags |= MOUNT_SYSTEM;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
if (do_what != nada)
|
|
|
|
usage ();
|
|
|
|
do_what = saw_remove_all_system_mounts;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
flags &= ~MOUNT_SYSTEM;
|
2000-10-28 07:00:00 +02:00
|
|
|
break;
|
2001-04-03 04:41:54 +02:00
|
|
|
case 'U':
|
|
|
|
if (do_what != nada)
|
|
|
|
usage ();
|
|
|
|
do_what = saw_remove_all_user_mounts;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage ();
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-04-03 04:41:54 +02:00
|
|
|
switch (do_what)
|
|
|
|
{
|
|
|
|
case saw_remove_all_mounts:
|
|
|
|
if (optind != argc)
|
|
|
|
usage ();
|
|
|
|
remove_all_mounts ();
|
|
|
|
break;
|
|
|
|
case saw_remove_cygdrive_prefix:
|
|
|
|
if (optind != argc)
|
|
|
|
usage ();
|
|
|
|
remove_cygdrive_prefix (flags);
|
|
|
|
break;
|
|
|
|
case saw_remove_all_system_mounts:
|
|
|
|
if (optind != argc)
|
|
|
|
usage ();
|
|
|
|
remove_all_system_mounts ();
|
|
|
|
break;
|
|
|
|
case saw_remove_all_user_mounts:
|
|
|
|
if (optind != argc)
|
2000-02-17 20:38:33 +01:00
|
|
|
usage ();
|
2001-04-03 04:41:54 +02:00
|
|
|
remove_all_user_mounts ();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (optind != argc - 1)
|
|
|
|
usage ();
|
|
|
|
if (cygwin_umount (argv[optind], flags) != 0)
|
|
|
|
error (argv[optind]);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove_all_mounts: Unmount all mounts. */
|
|
|
|
static void
|
|
|
|
remove_all_mounts ()
|
|
|
|
{
|
|
|
|
remove_all_user_mounts ();
|
|
|
|
remove_all_system_mounts ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove_all_user_mounts: Unmount all user mounts. */
|
|
|
|
static void
|
|
|
|
remove_all_user_mounts ()
|
|
|
|
{
|
|
|
|
FILE *m = setmntent ("/-not-used-", "r");
|
|
|
|
struct mntent *p;
|
|
|
|
|
|
|
|
while ((p = getmntent (m)) != NULL)
|
|
|
|
{
|
|
|
|
/* Remove the mount if it's a user mount. */
|
2001-06-15 06:50:57 +02:00
|
|
|
if (strncmp (p->mnt_type, "user", 4) == 0 &&
|
|
|
|
strstr (p->mnt_opts, "noumount") == NULL)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-06-08 14:54:12 +02:00
|
|
|
if (cygwin_umount (p->mnt_dir, 0))
|
|
|
|
error (p->mnt_dir);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* We've modified the table so we need to start over. */
|
|
|
|
endmntent (m);
|
|
|
|
m = setmntent ("/-not-used-", "r");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endmntent (m);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove_all_system_mounts: Unmount all system mounts. */
|
|
|
|
static void
|
|
|
|
remove_all_system_mounts ()
|
|
|
|
{
|
|
|
|
FILE *m = setmntent ("/-not-used-", "r");
|
|
|
|
struct mntent *p;
|
|
|
|
|
|
|
|
while ((p = getmntent (m)) != NULL)
|
|
|
|
{
|
|
|
|
/* Remove the mount if it's a system mount. */
|
2001-06-15 06:50:57 +02:00
|
|
|
if (strncmp (p->mnt_type, "system", 6) == 0 &&
|
|
|
|
strstr (p->mnt_opts, "noumount") == NULL)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-06-08 14:54:12 +02:00
|
|
|
if (cygwin_umount (p->mnt_dir, MOUNT_SYSTEM))
|
|
|
|
error (p->mnt_dir);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* We've modified the table so we need to start over. */
|
|
|
|
endmntent (m);
|
|
|
|
m = setmntent ("/-not-used-", "r");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endmntent (m);
|
|
|
|
}
|
2000-07-29 00:34:24 +02:00
|
|
|
|
|
|
|
/* remove_cygdrive_prefix: Remove cygdrive user or system path prefix. */
|
|
|
|
static void
|
|
|
|
remove_cygdrive_prefix (int flags)
|
|
|
|
{
|
2000-10-28 07:00:00 +02:00
|
|
|
int res = cygwin_umount (NULL, flags | MOUNT_AUTO);
|
2000-07-29 00:34:24 +02:00
|
|
|
if (res)
|
|
|
|
error ("remove_cygdrive_prefix");
|
2001-04-03 04:41:54 +02:00
|
|
|
exit (0);
|
2000-07-29 00:34:24 +02:00
|
|
|
}
|