An old question, but there are at least two ways to do this:
1) Use the audit subsystem
You can configure auditd and the Linux auditing subsystem to log a message every time any system call occurs. It will include a time stamp and a call process. Something that hooks 'connect ()' and / or 'bind ()' should get you what you need for sockets. This is what has been done for the audit.
2) Use ip_conntrack (netfilter / ip_tables)
Use something like the libnetfilter-conntrack library (which uses the ip_conntrack kernel module), you will receive notifications of all new sockets with filtering as desired. However, it will only indicate the local and remote address / port and timestamp, not the inode. This means that to correlate this with pid, you first need to read the notification from conntrack and then parse the files in the / proc / net / {tcp / udp / whatever} files to find the socket and inode and then parse all the / proc files / $ pid / fd / * to find out which pid owns this inode. At each step, you should hope that the socket does not disappear by the time you read the files in this three-step process. Such a system is used by flowtop from the netsniff-ng utils package.
All systems require root, although after auditd is configured with root, the logs can be read without root authority if you want. I think you would like to use auditd whenever possible. The ip_conntrack interface seems a bit nice at first, but auditd provides you with all the information you need, including pid tracking, for free.
source share