Skip to content

Commit

Permalink
[mi_fifo] trace fifo mi commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ionutrazvanionita committed Oct 25, 2016
1 parent 29a0a7d commit 8b5beec
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
55 changes: 55 additions & 0 deletions modules/mi_fifo/fifo_fnc.c
Expand Up @@ -45,6 +45,9 @@
#include "mi_parser.h"
#include "mi_writer.h"

#include "../../mi/mi_trace.h"
#define FIFO_REPLY_MAX_SIZE 512

static char *mi_buf = 0;
static char *reply_fifo_s = 0;
static int reply_fifo_len = 0;
Expand All @@ -55,6 +58,10 @@ static int mi_fifo_gid;

static int volatile mi_reload_fifo = 0;

char reply_buf[FIFO_REPLY_MAX_SIZE];
extern trace_dest trace_dst;


FILE* mi_create_fifo(void)
{
static int mi_fifo_read = 0;
Expand Down Expand Up @@ -489,6 +496,8 @@ void mi_fifo_server(FILE *fifo_stream)
struct mi_cmd *f;
FILE *reply_stream;

str command_s;

while(1) {
reply_stream = NULL;

Expand Down Expand Up @@ -520,6 +529,7 @@ void mi_fifo_server(FILE *fifo_stream)
goto consume1;
}
command = mi_buf+1;

file_sep=strchr(command, MI_CMD_SEPARATOR );
if (file_sep==NULL) {
LM_ERR("file separator missing\n");
Expand All @@ -541,6 +551,16 @@ void mi_fifo_server(FILE *fifo_stream)
}
/* make command zero-terminated */
*file_sep=0;
/* FIXME trace mi command here */
if (trace_dst) {
command_s.s = command;
command_s.len = file_sep - command;
if (trace_mi_message(NULL, NULL, &command_s, trace_dst) < 0) {
/* don't quit; only tracing failed; let the command execute */
LM_ERR("Failed to trace mi request!\n");
}
}


f=lookup_mi_cmd( command, strlen(command) );
if (f==0) {
Expand Down Expand Up @@ -623,3 +643,38 @@ void mi_fifo_server(FILE *fifo_stream)
mi_do_consume();
}
}

inline int trace_is_on(void)
{
return trace_dst ? 1 : 0;
}

/*
* WARNINING: THIS FUNCTION MUST BE USED ONLY AFTER
* trace_is_on() was checked, else might cause a segfault
* if trace not enabled and no trace destination is defined
*
*/
int trace_fifo_reply(char* reply_fmt, va_list arg)
{
int len;
str repl_s;

len=vsnprintf(reply_buf, FIFO_REPLY_MAX_SIZE, reply_fmt, arg);
if (len < 0 || len == FIFO_REPLY_MAX_SIZE) {
LM_ERR("failed to write mi command to buffer! probably this"
" is due to reply bigger than %d!\n",
FIFO_REPLY_MAX_SIZE);
return -1;
}

repl_s.s = reply_buf;
repl_s.len = len;

if (trace_mi_message(NULL, NULL, &repl_s, trace_dst) < 0) {
LM_ERR("failed to trace mi reply!\n");
return -1;
}

return 0;
}
14 changes: 13 additions & 1 deletion modules/mi_fifo/fifo_fnc.h
Expand Up @@ -39,23 +39,35 @@
#define FIFO_REPLY_WAIT 80000



FILE* mi_init_fifo_server(char *fifo_name, int mode, int uid, int gid,
char* fifo_reply_dir);

void mi_fifo_server(FILE *fifostream);

int mi_read_line( char *b, int max, FILE **stream, int *read);
int trace_fifo_reply(char* reply_fmt, va_list arg);
inline int trace_is_on(void);

static inline int mi_fifo_reply( FILE *stream, char *reply_fmt, ... )
{
/* FIXME trace fifo reply here */
int r;
va_list ap;

retry:
if (trace_is_on()) {
va_start(ap, reply_fmt);
if (trace_fifo_reply(reply_fmt, ap) < 0) {
/* don't exit; only tracing failed; rest still works fine */
LM_ERR("failed to trace mi message!\n");
}
va_end(ap);
}

va_start(ap, reply_fmt);
r = vfprintf( stream, reply_fmt, ap);
va_end(ap);

if (r<=0) {
if ((errno==EINTR)||(errno==EAGAIN)||(errno==EWOULDBLOCK)) {
goto retry;
Expand Down
17 changes: 17 additions & 0 deletions modules/mi_fifo/mi_fifo.c
Expand Up @@ -44,6 +44,10 @@
#include "mi_writer.h"
#include "fifo_fnc.h"

#include "../../mi/mi_trace.h"
//#include "../modules/proto_hep/hep.h"


static int mi_mod_init(void);
static int mi_child_init(int rank);
static void fifo_process(int rank);
Expand All @@ -62,6 +66,10 @@ static char *mi_fifo_gid_s = 0;
static int mi_fifo_mode = S_IRUSR| S_IWUSR| S_IRGRP| S_IWGRP; /* rw-rw---- */
static int read_buf_size = MAX_MI_FIFO_READ;

/* tracing params */
static char* trace_dest_name=NULL;
trace_dest trace_dst=NULL;



static param_export_t mi_params[] = {
Expand All @@ -73,6 +81,7 @@ static param_export_t mi_params[] = {
{"fifo_user", INT_PARAM, &mi_fifo_uid},
{"reply_dir", STR_PARAM, &mi_fifo_reply_dir},
{"reply_indent", STR_PARAM, &mi_reply_indent},
{"trace_destination",STR_PARAM, &trace_dest_name},
{0,0,0}
};

Expand Down Expand Up @@ -108,6 +117,7 @@ static int mi_mod_init(void)
{
int n;
struct stat filestat;
str trace_name_s;

/* checking the mi_fifo module param */
if (mi_fifo==NULL || *mi_fifo == 0) {
Expand Down Expand Up @@ -166,6 +176,13 @@ static int mi_mod_init(void)
}
}

if (trace_dest_name) {
trace_name_s.s = trace_dest_name;
trace_name_s.len = strlen(trace_name_s.s);
if (trace_api && trace_api->get_trace_dest_by_name)
trace_dst = trace_api->get_trace_dest_by_name(&trace_name_s);
}

return 0;
}

Expand Down

0 comments on commit 8b5beec

Please sign in to comment.