Like Tree1Likes
  • 1 Post By anduril462

access(char *, int) causes seg fault?

This is a discussion on access(char *, int) causes seg fault? within the Linux Programming forums, part of the Platform Specific Boards category; I'm using the access function to try to check if a file exists, but it appears like it's causing a ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    119

    access(char *, int) causes seg fault?

    I'm using the access function to try to check if a file exists, but it appears like it's causing a segfault. I have a loop like

    Code:
    int fileExists = F_OK;
    do{
       fileExists = access(relPath, F_OK);
       sleep(1);
    while(fileExists != F_OK);
    GDB is showing a seg fault at the do{ line. Is there some trick to using this function? Do I need to link some library for it to work?

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    119
    Ok, I found the error. I guess either gdb isn't all that accurate with line numbers, or the while part of the loop gets executed at the same time as the do part. I also had a pointer in my while condition that I found wasn't being initialized.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,700
    How have you defined relPath, and what value does it contain? What is the purpose of the sleep? How do you exit if the file is not F_OK?

    Also the above code is missing a closing brace before the while statement.

    The following program works fine for me.
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    
    int main()
    {
       int fileExists = F_OK;
       char relPath[100];
       strcpy(relPath,"garbage.txt");
       if((fileExists = access(relPath, F_OK)) != F_OK)
       {
          printf("Can't access file.\n");
          return(1);
       }
    
       return(0);
    
    }
    Jim

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,457
    This condition is wrong:
    Code:
    while(fileExists != F_OK);
    From the man page you linked to (emphasis mine):
    Quote Originally Posted by man 2 access
    Return Value
    On success (all requested permissions granted), zero is returned. On error (at least one bit in mode asked for a permission that is denied, or some other error occurred), -1 is returned, and errno is set appropriately.
    Nowhere on that page that I could find, does it say that F_OK is guaranteed in any way to be 0. F_OK may be defined as 1 on some systems, so do not check against F_OK, check against 0.
    Code:
    while (access(relPath, F_OK) != 0);
    As for the GDB line number issue, there are two common scenarios that come to mind:
    1. Your executable was not built from the same version of the source file you currently have. Maybe you added/removed a blank line or comment, or made some other minor change then saved the file without recompiling.
    2. You have optimizations enabled when you compile. The optimizations sometimes affect GDBs ability to correctly display line numbers. Disable optimizations and see if that fixes it.
    Salem likes this.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    119
    Thanks for that catch anduril. I was using the return value wrong. As for the GDB thing, nothing changed. I built and then immediately ran in GDB. No optimizations are specified either. I only have the -g option on. Maybe I need to explicitly disable optimizations?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,457
    Quote Originally Posted by homer_3 View Post
    Thanks for that catch anduril. I was using the return value wrong. As for the GDB thing, nothing changed. I built and then immediately ran in GDB. No optimizations are specified either. I only have the -g option on. Maybe I need to explicitly disable optimizations?
    Hmm...can you provide the smallest, complete example that demonstrates this problem (all include files listed, your main function, etc)? Also, OS, gcc version and gdb version, plus the exact command you use to compile it with any output produced.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    119
    I don't think it's worth it to do all that as the issue has been resolved. I'm now trying to fix another issue.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,457
    I guess that wasn't clear, I meant that demonstrates the issue with GDB. Frankly it got me curious, to see if I can replicate on my system. But it is a pretty trivial issue to deal with, so if you don't care than much, then no worries.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Violation: Segmentation Fault help
    By Isaiah in forum C Programming
    Replies: 4
    Last Post: 02-09-2012, 06:53 AM
  2. Replies: 8
    Last Post: 12-08-2009, 01:47 AM
  3. Getting Access Violation (Segmentation Fault)
    By Brownie in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2008, 11:43 AM
  4. Access Violation / Seg Fault
    By Korhedron in forum C++ Programming
    Replies: 31
    Last Post: 09-06-2008, 11:47 AM
  5. access violation (segmentation fault)
    By MarlonDean in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2008, 05:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21