Obtaining a list of open sockets from "struct task_struct"
Nuno Martins
nuno.m.g.martins at gmail.com
Fri Nov 11 11:42:55 EST 2011
On Fri, Nov 11, 2011 at 4:03 PM, Vimal <j.vimal at gmail.com> wrote:
> Hi all,
Hey,
>
> I am trying to understand more about kernel data structures and I
> would like to know how to obtain a list of TCP/UDP sockets, starting
> from a "struct task_struct" variable "task".
>
> So far, I have understood the following: Please correct me if I am wrong! :)
>
> - An open file descriptor is represented by a "struct file *" in the
> tasks's file table: task->files.fdt.fd
> - The file tables are organised as a linked list
> - The file table contains a structure fd_array, that is an array of
> "struct file *", each representing an open file
>
> But, almost all operations in the TCP code start from a "struct sock".
> Are the "struct file" and "struct sock" somehow connected?
If you are sure that that file descriptor is a socket then you can
cast to struct socket, the field "void * private_data" in struct file
[1] .
But not all file descriptors are sockets so you have a way to be sure
that you are dealing with a socket, you have a macro
#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
that macro is in [2] .
[1] http://lxr.linux.no/#linux+v3.1/include/linux/fs.h#L983
[2] http://lxr.linux.no/#liinux+v3.1/include/linux/stat.h#L30
And the struct socket has a field that is of type struct sock, so its direct.
The trick is that the private_data field is the socket structure, that
knows all about the network part.
>
> Thanks,
> --
> Vimal
>
Glad to help, i have searched all that because i had a project that i
needed to know which file descriptors were sockets, so i had to search
this information, i hope it's now a little be clear to you.
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
--
Nuno Martins
More information about the Kernelnewbies
mailing list