sys接口linux编程,网络编程常用接口的内核实现----sys_socket()

本文详细探讨了Linux内核中的sys_socket()函数,该函数在创建套接字时,主要调用sock_create()和sock_map_fd()。重点讲解了sock_create()的实现,包括如何通过__sock_create()分配inode和socket对象,以及对于TCP套接字如何调用inet_create()进行进一步处理。inet_create()中涉及到inet_protosw列表的查找、权限检查以及模块加载等关键步骤。
摘要由CSDN通过智能技术生成

sys_socket()的代码流程图如下所示:

cd01184cd8031792f16b18dfb5eaf726.png

从上面的代码及流程图中,可以看到sys_socket()主要调用sock_create()和sock_map_fd()来完成创建套接字的工作,在讨论这两个函数之前,要强调一下type参数。从linux 2.6.27开始,参数type除了指定套接字类型外,还可以通过或运算来指定SOCK_CLOEXEC和SOCK_NONBLOCK标志来改变套接字的行为。可以通过man socket命令来查看,原文是:

Since  Linux  2.6.27, the type argument serves a second purpose: in addition to specifying a

socket type, it may include the bitwise OR of any of the following  values,  to  modify  the

behavior of socket():

SOCK_NONBLOCK  Set the O_NONBLOCK file status flag on the new open file description.  Using

this flag saves extra calls to fcntl(2) to achieve the same result.

SOCK_CLOEXEC    Set the close-on-exec (FD_CLOEXEC) flag on the new file descriptor.  See the

description  of  the  O_CLOEXEC  flag in open(2) for reasons why this may be

useful.

一、sock_create()函数

sock_create()函数时对__sock_create()函数的简单封装,__sock_create()函数的代码流程图如下所示:

9a8de0a1bc5e70428a9e4c8b00a6e11e.png

__sock_create()调用sock_alloc()分配一个新的inode和socket对象,并设置套接字的权限位及套接字所属的用户和用户组。如果要创建的是TCP套接字,pf->create()中的pf是inet_family_ops变量,该变量的类型是net_proto_family结构,在net/ipv4/if_inet

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值