socket从userspace到kernel的api执行过程(不含tcp/ip协议栈部分)

glibc版本2.3.6Kernel版本:4.3

Userspace glibc接口说明

glibc中socket接口定义:(glibc-x.x.x/sysdeps/generic/socket.cx.x.x是版本号)

int __socket (domain, type, protocol)

    int domain;

    int type;

    int protocol;

{

 __set_errno (ENOSYS);

 return -1;

}

weak_alias (__socket, socket)

stub_warning (socket)

#include <stub-tag.h>

可以在glibc库中做权限检查,这样需要在调用socket接口时到系统服务去检查是否允许当前进程调用socket接口。需要事先配置好调用权限,在系统服务启动时候加载权限配置。gcc编译器扩展为socket定义了别名__socket,__socket的真实定义是在glibc-x.x.x/sysdeps/unix/sysv/linux/i386/socket.S。在i386、arm有对应的socket.S

socket.S关键执行点说明:

1、把socket对应的系统调用号mov到eax寄存器

movl$SYS_ify(socketcall), %eax      /* System call number in %eax.  */

2、把参数的地址弄到%esp寄存器

 lea4(%esp), %ecx

3、产生$0x80软中断,进入内核执行

socket.S 中调用ENTER_KERNEL

4、socket.S文件也有和C文件一致的别名定义,这样编译器就能找到别名的具体实现。


执行0x80软中端后就到达系统调用的总入口system_call()函数,system_call()最终使用汇编call指令(call *sys_call_table(,%eax, 4))根据寄存器%eax中的值执sys_call_table系统调用表102对应的函数指针指向的函数。102系统调用号对应的函数是:sys_socketcall(),glibc中所有socket相关的接口都走这个系统调用接口

sys_call_table系统调用号表定义在:arch/m32r/kernel/syscall_table.S

system_call()汇编接口定义在:arch/x86/kernel/entry_32.S

不同平台会有不同的定义文件


Kernel里面socket相关函数说明:

/net/socket.c里面有sys_socketcall()、sys_socket()、sys_bind()等等一系列socket函数的具体实现:

SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)

SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)

SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr,int, addrlen)

SYSCALL_DEFINE2(listen, int, fd, int, backlog)

SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *,upeer_sockaddr,int __user*, upeer_addrlen)

SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *,uservaddr,int,addrlen)

SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,unsignedint, flags, struct sockaddr __user *, addr, int,addr_len)

SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,unsignedint, flags)

SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t,size,unsignedint, flags, struct sockaddr __user *, addr, int __user*, addr_len)

SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size, unsignedint, flags)

SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,char __user*, optval, int, optlen)

SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,char __user*, optval, int __user *, optlen)

/include/linux/syscalls.h里面有上述宏的定义

socket(AF_INET, SOCK_STREAM, IPPROTO_TCP),以下接口都以此种类型的socket来说明

一、socket函数概要说明:

SYSCALL_DEFINE3(socket,int, family, int, type, int, protocol),以下的函数基本上都在socket.c文件里面实现,定义都是SYSCALL_DEFINEx()的格式。SYSCALL_DEFINEx的定义见include/linux/syscalls.h其中的x代表宏封装的函数有几个参数。宏SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)封装了socket这个函数,参数是family、type、protocol这三个,参数数据类型都是int。

1、  调用sock_create函数创建structsocket *sock;

retval = sock_create(family, type, protocol, &s

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值