Lines 38-43
Link Here
|
38 |
#include <sys/mount.h> |
38 |
#include <sys/mount.h> |
39 |
#include <stdlib.h> |
39 |
#include <stdlib.h> |
40 |
|
40 |
|
|
|
41 |
#define MAX_ATTEMPTS 3 |
42 |
#define EXTRA_FS 16 |
43 |
|
41 |
/* |
44 |
/* |
42 |
* Return information about mounted filesystems. |
45 |
* Return information about mounted filesystems. |
43 |
*/ |
46 |
*/ |
Lines 45-66
Link Here
|
45 |
getmntinfo(struct statfs **mntbufp, int mode) |
48 |
getmntinfo(struct statfs **mntbufp, int mode) |
46 |
{ |
49 |
{ |
47 |
static struct statfs *mntbuf; |
50 |
static struct statfs *mntbuf; |
48 |
static int mntsize; |
|
|
49 |
static long bufsize; |
51 |
static long bufsize; |
|
|
52 |
struct statfs *newbuf; |
53 |
long newsize; |
54 |
int n_fs; |
55 |
int n_try = 0; |
56 |
|
50 |
|
57 |
|
51 |
if (mntsize <= 0 && (mntsize = getfsstat(0, 0, MNT_NOWAIT)) < 0) |
58 |
/* Get the current list of available filesystems */ |
52 |
return (0); |
59 |
n_fs = getfsstat(NULL, 0, MNT_NOWAIT); |
53 |
if (bufsize > 0 && (mntsize = getfsstat(mntbuf, bufsize, mode)) < 0) |
60 |
if (n_fs < 0) |
54 |
return (0); |
61 |
return (0); |
55 |
while (bufsize <= mntsize * sizeof(struct statfs)) { |
62 |
|
56 |
if (mntbuf) |
63 |
do { |
57 |
free(mntbuf); |
64 |
int rc; |
58 |
bufsize = (mntsize + 1) * sizeof(struct statfs); |
65 |
|
59 |
if ((mntbuf = malloc(bufsize)) == NULL) |
66 |
/* Need room for more statfs in order to detect a growing list */ |
60 |
return (0); |
67 |
newsize = (n_fs+EXTRA_FS) * sizeof(struct statfs); |
61 |
if ((mntsize = getfsstat(mntbuf, bufsize, mode)) < 0) |
68 |
|
|
|
69 |
if (!mntbuf || bufsize < newsize) { |
70 |
/* Current buffer is too small -> grow it */ |
71 |
|
72 |
if (mntbuf) |
73 |
newbuf = realloc(mntbuf, newsize); |
74 |
else |
75 |
newbuf = malloc(newsize); |
76 |
|
77 |
if (!newbuf) { |
78 |
return (0); |
79 |
} |
80 |
|
81 |
/* Update stored buf pointer(s) and current size */ |
82 |
*mntbufp = mntbuf = newbuf; |
83 |
bufsize = newsize; |
84 |
} |
85 |
|
86 |
/* Load filesystem information into buffer */ |
87 |
if ((rc = getfsstat(mntbuf, bufsize, mode)) < 0) |
62 |
return (0); |
88 |
return (0); |
63 |
} |
89 |
|
|
|
90 |
if (rc <= n_fs) { |
91 |
/* Got the list of filesystem information */ |
92 |
*mntbufp = mntbuf; |
93 |
return (rc); |
94 |
} |
95 |
|
96 |
n_fs = rc; |
97 |
/* Retry the lookup (more filesystems found) */ |
98 |
} while (n_try++ < MAX_ATTEMPTS); |
99 |
|
100 |
/* The list keeps on growing, give up and just return the ones we found so far */ |
64 |
*mntbufp = mntbuf; |
101 |
*mntbufp = mntbuf; |
65 |
return (mntsize); |
102 |
return (n_fs); |
66 |
} |
103 |
} |