Find the answer to your Linux question:
Results 1 to 5 of 5
I have particular segment of code which do a checking of the file presence by stat() call and if it succeed, it call fopen(). But in strace I am seeing ...
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    2

    Exclamation strange behaviour of stat

    I have particular segment of code which do a checking of the file presence by stat() call and if it succeed, it call fopen(). But in strace I am seeing that the stat() return success but the fopen() (i.e - open() system) call is missing from it. I have given the sample segment of code and segment of strace output. I can not post the code and strace out put fully. Please help, why its showing this behavior. I am pretty sure that, no other section of code will disturb this below segment. Is there any possible way, that a stat() can be failure, like- OS file system crash, etc. ?

    Code:
     
    struct stat test;
    int i =0;
    char	file_name[80];
    
    for (i=0;i<32;i++)
    {
    sprintf(file_name, "/sys/class/scsi_host/host%d/proc_name", i);
    if (0 == stat(file_name, &test))
    {
    FILE *fd;
    fd = fopen(file_name, "r");
    
    ***** /* Do other stuff here just a read from the file*/
    }
    }
    Strace output -
    Code:
    stat64("/sys/class/scsi_host/host0/proc_name", {st_mode=S_IFREG|0444, st_size=4096, ...}) = 0
    
    stat64("/sys/class/scsi_host/host1/proc_name", {st_mode=S_IFREG|0444, st_size=4096, ...}) = 0
    
    stat64("/sys/class/scsi_host/host2/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host3/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host4/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host5/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host6/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host7/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host8/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host9/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host10/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host11/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host12/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host13/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host14/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host15/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host16/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host17/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host18/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host19/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host20/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host21/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host22/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host23/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host24/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host25/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host26/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host27/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host28/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host29/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host30/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)
    
    stat64("/sys/class/scsi_host/host31/proc_name", 0xffffae30) = -1 ENOENT (No such file or directory)

  2. #2
    Just Joined!
    Join Date
    Jul 2010
    Posts
    53
    ls -al /sys/class/scsi_host/

    maybe you really only have host0 and host1 ?

  3. #3
    Just Joined!
    Join Date
    Feb 2009
    Location
    Southport, England
    Posts
    31
    Quote Originally Posted by eaglebird View Post
    I have particular segment of code which do a checking of the file presence by stat() call and if it succeed, it call fopen(). But in strace I am seeing that the stat() ...
    Sorry if this is naive, but could it be because you have a method named the same as a struct's name ("stat")?

  4. #4
    Just Joined!
    Join Date
    Jul 2010
    Posts
    2
    Sorry, I forgot to mentioned that, this particular application runs on a sample server and this application (i.e this segment of code) works fine for almost 1year (System was up for almost one year and the application continue to run in certain interval, no issue to system so far) and this section of code gave proper strace with stat64() call and the open() call.

    Thanks in Advance. Thanks to lemons and chaosless for posting reply.

  5. #5
    Just Joined!
    Join Date
    Jul 2010
    Posts
    53
    can you post the output from:

    ls -al /sys/class/scsi_host/

    on my system - compiled your example (added headers as per man page) - and it works compiled either -m32 or -m64

    what happens when the same user uses the stat utility from the shell - e.g.:

    stat /sys/class/scsi_host/host1/proc_name
    stat /sys/class/scsi_host/host2/proc_name

    (if you really have the host2 symlink...)

Posting Permissions

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