linux下Read系统调用的全程

当我们在C程序中用到某些库函数进行文件读取操作时,后续的整个过程都是透明的,为了了解文件系统在其中起到了什么作用,又是如何和内核的其他部分进行协作的,我们可以对Read()函数进行追踪,下面的代码均来自linux2.6.11.10版本的内核。

每一串代码前我都标好了路径。

首先,我们写下如下的测试程序,test.c,其中1.txt里只有一句Hello,World。

#include <stdio.h>
#include <stdlib.h>
int main() {
    char word[20];
    FILE *fp;
    if((fp = fopen("1.txt","a+")) == NULL) {
        fprintf(stdout, "ERROR!");  
        exit(EXIT_FAILURE);
    }
    fscanf(fp,"%s",word);
    printf("%s\n",word);                             
    return 0;                                                
}

然后进行编译,并通过strace 工具查看函数运行时用到了哪些系统调用函数,并将结果输出到hello.txt中。

~/test$ gcc hello.c -o hello
~/test$ strace -o hello.txt ./hello

查看hello.txt中的主要内容如下

……
openat(AT_FDCWD, "x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT
……
openat(AT_FDCWD, "1.txt", O_RDWR|O_CREAT|O_APPEND, 0666) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=13, ...}) = 0
read(3, "Hello,World!\n", 4096)         = 13
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), ...}) = 0
write(1, "Hello,World!\n", 13)          = 13
lseek(3, -1, SEEK_CUR)                  = 12  
exit_group(0)                           = ?

可以看到首先打开了libc.so,这里面封装了我们需要的库函数,而后调用了write、read、lseek等库函数。

我们知道,系统调用有两种方式实现,一种是老旧的int $0x80方式,还有一种是sysenter,具体细节不纠结,但过程总是先将系统调用号存入$eax,然后进行系统调用,这部分实现已经完全放进库函数了,进行系统调用后,会查系统调用表,比如read的系统调用就是3,那么查表就能查到这个函数。

比如i386处理器的系统调用号局部如下所示

/linux-2.6.11.10/include/asm-i386/unistd.h
#define __NR_restart_syscall      0
#define __NR_exit         1
#define __NR_fork         2
#define __NR_read         3
#define __NR_write        4
#define __NR_open         5
#define __NR_close        6
#define __NR_waitpid      7
#define __NR_creat        8
#define __NR_link         9                          
#define __NR_unlink      10                           
#define __NR_execve      11                            
#define __NR_chdir       12                           
#define __NR_time        13                            
#define __NR_mknod       14                           
#define __NR_chmod       15                           
#define __NR_lchown      16                            
#define __NR_break       17

由上我们看到,调用read的系统调用号为3,在这个文件的下面我们还能看到比较老旧的系统调用实现代码,现在这个功能好像已经放到库中去实现了,不在内核中实现,这里内核版本较老,所以在内核中还能看到,这里用的是通过系统调用需要的参数个数来进行区分的。

/linux-2.6.11.10/include/asm-i386/unistd.h
#define __syscall_return(type, res) \                
do { \
    if ((unsigned long)(res) >= (unsigned long)(-(128 + 1))) { \
        errno = -(res); \
        res = -1; \
    } \
    return (type) (res); \
} while (0)
/* XXX - _foo needs to be __foo, while __NR_bar could be _NR_bar. */
#define _syscall0(type,name) \
type name(void) \
{ \
long __res; \
__asm__ volatile
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值