process_vm_readv()EFAULT

时间:2018-02-21 06:26:00

标签: c linux

我尝试创建子进程并访问其内存,但我的所有测试都导致EFAULT错误。

#define _GNU_SOURCE // needed for process_vm_readv
#include <stdio.h>
#include <sys/uio.h>
#include <errno.h>
#include <unistd.h>

int main(const int argc, char *const argv[]) {
    struct iovec local[2];
    struct iovec remote[1];
    char buf1[10];
    char buf2[10];
    ssize_t nread;
    pid_t pid; // PID of remote process
    pid=fork();
    if(pid==0) { // child process
        execvp("./target",argv);
        return 0;
    } else if(pid==-1) {
        puts("Error creating child process!");
        return 0;
    }
    printf("This process: %d\n",getpid());
    printf("Created process: %d\n",pid);
    local[0].iov_base=buf1;
    local[0].iov_len=10;
    local[1].iov_base=buf2;
    local[1].iov_len=10;
    remote[0].iov_base=(void*)0x10; // address 0x10
    remote[0].iov_len=20; // 20 bytes
    nread=process_vm_readv(pid,local,2,remote,1,0);
    if(nread==-1) {
        printf("process_vm_readv error: ");
        switch(errno) {
            case EINVAL: puts("EINVAL"); break;
            case EFAULT: puts("EFAULT"); break;
            case ENOMEM: puts("ENOMEM"); break;
            case EPERM: puts("EPERM"); break;
            case ESRCH: puts("ESRCH"); break;
        }
        return 1;
    }
    printf("%ld bytes read\n",nread);
    return 0;
}

0 个答案:

没有答案