Module: kamailio
Branch: master
Commit: e4163d287e1c8c617ef9f21452a88f09c040bd47
URL:
https://github.com/kamailio/kamailio/commit/e4163d287e1c8c617ef9f21452a88f0…
Author: Xenofon Karamanos <xk(a)gilawa.com>
Committer: Xenofon Karamanos <xenakiskaramanos(a)gmail.com>
Date: 2024-02-16T12:20:44+02:00
file_out: Switch to str type for string handling
---
Modified: src/modules/file_out/file_out.c
Modified: src/modules/file_out/types.c
Modified: src/modules/file_out/types.h
---
Diff:
https://github.com/kamailio/kamailio/commit/e4163d287e1c8c617ef9f21452a88f0…
Patch:
https://github.com/kamailio/kamailio/commit/e4163d287e1c8c617ef9f21452a88f0…
---
diff --git a/src/modules/file_out/file_out.c b/src/modules/file_out/file_out.c
index b52dad270d5..5815d61cb63 100644
--- a/src/modules/file_out/file_out.c
+++ b/src/modules/file_out/file_out.c
@@ -199,8 +199,14 @@ static void fo_log_writer_process(int rank)
return;
}
- fprintf(out, "%s\n", log_message.message);
- fflush(out);
+ if(fprintf(out, "%.*s\n", log_message.message->len,
+ log_message.message->s)
+ < 0) {
+ LM_ERR("Failed to write to file with err {%s}\n", strerror(errno));
+ }
+ if(fflush(out) < 0) {
+ LM_ERR("Failed to flush file with err {%s}\n", strerror(errno));
+ }
}
}
@@ -463,7 +469,7 @@ static int fo_write_to_file(sip_msg_t *msg, char *index, char
*log_message)
return -1;
}
- str value;
+ str value = str_init("");
result = get_str_fparam(&value, msg, (fparam_t *)log_message);
if(result < 0) {
LM_ERR("Failed to string from param 1: %d\n", result);
@@ -471,8 +477,8 @@ static int fo_write_to_file(sip_msg_t *msg, char *index, char
*log_message)
}
/* Add the logging string to the global gueue */
- fo_log_message_t logMessage;
- logMessage.message = value.s;
+ fo_log_message_t logMessage = {0, 0};
+ logMessage.message = &value;
logMessage.dest_file = file_index;
fo_enqueue(fo_queue, logMessage);
diff --git a/src/modules/file_out/types.c b/src/modules/file_out/types.c
index a80566dfc8f..d2815333dd0 100644
--- a/src/modules/file_out/types.c
+++ b/src/modules/file_out/types.c
@@ -22,6 +22,7 @@
#include <stdlib.h>
#include "types.h"
+#include "../../core/ut.h"
static fo_node_t *fo_new_node(fo_log_message_t data)
{
@@ -37,8 +38,18 @@ int fo_enqueue(fo_queue_t *q, fo_log_message_t data)
/*
Copy the contents of data.message
*/
- char *message_copy = (char *)shm_malloc(strlen(data.message) + 1);
- strcpy(message_copy, data.message);
+ str *message_copy = 0;
+ message_copy = (str *)shm_malloc(sizeof(str));
+ if(message_copy == 0) {
+ SHM_MEM_ERROR;
+ return -1;
+ }
+
+ if(shm_str_dup(message_copy, data.message) < 0) {
+ LM_ERR("Failed to duplicate message\n");
+ return -1;
+ }
+
data.message = message_copy;
fo_node_t *temp = fo_new_node(data);
@@ -74,10 +85,6 @@ int fo_dequeue(fo_queue_t *q, fo_log_message_t *data)
if(temp != NULL) {
- if(temp->data.message != NULL) {
- shm_free(temp->data.message);
- temp->data.message = NULL;
- }
shm_free(temp);
temp = NULL;
}
diff --git a/src/modules/file_out/types.h b/src/modules/file_out/types.h
index 0c81cb20a7b..c6fb72b28ef 100644
--- a/src/modules/file_out/types.h
+++ b/src/modules/file_out/types.h
@@ -22,7 +22,7 @@
typedef struct log_message
{
- char *message;
+ str *message;
int dest_file;
} fo_log_message_t;