Find the answer to your Linux question:
Results 1 to 6 of 6
Hello, I have a rather straightforward challenge and I need a little help. Let me preface that I am not a very experienced kernel module developer, but I have read ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2010
    Posts
    6

    Exclamation Traversing/Counting contents of /proc/[pid]/fd directories


    Hello,

    I have a rather straightforward challenge and I need a little help. Let me preface that I am not a very experienced kernel module developer, but I have read through some of the basic tutorials and examples. I also have a fair amount of C/C++ experience, so I am not a complete novice in those regards.

    The challenge is to count the contents of each /proc/[pid]/fd directory from a kernel module. Basically, counting the number of open file descriptors for each process. Now, I can get each pid using the following segment:

    Code:
    struct task_struct *task;
    for_each_process(task)
    {
        task->pid
    }
    From that, I can construct the absolute pathname /proc/[pid]/fd for each process, and use the path_loookup() method to begin further analysis of the directory. After that, I am struggling on how to traverse/count the contents of the /fd directories. I can get the struct dentry object for the directory using struct dentry *lookup_create(), but I don't have a good enough understanding of using the struct dentry or struct inode types to proceed after that.

    Any suggestions on how to properly proceed or recommendations on how I can accomplish this task. I know there are easy ways to do this from user space; however, that option is not available. The challenge is to do it from kernel space.

    I am developing this for kernel distro 2.6.31-14-generic on Ubuntu 9.10 Karmic Koala.

    Thank you for any help.

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,763
    I can get the struct dentry object for the directory using struct dentry *lookup_create(), but I don't have a good enough understanding of using the struct dentry or struct inode types to proceed after that.
    I found these link - might give some hint.
    http://www.fieldses.org/~bfields/kernel/vfs.txt
    A small trail through the Linux kernel: open
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  3. #3
    Just Joined!
    Join Date
    Jun 2010
    Posts
    6
    I have been doing some more looking around and have yet to figure out anything definitive.

    However, I did discover the functions proc_root_readdir() and others located in linux/fs/proc/root.c. Is this headed in the right direction or am I heading down the wrong "Rabbit Hole"?

    Another thought based on other examples and calls I have seen: should I try to get the "struct file" object for a /proc/[pid]/fd directory and then try to count the file offsets from the directory (ignoring the offsets 1 and 2 for "." and ".." respectively)?

    Once again, I apologize if I am way off base. If so, please provide some feedback to put me down the proper path.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,763

    Exclamation

    Quote Originally Posted by guppy73 View Post
    I have been doing some more looking around and have yet to figure out anything definitive.

    However, I did discover the functions proc_root_readdir() and others located in linux/fs/proc/root.c. Is this headed in the right direction or am I heading down the wrong "Rabbit Hole"?

    Another thought based on other examples and calls I have seen: should I try to get the "struct file" object for a /proc/[pid]/fd directory and then try to count the file offsets from the directory (ignoring the offsets 1 and 2 for "." and ".." respectively)?

    Once again, I apologize if I am way off base. If so, please provide some feedback to put me down the proper path.
    Sorry, I have not done any kernel space programming with directory traversal - so my knowledge is limited here. I would suggest you to drop a mail to ext3 mailing list,where you can receive suggestion from real file system experts !
    https://www.redhat.com/mailman/listinfo/ext3-users
    HTH
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  6. #5
    Just Joined!
    Join Date
    Jun 2010
    Posts
    6
    I had another thought after doing some more research.

    Does anyone know if the file descriptor table would contain the information I am looking for? It is my understanding the each process has a file descriptor table, and it will contain a listing of all file descriptors used by the process. Am I correct?

    Thank you

  7. #6
    Just Joined!
    Join Date
    Jun 2010
    Posts
    6
    Ok,

    It has taken me awhile to get around to posting this, but I did find the solution to my problem about a week ago. It looks like I was heading down the right path by looking at the file descriptor table. Below is a segment of code that will look through the file descriptor table for each process (PID) and identify each open file descriptor.

    Code:
    struct task_struct* ptask;
    struct fdtable* pfdt;
    
    for_each_process(ptask) 
    {
        rcu_read_lock();	
        pfdt = files_fdtable(ptask->files);
    
        for (int n = 0; n < pfdt->max_fds; n++)
        {
            if (NULL != pfdt->fd[n])
    	{
    	    // pfdt->fd[n] is an open file descriptor if it is not NULL,
                // and n (the index) corresponds to the file descriptor
                // number you will see in /proc/[pid]/fd
    	}            
        }
        rcu_read_unlock();
    }
    Good luck to anyone with related problems, and thank you for your comments and suggestions.

Posting Permissions

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