Skip to content
This repository has been archived by the owner on Feb 12, 2019. It is now read-only.

Commit

Permalink
router crashed several times on the day:
Browse files Browse the repository at this point in the history
Program received signal SIGSEGV, Segmentation fault.
0x0000000000406950 in message_log (nad=0x654270, r=0x624010,
    msg_from=0x64db40 "<email address hidden>/Chelny-14",
    msg_to=0x6581c0 "<email address hidden>/SARATOV-4") at router.c:1158
1158 router.c: access denied.
        in router.c

fix this problem and slightly modified logging format:
1. ISO8601 timestamps
2. space as field delimiter
3. character 0x01 for replace line endings in messages (simple code)

https://bugs.launchpad.net/jabberd2/+bug/1033826

fix #29 ([router.c:1206]: (error) Resource leak: message_file)
  • Loading branch information
dab18 committed Mar 25, 2014
1 parent f5c53f3 commit 7b463ef
Showing 1 changed file with 36 additions and 46 deletions.
82 changes: 36 additions & 46 deletions router/router.c
Expand Up @@ -1129,73 +1129,48 @@ int router_mio_callback(mio_t m, mio_action_t a, mio_fd_t fd, void *data, void *
}


int message_log(nad_t nad, router_t r, const char *msg_from, const char *msg_to)
{
int message_log(nad_t nad, router_t r, const char *msg_from, const char *msg_to) {
time_t t;
char *time_pos;
int time_sz;
struct tm *time_pos;
char timestamp[25];
struct stat filestat;
FILE *message_file;
short int new_msg_file = 0;
int i;
int nad_body_len = 0;
const char *nad_body_start = 0;
int body_count;
const char *nad_body = NULL;
char body[MAX_MESSAGE*2];
char *nad_body = NULL;
int elem;

assert((int) (nad != NULL));

/* timestamp */
t = time(NULL);
time_pos = ctime(&t);
time_sz = strlen(time_pos);
/* chop off the \n */
time_pos[time_sz-1]=' ';

// Find the message body
for (i = 0; NAD_ENAME_L(nad, i) > 0; i++)
{
if((NAD_ENAME_L(nad, i) == 4) && (strncmp("body", NAD_ENAME(nad, i), 4) == 0))
{
nad_body_len = NAD_CDATA_L(nad, i);
if (nad_body_len > 0) {
nad_body = NAD_CDATA(nad, i);
} else {
log_write(r->log, LOG_NOTICE, "message_log received a message with empty body");
return 0;
}
break;
}
elem = nad_find_elem(nad, 0, -1, "message", 1);
if (elem >= 0) {
elem = nad_find_elem(nad, elem, -1, "body", 1);
}

// Don't log anything if we found no NAD body
if (nad_body == NULL) {
if (elem == -1) {
return 0;
}

// Store original pointer address so that we know when to stop iterating through nad_body
nad_body_start = nad_body;
nad_body_len = NAD_CDATA_L(nad, elem);
nad_body = NAD_CDATA(nad, elem);

// replace line endings with "\n"
for (body_count = 0; (nad_body < nad_body_start + nad_body_len) && (body_count < (MAX_MESSAGE*2)-3); nad_body++) {
if (*nad_body == '\n') {
body[body_count++] = '\\';
body[body_count++] = 'n';
} else {
body[body_count++] = *nad_body;
// temporary replace line endings with 0x01, ASCII: <control> SOH <start of heading>
for (i = 0; i < nad_body_len; i++) {
if (nad_body[i] == '\n') {
nad_body[i] = 0x01;
}
}
body[body_count] = '\0';

// Log our message
umask((mode_t) 0077);
if (stat(r->message_logging_file, &filestat)) {
new_msg_file = 1;
}

if ((message_file = fopen(r->message_logging_file, "a")) == NULL)
{
if ((message_file = fopen(r->message_logging_file, "a")) == NULL) {
log_write(r->log, LOG_ERR, "Unable to open message log for writing: %s", strerror(errno));
return 1;
}
Expand All @@ -1207,16 +1182,31 @@ int message_log(nad_t nad, router_t r, const char *msg_from, const char *msg_to)
return 1;
}
fprintf(message_file, "# See router.xml for logging options.\n");
fprintf(message_file, "# Format: (Date)<tab>(From JID)<tab>(To JID)<tab>(Message Body)<line end>\n");
fprintf(message_file, "# Format: DateTime FromJID ToJID MessageBody<line end>\n");
}

if (! fprintf(message_file, "%s\t%s\t%s\t%s\n", time_pos, msg_from, msg_to, body))
{
log_write(r->log, LOG_ERR, "Unable to write to message log: %s", strerror(errno));
return 1;
/* ISO8601 timestamp */
t = time(NULL);
time_pos = localtime(&t);
if (strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S%z", time_pos) == 0) {
log_write(r->log, LOG_ERR, "strftime failed: %s", strerror(errno));
}

elem = fprintf(message_file, "%s %s %s %.*s\n", timestamp, msg_from, msg_to, nad_body_len, nad_body);

fclose(message_file);

// revert line endings
for (i = 0; i < nad_body_len; i++) {
if (nad_body[i] == 0x01) {
nad_body[i] = '\n';
}
}

if (!elem) {
log_write(r->log, LOG_ERR, "Unable to write to message log: %s", strerror(errno));
return 1;
}

return 0;
}

0 comments on commit 7b463ef

Please sign in to comment.