File descriptors in the Linux kernel (Part 2) --sockets and file descriptors

Publisher:玉米哥哥Latest update time:2016-03-02 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Kernel version:2.6.14

CPU architecture:ARM920T

Author:ce123(http://blog.csdn.net/ce123)

Sockets are closely related to the file system. We can operate sockets through the file system's open, read, write, and close functions. The following is a simple example.

 

[plain] view plain copy
 
 print?
  1. /****************************************************************************/  
  2. /*Introduction: TCPServer Example */  
  3. /****************************************************************************/  
  4. #include    
  5. #include    
  6. #include    
  7. #include    
  8. #include    
  9. #include    
  10. #include    
  11. #include    
  12. int main(int argc, char *argv[])   
  13. {   
  14.  int sockfd,new_fd;   
  15.  struct sockaddr_in server_addr;   
  16.  struct sockaddr_in client_addr;   
  17.  int sin_size,portnumber;   
  18.  const char hello[]="Hello\n";  
  19.   
  20.  if(argc!=2)   
  21.   {   
  22.      fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);   
  23.      exit(1);   
  24.   }   
  25.   if((portnumber=atoi(argv[1]))<0)   
  26.   {   
  27.       fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);   
  28.       exit(1);   
  29.  }   
  30.   /* The server starts to create a socket descriptor */   
  31.   if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)   
  32.   {   
  33.      fprintf(stderr,"Socket error:%s\n\a",strerror(errno));   
  34.      exit(1);   
  35.   }   
  36.   /* Server side fills in sockaddr structure */   
  37.   bzero(&server_addr,sizeof(struct sockaddr_in));   
  38.   server_addr.sin_family=AF_INET;   
  39.   server_addr.sin_addr.s_addr=htonl(INADDR_ANY);   
  40.   server_addr.sin_port=htons(portnumber);   
  41.   /* bind sockfd descriptor */   
  42.   if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==   
  43.   -1)   
  44.   {   
  45.      fprintf(stderr,"Bind error:%s\n\a",strerror(errno));   
  46.      exit(1);   
  47.   }   
  48.   /* Listen to sockfd descriptor */   
  49.   if(listen(sockfd,5)==-1)   
  50.   {   
  51.       fprintf(stderr,"Listen error:%s\n\a",strerror(errno));   
  52.       exit(1);   
  53.   }   
  54.   while(1)   
  55.   {   
  56.   /* The server blocks until the client program establishes a connection */   
  57.    sin_size=sizeof(struct sockaddr_in);   
  58.    if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1)   
  59.      {   
  60.       fprintf(stderr,"Accept error:%s\n\a",strerror(errno));   
  61.       exit(1);   
  62.      }   
  63.    fprintf(stderr,"Server get connection from %s\n",   
  64.    inet_ntoa(client_addr.sin_addr));   
  65.    if(write(new_fd,hello,strlen(hello))==-1)   
  66.    {   
  67.       fprintf(stderr,"Write Error:%s\n",strerror(errno));   
  68.       exit(1);   
  69.     }   
  70.      /* This communication has ended */   
  71.       close(new_fd);   
  72.   /* loop next */   
  73.   }   
  74.   close(sockfd);   
  75.   exit(0);   
  76. }  

 

The following figure illustrates how socket and fd are connected.

 

Let's analyze it in detail. sys_socket is the main entry point for socket related functions.

 

