Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Linux - Kernel This forum is for all discussion relating to the Linux kernel.


  Search this Thread
Old 06-03-2010, 10:34 PM   #1
LQ Newbie
Registered: Jun 2010
Posts: 2

Rep: Reputation: 0
Traversing/Counting contents of /proc/[pid]/fd directories


I posted this on another forum, and I figure I would try here as well.

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:

struct task_struct *task;
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.

Updated later with the follow-up 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.
Old 06-08-2010, 09:45 PM   #2
LQ Newbie
Registered: Jun 2008
Location: DaeJeon,Korea
Posts: 5

Rep: Reputation: 0
Because of my poor english,this may out of some misunderstanding....

I am not a programmer either linux user,further have no computer...this is my smartphone;;;

Per-Process file table ->fdtable
Open file table -> files_struct

So,The 'fdtable' struct may have key possiblity to solve this problem

This is my first posting to LQ which requires at least one posting to access full user account...
This policy force to use english all over the world...may not a quite wide open concepts

below detail description and blog link is just for Korean Alphabet (called Han Gl)


위 커널소스에서 fdtable 구조체가 하나의 프로세서와 링크된 파일 디스크립터 테이블 이다.아마도 이 질문자는 /proc 디렉토리에 집착하고 있는데 이는 디스크의 실제 파일이 아니라 시스템 메모리 트리구조에 그대로 대응되는 것이므로 이 디렉토리의 각종 값을 만지면 메모리를 임의로 만지는 것이 된다.

물론 on/off 플래그나 16비트 상수를 cat >> 명령 등으로 쓰는 정도는 자주 이용되지만 fd 를 만지면 실제 파일 컨텐츠를 포함한 메모리블럭은 링크가 깨진 object 가 된다.
아마도 일정 시간이 지나면 이런 메모리블럭이 리플레쉬 되는 루틴이 있겠지만 나는 알지 못한다.

다음은 파일디스크립터와 관련된 내 멋대로의 글이다.
오류에 대해서는 책임지지 않는다.

Last edited by ggeagle; 06-10-2010 at 08:20 AM.
Old 06-12-2010, 09:41 AM   #3
LQ Newbie
Registered: Jun 2010
Posts: 2

Original Poster
Rep: Reputation: 0

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.

struct task_struct* ptask;
struct fdtable* pfdt;

    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
Good luck to anyone with related problems, and thank you for your comments and suggestions.


descriptor, descriptors, file, filesystem, kernel, module, proc

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
pmap or /proc/<pid>smap or /proc/<pid>/status iQoder Linux - Newbie 1 07-16-2009 07:32 PM
/proc/pid/maps Moraxus Linux - General 0 02-11-2009 09:49 AM
/proc/pid/io seems not to work kornelix Linux - Server 5 06-17-2007 03:39 AM
hidden directories under proc... what are they for? : (/proc/.23142) syssyphus Linux - General 1 04-10-2006 04:23 PM
Recursively traversing sub-directories ark86 Linux - Newbie 2 01-29-2006 02:15 PM > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 05:14 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration