Skip to content
Advertisement

get process inode using netlink

I want to try and correlate an IP packet (using libpcap) to a process. I have had some limited success using the relevant /proc/net/ files but found that on some of the machines i’m using, this file can be many thousands of lines and parsing it is not efficient (caching has alleviated some performance problems).

I read that using sock_diag netlink subsystem could help by directly querying the kernel about the socket I am interested in. I’ve had limited success with my attempts but have hit a mental block on what is wrong.

For the initial query I have:

JavaScript

For the receive code I have:

JavaScript

The Problem:

The diag->udiag_inode value doesn’t match the one I see in netstat output or in the /proc/net/ files. Is it supposed too? If not, is it possible to use this approach to retrieve the inode number for the process so that I can then query /proc for the corresponding PID?

Another thing I didn’t quite understand is the NLMSG_DONE when checking the nlmsg_type in the header. What I am seeing:

JavaScript

So I get an inode number on first query, then some NLMSG_DONE returns (stepping through code confirmed this was the path). Why don’t I get the same result for say lines 1 and 3?

Appreciate any help or advice.

Advertisement

Answer

Found the answer and posting in case anyone stumbles across it:

  1. I had a uint16_t as the return type from the recv code when in fact it should have been ino_t or uint32_t. I discovered this when I noticed that a few of the inodes matched correctly after a fresh reboot and then after a while stopped matching with no code changed (inode count obviously incrementing). Using the correct type in the function return sorted the problem (so the code I posted is actually correct!)

  2. I was getting multi part messages. I should have looped whilst NLM_F_MULTI was set in the flags and then left the loop when receiving NLMSG_DONE.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement