<div dir="ltr">Hi,<br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Oct 13, 2015 at 2:29 AM, Cyril Hrubis <span dir="ltr"><<a href="mailto:chrubis@suse.cz" target="_blank">chrubis@suse.cz</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi!<br>
Generall idea looks good, a few comments below.<br>
<span class=""><br>
> +/* MMAP */<br>
> +int range_is_mapped(unsigned long low, unsigned long high)<br>
>  #endif<br>
> diff --git a/testcases/kernel/mem/lib/mem.c b/testcases/kernel/mem/lib/mem.c<br>
> index 118b6c0..e0e1bdf 100644<br>
> --- a/testcases/kernel/mem/lib/mem.c<br>
> +++ b/testcases/kernel/mem/lib/mem.c<br>
> @@ -1113,3 +1113,46 @@ void update_shm_size(size_t * shm_size)<br>
>               *shm_size = shmmax;<br>
>       }<br>
>  }<br>
> +<br>
> +int range_is_mapped(unsigned long low, unsigned long high)<br>
> +{<br>
> +     FILE *f;<br>
> +     int MAPS_BUF_SZ = 4096;<br>
> +     char line[MAPS_BUF_SZ];<br>
> +     char *tmp;<br>
> +<br>
> +     f = fopen("/proc/self/maps", "r");<br>
> +     if (!f) {<br>
> +             printf("Failed to open /proc/self/maps.\n");<br>
</span>                ^<br>
                should rather be tst_resm(TWARN, );<br></blockquote><div><br></div><div>ok. looks like using<br>    FILE * f = SAFE_FOPEN(NULL, "/proc/self/maps", "r");<br></div><div>is better than above.<br></div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<span class="">> +             return -1;<br>
> +     }<br>
> +<br>
> +     while (1) {<br>
> +             unsigned long start, end;<br>
> +             int ret;<br>
> +<br>
> +             tmp = fgets(line, MAPS_BUF_SZ, f);<br>
> +             if (!tmp)<br>
> +                     break;<br>
> +<br>
> +             ret = sscanf(line, "%lx-%lx", &start, &end);<br>
> +             if (ret != 2) {<br>
> +                     printf("Couldn't parse /proc/self/maps line: %s\n",<br>
> +                                     line);<br>
</span>                        ^<br>
                        here as well<br></blockquote><div><br></div><div>ok.<br> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<span class="">> +                     fclose(f);<br>
> +                     return -1;<br>
> +             }<br>
<br>
</span>Why don't you just use fscanf()? It doesn't make sense to read the line<br>
into the buffer and then sscanf() the values.<br></blockquote><div><br></div><div>hmm, we need to read the '/proc/self/maps' line by line, It easy to use <code>fgets() </code>to get each line in and then <code>sscanf()</code> that.

<p><code>fscanf()</code> will never read the newline at the end of the first 
line.  So when it is called the second time, it will fail (returning 0, 
not EOF) and read nothing, leaving the [start, end] unchanged.<br></p>maybe I don't know if fscanf() could achieve that also? <br><br></div><div>so what about something like:<br>--------------------------------------<br>int range_is_mapped(unsigned long low, unsigned long high)<br>{<br>    FILE *f = SAFE_FOPEN(NULL, "/proc/self/maps", "r");<br><br>    int MAPS_BUF_SZ = 4096;<br>    char line[MAPS_BUF_SZ];<br><br>    while (fgets(line, MAPS_BUF_SZ, f)) {<br>        unsigned long start, end;<br>        int ret;<br><br>        ret = sscanf(line, "%lx-%lx", &start, &end);<br>        if (ret != 2) {<br>            tst_resm(TWARN | TERRNO, "Couldn't parse /proc/self/maps line.");<br>            SAFE_FCLOSE(NULL, f);<br>        }<br><br>        if ((start >= low) && (start < high)) {<br>            SAFE_FCLOSE(NULL, f);<br>            return 1;<br>        }<br>        if ((end >= low) && (end < high)) {<br>            SAFE_FCLOSE(NULL, f);<br>            return 1;<br>        }<br>    }<br><br>    SAFE_FCLOSE(NULL, f);<br>    return 0;<br>}<br><br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div class=""><div class="h5"><br>
> +             if ((start >= low) && (start < high)) {<br>
> +                     fclose(f);<br>
> +                     return 1;<br>
> +             }<br>
> +             if ((end >= low) && (end < high)) {<br>
> +                     fclose(f);<br>
> +                     return 1;<br>
> +             }<br>
> +     }<br>
> +<br>
> +     fclose(f);<br>
> +     return 0;<br>
> +}<br>
<br>
--<br>
</div></div><span class=""><font color="#888888">Cyril Hrubis<br>
<a href="mailto:chrubis@suse.cz">chrubis@suse.cz</a><br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>Regards,<br></div>Li Wang<br></div><div>Email: <a href="mailto:liwang@redhat.com" target="_blank">liwang@redhat.com</a><br></div></div></div></div></div></div>
</div></div>