2 套接字接口函数sys_socketcall
sys_socketcall()位于/net/socket.c中,当我们调用socket()、bind()、write()等所有与套接字有关的函数,都是调用的内核的一个系统调用:sys_socketcall,然后再有这个函数通过switch判断类型,进行进一步处理,相当于一个封装函数。
它此函数并不是上述函数直接调用,而是会由系统回调。回调函数在文件entry.S中定义,但entry.S中都是汇编语句,还看不明白,这部分回调怎么实现的,以后再说。
asmlinkage long sys_socketcall(int call, unsigned long* args)
{
unsigned long a[6]; //用来存放参数
unsigend long a0, a1;
int err;
//将参数复制到a[]中
if( copy_from_user(a, args, nargs[call])) return -EFAULT;
//用于安全审计方面赋值,此方面内容本书略过,请参考相关专门书籍
err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
if (err) return err;
a0 = a[0]; a1 = a[1];
//大switch判断调用类型,所有的套接字调用均需经过此判断后,调用实际的处理函数
//可以看出后文中,将a0,a1转换为实际参数,其中的__user是说明此为用户空间地址
switch( call )
{
case SYS_SOCKET:
err = sys_socket(a0, a1, a[2]);
break;
case SYS_BIND:
err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
break;
case SYS_CONNECT:
err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
break;
case SYS_LISTEN:
err = sys_listen(a0, a1);
break;
case SYS_ACCEPT:
err = sys_accept(a0, (struct sockaddr __user *)a1,(int __user *)a[2]);
break;
case SYS_GETSOCKNAME:
err = sys_getsockname(a0, (struct sockaddr __user *)a1, (int __user *)a[2]);
break;
case SYS_GETPEERNAME:
err = sys_getpeername(a0, (struct sockaddr __user *)a1,(int __user *)a[2]);
break;
case SYS_SOCKETPAIR:
err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
break;
case SYS_SEND:
err = sys_send(a0, (void __user *)a1, a[2], a[3]);
break;
case SYS_SENDTO:
err = sys_sendto(a0, (void __user *)a1, a[2], a[3], (struct sockaddr __user *)a[4], a[5]);
break;
case SYS_RECV:
err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
break;
case SYS_RECVFROM:
err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3], (struct sockaddr __user *)a[4], (int __user *)a[5]);
break;
case SYS_SHUTDOWN:
err = sys_shutdown(a0, a1);
break;
case SYS_SETSOCKOPT:
err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
break;
case SYS_GETSOCKOPT:
err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]);
break;
case SYS_SENDMSG:
err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
break;
case SYS_RECVMSG:
err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
break;
default:
err = -EINVAL;
break;
}
return err;
}