Link to home
Start Free TrialLog in
Avatar of b123coder
b123coder

asked on

getting inode from filename and its associated fds

hello,
           I want to know is there any command on LINUX that will do following or I have to write a C program?
          if i know filename how to get its inode no. and if that file is open what is its no. of FD's (in case file is opened in many programs)

SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of b123coder
b123coder

ASKER

hello sunnycoder,
               So there's a command stat that gives all info about file.   can you tell me please how to use that in my C program? Actually what i want  is that my program is receving filename and i want to print its inode no. in my C program also want to print pid's of processes that are using this filename?
use popen to read from the command and parse the results

man popen
man pclose
man fgets
In Linux, you have two stats:  a command-line program and a C stdlib call.  Since this is the C programming category, I'm assuming you want the C call.

Check out the stat(2) man page with the command "man -s2 stat".  (don't just use "man stat" because that will give you the command-line program).

       #include <sys/types.h>
       #include <sys/stat.h>
       #include <unistd.h>

       int stat(const char *file_name, struct stat *buf);
       int fstat(int filedes, struct stat *buf);
       int lstat(const char *file_name, struct stat *buf);

It's pretty easy.  Pass the filename to stat in the file_name parameter, along with a struct stat.  The OS will stick the information in your struct stat.  Note the 'st_ino' field.




              struct stat {
                  dev_t         st_dev;      /* device */
                  ino_t         st_ino;      /* inode */
                  mode_t        st_mode;     /* protection */
                  nlink_t       st_nlink;    /* number of hard links */
                  uid_t         st_uid;      /* user ID of owner */
                  gid_t         st_gid;      /* group ID of owner */
                  dev_t         st_rdev;     /* device type (if inode device) */
                  off_t         st_size;     /* total size, in bytes */
                  blksize_t     st_blksize;  /* blocksize for filesystem I/O */
                  blkcnt_t      st_blocks;   /* number of blocks allocated */
                  time_t        st_atime;    /* time of last access */
                  time_t        st_mtime;    /* time of last modification */
                  time_t        st_ctime;    /* time of last status change */
              };



Hello ,
           I am getting segmentation fault in following code.
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

main()
{
long int err=0;
struct stat *buf;
err=stat("/root/install.log",buf);
if(err!=0)
exit(1);
printf("inode no. is =%ld\n",buf->st_ino);
}

Also how to modify this to find File descriptors and PID of processes that uses the filename given to function stat
you did not allocate any memory for struct stat

struct stat buf;
err=stat("/root/install.log",&buf);
Hello sunnycoder,
          Sorry for asking silly thing of not allocating struct stat well. But please can it be possible to modify my program to find File descriptors and PID of processes that uses the filename given to function stat
file descriptors are local to a process .. the information regarding file descriptors, as to which descriptor of a process refers to which file is available either to the process or needs to be dug in from the kernel ...

If I remember correctly, you can inspect the file descriptor table in the proc filesystem somewhere under /proc/<PID> ... but that would mean examining the table for each and every process (and you will need root permission for that) and shortlisting the PIDs which have that particular file open ... Also this method is likely to be less portable as proc filesystem is not standard (but fairly common in the Linux world)
Just a couple small changes.  You need to provide the struct stat for stat() to write into.

main()
{
long int err=0;
struct stat buf;
err=stat("/root/install.log",&buf);
if(err!=0)
exit(1);
printf("inode no. is =%ld\n",buf.st_ino);
}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay, now I feel kinda dumb... looking back on your question:

> I want to know is there any command on LINUX that will do following or I have to write a C program?

Then the simple answer is "yes, you can use lsof."  The columns /should/ be:

* Process name.
* Process ID.
* Real user.
* FD.
* File type (file, char special, block special, fifo...).
* Major/minor numbers for special nodes.
* File size for real files.
* Inode.
* The filename.

Now that was a bit easier than I made it out to be, wasn't it?  =)