Release 1.10.1 from staging
[ric-app/mc.git] / sidecars / listener / src / pipe_reader.c
index 4804266..88ad08f 100644 (file)
@@ -33,6 +33,7 @@
 #include <stdlib.h>
 #include <time.h>
 #include <string.h>
+#include <signal.h>
 
 
 #include "mcl.h"
@@ -48,9 +49,19 @@ static void usage( char* argv0 ) {
        fprintf( stderr, "   -d  dir (default is /tmp/mcl/fifos)\n" );
        fprintf( stderr, "   -e  disable extended headers expectation in FIFO data\n" );
        fprintf( stderr, "   -m  msg-type (default is 0)\n" );
+       fprintf( stderr, "   -M  max msg to read (default is all)\n" );
        fprintf( stderr, "   -s  stats only mode\n" );
 }
 
+/*
+       We exit on any trapped signal so that we can kill  -15 the proecss
+       and still get gcoverage information to keep sonar happy.
+*/
+static void sigh( int sig ) {
+       fprintf( stderr, "\n[INFO] exiting on signal %d\n", sig );
+       exit( 0 );
+}
+
 //------------------------------------------------------------------------------------------
 int main( int argc,  char** argv ) {
        void*   ctx;                                                    // the mc listener library context
@@ -66,17 +77,21 @@ int main( int argc,  char** argv ) {
        char    timestamp[MCL_TSTAMP_SIZE];             // we'll get the timestamp from this
        long    count = 0;
        int             blabber = 0;
+       int             max = 0;                                                // we'll force one reader down early to simulate MC going away
+
+       signal( SIGINT, sigh );
+       signal( SIGTERM, sigh );
 
        while( pidx < argc && argv[pidx][0] == '-' ) {                  // simple argument parsing (-x  or -x value)
                switch( argv[pidx][1] ) {
                        case 'd':
                                if( pidx+1 < argc ) {
                                        dname = strdup( argv[pidx+1] );
-                                       pidx++; 
+                                       pidx++;
                                } else {
-                                       bad_arg( argv[pidx] ); 
+                                       bad_arg( argv[pidx] );
                                        error = 1;
-                               }       
+                               }
                                break;
 
                        case 'e':
@@ -90,11 +105,21 @@ int main( int argc,  char** argv ) {
                        case 'm':
                                if( pidx+1 < argc ) {
                                        mtype = atoi( argv[pidx+1] );
-                                       pidx++; 
+                                       pidx++;
                                } else {
-                                       bad_arg( argv[pidx] ); 
+                                       bad_arg( argv[pidx] );
                                        error = 1;
-                               }       
+                               }
+                               break;
+
+                       case 'M':
+                               if( pidx+1 < argc ) {
+                                       max = atoi( argv[pidx+1] );
+                                       pidx++;
+                               } else {
+                                       bad_arg( argv[pidx] );
+                                       error = 1;
+                               }
                                break;
 
                        case 's':
@@ -105,7 +130,6 @@ int main( int argc,  char** argv ) {
                        case '?':
                                usage( argv[0] );
                                exit( 0 );
-                               break;
 
                        default:
                                bad_arg( argv[pidx] );
@@ -120,14 +144,15 @@ int main( int argc,  char** argv ) {
                usage( argv[0] );
                exit( 1 );
        }
-       
+
        ctx = mcl_mk_context( dname );                  // initialise the library context
        if( ctx == NULL ) {
                fprintf( stderr, "[FAIL] couldn't initialise the mc listener library" );
                exit( 1 );
        }
 
-       while( 1 ) {
+       fprintf( stderr, "[INFO] max = %d\n", max );
+       while( max == 0 || count < max ) {
                len = mcl_fifo_tsread1( ctx, mtype, buf, sizeof( buf ) -1, long_hdrs, timestamp );
                if( len > 0 ) {
                        if( stats_only ) {
@@ -144,9 +169,11 @@ int main( int argc,  char** argv ) {
                        }
 
                        count++;
+               } else {
+                       sleep( 1 );
                }
        }
 
-       fprintf( stderr, "[INFO] mc listener is finished.\n" );
+       fprintf( stderr, "[INFO] max reached: %d\n", max );
 }