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