内核中的UDP socket流程(3)(4)——sock_create
来源:互联网 发布:状态机编程的优缺点 编辑:程序博客网 时间:2024/05/16 14:23
retval = sock_create(family, type, protocol, &sock);
下面看sock_create的代码
int sock_create(int family, int type, int protocol, struct socket **res)
{
return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
}
struct nsproxy {
atomic_t count;
struct uts_namespace *uts_ns;
struct ipc_namespace *ipc_ns;
struct mnt_namespace *mnt_ns;
struct pid_namespace *pid_ns;
struct net *net_ns;
};
static int __sock_create(struct net *net, int family, int type, int protocol,
struct socket **res, int kern)
{
int err;
struct socket *sock;
const struct net_proto_family *pf;
/*
* Check protocol is in range
*/
if (family < 0 || family >= NPROTO)
return -EAFNOSUPPORT;
if (type < 0 || type >= SOCK_MAX)
return -EINVAL;
/*
* Skip some codes
*/
}
首先这个函数,先对family和type进行检查,查看是否超出正常范围。
/* Compatibility.
This uglymoron is moved from INET layer to here to avoid
deadlock in module load.
*/
if (family == PF_INET && type == SOCK_PACKET) {
static int warned;
if (!warned) {
warned = 1;
printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
current->comm);
}
family = PF_PACKET;
}
err = security_socket_create(family, type, protocol, kern);
if (err)
return err;
/*
* Allocate the socket and allow the family to set things up. if
* the protocol is 0, the family is instructed to select an appropriate
* default.
*/
sock = sock_alloc();
if (!sock) {
if (net_ratelimit())
printk(KERN_WARNING "socket: no more sockets\n");
return -ENFILE; /* Not exactly a match, but its the
closest posix thing */
}
static struct inode *alloc_inode(struct super_block *sb)
{
struct inode *inode;
return inode;
}
struct inode *new_inode_pseudo(struct super_block *sb)
{
struct inode *inode = alloc_inode(sb);
return inode;
}
static struct socket *sock_alloc(void)
{
struct inode *inode;
struct socket *sock;
inode = new_inode_pseudo(sock_mnt->mnt_sb);
if (!inode)
return NULL;
sock = SOCKET_I(inode);
kmemcheck_annotate_bitfield(sock, type);
inode->i_mode = S_IFSOCK | S_IRWXUGO;
inode->i_uid = current_fsuid();
inode->i_gid = current_fsgid();
percpu_add(sockets_in_use, 1);
return sock;
}
struct socket_alloc {
struct socket socket;
struct inode vfs_inode;
};
rcu_read_lock();
pf = rcu_dereference(net_families[family]);
err = -EAFNOSUPPORT;
if (!pf)
goto out_release;
/*
* We will call the ->create function, that possibly is in a loadable
* module, so we have to bump that loadable module refcnt first.
*/
if (!try_module_get(pf->owner))
goto out_release;
/* Now protected by module ref count */
rcu_read_unlock();
err = pf->create(net, sock, protocol, kern);
if (err < 0)
goto out_module_put;
static const struct net_proto_family inet_family_ops = {
.family = PF_INET,
.create = inet_create,
.owner = THIS_MODULE,
};
/*
* Now to bump the refcnt of the [loadable] module that owns this
* socket at sock_release time we decrement its refcnt.
*/
if (!try_module_get(sock->ops->owner))
goto out_module_busy;
/*
* Now that we're done with the ->create function, the [loadable]
* module can have its refcnt decremented
*/
module_put(pf->owner);
err = security_socket_post_create(sock, family, type, protocol, kern);
if (err)
goto out_sock_release;
*res = sock;
return 0;
- 内核中的UDP socket流程(3)(4)——sock_create
- 内核中的UDP socket流程(5)——inet_create
- 内核中的UDP socket流程(6)——sendto
- 内核中的UDP socket流程(7)——udp_sendmsg
- 内核中的UDP socket流程(1)
- 内核中的UDP socket流程(2)——API “sys_socket”
- Java中的Socket通信(UDP)
- socket编程(3)—— UDP协议通信
- TCP UDP Socket流程
- UDP socket(一)
- UDP socket(二)
- 实例解析lua中的socket(TCP,UDP)
- Socket套接字编程(二)——UDP编程
- 内核态 Socket UDP编程
- 对Socket CAN的理解(4)——【Socket CAN接收数据流程】
- 对Socket CAN的理解(4)——【Socket CAN接收数据流程】 .
- 对Socket CAN的理解(4)——【Socket CAN接收数据流程】
- 对Socket CAN的理解(4)——【Socket CAN接收数据流程】
- 非法集资(Illegal Fund-raising)
- 解决ListView和ScrollView共存问题
- matlab 画图函数,文件的读写
- 【原创】c#如何实现RTU远程数据采集功能及RTU在水利工程中的运用
- linux中文件描述符fd和文件指针flip的理解
- 内核中的UDP socket流程(3)(4)——sock_create
- Tips For Codeigniter
- vxworks解决注释不匹配问题
- phpmyadmin导入sql文件出错
- 智力题
- hasOwnProperty,Object.keys(),propertyIsEnumerable,for-in(loop)
- POJ 2104(K-th Number-区间第k大-主席树)
- Windows驱动_USB驱动之四
- 谷歌浏览器开发工具使用教程