[plain] view plain copy
 
 print?
  1. net/socket.c  
  2. /*  
  3.  *  System call vectors.   
  4.  *  
  5.  *  Argument checking cleaned up. Saved 20% in size.  
  6.  *  This function doesn't need to set the kernel lock because  
  7.  *  it is set by the callees.   
  8.  */  
  9.   
  10. asmlinkage long sys_socketcall(int call, unsigned long __user *args)  
  11. {  
  12.     unsigned long a[6];  
  13.     unsigned long a0,a1;  
  14.     int err;  
  15.   
  16.     if(call<1||call>SYS_RECVMSG)  
  17.         return -SINGLE SELECT;  
  18.   
  19.     /* copy_from_user should be SMP safe. */  
  20.     if (copy_from_user(a, args, nargs[call]))  
  21.         return -EFAULT;  
  22.   
  23.     err = audit_socketcall(nargs[call]/sizeof(unsigned long), a);  
  24.     if (err)  
  25.         return err;  
  26.   
  27.     a0=a[0];  
  28.     a1=a[1];  
  29.       
  30.     switch(call)   
  31.     {  
  32.         case SYS_SOCKET:  
  33.             err = sys_socket(a0,a1,a[2]);  
  34.             break;  
  35.         case SYS_BIND:  
  36.             err = sys_bind(a0,(struct sockaddr __user *)a1, a[2]);  
  37.             break;  
  38.         case SYS_CONNECT:  
  39.             err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);  
  40.             break;  
  41.         case SYS_LISTEN:  
  42.             err = sys_listen(a0,a1);  
  43.             break;  
  44.         case SYS_ACCEPT:  
  45.             err = sys_accept(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);  
  46.             break;  
  47.         case SYS_GETSOCKNAME:  
  48.             err = sys_getsockname(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);  
  49.             break;  
  50.         case SYS_GETPEERNAME:  
  51.             err = sys_getpeername(a0, (struct sockaddr __user *)a1, (int __user *)a[2]);  
  52.             break;  
  53.         case SYS_SOCKETPAIR:  
  54.             err = sys_socketpair(a0,a1, a[2], (int __user *)a[3]);  
  55.             break;  
  56.         case SYS_SEND:  
  57.             err = sys_send(a0, (void __user *)a1, a[2], a[3]);  
  58.             break;  
  59.         case SYS_SENDTO:  
  60.             err = sys_sendto(a0,(void __user *)a1, a[2], a[3],  
  61.                      (struct sockaddr __user *)a[4], a[5]);  
  62.             break;  
  63.         case SYS_RECV:  
  64.             err = sys_recv(a0, (void __user *)a1, a[2], a[3]);  
  65.             break;  
  66.         case SYS_RECVFROM:  
  67.             err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],  
  68.                        (struct sockaddr __user *)a[4], (int __user *)a[5]);  
  69.             break;  
  70.         case SYS_SHUTDOWN:  
  71.             err = sys_shutdown(a0,a1);  
  72.             break;  
  73.         case SYS_SETSOCKOPT:  
  74.             err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);  
  75.             break;  
  76.         case SYS_GETSOCKOPT:  
  77.             err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]);  
  78.             break;  
  79.         case SYS_SENDMSG:  
  80.             err = sys_sendmsg(a0, (struct msghdr __user *) a1, a[2]);  
  81.             break;  
  82.         case SYS_RECVMSG:  
  83.             err = sys_recvmsg(a0, (struct msghdr __user *) a1, a[2]);  
  84.             break;  
  85.         default:  
  86.             err = -ONE SELECT;  
  87.             break;  
  88.     }  
  89.     return err;  
  90. }   /* It may be already another descriptor 8) Not kernel problem. */  
  91.     return retval;  
  92.   
  93. out_release:  
  94.     sock_release(sock);  
  95.     return retval;  
  96. }  
When an application creates a socket using socket(), sys_socket is executed, which is defined as follows

 

 

[plain] view plain copy
 
 print?
  1. asmlinkage long sys_socket(int family, int type, int protocol)  
  2. {  
  3.     int retval;  
  4.     struct socket *sock;  
  5.   
  6.     retval = sock_create(family, type, protocol, &sock);//创建socket  
  7.     if (retval < 0)  
  8.         goto out;  
  9.   
  10.     retval = sock_map_fd(sock); //Assign an unused file descriptor fd and associate the socket with fd  
  11.     if (retval < 0)  
  12.         goto out_release;  
  13.   
  14. out:  
  15.     /* It may be already another descriptor 8) Not kernel problem. */  
  16.     return retval;  
  17.   
  18. out_release:  
  19.     sock_release(sock);  
  20.     return retval;  
  21. }  
