morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

umount.c (1428B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <sys/mount.h>
      3 
      4 #include <mntent.h>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <string.h>
      8 
      9 #include "util.h"
     10 
     11 static int umountall(int);
     12 
     13 static void
     14 usage(void)
     15 {
     16 	weprintf("usage: %s [-lfn] target...\n", argv0);
     17 	weprintf("usage: %s -a [-lfn]\n", argv0);
     18 	exit(1);
     19 }
     20 
     21 int
     22 main(int argc, char *argv[])
     23 {
     24 	int i;
     25 	int aflag = 0;
     26 	int flags = 0;
     27 	int ret = 0;
     28 
     29 	ARGBEGIN {
     30 	case 'a':
     31 		aflag = 1;
     32 		break;
     33 	case 'f':
     34 		flags |= MNT_FORCE;
     35 		break;
     36 	case 'l':
     37 		flags |= MNT_DETACH;
     38 		break;
     39 	case 'n':
     40 		break;
     41 	default:
     42 		usage();
     43 	} ARGEND;
     44 
     45 	if (argc < 1 && aflag == 0)
     46 		usage();
     47 
     48 	if (aflag == 1)
     49 		return umountall(flags);
     50 
     51 	for (i = 0; i < argc; i++) {
     52 		if (umount2(argv[i], flags) < 0) {
     53 			weprintf("umount2 %s:", argv[i]);
     54 			ret = 1;
     55 		}
     56 	}
     57 	return ret;
     58 }
     59 
     60 static int
     61 umountall(int flags)
     62 {
     63 	FILE *fp;
     64 	struct mntent *me;
     65 	int ret;
     66 	char **mntdirs = NULL;
     67 	int len = 0;
     68 
     69 	fp = setmntent("/proc/mounts", "r");
     70 	if (!fp)
     71 		eprintf("setmntent %s:", "/proc/mounts");
     72 	while ((me = getmntent(fp))) {
     73 		if (strcmp(me->mnt_type, "proc") == 0)
     74 			continue;
     75 		mntdirs = erealloc(mntdirs, ++len * sizeof(*mntdirs));
     76 		mntdirs[len - 1] = estrdup(me->mnt_dir);
     77 	}
     78 	endmntent(fp);
     79 	while (--len >= 0) {
     80 		if (umount2(mntdirs[len], flags) < 0) {
     81 			weprintf("umount2 %s:", mntdirs[len]);
     82 			ret = 1;
     83 		}
     84 		free(mntdirs[len]);
     85 	}
     86 	free(mntdirs);
     87 	return ret;
     88 }