Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading proc/pid/mem File

1,253 views
Skip to first unread message

sternr

unread,
Feb 6, 2011, 6:25:10 AM2/6/11
to
(Figured it deserves a different thread).

I'm trying to read a different process's proc/pid/mem file given I know it's pid.
here's my code:
===START
string fn = "/proc/pid/mem"; //where pid is the right pid
int fd = open(fn.c_str(), O_RDONLY);
char* buff = new char[BUFF_SIZE];
int size = read(fd, buff, BUFF_SIZE);
===END
No matter what BUFF_SIZE is, or whether I use lseek before (to change the cursur to a specific location), read always returns -1.

I tried running the process with sudo but still no success.

Any ideas?

Thanks!

--sternr

Richard Kettlewell

unread,
Feb 6, 2011, 6:27:54 AM2/6/11
to
sternr <ste...@gmail.com> writes:

> (Figured it deserves a different thread).
>
> I'm trying to read a different process's proc/pid/mem file given I know it's pid.
> here's my code:
> ===START
> string fn = "/proc/pid/mem"; //where pid is the right pid
> int fd = open(fn.c_str(), O_RDONLY);

You should check whether the open() succeeds.

> char* buff = new char[BUFF_SIZE];
> int size = read(fd, buff, BUFF_SIZE);
> ===END
> No matter what BUFF_SIZE is, or whether I use lseek before (to change
> the cursur to a specific location), read always returns -1.
>
> I tried running the process with sudo but still no success.
>
> Any ideas?

See what value errno is set to.

--
http://www.greenend.org.uk/rjk/

sternr

unread,
Feb 6, 2011, 6:42:50 AM2/6/11
to
The open call goes with errno = 0.
The read, returns with errno = 3 which suppose to mean "NO Process found"
But the process is still in the air, and anyway the open succeeded so how can that be? or is it not the right errno description?

Jorgen Grahn

unread,
Feb 6, 2011, 9:30:55 AM2/6/11
to
On Sun, 2011-02-06, sternr wrote:

Please quote some context.

You should check the return code from open(2), and only check errno if
it returned -1. See the man page. You should also feed the errno to
strerror() or perror() to find out what it means, rather than guess at
the meaning of 0 and 3.

Or, if it's just for a quick test and you're too lazy to write all the
error handling code[1], you can run the program under strace
which will show exactly how the calls failed.

I have the feeling you're misinterpreting what's happening.

/Jorgen

[1] No offense intended; sometimes it makes sense to be lazy.

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Richard Kettlewell

unread,
Feb 6, 2011, 9:52:06 AM2/6/11
to
sternr <ste...@gmail.com> writes:

Having looked into it a bit further, you're not allowed to read memory
belonging to other processes (apart from an exception involving ptrace).

Getting ESRCH instead of EPERM may be a bug. See fs/proc/base.c, in
particular mem_read() and check_mem_permission().

--
http://www.greenend.org.uk/rjk/

sternr

unread,
Feb 6, 2011, 9:56:24 AM2/6/11
to
Here's the updated test program followed by the output:

=====Start Code
string fn = "/proc/pid/mem";


int fd = open(fn.c_str(), O_RDONLY);

perror("open status");
char* buf = new char[100];
int count = read(fd,buf,10);
perror("read status");
=====Start Output:
open status: Success
read status: No such process
=====End

(This is example code only, obviously my final program will not look like it...)
Any ideas?

sternr

unread,
Feb 6, 2011, 9:58:42 AM2/6/11
to
I was afraid you'd say that.

So I can only read the memory of a self or a child process?

Thanks again for the help!

sternr

unread,
Feb 7, 2011, 2:36:30 AM2/7/11
to
0 new messages