<div dir="ltr">Hi,<br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Oct 13, 2015 at 1:39 PM, Li Wang <span dir="ltr"><<a href="mailto:liwang@redhat.com" target="_blank">liwang@redhat.com</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"><div dir="ltr">Hi,<br><div class="gmail_extra"><br><div class="gmail_quote"><div><div>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><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></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><span><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>> + 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></span><div>ok.<br> <br></div><span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<span>> + 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></span></div></div></div></blockquote><div><br></div><div>okay, seems you are right. I tried and achieved another one, following code test pass on my s390x system.<br></div><div>-----------------------------------------<br>int range_is_mapped(unsigned long low, unsigned long high)<br>{<br> FILE *f = SAFE_FOPEN(NULL, "/proc/self/maps", "r");<br><br> char buf[512];<br><br> while (!feof(f)) {<br> unsigned long start, end;<br> int ret;<br><br> ret = fscanf(f, "%lx-%lx %[^\n]%*c]", &start, &end, buf);<br> if (ret != 3) {<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></div><div>so, which one is better? I will format patch V3 when getting some proposals.<br><br></div><div>cc' jstancek@ if has some good idea.<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"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"></blockquote><div><br></div></span><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>--------------------------------------<span><br>int range_is_mapped(unsigned long low, unsigned long high)<br></span>{<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;<span><br><br> ret = sscanf(line, "%lx-%lx", &start, &end);<br></span> if (ret != 2) {<br> tst_resm(TWARN | TERRNO, "Couldn't parse /proc/self/maps line.");<br> SAFE_FCLOSE(NULL, f);<span><br> }<br><br> if ((start >= low) && (start < high)) {<br></span> SAFE_FCLOSE(NULL, f);<br> return 1;<span><br> }<br> if ((end >= low) && (end < high)) {<br></span> SAFE_FCLOSE(NULL, f);<br> return 1;<br> }<br> }<br><br> SAFE_FCLOSE(NULL, f);<br> return 0;<br>}<br><br><br></div><span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div><div><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><font color="#888888">Cyril Hrubis<br>
<a href="mailto:chrubis@suse.cz" target="_blank">chrubis@suse.cz</a><br>
</font></span></blockquote></span></div><br><br clear="all"><span><br>-- <br><div><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>
</span></div></div>
</blockquote></div><br><br clear="all"><br>-- <br><div><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>