系统调用(追踪sys_socket)

系统调用(追踪sys_socket

include/linux/syscalls.h中定义了sys_socket函数的函数原型(prototype

asmlinkage long sys_socket(int, int, int);

系统调用函数必须满足:

asmlinkage long sys_##function-name(##args){ ,return ret}

 

include/linux/unistd.h中,将sys_socket系统调用和系统调用好关联起来

#define __NR_socket 198       

__SYSCALL(__NR_socket, sys_socket)//系统调用号为198

unistd.h中,同时给出注释表明,给函数的实现在socket.c

进入socket.cnet/中),发现有这样一个函数实现(或者定义)

SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol){ ….}

3:代表有3个参数,用于解析参数时候用)

肯定这就是asmlinkage long sys_socketintintint)的实现了。为了表明这一切,需要进一步查看宏SYSCALL_DEFINE3的定义

值得高兴的是SYSCALL_DEINFE3的定义也在syscalls.h

#define SYSCALL_DEFINE1(name, ...) SYSCALL_DEFINEx(1, _##name, __VA_ARGS__)

#define SYSCALL_DEFINE2(name, ...) SYSCALL_DEFINEx(2, _##name, __VA_ARGS__)

#define SYSCALL_DEFINE3(name, ...) SYSCALL_DEFINEx(3, _##name, __VA_ARGS__)

#define SYSCALL_DEFINE4(name, ...) SYSCALL_DEFINEx(4, _##name, __VA_ARGS__)

#define SYSCALL_DEFINE5(name, ...) SYSCALL_DEFINEx(5, _##name, __VA_ARGS__)

#define SYSCALL_DEFINE6(name, ...) SYSCALL_DEFINEx(6, _##name, __VA_ARGS__)

那么:SYSCALL_DEFINE3(socket,int,family,int,type,int,protocal)===

SYSCALL_DEFINEX(3,socket,__VA_ARGS__)

在同一文件中,定义了SYSCALL_DEFINEX(….)

#ifdef CONFIG_FTRACE_SYSCALLS

#define SYSCALL_DEFINEx(x, sname, ...)                          /

       static const char *types_##sname[] = {                    /

              __SC_STR_TDECL##x(__VA_ARGS__)                 /

       };                                              /

       static const char *args_##sname[] = {               /

              __SC_STR_ADECL##x(__VA_ARGS__)                 /

       };                                              /

       SYSCALL_METADATA(sname, x);                      /

       __SYSCALL_DEFINEx(x, sname, __VA_ARGS__)

#else

#define SYSCALL_DEFINEx(x, sname, ...)                          /

       __SYSCALL_DEFINEx(x, sname, __VA_ARGS__)

#endif

 

#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS

 

#define SYSCALL_DEFINE(name) static inline long SYSC_##name

 

#define __SYSCALL_DEFINEx(x, name, ...)                              /

       asmlinkage long sys##name(__SC_DECL##x(__VA_ARGS__));        /

       static inline long SYSC##name(__SC_DECL##x(__VA_ARGS__));    /

       asmlinkage long SyS##name(__SC_LONG##x(__VA_ARGS__))        /

       {                                                      /

              __SC_TEST##x(__VA_ARGS__);                      /

              return (long) SYSC##name(__SC_CAST##x(__VA_ARGS__));     /

       }                                                      /

       SYSCALL_ALIAS(sys##name, SyS##name);                         /

       static inline long SYSC##name(__SC_DECL##x(__VA_ARGS__))

 

#else /* CONFIG_HAVE_SYSCALL_WRAPPERS */

 

#define SYSCALL_DEFINE(name) asmlinkage long sys_##name

#define __SYSCALL_DEFINEx(x, name, ...)                              /

       asmlinkage long sys##name(__SC_DECL##x(__VA_ARGS__))

 

#endif /* CONFIG_HAVE_SYSCALL_WRAPPERS */

由此,我们得到

SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol){ ….}

ó SYSCALL_DEFINEX(3,_socket,__VA_ARGS__)

ó_SYSCALL_DEFINE(3,_socket,__VA_ARGS__)

óasmlinkage long sys_socket(int family,int type,int protocol)

而他的原型正是:asmlinkage long sys_socketintintint

分析了系统是如何定义和实现sys_socket的系统调用。接下来,仔细分析sys_socket是如何实现创socket的。在前面,我们知道inet_family_ops中的create函数为inet_create,也就是说,如果要创建inet型的socket,将由函数inet_create来创建。

先来看看inet_family_ops

static const struct net_proto_family inet_family_ops = {

       .family = PF_INET,

       .create = inet_create,

       .owner  = THIS_MODULE,

};

 

 

 

下面看看sys_socket中的函数调用关系:

sys_socket

       |

       +--------- sock_create

       |                    |

       |                    +------- __sock_create

       |                                         |

       |                                         +------- security_socket_create

       |                                         +-------- sock_alloc()

       |                                         +--------- rcu_dereference(net_families[family])

       |                                         +--------- pf->create(net, sock, protocol, kern)

       |                                         +--------- module_put(pf->owner)

       |                                         +--------- security_socket_post_create

       +---------- sock_map_fd

sys_socket 调用sock_create函数,最终调用rcu_dereference函数来得到相应的net_family_ops,在这里是inet_family_ops,然后调用inet_family_ops结构中的create函数,这里是inet_create函数,来创建socketsock_map_fd是得到一个文件号。

(not confirm)

当使用socket(int,int,int)创建一个socket时,socket会调用sys_socket来完成socket的创建。

当使用sys_socket创建完socket后,会调用bind来执行端口等绑定设置,这个我想应该是由sys_bind来实现,然后就调用send发送数据了,这个由sys_send来实现。那么具体是什么函数来执行数据的发送呢?(undone)

你可能感兴趣的:(系统调用(追踪sys_socket))