Linux内核网络协议栈2-socket从库函数到内核
?
一、socket()库函数到系统调用,再到内核
1、Linux运行的C库是glibc;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这里,
#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_socketcall102SOCKOP_socket:glibc-2.3.6/sysdept/unix/sysv/linux/Socketcall.h
#define SOCKOP_socket1因此,中断号是102,子中断号是1;4) int 0x80进入内核:glibc-2.3.6/sysdept/unix/sysv/linux/i386/Sysdept.h:
# define ENTER_KERNEL int $0x805) system_call中断入口:kernel/arch/x86/kernel/entry_32.S:
syscall_call:call *sys_call_table(,%eax,4)?
.long sys_socketcall?
switch (call) {case SYS_SOCKET:break;case SYS_BIND:…...?
#definesocketbind#defineNARGS3#define NO_WEAK_ALIAS1#include <socket.S>weak_alias (bind, __bind)b) accept.S:
#definesocketaccept#define__socket __libc_accept#defineNARGS3#define NEED_CANCELLATION#include <socket.S>libc_hidden_def (accept)在各个库函数调用中,设置不同的参数,如socket(用于设置子中断号), NARGS(系统调用的参数个数)等,最终由
movl $P(SOCKOP_,socket), %ebx/* Subcode is first arg to syscall. */来生成最终的子中断号,然后放到ebx寄存器中;
#define SOCKOP_socket1#define SOCKOP_bind2#define SOCKOP_connect3#define SOCKOP_listen4#define SOCKOP_accept5#define SOCKOP_getsockname6#define SOCKOP_getpeername7#define SOCKOP_socketpair8#define SOCKOP_send9#define SOCKOP_recv10#define SOCKOP_sendto11#define SOCKOP_recvfrom12#define SOCKOP_shutdown13#define SOCKOP_setsockopt14#define SOCKOP_getsockopt15#define SOCKOP_sendmsg16#define SOCKOP_recvmsg17?
?