The definition of the structure socket is as follows (include\linux\net.h):

 

 

[plain] view plain copy
 
 print?
  1. struct socket {  
  2.     socket_state        state;  
  3.     unsigned long       flags;  
  4.     struct proto_ops    *ops;  
  5.     struct fasync_struct    *fasync_list;  
  6.     struct file *file; //Establish a connection with the file descriptor through this  
  7.     struct sock     *sk;  
  8.     wait_queue_head_t   wait;  
  9.     short           type;  
  10. };  
Next, let's take a look at the sock_map_fd function

 

[plain] view plain copy
 
 print?
  1. int sock_map_fd(struct socket *sock)  
  2. {  
  3.     int fd;  
  4.     struct qstr this;  
  5.     char name[32];  
  6.   
  7.     /*  
  8.      *  Find a file descriptor suitable for return to the user.   
  9.      */  
  10.   
  11.     fd = get_unused_fd(); //Assign an unused fd  
  12.     if (fd >= 0) {  
  13.         struct file *file = get_empty_filp();  
  14.   
  15.         if (!file) {  
  16.             put_unused_fd(fd);  
  17.             fd = -ENFILE;  
  18.             goto out;  
  19.         }  
  20.   
  21.         this.len = sprintf(name, "[%lu]", SOCK_INODE(sock)->i_ino);  
  22.         this.name = name;  
  23.         this.hash = SOCK_INODE(sock)->i_ino;  
  24.   
  25.         file->f_dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this);  
  26.         if (!file->f_dentry) {  
  27.             put_filp(file);  
  28.             put_unused_fd(fd);  
  29.             fd = -ENOMEM;  
  30.             goto out;  
  31.         }  
  32.         file->f_dentry->d_op = &sockfs_dentry_operations;  
  33.         d_add(file->f_dentry, SOCK_INODE(sock));  
  34.         file->f_vfsmnt = mntget(sock_mnt);  
  35.         file->f_mapping = file->f_dentry->d_inode->i_mapping;  
  36.   
  37.         sock->file = file; // establish connection  
  38.         file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops; //socket operation function. When using the file system's IO function, the socket's IO function is actually used.  
  39.         file->f_mode = FMODE_READ | FMODE_WRITE;  
  40.         file->f_flags = O_RDWR;  
  41.         file->f_pos = 0;  
  42.         file->private_data = sock;  
  43.         fd_install(fd, file);  
  44.     }  
  45.   
  46. out:  
  47.     return fd;  
  48. }  
  49.   
  50. static struct file_operations socket_file_ops = {  
  51.     .owner =    THIS_MODULE,  
  52.     .llseek =   no_llseek,  
  53.     .aio_read = sock_aio_read,  
  54.     .aio_write =    sock_aio_write,  
  55.     .poll =     sock_poll,  
  56.     .unlocked_ioctl = sock_ioctl,  
  57.     .mmap =     sock_mmap,  
  58.     .open =     sock_no_open,   /* special open code to disallow open via /proc */  
  59.     .release =  sock_close,  
  60.     .fasync =   sock_fasync,  
  61.     .readv =    sock_readv,  
  62.     .writev =   sock_writev,  
  63.     .sendpage = sock_sendpage  
  64. };  

Reference address:File descriptors in the Linux kernel (Part 2) --sockets and file descriptors

Previous article:File descriptors in the Linux kernel (Part 3) -- recycling of fd
Next article:File descriptors in the Linux kernel (I) -- Basic knowledge introduction

Recommended ReadingLatest update time:2024-11-16 13:25

Design and implementation of Socket communication based on MC9328MXl
1 Introduction At present, embedded systems have been widely used in people's work and life. In 2003, the total economic volume of embedded system application products in my country reached 100 billion yuan. With the development of technology, the 8-bit MCU market has gradually stabilized, and the 32-bit MPU represent
[Microcontroller]
Design and implementation of Socket communication based on MC9328MXl
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号