Find the answer to your Linux question:
Results 1 to 5 of 5
As far as I know there is a task_struct stcuture which relates all the data related to the process. In case of socket there is no socket pointer in task_struct ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2011
    Posts
    30

    how to print socket data structures


    As far as I know there is a task_struct stcuture which relates all the data related to the process.
    In case of socket there is no socket pointer in task_struct . then how to relate the socket data structure of a process through task_struct ????
    Is there another method to do so ??
    I want to know the deifferences between data structure of two sockets (AF_UNIX) for this I want to print this data structure . So, I cant print it without the use of task_struct and also I tried including the pointer of socket in task_struct there are many pointers which I need to include as only socket pointer doesnot contain socket info there are other stucture like sock,sock_common .Also including these pointer and their related .h files gives lots of errors during compiling.
    Also printk can print all the data structures but there are many files which are related to socket. So we can miss out many things and also it will take lots of time and is inefficient.
    So my idea was to create a kernel module with parameter as pid which will print all the socket data through task_struct. If not possible through task_struct then how to do so??????
    Hence ,I want to know How the kernel relates the socket data structures and the corresponding process.
    Plzzzzz help me out!!!!!

  2. #2
    Just Joined!
    Join Date
    Feb 2012
    Location
    Hyderabad, India
    Posts
    11
    Can you try this module.

    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/fs.h>
    #include <linux/pid.h>
    #include <linux/net.h>
    #include <linux/sched.h>
    #include <linux/fdtable.h>

    unsigned int pidnr = 1;

    module_param(pidnr, uint, 0644);

    int mod_init(void)
    {
    struct file *file;
    struct socket *sock;
    int fd = 0;
    struct inode *inode;
    struct task_struct *task;
    struct pid *mypid;
    struct files_struct *files;

    mypid = find_vpid(pidnr);
    if (!mypid) {
    return -ESRCH;
    }

    task = pid_task(mypid, PIDTYPE_PID);
    if (!task) {
    printk("\nNo Such Process\n");
    return -ESRCH;
    }

    printk("\nProcess %s\n", task->comm);

    files = task->files;

    rcu_read_lock();
    file = fcheck_files(files, fd);
    rcu_read_unlock();
    while(file) {
    inode = file->f_path.dentry->d_inode;
    if (S_ISSOCK(inode->i_mode)) {
    sock = file->private_data;
    printk("\ntype = %d\n", sock->type);
    }
    fd++;
    rcu_read_lock();
    file = fcheck_files(files, fd);
    rcu_read_unlock();
    }

    return 0;
    }
    void mod_exit(void)
    {

    }

    module_init(mod_init);
    module_exit(mod_exit);

    MODULE_LICENSE("GPL");

  3. #3
    Just Joined!
    Join Date
    Dec 2011
    Posts
    30
    Quote Originally Posted by vedasolutions View Post
    Can you try this module.

    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/fs.h>
    #include <linux/pid.h>
    #include <linux/net.h>
    #include <linux/sched.h>
    #include <linux/fdtable.h>

    unsigned int pidnr = 1;

    module_param(pidnr, uint, 0644);

    int mod_init(void)
    {
    struct file *file;
    struct socket *sock;
    int fd = 0;
    struct inode *inode;
    struct task_struct *task;
    struct pid *mypid;
    struct files_struct *files;

    mypid = find_vpid(pidnr);
    if (!mypid) {
    return -ESRCH;
    }

    task = pid_task(mypid, PIDTYPE_PID);
    if (!task) {
    printk("\nNo Such Process\n");
    return -ESRCH;
    }

    printk("\nProcess %s\n", task->comm);

    files = task->files;

    rcu_read_lock();
    file = fcheck_files(files, fd);
    rcu_read_unlock();
    while(file) {
    inode = file->f_path.dentry->d_inode;
    if (S_ISSOCK(inode->i_mode)) {
    sock = file->private_data;
    printk("\ntype = %d\n", sock->type);
    }
    fd++;
    rcu_read_lock();
    file = fcheck_files(files, fd);
    rcu_read_unlock();
    }

    return 0;
    }
    void mod_exit(void)
    {

    }

    module_init(mod_init);
    module_exit(mod_exit);

    MODULE_LICENSE("GPL");
    THANKS !! for your reply.. It really helped!!.
    Last edited by Innovator; 02-09-2012 at 12:10 PM.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Feb 2012
    Location
    Hyderabad, India
    Posts
    11
    Quote Originally Posted by Innovator View Post
    THANKS !! for your reply.. It really helped!!.
    To access the struct sock you need to include <net/sock.h> and you can access it like this in the
    above module

    #include <net/sock.h>

    struct sock *sk;

    sk = sock->sk;

  6. #5
    Just Joined!
    Join Date
    Dec 2011
    Posts
    30
    Sir, how to access the msghdr pointer for socket??
    We could not find any structure which contains the pointer for msghdr
    eg:socket structure and sock structure etc.
    Sock is in socket so we can access it but then how to access msghdr
    Same is the problem with sk_buff;

    Actually, I want to print the difference in the variables of two client programs(using AF_UNIX sockets) which are connected to the same server.
    For example for two open file can be distinguished by their inode , etc
    Similar what are the variables related to the sockets that differ between the two clients??
    Is it that only the socket discriptor and the run time buffer for the two clients changes?? or there are other variables also that differ??
    Because I have tried printing the socket structure variables and sock structure variables but there is no major change between the variables for the two clients..
    I am very confused
    Plzzzz help... :'(

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •