Tweak unit tests to save coverage in common dir
[ric-app/mc.git] / sidecars / listener / src / mc_listener.c
1 // vim: ts=4 sw=4 noet:
2 /*
3 --------------------------------------------------------------------------------
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10            http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 --------------------------------------------------------------------------------
18 */
19
20 /*
21         Mnemonic:       mc_listener.c
22         Abstract:       This application (management campaign listener) will listen for
23                                 RMR based messages and write the payloads into FIFOs which
24                                 correspond to the message type.
25                 
26                                 Defaults:
27                                         /var/lib/mc/listener  -- directory for FIFOs
28
29                                 Command line options:
30                                         -d <path>   FIFO directory (default is /tmp/mcl/fifos)
31                                         -p <port>       The port to set RMR listener on (default is 4560)
32                                         -r <seconds>  The frequency that count reports are written to
33                                                                         stderr. 0 == 0ff; default is 60.
34
35
36                                 RMR based environment variables which might be needed:
37                                         RMR_SEED_RT -- path to the static routing table
38                                         RMR_RTG_SVC -- port to listen for RTG connections
39
40         Date:           22 August 2019
41         Author:         E. Scott Daniels
42 */
43
44 #include <unistd.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <time.h>
49 #include <string.h>
50 #include <signal.h>
51
52
53 #include "mcl.h"
54
55 //---- support -----------------------------------------------------------------------------
56 char* usage_str =
57                 "[-d fifo-dir] [-e] [-p listen-port] [-q | -r report-freq]\n"
58                 "  -e  disable extended header in buffers written to FIFOs\n"
59                 "\n"
60                 "The following environment variables may be set to affect operation:\n"
61                 "  MCL_RDC_STAGE: the directory where raw data capture files are staged. (/tmp/rdc/stage)\n"
62                 "  MCL_RDC_FINAL: the directory where raw data capture files are placed for export. (/tmp/rdc/final)\n"
63                 "  MCL_RDC_SUFFIX: the suffix written on each raw data capture file; must include '.'. (.rdc)\n"
64                 "  MCL_RDC_SOURCE: a short string used as source identification in rdc file names.\n"
65                 "  MCL_RDC_FREQ: the amount of time (seconds) that raw capture files are rolled. (300)\n"
66                 "\nIf either final or staging directories are defined by environment vars, they MUST exist.\n";
67
68 /*
69         Bad argument error.
70 */
71 static void ba_err( char* what ) {
72         fprintf( stderr, "[ERR] option is unrecognised or isn't followed by meaningful data: %s\n", what );
73 }
74
75 static void usage( char* argv0 ) {
76         fprintf( stderr, "usage: %s %s\n", argv0, usage_str );
77 }
78
79 /*
80         Exit on trapped signal allowing ctl-C or SIGTERM to stop us gracefully and capture
81         the gcda data for coverage.
82 */
83 static void sigh( int sign ) {
84         fprintf( stderr, "\n[INFO] exiting on signal %d\n", sign );
85         exit( 0 );
86 }
87
88 //------------------------------------------------------------------------------------------
89 int main( int argc,  char** argv ) {
90         void*   ctx;                                                    // the mc listener library context
91         char*   dname = NULL;                                   // default directory where we open fifos
92         char*   port =  NULL;
93         int             report_freq = 60;                               // report stats every n seconds
94         int             pidx = 1;                                               // parameter index
95         int             error = 0;
96         int             long_hdrs = 1;                                  // -e sets and causes extended headers to be written
97
98         signal( SIGINT, sigh );
99         signal( SIGTERM, sigh );
100
101         dname = strdup( "/tmp/mcl/fifos" );                                     // so we can always free
102         port = strdup( "4560" );                                                        // default port
103
104         while( pidx < argc && argv[pidx][0] == '-' ) {                  // simple argument parsing (-x  or -x value)
105                 switch( argv[pidx][1] ) {
106                         case 'd':
107                                 if( pidx+1 < argc ) {
108                                         free( dname );
109                                         dname = strdup( argv[pidx+1] );
110                                         pidx++;
111                                 } else {
112                                         ba_err( argv[pidx] );
113                                         error = 1;
114                                 }
115                                 break;
116
117                         case 'e':
118                                 long_hdrs = 0;
119                                 break;
120
121                         case 'p':
122                                 if( pidx+1 < argc ) {
123                                         free( port );
124                                         port = strdup( argv[pidx+1] );
125                                         pidx++;
126                                 } else {
127                                         ba_err( argv[pidx] );
128                                         error = 1;
129                                 }
130                                 break;
131
132                         case 'q':
133                                 report_freq = 0;
134                                 break;
135
136                         case 'r':
137                                 if( pidx+1 < argc ) {
138                                         report_freq = atoi( argv[pidx+1] );
139                                         pidx++;
140                                 } else {
141                                         ba_err( argv[pidx] );
142                                         error = 1;
143                                 }
144                                 break;
145
146                         case 'h':
147                         case '?':
148                                 usage( argv[0] );
149                                 exit( 0 );
150
151                         default:
152                                 ba_err( argv[pidx] );
153                                 error = 1;
154                                 break;
155                 }
156
157                 pidx++;
158         }
159
160         if( error ) {
161                 free( dname );
162                 usage( argv[0] );
163                 exit( 1 );
164         }
165
166         ctx = mcl_mk_context( dname );                  // initialise the library context
167         if( ctx == NULL ) {
168                 fprintf( stderr, "[FAIL] couldn't initialise the mc listener environment\n" );
169                 free( dname );
170                 exit( 1 );
171         }
172         mcl_set_sigh();                                                                 // set signal handler(s)
173
174         mcl_start_listening( ctx,  port, MCL_NOWAIT );          // start the listener, no waiting for rt since we don't send
175         mcl_fifo_fanout( ctx, report_freq, long_hdrs );         // listen and fanout messages to fifo; report to stdout every ~2sec
176
177         fprintf( stderr, "[INFO] mc listener is finished.\n" );
178
179         free( port );                   // uneeded, but these keep sonar from twisting it's knickers about leaks
180         free( dname );
181 }
182