Add device_is_mounted() support for NetBSD
[fio.git] / lib / mountcheck.c
1 #include <stdio.h>
2 #include <string.h>
3
4 #ifdef CONFIG_GETMNTENT
5 #include <mntent.h>
6
7 #include "lib/mountcheck.h"
8
9 #define MTAB    "/etc/mtab"
10
11 int device_is_mounted(const char *dev)
12 {
13         FILE *mtab;
14         struct mntent *mnt;
15         int ret = 0;
16
17         mtab = setmntent(MTAB, "r");
18         if (!mtab)
19                 return 0;
20
21         while ((mnt = getmntent(mtab)) != NULL) {
22                 if (!mnt->mnt_fsname)
23                         continue;
24                 if (!strcmp(mnt->mnt_fsname, dev)) {
25                         ret = 1;
26                         break;
27                 }
28         }
29
30         endmntent(mtab);
31         return ret;
32 }
33
34 #elif defined(CONFIG_GETMNTINFO)
35 /* for most BSDs */
36 #include <sys/param.h>
37 #include <sys/mount.h>
38
39 int device_is_mounted(const char *dev)
40 {
41         struct statfs *st;
42         int i, ret;
43
44         ret = getmntinfo(&st, MNT_NOWAIT);
45         if (ret <= 0)
46                 return 0;
47
48         for (i = 0; i < ret; i++) {
49                 if (!strcmp(st[i].f_mntfromname, dev))
50                         return 1;
51         }
52
53         return 0;
54 }
55
56 #elif defined(CONFIG_GETMNTINFO_STATVFS)
57 /* for NetBSD */
58 #include <sys/statvfs.h>
59
60 int device_is_mounted(const char *dev)
61 {
62         struct statvfs *st;
63         int i, ret;
64
65         ret = getmntinfo(&st, MNT_NOWAIT);
66         if (ret <= 0)
67                 return 0;
68
69         for (i = 0; i < ret; i++) {
70                 if (!strcmp(st[i].f_mntfromname, dev))
71                         return 1;
72         }
73
74         return 0;
75 }
76
77 #else
78 /* others */
79
80 int device_is_mounted(const char *dev)
81 {
82         return 0;
83 }
84
85 #endif