您正在以只读模式(O_RDONLY)打开文件.我建议再试一次O_RDWR:
mem_fd = open(mem_file_name,O_RDWR);
然而,从man proc来看,目前尚不清楚它是否会起作用:
06001
编辑:
我也很好奇,所以我直接用ptrace()把这个例子放在一起:
#include
#include
#include
#include
#include
#define SHOW(call) ({ int _ret = (int)(call); printf("%s -> %d\n",#call,_ret); if (_ret < 0) { perror(NULL); }})
char changeme[] = "This is a test";
int main (void)
{
pid_t pid = fork();
int ret;
int i;
union {
char cdata[8];
int64_t data;
} u = { "Hijacked" };
switch (pid) {
case 0: /* child */
sleep(1);
printf("Message: %s\n",changeme);
exit(0);
case -1:
perror("fork");
exit(1);
break;
default: /* parent */
SHOW(ptrace(PTRACE_ATTACH,pid,0));
SHOW(ptrace(PTRACE_POKEDATA,changeme,u.data));
SHOW(ptrace(PTRACE_CONT,0));
wait(NULL);
break;
}
return 0;
}