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

Linux内核网络协议栈2-socket从库函数到内核

?

一、socket()库函数到系统调用,再到内核

1、Linux运行的C库是glibc;
2、socket()调用如下:
1) socket()->__socket():glibc-2.3.6/sysdept/generic/socket.c (weak_alias(name1, name2))
2) __socket():glibc-2.3.6/sysdept/unix/sysv/linux/i386/socket.S
3) ENTER_KERNEL:
movl $SYS_ify(socketcall), %eax	/* System call number in %eax.  */

/* Use ## so `socket' is a separate token that might be #define'd.  */
movl $P(SOCKOP_,socket), %ebx	/* Subcode is first arg to syscall.  */
lea 4(%esp), %ecx		/* Address of args is 2nd arg.  */

        /* Do the system call trap.  */
ENTER_KERNEL
这里,
SYS_ify宏定义为:glibc-2.3.6/sysdept/unix/sysv/linux/i386/Sysdept.h
#define SYS_ify(syscall_name)	__NR_##syscall_name;
P宏定义为:glibc-2.3.6/sysdept/unix/sysv/linux/i386/socket.S
#define P(a, b) P2(a, b)
#define P2(a, b) a##b
其中,##为连接符号;
#define __NR_socketcall		102
SOCKOP_socket:glibc-2.3.6/sysdept/unix/sysv/linux/Socketcall.h