55
55
#ifdef HAVE_SYS_MMAN_H
56
56
#include <sys/mman.h>
57
57
#endif
58
+ #include <mntent.h>
58
59
59
60
#include <fcntl.h>
60
61
@@ -202,31 +203,31 @@ static int page_compare (opal_list_item_t **a, opal_list_item_t **b) {
202
203
static void mca_mpool_hugepage_find_hugepages (void ) {
203
204
mca_mpool_hugepage_hugepage_t * hp ;
204
205
FILE * fh ;
205
- char * path ;
206
- char buffer [1024 ];
207
- char * ctx , * tok ;
206
+ struct mntent * mntent ;
207
+ char * opts , * tok , * ctx ;
208
208
209
- fh = fopen ("/proc/mounts" , "r" );
209
+ fh = setmntent ("/proc/mounts" , "r" );
210
210
if (NULL == fh ) {
211
211
return ;
212
212
}
213
213
214
- while (fgets ( buffer , 1024 , fh )) {
214
+ while (NULL != ( mntent = getmntent ( fh ) )) {
215
215
unsigned long page_size = 0 ;
216
216
217
- (void ) strtok_r (buffer , " " , & ctx );
218
- path = strtok_r (NULL , " " , & ctx );
219
- tok = strtok_r (NULL , " " , & ctx );
220
-
221
- if (0 != strcmp (tok , "hugetlbfs" )) {
217
+ if (0 != strcmp (mntent -> mnt_type , "hugetlbfs" )) {
222
218
continue ;
223
219
}
224
220
225
- tok = strtok_r (NULL , " " , & ctx );
226
- tok = strtok_r (tok , "," , & ctx );
221
+ opts = strdup (mntent -> mnt_opts );
222
+ if (NULL == opts ) {
223
+ break ;
224
+ }
225
+
226
+ tok = strtok_r (opts , "," , & ctx );
227
227
228
228
do {
229
229
if (0 == strncmp (tok , "pagesize" , 8 )) {
230
+ free (opts );
230
231
break ;
231
232
}
232
233
tok = strtok_r (NULL , "," , & ctx );
@@ -236,15 +237,16 @@ static void mca_mpool_hugepage_find_hugepages (void) {
236
237
#if defined(USE_STATFS )
237
238
struct statfs info ;
238
239
239
- statfs (path , & info );
240
+ statfs (mntent -> mnt_dir , & info );
240
241
#elif defined(HAVE_STATVFS )
241
242
struct statvfs info ;
242
- statvfs (path , & info );
243
+ statvfs (mntent -> mnt_dir , & info );
243
244
#endif
244
245
page_size = info .f_bsize ;
245
246
} else {
246
247
(void ) sscanf (tok , "pagesize=%lu" , & page_size );
247
248
}
249
+ free (opts );
248
250
249
251
if (0 == page_size ) {
250
252
/* could not get page size */
@@ -256,7 +258,7 @@ static void mca_mpool_hugepage_find_hugepages (void) {
256
258
break ;
257
259
}
258
260
259
- hp -> path = strdup (path );
261
+ hp -> path = strdup (mntent -> mnt_dir );
260
262
hp -> page_size = page_size ;
261
263
262
264
OPAL_OUTPUT_VERBOSE ((MCA_BASE_VERBOSE_INFO , opal_mpool_base_framework .framework_output ,
@@ -268,7 +270,7 @@ static void mca_mpool_hugepage_find_hugepages (void) {
268
270
269
271
opal_list_sort (& mca_mpool_hugepage_component .huge_pages , page_compare );
270
272
271
- fclose (fh );
273
+ endmntent (fh );
272
274
}
273
275
274
276
static int mca_mpool_hugepage_query (const char * hints , int * priority_out ,
0 commit comments