Implementing the lsof command in Minix 3
I was looking for the lsof command inside minix 3, but I couldn' find.
So, I am trying to implement my own lsof command.
First of all, I searched where I could find information about what
files are being used by which processes. I found that information
inside the process table managed by FS server, inside /usr/src/server/
fs/fproc.h and /usr/src/server/fs/file.h
So for each process in the fproc table, there is an array of pointers
to a entry in the filp table (file.h). I can index that array by
the file descriptor, and fund the file's i-node through filp table.
My problem now is how to get the name of a file, if I have its file
descriptor? In other words, how I can find the file name through its
entry in the filp table?
I know that this is related to directories job, that translates a file
name into its i-node, but how can I make the inverse?
Re: Implementing the lsof command in Minix 3
Hugo Barauna <hugo.barauna@gmail.com> wrote:[color=blue]
> I know that this is related to directories job, that translates a file
> name into its i-node, but how can I make the inverse?[/color]
You can't, really, at least not in a completely correct way. The reason is
that one file can have multiple file names (i.e. multiple hard links to
one file). So, there is no one-to-one mapping of i-node numbers to file
names. What you could do is scan the directory tree until you hit the
right i-node number and take that file name. However, this will take a
lot of time on larger file systems.
If you want lsof functionality, you'd probably best alter the FS a bit so
that it stores file names together with i-node numbers in the filp table.
Then, to get the list of open files, you can simple walk through the filp
table and get all file names of open files. This will cause the FS to use
a bit more memory (probably something like 256 bytes per entry in the filp
table) but this should not be a huge problem on modern machines.
Good luck!
Jens
--
Jens de Smit
Student Computer Science | Vrije Universiteit Amsterdam
[email]jfdsmit@few.vu.nl[/email] | [url]http://www.few.vu.nl/~jfdsmit[/url]
"[In the end, people] get furious at IT that the goddamn magic isn't working"
-- Stewart Dean
Re: Implementing the lsof command in Minix 3
Hi Unix,
I am a graduate student & studying Minix these days.
I was interested in studying implementation of Minix lsof command.
Can you please provide me source code of your lsof implentation for study?
OR else can you please give me some pointers for the same?
Re: Implementing the lsof command in Minix 3
I know this is an old thread, but the original poster please help with a question I have. what process do you use to fill the fproc struct for a given process id?