日期:2014-05-16  浏览次数:20698 次

Linux内核网络协议栈3-创建socket

?

1、示例及函数入口:
1) 示例代码如下:
int server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

2) 入口:
net/Socket.c:sys_socketcall(),根据子系统调用号,创建socket会执行sys_socket()函数;

2、分配socket结构:
1) 调用链:
net/Socket.c:sys_socket()->sock_create()->__sock_create()->sock_alloc();

2) 在socket文件系统中创建i节点:
inode = new_inode(sock_mnt->mnt_sb);
其中,sock_mnt为socket文件系统的根节点,是在内核初始化安装socket文件系统时赋值的,mnt_sb是该文件系统安装点的超级块对象的指针;
这里,new_inode函数是文件系统的通用函数,其作用是在相应的文件系统中创建一个inode;其主要代码如下(fs/Inode.c):
struct inode *new_inode(struct super_block *sb) {
	struct inode * inode;
	inode = alloc_inode(sb);
	…...
	return inode;
}
这里调用了alloc_inode函数分配inode结构(fs/Inode.c):
static struct inode *alloc_inode(struct super_block *sb) {
	struct inode *inode;

	if (sb->s_op->alloc_inode)
		inode = sb->s_op->alloc_inode(sb);
	else
		inode = (struct inode *) kmem_cache_alloc(inode_cachep, GFP_KERNEL);
            …...
}