Thread: Problem with children processes and already opened files

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    66

    Problem with children processes and already opened files

    Hi.

    I have an application where I create a tcp socket and pull data from the connected clients. Before creating the socket, I close the parent process, and create a new child process (basically i create a daemon) using this method:

    Linux Daemon Writing HOWTO

    My problem is the following: Before I create the daemon, I open a log file where I store the error messages. If the file has opened without any problem I create the daemon. If I write something to the file before creating the daemon it works, but not after. I also tried to open the log files again, but nothing happens. I can`t write to the file anymore.

    Can you explain to me, what can be the problem, and how to solve it?

    P.S. The messages are stored in /var/log/application/errors.log but I run the application with root user. Something happens with the permissions after the child is created?


    Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    So you want us to guess at all the possible things you could have done different to the "Complete Sample" at the end of the HOWTO?

    Take the example, add some very simple "logging code" and see if it works. If it doesn't, then post it here.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    66
    I ran some tests and I figured out, that not the daemon is the problem. Here are the functions which handle the error messages:

    Code:
    static FILE *pmessages = NULL;
    
    int log_open_log()
    {
    if (pmessages == NULL)
    {
    pmessages = fopen(MESSAGES_PATH,"a+");
    }
    }
    
    void log_message(const char *format, ...)
    {
    	if (pmessages != NULL)
    	{
    		va_list args;
    		va_start (args, format);
    			vfprintf(pmessages, format, args);
    		va_end (args);
    	}
    }

    And here is the part which opens the socket, waits for the connecting clients, and sends them to a separate thread;


    Code:
    server_t *server 		= socket_server_create(DEFAULT_SERVER_PORT);
    
    	if (server == NULL)
    	{
    		return 0;
    	}
    
    /* opening log files */
        if (log_open_log() != 0)
        {
            fprintf(stderr, "Unable to open log files! please check if they exist in /var/log/...\n");
            exit(EXIT_FAILURE);
        }
    
    
    if (socket_server_listen(server, 5) > -1)
    	{
            
            log_message("Server created successfully on port: %d, waiting for clients...\n",DEFAULT_SERVER_PORT);
            
    
    		while (1)
    		{
    			client = socket_server_accept(server);
    if (client != NULL)
    {
    log_message("Client was connected to the server...\n");
    pthread_create(&thread_id, NULL, (void *)worker_thread, (void *){client,&thread_id});
    }
    }
    
    }
    I didn`t paste the whole source code, just the relevant part.

    The socket_server_create(), socket_server_listent() and socket_server_accept() are just wrapper functions for socket(), bind(), listen() and accept().

    If I execute the log_message() or log_error() functions before the infinite loop, the message is written successfuly into the log file. If I execute the call in the infinite loop (even in the separate thread) nothing happens.

    The interesting thing is, that if I replace the log_message() functions with simple printf() all the messages are shown on the screen. I even tried to leave both the log_message() and printf()-s with the same message. The printf() is working, the message appears on the screen, and one line below, the log_message() fails.

    Can it be because of the infinite loop? Somehow the printf() succeeds and the log_message() fails.

    I checked if the fopen can open the file, everything is ok. And if I put a return 0 after the first client was accepted, the messages again are written in the log file.

    Thank you.

    P.S. sorry for the lack of tabs in the source codes, but I copy-pasted from my text editor, and here I can`t add tabs
    Last edited by raczzoli; 10-20-2011 at 02:25 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Well your code looks messy when posted here, because you're mixing spaces and tabs for indentation.
    If you want your code to look the same everywhere, then set your IDE/editor to use "spaces for tabs".

    As for text not appearing, perhaps it is down to buffering.
    Until you close the file, or flush the stream, a fully buffered file could save quite a bit of info before writing the file.

    So perhaps this -> man page setvbuf section 3
    to set the log file to line buffered (same as stdout)

    Or perhaps in your log function, always call
    fflush(pmessages);
    after printing something.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    66
    You are awsome Salem. Thank you very much for your help. I have lost almost a day with this problem. Now it works with fflush().

    Have a good day.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    pthread_create(&thread_id, NULL, (void *)worker_thread, (void *){client,&thread_id})
    I believe the part is read is a compound literal that evaluates to a local array of void* pointers. That is bad. There is no guarantee that the stack memory containing this array will be valid by the time the thread has a chance to dereference it.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-04-2010, 03:52 PM
  2. Replies: 1
    Last Post: 03-08-2009, 09:07 AM
  3. too many files opened + segmentation fault
    By kfc in forum C Programming
    Replies: 14
    Last Post: 09-03-2006, 02:47 PM
  4. max number of simultanious opened files
    By johny145 in forum Windows Programming
    Replies: 2
    Last Post: 07-22-2005, 05:17 PM
  5. freading from multiple opened files
    By TeQno in forum C Programming
    Replies: 1
    Last Post: 05-23-2003, 07:03 AM