views:

520

answers:

7

A process on my Linux system, strace tells me, is talking on a socket which has file descriptor 10. lsof tells me that this is a unix socket with inode 11085, and netstat further tells me that inode 11085 a stream socket, and that it's connected.

Given that this process doesn't have any other threads, there must therefore be another process on the system that's connected to the other end of this socket. How do I find out what it is?

Update:

There's some illumination from the lsof author here. Essentially, it seems that Linux just doesn't provide this information.

+4  A: 

Does netstat -p help ?

From Manpage:

  -p,
  --program Show the PID and name of the program to which each socket belongs.
HaBaLeS
Unfortunately, this is busybox netstat, so it doesn't support -p. But node 11085 only turns up in the netstat output once anyway, so it seems that it only belongs to the process I already know about.
daf
@daf you should mention the use of busybox in your question (and maybe in a tag, too)
lothar
A: 

If for some reason you have no luck with the appropriate lsof and netstat options, you can also do the following:

find /proc -lname '*11085*' 2> /dev/null
efficientjelly
Cunning! As the system only has busybox find, -lname doesn't work, but we can emulate it:$ for i in `find /proc -type l`; do readlink "$i" | grep -q 11085 done/proc/1892/task/1892/fd/10/proc/1892/fd/10So, only the original process comes up.
daf
A: 
sigjuice
I'm already running it as root.
daf
+1  A: 

How about this: grep 11085 /proc/net/unix. Assuming there is a non-empty path present on the line with the inode you're interested in, grep for that path in /proc/net/unix to find the the inode for the other end of the connection, then use efficientjelly's method to map the other inode to a pid.

A key point here is the fact that the two connected sockets will each have a different inode number.

Lance Richardson
Cunning! I didn't know about /proc/net/unix. Sadly, there isn't a path associated with this socket.
daf
A: 

Simply Just print the socket object also u can use socket.getRemotePort()

Thanks Bapi

Deepak
A: 

The reply from lsof author is seven years old. Is it still not possible to get this information from Linux kernel?

FWIW lsof on RHEL5 shows some memory address in the DEVICE column. According to lsof(8) this might be "a kernel reference address that identifies the file (The kernel reference address may be used for FIFO's, for example.)". Is there any to resolve this address into details about the pipe?

oliver
A: 

Looks like if you're really desperate, you can get that information directly from Linux kernel memory by using some kernel debugger. With RHEL5's "crash" tool:

  • get uncompressed vmlinux image (eg. install kernel-debuginfo rpm, or extract vmlinux file from that rpm)
  • run "crash /path/to/vmlinux"
  • "net -s 12345" lists all sockets for PID 12345
  • find the interesting socket (has to be of family/type "UNIX:STREAM"), and note its "SOCK" value:
    • PID: 12345 TASK: e903d000 CPU: 0 COMMAND: "someapp"
    • FD SOCKET SOCK FAMILY:TYPE SOURCE-PORT DESTINATION-PORT
    • 36 cadd0240 c8a64040 UNIX:STREAM
  • you now have the address of the unix_sock struct for this socket
    • basically, unix_sock.peer.name is the name structure of the other end of the socket
    • print it with "p ((*((struct unix_sock*)( (struct unix_sock*)0xc8a64040)->peer)).addr).name"

Really sad that this information is not directly exported to userspace.

oliver