Possible bug in kvm_read()

Hi,

I have got the kvm.c from QNX CVS. There seems to be a possible bug in
kvm_read() function.
On my machine kvm_read() opens ‘/proc/98317/as’ file and seeks to the given
offset. After that, the following code is executed.

while (len)
{
if ((r = read(fd, data, len)) == -1)
{
fd = -1;
return -1;
}
len -= r;
data += r;
}

On my machine (QNX RTP 6) due to some reasons, the read() function reads
zero bytes (r=0)and does not return -1 (no error). Due to this, the above
loop becomes an infinite loop as long as read() keeps reading zero bytes.
Isn’t it a bug that the possibility of zero bytes being read is not
considered?

I would also like to know why read() is reading zero bytes on my machine? On
some other machines, the exact same code works perfectly. Any ideas?

Thanks in advance…

-Farooque

This is fixed in 6.2. AFAIK, this only happens when the passed in
offset is 0. Also, make sure you’re running as root.

-seanb

Farooque Khan <farooquek@concretioindia.com> wrote:
: Hi,

: I have got the kvm.c from QNX CVS. There seems to be a possible bug in
: kvm_read() function.
: On my machine kvm_read() opens ‘/proc/98317/as’ file and seeks to the given
: offset. After that, the following code is executed.

: while (len)
: {
: if ((r = read(fd, data, len)) == -1)
: {
: fd = -1;
: return -1;
: }
: len -= r;
: data += r;
: }

: On my machine (QNX RTP 6) due to some reasons, the read() function reads
: zero bytes (r=0)and does not return -1 (no error). Due to this, the above
: loop becomes an infinite loop as long as read() keeps reading zero bytes.
: Isn’t it a bug that the possibility of zero bytes being read is not
: considered?

: I would also like to know why read() is reading zero bytes on my machine? On
: some other machines, the exact same code works perfectly. Any ideas?

: Thanks in advance…

: -Farooque

Sean Boudreau <seanb@qnx.com> wrote:

This is fixed in 6.2. AFAIK, this only happens when the passed in
offset is 0.

Actually, just testing lseek() & read() [not kvm_read] on a
/proc/pid/as entry, it seems that read() will return 0 for any
part of the address space that does not have something mapped in.

Sample test program:

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

char buf[5000];

int main( int argc, char **argv )
{
int fd;
int ret;
int off;

off = atoi(argv[2]);
fd = open( argv[1], O_RDONLY );
printf(“open returned %d, errno %d\n”, fd, errno );

ret = lseek( fd, off, SEEK_SET );
printf(“lseek returned %d, errno %d\n”, ret, errno );

ret = read(fd, buf, 2000);
printf(“read returned %d, errno %d\n”, ret, errno );
}

Sample output:

$ read_pidmem /proc/126996/as 1346720
open returned 3, errno 0
lseek returned 1346720, errno 0
read returned 0, errno 0

$ read_pidmem /proc/126996/as 134672384
open returned 3, errno 0
lseek returned 134672384, errno 0
read returned 2000, errno 0

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.