Maybe simplify the code by using fopen/fread?

-Andrew Hastings
  Cray Inc.


Eric B Munson wrote:
> If a machine has a /proc/mounts file larger than 4kb and the hugetlb mount 
> point
> is after the 4kb mark the library currently fails to find the hugetlb mount.
> This patch ensures that the entire mounts file is searched for the hugetlb 
> mount
> before giving up.
> 
> Signed-off-by: Eric Munson <[EMAIL PROTECTED]>
> 
> ---
> 
>  hugeutils.c |  103 
> +++++++++++++++++++++++++++++++++++++++++++----------------
>  1 files changed, 75 insertions(+), 28 deletions(-)
> 
> diff --git a/hugeutils.c b/hugeutils.c
> index 8244c84..22f886f 100644
> --- a/hugeutils.c
> +++ b/hugeutils.c
> @@ -158,11 +158,35 @@ int hugetlbfs_test_path(const char *mount)
>  }
>  
>  #define MOUNTS_SZ    4096
> +#define READ_SZ              (MOUNTS_SZ - 1)
> +
> +static const char *path_search(char *buf)
> +{
> +     int err;
> +     char *tmp = buf;
> +     while (tmp) {
> +             err = sscanf(tmp,
> +                  "%*s %" stringify(PATH_MAX)
> +                  "s hugetlbfs ",
> +                  htlb_mount);
> +             if ((err == 1) && (hugetlbfs_test_path(htlb_mount) == 1))
> +                     return htlb_mount;
> +
> +             memset(htlb_mount, 0, sizeof(htlb_mount));
> +
> +             tmp = strchr(tmp, '\n');
> +             if (tmp)
> +                     tmp++;
> +     }
> +     return NULL;
> +}
>  
>  const char *hugetlbfs_find_path(void)
>  {
>       int err, readerr;
> -     char *tmp;
> +     char *start, *newline = NULL, *end = NULL;
> +     const char *tmp;
> +     char hold;
>       int fd, len;
>       char buf[MOUNTS_SZ];
>  
> @@ -197,35 +221,58 @@ const char *hugetlbfs_find_path(void)
>               }
>       }
>  
> -     len = read(fd, buf, sizeof(buf));
> -     readerr = errno;
> -     close(fd);
> -     if (len < 0) {
> -             ERROR("Error reading mounts (%s)\n", strerror(errno));
> -             return NULL;
> -     }
> -     if (len >= sizeof(buf)) {
> -             ERROR("/proc/mounts is too long\n");
> -             return NULL;
> -     }
> -     buf[sizeof(buf)-1] = '\0';
> -
> -     tmp = buf;
> -     while (tmp) {
> -             err = sscanf(tmp,
> -                          "%*s %" stringify(PATH_MAX)
> -                          "s hugetlbfs ",
> -                          htlb_mount);
> -             if ((err == 1) && (hugetlbfs_test_path(htlb_mount) == 1))
> -                     return htlb_mount;
> -
> -             memset(htlb_mount, 0, sizeof(htlb_mount));
> +     start = buf;
> +     do {
> +             len = read(fd, start, READ_SZ - (end - newline));
> +             readerr = errno;
> +             if (len < 0) {
> +                     if (readerr == EAGAIN || readerr == EINTR)
> +                             continue;
> +                     ERROR("Error reading mounts (%s)\n", strerror(errno));
> +                     close(fd);
> +                     return NULL;
> +             }
>  
> -             tmp = strchr(tmp, '\n');
> -             if (tmp)
> -                     tmp++;
> -     }
> +             if (len == 0) {
> +                     buf[READ_SZ] = '\0';
> +                     tmp = path_search(buf);
> +                     if (tmp) {
> +                             close(fd);
> +                             return tmp;
> +                     }
> +             } else {
> +                     /*
> +                      * There is more to read so we will examine the buffer
> +                      * that we have for the hugetlb entry, if we find it
> +                      * go out, otherwise pull the next chunk.
> +                      */
> +
> +                     newline = end = start + len;
> +                     while (*newline != '\n' && newline > start)
> +                             newline--;
> +                     if (newline <= start) {
> +                             ERROR("/proc/mounts is not readable.\n");
> +                             close(fd);
> +                             return NULL;
> +                     }
> +
> +                     newline++;
> +                     hold = *newline;
> +                     *newline = '\0';
> +                     tmp = path_search(buf);
> +                     if (tmp) {
> +                             close(fd);
> +                             return tmp;
> +                     }
> +
> +                     *newline = hold;
> +                     memcpy(buf, newline, end - newline);
> +                     start = buf + (end - newline);
> +             }
> +     } while (len != 0);
>  
> +     close(fd);
> +     memset(htlb_mount, 0, sizeof(htlb_mount));
>       WARNING("Could not find hugetlbfs mount point in /proc/mounts. "
>                       "Is it mounted?\n");
>  
> 
> 
> 
> ------------------------------------------------------------------------
> 
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft 
> Defy all challenges. Microsoft(R) Visual Studio 2008. 
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Libhugetlbfs-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/libhugetlbfs-devel


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Libhugetlbfs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libhugetlbfs-devel

Reply via email to