Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This pull request fix follow issues #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions udplogger.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Written by Tetsuo Handa <[email protected]>
*/
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
Expand Down Expand Up @@ -36,6 +38,15 @@ static int wait_timeout = 10;
/* Try to release unused memory? */
static _Bool try_drop_memory_usage = 0;

static void flush_all_and_abort(char *reason);


void sig_handler(int signo)
{
if (signo == SIGINT)
flush_all_and_abort("terminated by user");
}

/**
* switch_logfile - Close yesterday's log file and open today's log file.
*
Expand All @@ -54,8 +65,15 @@ static void switch_logfile(struct client* client, struct tm *tm)
snprintf(filename, sizeof(filename) - 1, "%s/%04u-%02u-%02u.log",
client->addr_str, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
client->log_fp = fopen(filename, "a");
if (!fp)
if (!fp) {
if (!client->log_fp) {
char errmsg[255];
snprintf(errmsg, sizeof(errmsg) - 1, "unnabble open file '%s', error because %s",
filename, strerror(errno));
flush_all_and_abort(errmsg);
}
return;
}
/* If open() failed, continue using old one. */
if (client->log_fp)
fclose(fp);
Expand All @@ -79,7 +97,7 @@ static void write_logfile(struct client *ptr, const _Bool forced)
char *buffer = ptr->buffer;
int avail = ptr->avail;
const time_t now_time = ptr->stamp;
if (last_time != now_time) {
if (last_time != now_time || ptr->log_fp == 0) {
struct tm *tm = localtime(&now_time);
if (!tm)
tm = &ptr->last_tm;
Expand Down Expand Up @@ -161,17 +179,21 @@ static void drop_memory_usage(void)
/**
* flush_all_and_abort - Clean up upon out of memory.
*
* @reason: Reason why program aborted.
*
* This function does not return.
*/
static void flush_all_and_abort(void)
static void flush_all_and_abort(char *reason)
{
int i;
for (i = 0; i < num_clients; i++)
if (clients[i].avail) {
write_logfile(&clients[i], 1);
free(clients[i].buffer);
fprintf(clients[i].log_fp, "[aborted due to memory allocation failure]\n");
fflush(clients[i].log_fp);
if (clients[i].log_fp) {
if (clients[i].avail) {
write_logfile(&clients[i], 1);
free(clients[i].buffer);
}
fprintf(clients[i].log_fp, "[aborted due to %s]\n", reason);
fflush(clients[i].log_fp);
}
exit(1);
}
Expand Down Expand Up @@ -254,7 +276,7 @@ static void do_main(const int fd)
/* Append data to the line. */
tmp = realloc(ptr->buffer, round_up(ptr->avail + len));
if (!tmp)
flush_all_and_abort();
flush_all_and_abort("memory allocation failure");
memmove(tmp + ptr->avail, buf, len);
ptr->avail += len;
ptr->buffer = tmp;
Expand Down Expand Up @@ -398,6 +420,8 @@ static int do_init(int argc, char *argv[])

int main(int argc, char *argv[])
{
if (signal(SIGINT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGINT\n");
const int fd = do_init(argc, argv);
do_main(fd);
return 0;
Expand Down