2013-08-07 06:41:42 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-06-30 14:03:41 -04:00
|
|
|
#include <sys/mount.h>
|
|
|
|
|
2014-03-07 08:49:40 -05:00
|
|
|
#include <mntent.h>
|
2013-08-07 04:52:10 -04:00
|
|
|
#include <stdio.h>
|
2013-10-07 14:11:40 -04:00
|
|
|
#include <stdlib.h>
|
2014-03-12 10:35:20 -04:00
|
|
|
#include <string.h>
|
2014-06-30 14:03:41 -04:00
|
|
|
|
2013-08-07 04:52:10 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2014-03-15 14:37:16 -04:00
|
|
|
static int umountall(int);
|
|
|
|
|
2013-08-07 04:52:10 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-04-13 11:28:08 -04:00
|
|
|
weprintf("usage: %s [-lfn] target...\n", argv0);
|
2014-03-12 10:28:56 -04:00
|
|
|
weprintf("usage: %s -a [-lfn]\n", argv0);
|
|
|
|
exit(EXIT_FAILURE);
|
2013-08-07 04:52:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-08-16 06:10:13 -04:00
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2013-08-07 04:52:10 -04:00
|
|
|
int i;
|
2014-03-07 08:49:40 -05:00
|
|
|
int aflag = 0;
|
2013-08-07 05:33:19 -04:00
|
|
|
int flags = 0;
|
2013-10-07 14:11:40 -04:00
|
|
|
int ret = EXIT_SUCCESS;
|
2013-08-07 04:52:10 -04:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2014-03-07 08:49:40 -05:00
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
2013-08-07 04:52:10 -04:00
|
|
|
case 'f':
|
2013-08-14 09:19:05 -04:00
|
|
|
flags |= MNT_FORCE;
|
2013-08-07 05:33:19 -04:00
|
|
|
break;
|
|
|
|
case 'l':
|
2013-08-14 09:19:05 -04:00
|
|
|
flags |= MNT_DETACH;
|
2013-08-07 04:52:10 -04:00
|
|
|
break;
|
2013-09-06 06:01:03 -04:00
|
|
|
case 'n':
|
|
|
|
break;
|
2013-08-07 04:52:10 -04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2013-08-10 06:38:30 -04:00
|
|
|
|
2014-03-07 08:49:40 -05:00
|
|
|
if (argc < 1 && aflag == 0)
|
2013-08-07 04:52:10 -04:00
|
|
|
usage();
|
2013-08-10 06:38:30 -04:00
|
|
|
|
2014-03-15 14:37:16 -04:00
|
|
|
if (aflag == 1)
|
|
|
|
return umountall(flags);
|
2014-03-07 08:49:40 -05:00
|
|
|
|
2013-08-07 04:52:10 -04:00
|
|
|
for (i = 0; i < argc; i++) {
|
2014-03-07 08:52:31 -05:00
|
|
|
if (umount2(argv[i], flags) < 0) {
|
2014-03-15 14:10:01 -04:00
|
|
|
weprintf("umount2 %s:", argv[i]);
|
2014-03-07 08:52:31 -05:00
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
}
|
2013-08-07 04:52:10 -04:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2014-03-15 14:37:16 -04:00
|
|
|
|
|
|
|
static int
|
|
|
|
umountall(int flags)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
struct mntent *me;
|
|
|
|
int ret;
|
2014-03-15 14:44:16 -04:00
|
|
|
char **mntdirs = NULL;
|
|
|
|
int len = 0;
|
2014-03-15 14:37:16 -04:00
|
|
|
|
2014-03-18 10:26:33 -04:00
|
|
|
fp = setmntent("/proc/mounts", "r");
|
2014-03-15 14:37:16 -04:00
|
|
|
if (!fp)
|
2014-03-18 10:26:33 -04:00
|
|
|
eprintf("setmntent %s:", "/proc/mounts");
|
2014-03-15 14:37:16 -04:00
|
|
|
while ((me = getmntent(fp))) {
|
|
|
|
if (strcmp(me->mnt_type, "proc") == 0)
|
|
|
|
continue;
|
2014-04-30 08:08:16 -04:00
|
|
|
mntdirs = erealloc(mntdirs, ++len * sizeof(*mntdirs));
|
|
|
|
mntdirs[len - 1] = estrdup(me->mnt_dir);
|
2014-03-15 14:44:16 -04:00
|
|
|
}
|
|
|
|
endmntent(fp);
|
|
|
|
while (--len >= 0) {
|
|
|
|
if (umount2(mntdirs[len], flags) < 0) {
|
|
|
|
weprintf("umount2 %s:", mntdirs[len]);
|
2014-03-15 14:37:16 -04:00
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
}
|
2014-03-15 14:44:16 -04:00
|
|
|
free(mntdirs[len]);
|
2014-03-15 14:37:16 -04:00
|
|
|
}
|
2014-03-15 14:44:16 -04:00
|
|
|
free(mntdirs);
|
2014-03-15 14:37:16 -04:00
|
|
|
return ret;
|
|
|
|
}
|