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:
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.
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 가 된다.
아마도 일정 시간이 지나면 이런 메모리블럭이 리플레쉬 되는 루틴이 있겠지만 나는 알지 못한다.
다음은 파일디스크립터와 관련된 내 멋대로의 글이다.
오류에 대해서는 책임지지 않는다.
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.
|All times are GMT -5. The time now is 05:25 PM.|