2

I am writing a kernel module that polls TCP sockets for their queue sizes. I pass the socket file descriptor number to the module through procfs and then use sockfd_lookup to find the socket structure (struct socket) from that.

I am using lsof to obtain the socket fds but it seems that sockfd_lookup is always unable to find the appropriate socket. Is there something I am missing concerning the mapping from the output of lsof to the socket file descriptor numbers?

  • Are you operating in the context of the process whose file descriptor you're trying to query? The fact that you are getting info from `lsof` and passing it in to your module suggests you're not. File descriptors are per-process not unique system-wide. You would need to find the particular process' `task_struct` and look at its own `files` array in order to do this successfully. That will be complex. – Gil Hamilton Mar 02 '18 at 20:35
  • Have you looked at the info `netstat` provides? It may already produce the sizes you need. And if not, looking at its source code might be instructive anyway. – Gil Hamilton Mar 02 '18 at 20:38
  • @GilHamilton Thank you, that was very helpful. I looked at netstat but it didn't contain the stuff I was looking for. However, I was able to solve the issue using your suggestion of looking at the `task_struct` of the process and looking into the `files` array. – Johannes DeSilentio Mar 19 '18 at 18:47

0 Answers0