Where in the linux kernel does the closing of a socket’s file descriptor occur? I know for a file, the file’s file descriptor is closed in fs/open.cs function sys_close(). However, for a socket file descriptor, is this the same location or somewhere else?
Also, do sockets utilize the file.c alloc_fd to allocate the file descriptor or do they utilize some other function?
Yes,
sys_close()is the entry point for closing all file descriptors, including sockets.sys_close()callsfilp_close(), which callsfput()on thestruct fileobject. When the last reference to thestruct filehas been put,fput()calls the file object’s.release()method, which for sockets, is thesock_close()function innet/socket.c.The socket code uses
get_unused_fd()andput_unused_fd()to acquire and release file descriptors.