1

I'm trying to find the PID from struct sock, so I used the following code:

struct task_struct *get_task_from_sock(struct sock *sk)
{
    struct task_struct *task = NULL;
    if (sk == NULL)
    {
        pr_info("sk is NULL");
        return 0;
    }

    if (sk->sk_peer_pid == NULL)
    {
        pr_info("sk_peer_pid is NULL");
        return 0;
    }

    return get_pid_task(sk->sk_peer_pid, PIDTYPE_PID);
}

But sk_peer_pid is always NULL. So I referred to the source code:

# grep sk_peer_pid -r
include/net/sock.h:  *  @sk_peer_pid: &struct pid for this socket's peer
include/net/sock.h: struct pid      *sk_peer_pid;
net/core/sock.c:        cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred);
net/core/sock.c:    put_pid(sk->sk_peer_pid);
net/core/sock.c:    sk->sk_peer_pid     =   NULL;
net/unix/af_unix.c: put_pid(sk->sk_peer_pid);
net/unix/af_unix.c: sk->sk_peer_pid  = get_pid(task_tgid(current));
net/unix/af_unix.c: put_pid(sk->sk_peer_pid);
net/unix/af_unix.c: sk->sk_peer_pid  = get_pid(peersk->sk_peer_pid);

And its only available for unix local socket?

How can I find the owner of a struct sock? I've read this post already, it never mentions how to get PID from struct sock.

Does anyone know?

daisy
  • 22,498
  • 29
  • 129
  • 265

0 Answers0