-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnt-umnt.c
65 lines (57 loc) · 1.2 KB
/
mnt-umnt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <err.h>
#include <mntent.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mount.h>
int main(int argc, char **argv)
{
FILE *fp;
struct mntent m, *p;
char a[4096];
char buf[4096 + 1024];
int r, found=0, lines=0;
char *target;
int repeat = 1;
if (argc < 2 || argc > 3) {
errx(1, "Usage: %s <mountpoint> [iterations]", argv[0]);
}
target = argv[1];
if (target[0] != '/') {
errx(1, "%s: absolute path required", argv[0]);
}
if (argc > 2) {
repeat = atoi(argv[2]);
}
again:
/* 1. mount target */
r = mount("none", target, "tmpfs", 0, NULL);
if (r != 0) {
err(2, "mount %s", target);
}
/* 2. look it up */
fp = setmntent("/proc/self/mounts", "r");
if (!fp) {
err(3, "setmntent");
}
setvbuf(fp, a, _IOLBF, sizeof(a));
while ((p = getmntent_r(fp, &m, buf, sizeof(buf)))) {
lines++;
if (!strcmp(p->mnt_dir, target)) {
found++;
}
}
endmntent(fp);
// printf("%s: found %d entries for %s (%d lines seen)\n", argv[0], found, target, lines);
if (found == 0) {
fprintf(stderr, "%s: can't find %s\n", argv[0], target);
}
/* 3. umount */
if (umount(target) != 0) {
err(5, "umount %s", target);
}
if (--repeat > 0) {
goto again;
}
return 0;
}