weird segfault in fgets in process

This is a discussion on weird segfault in fgets in process within the C Programming forums, part of the General Programming Boards category; I have question about this piece of code fragment. When I run a c-programb on its own, it works fine. ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    23

    weird segfault in fgets in process

    I have question about this piece of code fragment. When I run a c-programb on its own, it works fine.

    But when I fork this program from another program, I get this program to do segfault at fgets.
    Any suggestions from what's wrong with fgets? I just want read content fo the file upto what the buffer can hold. These three lines never get executed:

    syslog(LOG_INFO,"Reading 100 Bytes of config file\n");
    fclose(configfile);
    syslog(LOG_INFO,"Closing of config file\n");

    server.conf:
    Code:
    num_of_files=3;
    progb.c:
    Code:
    	FILE *configfile;
    	char conf[100];
    
    	syslog(LOG_INFO,"Reading config file\n");
    	configfile= fopen("./server.conf", "r");
    	syslog(LOG_INFO,"After Opening config file\n");
    	fgets(conf,100, configfile);
    	syslog(LOG_INFO,"Reading 100 Bytes of config file\n");
    	fclose(configfile);
    	syslog(LOG_INFO,"Closing of config file\n");
    syslog:
    Dec 28 15:48:53 localhost [19674]: Version 1.0
    Dec 28 15:48:53 localhost [19674]: Reading config file
    Dec 28 15:48:53 localhost [19674]: After Opening config file
    According to gdb:
    (gdb) out conf
    "num_of_files=3;\n", '\0' <repeats 80 times>(gdb)

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    You never check if fopen() succeeded.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    23
    I did this and not reading the ./server.conf file now. Permission is looking good on the file:
    Code:
    if((configfile= fopen("./server.conf", "r"))==NULL);{
    		syslog(LOG_INFO,"%s unable to read config file\n", configfile);
    		exit(1);
    	}
    Dec 28 17:12:51 localhost [20794]: Version 1.00
    Dec 28 17:12:51 localhost [20794]: Reading config file
    Dec 28 17:12:51 localhost [20794]: (null) unable to read config file
    My server conf file is as follows:
    -rw-rw-r-- 1 joe joe 18 Dec 28 15:07 server.conf

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Putting a semicolon after your if statement is a rather stupid thing to do in this case.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    23
    Now the program runs independently on its own. If I just run progb, it reads the config file and completes as expected. That semi-colon caused bitter headache. Thank you for it.

    But I am back to square one: when I fork a child process from proga to run progb, progb fails at fopen.
    Dec 28 21:39:35 localhost progb[4382]: Reading config file
    Dec 28 21:39:35 localhost progb[4382]: (null) unable to read config file
    Any idea why progB fail at fopen?

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by 911help View Post
    Now the program runs independently on its own. If I just run progb, it reads the config file and completes as expected. That semi-colon caused bitter headache. Thank you for it.

    But I am back to square one: when I fork a child process from proga to run progb, progb fails at fopen.

    Any idea why progB fail at fopen?
    Who knows. Maybe it is changing directories somehow.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    23
    proga is located at the root of folder:
    myprogs

    progb is located in folder "server", which is inside "myprogs" folder.

    myprogs:
    -proga.o
    -setup.conf
    server
    server:
    -progb.o
    -server.conf
    In proga, I read setup file using
    fopen("./setup.conf", "r")
    . And then have proga call progb by
    execl("./server/progb", "./server/progb", (char*)0)
    . Inside progb, I use
    fopen("./server.conf","r")
    Doing this type of stuff causes problem? If it's a problem calling programs on different folder locations, how can I go about getting it to work?
    fedora 6, gcc 4.1.2

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Try calling getcwd() to get the current directory in the forked child (before the exec call), and print it. That'll prove whether that's the problem or not.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    23
    Quote Originally Posted by brewbuck View Post
    Try calling getcwd() to get the current directory in the forked child (before the exec call), and print it. That'll prove whether that's the problem or not.
    I did print the getcwd inside the forked child before execl command, it points to /home/joe/myprogs. Which correct by the way.

    I think forking to call another program has to do with file descriptor sharing and closing, especially, file descriptors 0,1,2 (standard input, output and error). Would calling progB via forking from progA cause both program to share file descriptors 0,1,2? I maybe wrong(too much time wasted on trying to get this work, maybe time to throw in the towel.).
    fedora 6, gcc 4.1.2

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by 911help View Post
    Would calling progB via forking from progA cause both program to share file descriptors 0,1,2? I maybe wrong(too much time wasted on trying to get this work, maybe time to throw in the towel.).
    Yes, they would both read and write to the same standard file descriptors. Normally one process would close all of these after forking to avoid any possibility of two processes using the same file objects.

    Don't give up, call perror() to figure out why fopen() isn't working:

    Code:
    fp = fopen(filename, "r");
    if(!fp) perror("Couldn't open file");

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    23
    Quote Originally Posted by brewbuck View Post
    Don't give up, call perror() to figure out why fopen() isn't working:

    Code:
    fp = fopen(filename, "r");
    if(!fp) perror("Couldn't open file");
    Thanks, encouragement helped. perror helped solve the problem. I was using relative path to read the conf file in progB and I changed to absolute path to conf file in progB. It's working now.

    Thank you all for the help.
    fedora 6, gcc 4.1.2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  2. process ring
    By gregulator in forum C++ Programming
    Replies: 0
    Last Post: 02-28-2005, 07:21 PM
  3. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-27-2005, 11:59 PM
  4. Almost finished with Round Robin algorithm
    By Shinobi-wan in forum C++ Programming
    Replies: 2
    Last Post: 12-19-2004, 02:00 PM
  5. Linux: Send keyboard input to background process
    By xErath in forum Tech Board
    Replies: 2
    Last Post: 12-09-2004, 06:02 PM

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