diff mbox

[RFC] af_unix: Add peer inode to /proc/net/unix

Message ID 20100301045206.GB496@home.unix.ba
State RFC, archived
Delegated to: David Miller
Headers show

Commit Message

Kenan Kalajdzic March 1, 2010, 4:52 a.m. UTC
Recently, I had a problem identifying local X-clients on a desktop system from 
within a userspace application.  So, I thought it may be a good idea to add the 
peer inode field to /proc/net/unix.

Instead of adding a new column to /proc/net/unix (which breaks important 
userspace programs, including 'netstat' and 'lsof'), we can make use of the 
fact that most client sockets (including these of X-clients) and anonymous 
sockets have an empty path field in /proc/net/unix, which can be used for 
specifying the peer inode.  In order to differentiate between a regular path 
and an entry which contains the inode number of the peer socket, I chose to 
simply add the 'peer=' prefix to the peer inode number.

Signed-off-by: Kenan Kalajdzic <kenan@unix.ba>
---
 net/unix/af_unix.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)

Comments

Changli Gao March 1, 2010, 7:05 a.m. UTC | #1
On Mon, Mar 1, 2010 at 12:52 PM, Kenan Kalajdzic <kenan@unix.ba> wrote:
>  net/unix/af_unix.c |    9 ++++++++-
>  1 files changed, 8 insertions(+), 1 deletions(-)
>
> diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
> index f255119..e778428 100644
> --- a/net/unix/af_unix.c
> +++ b/net/unix/af_unix.c
> @@ -2157,7 +2157,7 @@ static int unix_seq_show(struct seq_file *seq, void *v)
>                seq_puts(seq, "Num       RefCount Protocol Flags    Type St "
>                         "Inode Path\n");
>        else {
> -               struct sock *s = v;
> +               struct sock *s = v, *peer;
>                struct unix_sock *u = unix_sk(s);
>                unix_state_lock(s);
>
> @@ -2186,6 +2186,13 @@ static int unix_seq_show(struct seq_file *seq, void *v)
>                        }
>                        for ( ; i < len; i++)
>                                seq_putc(seq, u->addr->name->sun_path[i]);
> +               } else {
> +                       peer = unix_peer(s);
> +                       if (peer) {
> +                               unix_state_lock(peer);
> +                               seq_printf(seq, " peer=%lu", sock_i_ino(peer));
> +                               unix_state_unlock(peer);
> +                       }
>                }
>                unix_state_unlock(s);
>                seq_putc(seq, '\n');

If there are two read requests, the system maybe locked up. please use
unix_state_double_lock()/unix_state_double_unlock(), instead. And the
file isn't consistent with the old one. You'd better use a column
title "Peer" instead of "peer=".
diff mbox

Patch

diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index f255119..e778428 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -2157,7 +2157,7 @@  static int unix_seq_show(struct seq_file *seq, void *v)
 		seq_puts(seq, "Num       RefCount Protocol Flags    Type St "
 			 "Inode Path\n");
 	else {
-		struct sock *s = v;
+		struct sock *s = v, *peer;
 		struct unix_sock *u = unix_sk(s);
 		unix_state_lock(s);
 
@@ -2186,6 +2186,13 @@  static int unix_seq_show(struct seq_file *seq, void *v)
 			}
 			for ( ; i < len; i++)
 				seq_putc(seq, u->addr->name->sun_path[i]);
+		} else {
+			peer = unix_peer(s);
+			if (peer) {
+				unix_state_lock(peer);
+				seq_printf(seq, " peer=%lu", sock_i_ino(peer));
+				unix_state_unlock(peer);
+			}
 		}
 		unix_state_unlock(s);
 		seq_putc(seq, '\n');