sys_read在linux系统调用中失败

时间:2017-02-05 08:48:14

标签: c linux linux-kernel kernel

我尝试运行时,代码中的void rread(int fd, char *s) { char buff[256]; long n; n = sys_read(fd, buff, (long)sizeof buff); if(n <= 0) { printk("Unable to read %d",n); } while(n > 0) { printk("Hello World"); printk("%s",buff); n = sys_read(fd, buff, (long)sizeof buff); } } long sys_rread(int argc, char *argv[]) { int f, i; f = sys_open(argv[0], O_RDONLY, S_IRWXU); if(f < 0) printk("file open error"); rread(f, argv[0]); return 0; } 函数失败。它返回-14。我修改了系统调用表,将此函数添加到内核4.7.1的源代码(也尝试了其他4.x内核)并且编译时没有任何错误。

int main()
{  
    printf("Invoking system call");
    char* s[] = {"/etc/adduser.conf"};
    long int ret_status = syscall(332,1,s ); // 332 is the syscall number
    if(ret_status == 0) 
         printf("System call executed correctly. Use dmesg to check \n");
    else 
         printf("System call did not execute as expected %d\n", ret_status);

     return 0;
}

我正在尝试在新编译的内核中使用此程序测试此系统调用

void rread(int fd, char *s)
{
    char buff[256];
    long n;
    mm_segment_t oldfs; 
    oldfs = get_fs();
    set_fs(KERNEL_DS); 
    n = sys_read(fd, buff, (long)sizeof buff);
    set_fs(oldfs); 
    if(n <= 0) {
        printk("Unable to read %d",n);
    }
    while(n > 0) 
    {
        printk("%s",buff);
        oldfs = get_fs();
        set_fs(KERNEL_DS); 
        n = sys_read(fd, buff, (long)sizeof buff);
        set_fs(oldfs); 
    }
}

这打印&#34;系统调用正确执行&#34;但是在dmesg中输出是&#34;无法读取-14&#34;。我该如何更正代码?

编辑我搜索了这个问题并找到this。根据页面,所有sys_ *函数都接受来自用户空间的地址。进行更改后,sys_read看起来像这样 -

unable to read -512

但是现在执行程序时,它会被卡住。要停止它,我按下Ctrl-C并检查dmesg,现在显示chown -R yourusername /usr/local chmod -R u+w /usr/local brew link --overwrite autoconf

0 个答案:

没有答案