Re-enable RMR libary's module tests
[ric-plt/lib/rmr.git] / src / rmr / si / src / si95 / siinit.c
1 // vim: noet sw=4 ts=4:
2 /*
3 ==================================================================================
4     Copyright (c) 2020 Nokia
5     Copyright (c) 2020 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22 **************************************************************************
23 *  Mnemonic: SIinitialise
24 *  Abstract: Initialisation and other context management functions.
25 *
26 *  Date:     26 March 1995
27 *  Author:   E. Scott Daniels
28 *
29 *  Mod:         17 FEB 2002 - To convert to a globally managed gpointer
30 *                       09 Mar 2007 - To allow for ipv6 (added SIinitialise() to
31 *                               replace SIinit())
32 **************************************************************************
33 */
34 #include  "sisetup.h"     //  get the setup stuff
35
36 /*
37         Initialise the SI environment. Specifically:
38                 allocate the global info block (context)
39
40         Returns a pointer to the block or nil on failure.
41         On failure errno should indicate the problem.
42 */
43 extern struct ginfo_blk* SIinitialise( int opts )
44 {
45         struct ginfo_blk *gptr = NULL;  //  pointer at gen info blk
46         int     status = SI_OK;                 //  status of internal processing
47         struct  tp_blk *tpptr;          //  pointer at tp stuff
48         struct  sigaction sact;                 //  signal action block
49         int     i;                                                      //  loop index
50         int     signals = SI_DEF_SIGS;          //  signals to be set in SIsetsig
51
52         errno = ENOMEM;
53
54         if( (gptr = SInew( GI_BLK )) != NULL ) {                //  make our context
55                 gptr->rbuf = (char *) malloc( MAX_RBUF );   //  get rcv buffer
56                 gptr->rbuflen = MAX_RBUF;
57                 gptr->tp_map = (struct tp_blk **) malloc( sizeof( struct tp_blk *) * MAX_FDS );
58                 if( gptr->tp_map == NULL ) {
59                         fprintf( stderr, "SIinit: unable to initialise tp_map: no memory\n" );
60                         free( gptr );
61                         return NULL;
62                 }
63                 memset( gptr->tp_map, 0, sizeof( struct tp_blk *) * MAX_FDS );
64
65                 gptr->cbtab = (struct callback_blk *) malloc(
66                         (sizeof( struct callback_blk ) * MAX_CBS ) );
67                 if( gptr->cbtab != NULL ) {
68                         for( i = 0; i < MAX_CBS; i++ ) {     //  initialize callback table
69                                 gptr->cbtab[i].cbdata = NULL;    //  no data and no functions
70                                 gptr->cbtab[i].cbrtn = NULL;
71                         }
72                 } else {                 //  if call back table allocation failed - error off
73                         SIshutdown( gptr );  //  clean up any open fds
74                         free( gptr->tp_map );
75                         free( gptr );
76                         gptr = NULL;       //  dont allow them to continue
77                 }
78         }                     //  end if gen infor block allocated successfully
79
80
81         memset( &sact, 0, sizeof( sact ) );
82         sact.sa_handler = SIG_IGN;
83         sigaction( SIGPIPE, &sact, NULL );              // ignore pipe signals as for some bloody reason linux sets this off if write to closed socket
84
85         return gptr;            //  all's well that ends well
86 }
87
88 /*
89         This will set all of the tcp oriented flags in mask (SI_TF_* constants).
90 */
91 extern void SIset_tflags( struct ginfo_blk* gp, int mask )  {
92         if( gp != NULL ) {
93                 gp->tcp_flags |= mask;
94         }
95 }
96
97 /*
98         This will clear all tcp oriented flags set in mask.
99 */
100 extern void SIclr_tflags( struct ginfo_blk* gp, int mask )  {
101         if( gp != NULL ) {
102                 gp->tcp_flags &= ~mask;
103         }
104 }
105
106 /*
107         Dump stats to stderr.
108
109         NOTE:  the receive stats are the number of times that wait popped for
110                 a file descriptor and NOT the actual number of RMR messages which
111                 were contained.  Thus it is VERY likely that the receive count
112                 reported will not match the number of actual messages sent. These
113                 counts should be used only to track activity on a socket.
114 */
115 extern void SItp_stats( void *vgp ) {
116         struct ginfo_blk* gp;
117         struct tp_blk* tp;
118
119         if( (gp = (struct ginfo_blk *) vgp) != NULL ) {
120                 for( tp = gp->tplist; tp != NULL; tp = tp->next ) {
121                         rmr_vlog( RMR_VL_DEBUG, "si95: tp: fd=%d sent=%lld rcvd=%lld qc=%lld\n", tp->fd, tp->sent, tp->rcvd, tp->qcount );
122                 }
123         }
124 }