Linux API to determine sockets owned by a process

后端 未结 7 2086
被撕碎了的回忆
被撕碎了的回忆 2020-12-23 01:50

Is there a Linux library that will enable me to tell what IP sockets are owned by what processes? I guess I\'m looking for the programmatic equivalent of lsof -i

7条回答
  •  情话喂你
    2020-12-23 02:31

    The /proc filesystem provides details on each process, including networking information. Open socket information is listed in /proc/net/tcp. The IPv6 sockets are listed separately in the tcp6 file. The socket information includes information such as the local and remote ports, and the socket inode number, which can be mapped back to the process by parsing the /proc/{pid}/fd/* information.

    If you aren't familiar with the /proc filesystem, it is basically a virtual filesystem that allows the kernel to publish all sorts of useful information to user-space. The files are normally simple structured text files that are easy to parse.

    For example, on my Ubuntu system I used netcat for testing, and ran nc -l -p 8321 to listen on port 8321. Looking at the tcp socket information:

    $ cat /proc/net/tcp
      sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode                                                     
       0: 00000000:2081 00000000:0000 0A 00000000:00000000 00:00000000 00000000  1000        0 26442 1 de0c8e40 300 0 0 2 -1                             
       1: 0100007F:0277 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 7019 1 de0c84c0 300 0 0 2 -1                              
    

    The first line shows it is listening on all addresses to point 8321 (0x2081). The inode number is 26442, which we can use to look up the matching pid in /proc/{pid}/fd/*, which consists of a bunch of symlinks from the file handle number to the device. So if we look up the pid for netcat, and check its fd mapping:

    $ ls -l /proc/7266/fd
    total 0
    lrwx------ 1 gavinb gavinb 64 2009-12-31 09:10 0 -> /dev/pts/1
    lrwx------ 1 gavinb gavinb 64 2009-12-31 09:10 1 -> /dev/pts/1
    lrwx------ 1 gavinb gavinb 64 2009-12-31 09:10 2 -> /dev/pts/1
    lrwx------ 1 gavinb gavinb 64 2009-12-31 09:10 3 -> socket:[26442]
    

    And there we see that file descriptor 3 in this process is mapped to the socket with inode 26442, just as we expect.

    So obviously to build a complete map of sockets, you will need to first enumerate all the /proc/**/fd/* files, look up the socket symlinks, then match the socket inode against the tables from /proc/net/tcp which has the endpoint information.

    This is the way the lsof tool works (see lsof/dialects/linux/dsocket.c for the implementation).

    • Wikipedia on procfs
    • The Linux /proc filesystem as a Programmer's Tool

提交回复
热议问题