Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Problem with reading procfs

Reply
Thread Tools

Problem with reading procfs

 
 
Simone
Guest
Posts: n/a
 
      12-11-2007
Hi, I'm developing an application that access the procfs and then
read/write /proc/<PID>/mem of a process to change the content of a
file
owned by that process.

The sequential steps performed by the application are the followings:

1) catch, by using ptrace, of the "read" system call
2) read the file content directly on /proc/PID/mem
3) modify this content
4) write back the modified content on /proc/PD/mem

The problem is that it returns "Error 22 Invalid argument" every time
it tries to read (or write) on procfs.
(I've already patched the kernel to allow read/write operation on
/proc/<pid>/mem and I've tried the pread/pwrite functions, but they
have the same behavior).

This is an extract of my code:


....
int eax_reg, ecx_reg, ret;
char * buffer;
char orig_buf[eax_reg+1];
char proc_path[MAX_FILE_NAME+1];

// traced_proc is the pid_t of the traced process
// get number of bytes to read
eax_reg = ptrace(PTRACE_PEEKUSER, traced_proc, 4*EAX, NULL);
// get traced process space buffer
ecx_reg = ptrace(PTRACE_PEEKUSER, traced_proc, 4*ECX, NULL);

buffer = malloc(sizeof(char)*eax_reg);
memset(orig_buf,0,eax_reg+1);
memset(proc_path, 0, sizeof(proc_path));

sprintf(proc_path, "/proc/%d/mem", traced_proc);
fd_proc = open(proc_path, O_RDWR);

lseek(fd_proc, ecx_reg, SEEK_SET);
int ret = read(fd_proc, buffer, eax_reg);
if (ret < 0) {
perror("read");
fprintf(stderr, "Failed to read: %d - %s\n", errno, strerror(errno));
}
....


if I run the application with strace, the output for the code shown
above is:

open("/proc/13271/mem", O_RDWR) = 3
lseek(3, 3086368768, SEEK_SET) = 3086368768
read(3, 0xbfca0940, 280) = -1 EINVAL (Invalid argument)

Can someone tell me where I'm wrong?
Thanks in advance for any help...
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      12-11-2007
In article <(E-Mail Removed)>,
Simone <(E-Mail Removed)> wrote:
>Hi, I'm developing an application that access the procfs and then
>read/write /proc/<PID>/mem of a process to change the content of a
>file
>owned by that process.


Everything you describe is operating system dependant, and so should
be addressed to a newsgroup that is specific to your OS.

Even different OSs that support procfs have differences in how to
use them properly.
--
"I will speculate that [...] applications [...] could actually see a
performance boost for most users by going dual-core [...] because it
is running the adware and spyware that [...] are otherwise slowing
down the single CPU that user has today" -- Herb Sutter
 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      12-11-2007
Simone wrote, On 11/12/07 21:29:
> Hi, I'm developing an application that access the procfs and then
> read/write /proc/<PID>/mem of a process to change the content of a


<snip>

> open("/proc/13271/mem", O_RDWR) = 3
> lseek(3, 3086368768, SEEK_SET) = 3086368768
> read(3, 0xbfca0940, 280) = -1 EINVAL (Invalid argument)
>
> Can someone tell me where I'm wrong?
> Thanks in advance for any help...


This is all very Linux specific (not even Posix I believe) so you should
ask in one of the Linux groups after checking the relevant FAQs/charters
and a weeks worth of postings to see what goes on.

Note that just because someone once posted about a topic on a group that
does not mean the post was topical, and it might be that the responses
(if any) were just pointing out that it was not topical, so you need to
read posts not just look at subject lines.

For more information about what a lot of regulars consider to be topical
and why see http://clc-wiki.net/wiki/Introduction_to_comp.lang.c
--
Flash Gordon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a file and resuming reading. Karim Ali Python 2 05-25-2007 02:04 PM
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
DVD ROM not reading DVDs but reading CDs sayeed.afzal@gmail.com Computer Information 0 05-22-2006 10:37 AM
ASP.NET Reading problem (reading .xls) Wael Soliman ASP .Net 2 01-03-2005 05:33 PM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM



Advertisments