5555#ifdef HAVE_SYS_MMAN_H
5656#include <sys/mman.h>
5757#endif
58+ #include <mntent.h>
5859
5960#include <fcntl.h>
6061
@@ -202,31 +203,31 @@ static int page_compare (opal_list_item_t **a, opal_list_item_t **b) {
202203static void mca_mpool_hugepage_find_hugepages (void ) {
203204 mca_mpool_hugepage_hugepage_t * hp ;
204205 FILE * fh ;
205- char * path ;
206- char buffer [1024 ];
207- char * ctx , * tok ;
206+ struct mntent * mntent ;
207+ char * opts , * tok , * ctx ;
208208
209- fh = fopen ("/proc/mounts" , "r" );
209+ fh = setmntent ("/proc/mounts" , "r" );
210210 if (NULL == fh ) {
211211 return ;
212212 }
213213
214- while (fgets ( buffer , 1024 , fh )) {
214+ while (NULL != ( mntent = getmntent ( fh ) )) {
215215 unsigned long page_size = 0 ;
216216
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" )) {
222218 continue ;
223219 }
224220
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 );
227227
228228 do {
229229 if (0 == strncmp (tok , "pagesize" , 8 )) {
230+ free (opts );
230231 break ;
231232 }
232233 tok = strtok_r (NULL , "," , & ctx );
@@ -236,15 +237,16 @@ static void mca_mpool_hugepage_find_hugepages (void) {
236237#if defined(USE_STATFS )
237238 struct statfs info ;
238239
239- statfs (path , & info );
240+ statfs (mntent -> mnt_dir , & info );
240241#elif defined(HAVE_STATVFS )
241242 struct statvfs info ;
242- statvfs (path , & info );
243+ statvfs (mntent -> mnt_dir , & info );
243244#endif
244245 page_size = info .f_bsize ;
245246 } else {
246247 (void ) sscanf (tok , "pagesize=%lu" , & page_size );
247248 }
249+ free (opts );
248250
249251 if (0 == page_size ) {
250252 /* could not get page size */
@@ -256,7 +258,7 @@ static void mca_mpool_hugepage_find_hugepages (void) {
256258 break ;
257259 }
258260
259- hp -> path = strdup (path );
261+ hp -> path = strdup (mntent -> mnt_dir );
260262 hp -> page_size = page_size ;
261263
262264 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) {
268270
269271 opal_list_sort (& mca_mpool_hugepage_component .huge_pages , page_compare );
270272
271- fclose (fh );
273+ endmntent (fh );
272274}
273275
274276static int mca_mpool_hugepage_query (const char * hints , int * priority_out ,
0 commit comments