2005-04-21 16:53:53 +02:00
|
|
|
/* umount - unmount a file system Author: Andy Tanenbaum */
|
|
|
|
|
|
|
|
#define _MINIX 1 /* for proto of the non-POSIX umount() */
|
|
|
|
#define _POSIX_SOURCE 1 /* for PATH_MAX from limits.h */
|
|
|
|
|
2006-06-20 10:46:45 +02:00
|
|
|
#include <minix/type.h>
|
2005-04-21 16:53:53 +02:00
|
|
|
#include <sys/types.h>
|
2010-01-13 00:08:50 +01:00
|
|
|
#include <sys/stat.h>
|
2005-04-21 16:53:53 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <minix/minlib.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2009-08-12 21:57:37 +02:00
|
|
|
#include <sys/mount.h>
|
2005-04-21 16:53:53 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
_PROTOTYPE(int main, (int argc, char **argv));
|
2010-01-13 00:08:50 +01:00
|
|
|
_PROTOTYPE(int find_mtab_entry, (char *name));
|
|
|
|
_PROTOTYPE(void update_mtab, (void));
|
2005-04-21 16:53:53 +02:00
|
|
|
_PROTOTYPE(void usage, (void));
|
|
|
|
|
2010-01-13 00:08:50 +01:00
|
|
|
static char device[PATH_MAX+1], mountpoint[PATH_MAX+1], vs[10], rw[10];
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
int main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
2010-01-13 00:08:50 +01:00
|
|
|
int found;
|
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
if (argc != 2) usage();
|
2010-01-13 00:08:50 +01:00
|
|
|
found = find_mtab_entry(argv[1]);
|
2009-04-27 16:23:57 +02:00
|
|
|
if (umount(argv[1]) < 0) {
|
2005-04-21 16:53:53 +02:00
|
|
|
if (errno == EINVAL)
|
2010-01-13 00:08:50 +01:00
|
|
|
std_err("umount: Device not mounted\n");
|
|
|
|
else if (errno == ENOTBLK)
|
|
|
|
std_err("unount: Not a mountpoint\n");
|
2005-04-21 16:53:53 +02:00
|
|
|
else
|
|
|
|
perror("umount");
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-01-13 00:08:50 +01:00
|
|
|
if (found) {
|
|
|
|
printf("%s unmounted from %s\n", device, mountpoint);
|
|
|
|
update_mtab();
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
2010-01-13 00:08:50 +01:00
|
|
|
else printf("%s unmounted (mtab not updated)\n", argv[1]);
|
2005-04-21 16:53:53 +02:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2010-01-13 00:08:50 +01:00
|
|
|
int find_mtab_entry(name)
|
|
|
|
char *name;
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-01-13 00:08:50 +01:00
|
|
|
/* Find a matching mtab entry for 'name' which may be a special or a path,
|
|
|
|
* and generate a new mtab file without this entry on the fly. Do not write
|
|
|
|
* out the result yet. Return whether we found a matching entry.
|
|
|
|
*/
|
2005-04-21 16:53:53 +02:00
|
|
|
char special[PATH_MAX+1], mounted_on[PATH_MAX+1], version[10], rw_flag[10];
|
2010-01-13 00:08:50 +01:00
|
|
|
struct stat nstat, mstat;
|
|
|
|
int n, found;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2010-01-13 00:08:50 +01:00
|
|
|
if (load_mtab("umount") < 0) return 0;
|
|
|
|
|
|
|
|
if (stat(name, &nstat) != 0) return 0;
|
|
|
|
|
|
|
|
found = 0;
|
2005-04-21 16:53:53 +02:00
|
|
|
while (1) {
|
|
|
|
n = get_mtab_entry(special, mounted_on, version, rw_flag);
|
|
|
|
if (n < 0) break;
|
2010-01-13 00:08:50 +01:00
|
|
|
if (strcmp(name, special) == 0 || (stat(mounted_on, &mstat) == 0 &&
|
|
|
|
mstat.st_dev == nstat.st_dev && mstat.st_ino == nstat.st_ino))
|
|
|
|
{
|
|
|
|
/* If we found an earlier match, keep that one. Mountpoints
|
|
|
|
* may be stacked on top of each other, and unmounting should
|
|
|
|
* take place in the reverse order of mounting.
|
|
|
|
*/
|
|
|
|
if (found) {
|
|
|
|
(void) put_mtab_entry(device, mountpoint, vs, rw);
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(device, special);
|
2005-04-21 16:53:53 +02:00
|
|
|
strcpy(mountpoint, mounted_on);
|
2010-01-13 00:08:50 +01:00
|
|
|
strcpy(vs, version);
|
|
|
|
strcpy(rw, rw_flag);
|
|
|
|
found = 1;
|
2005-04-21 16:53:53 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(void) put_mtab_entry(special, mounted_on, version, rw_flag);
|
|
|
|
}
|
2010-01-13 00:08:50 +01:00
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_mtab()
|
|
|
|
{
|
|
|
|
/* Write out the new mtab file. */
|
|
|
|
int n;
|
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
n = rewrite_mtab("umount");
|
|
|
|
if (n < 0) {
|
|
|
|
std_err("/etc/mtab not updated.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void usage()
|
|
|
|
{
|
2010-01-13 00:08:50 +01:00
|
|
|
std_err("Usage: umount name\n");
|
2005-04-21 16:53:53 +02:00
|
|
|
exit(1);
|
|
|
|
}
|