5555#ifdef  HAVE_SYS_MMAN_H 
5656#include  <sys/mman.h> 
5757#endif 
58+ #ifdef  HAVE_MNTENT_H 
59+ #include  <mntent.h> 
60+ #endif 
5861
5962#include  <fcntl.h> 
6063
@@ -200,33 +203,34 @@ static int page_compare (opal_list_item_t **a, opal_list_item_t **b) {
200203}
201204
202205static  void  mca_mpool_hugepage_find_hugepages  (void ) {
206+ #ifdef  HAVE_MNTENT_H 
203207    mca_mpool_hugepage_hugepage_t  * hp ;
204208    FILE  * fh ;
205-     char  * path ;
206-     char  buffer [1024 ];
207-     char  * ctx , * tok ;
209+     struct  mntent  * mntent ;
210+     char  * opts , * tok , * ctx ;
208211
209-     fh  =  fopen  ("/proc/mounts" , "r" );
212+     fh  =  setmntent  ("/proc/mounts" , "r" );
210213    if  (NULL  ==  fh ) {
211214        return ;
212215    }
213216
214-     while  (fgets  ( buffer ,  1024 ,  fh )) {
217+     while  (NULL   !=  ( mntent   =   getmntent ( fh ) )) {
215218        unsigned long  page_size  =  0 ;
216219
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" )) {
220+         if  (0  !=  strcmp (mntent -> mnt_type , "hugetlbfs" )) {
222221            continue ;
223222        }
224223
225-         tok  =  strtok_r  (NULL , " " , & ctx );
226-         tok  =  strtok_r  (tok , "," , & ctx );
224+         opts  =  strdup (mntent -> mnt_opts );
225+         if  (NULL  ==  opts ) {
226+             break ;
227+         }
228+ 
229+         tok  =  strtok_r  (opts , "," , & ctx );
227230
228231        do  {
229232            if  (0  ==  strncmp  (tok , "pagesize" , 8 )) {
233+                 free (opts );
230234                break ;
231235            }
232236            tok  =  strtok_r  (NULL , "," , & ctx );
@@ -236,15 +240,16 @@ static void mca_mpool_hugepage_find_hugepages (void) {
236240#if  defined(USE_STATFS )
237241            struct  statfs  info ;
238242
239-             statfs  (path , & info );
243+             statfs  (mntent -> mnt_dir , & info );
240244#elif  defined(HAVE_STATVFS )
241245            struct  statvfs  info ;
242-             statvfs  (path , & info );
246+             statvfs  (mntent -> mnt_dir , & info );
243247#endif 
244248            page_size  =  info .f_bsize ;
245249        } else  {
246250            (void ) sscanf  (tok , "pagesize=%lu" , & page_size );
247251        }
252+         free (opts );
248253
249254        if  (0  ==  page_size ) {
250255            /* could not get page size */ 
@@ -256,7 +261,7 @@ static void mca_mpool_hugepage_find_hugepages (void) {
256261            break ;
257262        }
258263
259-         hp -> path  =  strdup  (path );
264+         hp -> path  =  strdup  (mntent -> mnt_dir );
260265        hp -> page_size  =  page_size ;
261266
262267        OPAL_OUTPUT_VERBOSE ((MCA_BASE_VERBOSE_INFO , opal_mpool_base_framework .framework_output ,
@@ -268,7 +273,8 @@ static void mca_mpool_hugepage_find_hugepages (void) {
268273
269274    opal_list_sort  (& mca_mpool_hugepage_component .huge_pages , page_compare );
270275
271-     fclose  (fh );
276+     endmntent  (fh );
277+ #endif 
272278}
273279
274280static  int  mca_mpool_hugepage_query  (const  char  * hints , int  * priority_out ,
0 commit comments