Release 1.10.1 from staging
[ric-app/mc.git] / sidecars / listener / src / mc_listener.c
index f9fab09..f1b6507 100644 (file)
 #include <stdlib.h>
 #include <time.h>
 #include <string.h>
+#include <signal.h>
 
 
 #include "mcl.h"
 
 //---- support -----------------------------------------------------------------------------
+char* usage_str =
+               "[-d fifo-dir] [-e] [-p listen-port] [-q | -r report-freq]\n"
+               "  -e  disable extended header in buffers written to FIFOs\n"
+               "\n"
+               "The following environment variables may be set to affect operation:\n"
+               "  MCL_RDC_STAGE: the directory where raw data capture files are staged. (/tmp/rdc/stage)\n"
+               "  MCL_RDC_FINAL: the directory where raw data capture files are placed for export. (/tmp/rdc/final)\n"
+               "  MCL_RDC_SUFFIX: the suffix written on each raw data capture file; must include '.'. (.rdc)\n"
+               "  MCL_RDC_SOURCE: a short string used as source identification in rdc file names.\n"
+               "  MCL_RDC_FREQ: the amount of time (seconds) that raw capture files are rolled. (300)\n"
+               "\nIf either final or staging directories are defined by environment vars, they MUST exist.\n";
 
-static void bad_arg( char* what ) {
+/*
+       Bad argument error.
+*/
+static void ba_err( char* what ) {
        fprintf( stderr, "[ERR] option is unrecognised or isn't followed by meaningful data: %s\n", what );
 }
 
 static void usage( char* argv0 ) {
-       fprintf( stderr, "usage: %s [-d fifo-dir] [-e] [-p listen-port] [-q | -r report-freq]\n", argv0 );
-       fprintf( stderr, "  -e  disable extended header in buffers written to FIFOs\n" );
-       fprintf( stderr, "\n" );
-       fprintf( stderr, "The following environment variables may be set to affect operation:\n" );
-       fprintf( stderr, "  MCL_RDC_STAGE: the directory where raw data capture files are staged. (/tmp/rdc/stage)\n" );
-       fprintf( stderr, "  MCL_RDC_FINAL: the directory where raw data capture files are placed for export. (/tmp/rdc/final)\n" );
-       fprintf( stderr, "  MCL_RDC_SUFFIX: the suffix written on each raw data capture file; must include '.'. (.rdc)\n" );
-       fprintf( stderr, "  MCL_RDC_SOURCE: a short string used as source identification in rdc file names.\n" );
-       fprintf( stderr, "  MCL_RDC_FREQ: the amount of time (seconds) that raw capture files are rolled. (300)\n" );
-       fprintf( stderr, "\nIf either final or staging directories are defined by environment vars, they MUST exist.\n" );
-       fprintf( stderr, "\n" );
+       fprintf( stderr, "usage: %s %s\n", argv0, usage_str );
+}
+
+/*
+       Exit on trapped signal allowing ctl-C or SIGTERM to stop us gracefully and capture
+       the gcda data for coverage.
+*/
+static void sigh( int sign ) {
+       fprintf( stderr, "\n[INFO] exiting on signal %d\n", sign );
+       exit( 0 );
 }
 
 //------------------------------------------------------------------------------------------
 int main( int argc,  char** argv ) {
        void*   ctx;                                                    // the mc listener library context
-       char*   dname = "/tmp/mcl/fifos";               // default directory where we open fifos
-       char*   port = "4560";                                  // default rmr port
-       char*   siphon_dir = "/tmp/mci/siphon"; // where siphon files are placed
-       int             siphon = 0;                                             // percentage of messages to siphone off
+       char*   dname = NULL;                                   // default directory where we open fifos
+       char*   port =  NULL;
        int             report_freq = 60;                               // report stats every n seconds
        int             pidx = 1;                                               // parameter index
        int             error = 0;
        int             long_hdrs = 1;                                  // -e sets and causes extended headers to be written
 
+       signal( SIGINT, sigh );
+       signal( SIGTERM, sigh );
+
+       dname = strdup( "/tmp/mcl/fifos" );                                     // so we can always free
+       port = strdup( "4560" );                                                        // default port
+
        while( pidx < argc && argv[pidx][0] == '-' ) {                  // simple argument parsing (-x  or -x value)
                switch( argv[pidx][1] ) {
                        case 'd':
                                if( pidx+1 < argc ) {
+                                       free( dname );
                                        dname = strdup( argv[pidx+1] );
                                        pidx++;
                                } else {
-                                       bad_arg( argv[pidx] );
+                                       ba_err( argv[pidx] );
                                        error = 1;
                                }
                                break;
@@ -101,10 +120,11 @@ int main( int argc,  char** argv ) {
 
                        case 'p':
                                if( pidx+1 < argc ) {
+                                       free( port );
                                        port = strdup( argv[pidx+1] );
                                        pidx++;
                                } else {
-                                       bad_arg( argv[pidx] );
+                                       ba_err( argv[pidx] );
                                        error = 1;
                                }
                                break;
@@ -118,7 +138,7 @@ int main( int argc,  char** argv ) {
                                        report_freq = atoi( argv[pidx+1] );
                                        pidx++;
                                } else {
-                                       bad_arg( argv[pidx] );
+                                       ba_err( argv[pidx] );
                                        error = 1;
                                }
                                break;
@@ -127,10 +147,9 @@ int main( int argc,  char** argv ) {
                        case '?':
                                usage( argv[0] );
                                exit( 0 );
-                               break;
 
                        default:
-                               bad_arg( argv[pidx] );
+                               ba_err( argv[pidx] );
                                error = 1;
                                break;
                }
@@ -139,6 +158,7 @@ int main( int argc,  char** argv ) {
        }
 
        if( error ) {
+               free( dname );
                usage( argv[0] );
                exit( 1 );
        }
@@ -146,6 +166,7 @@ int main( int argc,  char** argv ) {
        ctx = mcl_mk_context( dname );                  // initialise the library context
        if( ctx == NULL ) {
                fprintf( stderr, "[FAIL] couldn't initialise the mc listener environment\n" );
+               free( dname );
                exit( 1 );
        }
        mcl_set_sigh();                                                                 // set signal handler(s)
@@ -154,5 +175,8 @@ int main( int argc,  char** argv ) {
        mcl_fifo_fanout( ctx, report_freq, long_hdrs );         // listen and fanout messages to fifo; report to stdout every ~2sec
 
        fprintf( stderr, "[INFO] mc listener is finished.\n" );
+
+       free( port );                   // uneeded, but these keep sonar from twisting it's knickers about leaks
+       free( dname );
 }