3a9a8ed25c29ee5516de37a9eb330d22c43fb685
[ric-plt/lib/rmr.git] / src / rmr / si / src / si95 / siwait.c
1 // vim: noet sw=4 ts=4:
2 /*
3 ==================================================================================
4     Copyright (c) 2020-2021 Nokia
5     Copyright (c) 2020-2021 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: SIwait
24 *  Abstract: This  routine will wait for an event to occur on the
25 *            connections in tplist. When an event is received on a fd
26 *            the status of the fd is checked and the event handled, driving
27 *            a callback routine if necessary. The system call poll is usd
28 *            to wait, and will be interrupted if a signal is caught,
29 *            therefore the routine will handle any work that is required
30 *            when a signal is received. The routine continues to loop
31 *            until the shutdown flag is set, or until there are no open
32 *            file descriptors on which to wait.
33 *  Parms:    gptr - Pointer to the global information block
34 *  Returns:  SI_OK if the caller can continue, SI_ERROR if all sessions have been
35 *            stopped, or the interface cannot proceed. When SI_ERROR is
36 *            returned the caller should cleanup and exit immediatly (we
37 *            have probably received a sigter or sigquit.
38 *  Date:     28 March 1995
39 *  Author:   E. Scott Daniels
40 *
41 *  Modified: 11 Apr 1995 - To pass execption to select when no keyboard
42 *            19 Apr 1995 - To call do key to do better keyboard editing
43 *            18 Aug 1995 - To init kstat to 0 to prevent key hold if
44 *                          network data pending prior to entry.
45 *                       31 Jul 2016 - Major formatting clean up in the main while loop.
46 **************************************************************************
47 */
48 #include  "sisetup.h"     //  get the setup stuff
49 #include "sitransport.h"
50 #include        <sys/wait.h>
51
52 /*
53         The select timeout is about 300 mu-sec. This is fast enough to add a new
54         outbound connection to the poll list before the other side responds,
55         but slow enough so as not to consume excess CPU when idle.
56 */
57 #define SI_SELECT_TIMEOUT 300000
58
59 #ifndef SYSTEM_UNDER_TEST
60 #       define SYSTEM_UNDER_TEST 0
61 #endif
62
63 extern int SIwait( struct ginfo_blk *gptr ) {
64         int fd = -1;                                    //  file descriptor for use in this routine
65         int ((*cbptr)());                               //  pointer to callback routine to call
66         int status = SI_OK;                             //  return status
67         int addrlen = 0;                                //  length of address from recvfrom call
68         int i;                                                  //  loop index
69         struct tp_blk *tpptr = NULL;    //  pointer at tp stuff
70         struct tp_blk *nextone= NULL;   //  point at next block to process in loop
71         int pstat = 0;                                  //  poll status
72         struct timeval  timeout;                //  delay to use on select call
73         char *buf = NULL;
74         char *ibuf = NULL;
75
76         if( gptr->magicnum != MAGICNUM ) {                              //  if not a valid ginfo block
77                 rmr_vlog( RMR_VL_CRIT, "SI95: wait: bad global info struct magic number is wrong\n" );
78                 return SI_ERROR;
79         }
80
81         if( gptr->flags & GIF_SHUTDOWN ) {                              //  cannot do if we should shutdown
82                 return SI_ERROR;                                                        //  so just get out
83         }
84
85         if( ( ibuf = (char *) malloc( 2048 ) ) == NULL ) {
86                         rmr_vlog( RMR_VL_WARN, "ibuf malloc fail\n" );
87                         return SI_ERROR;
88         }
89
90         do {                                                                    // spin until a callback says to stop (likely never)
91                 timeout.tv_sec = 0;                                     // must be reset on every call!
92                 timeout.tv_usec = SI_SELECT_TIMEOUT;
93
94                 SIbldpoll( gptr );                                      // poll list is trashed on each pop; must rebuild
95                 pstat = select( gptr->fdcount, &gptr->readfds, &gptr->writefds, &gptr->execpfds, &timeout );
96
97                 if( (pstat < 0 && errno != EINTR)  ) {
98                         gptr->fdcount = 0;                              //  prevent trying to look at a session
99                         gptr->flags |= GIF_SHUTDOWN;    //  cause cleanup and exit at end
100                 }
101
102                 if( pstat > 0  &&  (! (gptr->flags & GIF_SHUTDOWN)) ) {
103                         tpptr = gptr->tplist;
104                         while( tpptr != NULL ) {
105                                 nextone = tpptr->next;                          //  prevent issues if we delete the block during loop
106
107                                 if( tpptr->fd >= 0 ) {
108                                         if( tpptr->squeue != NULL && (FD_ISSET( tpptr->fd, &gptr->writefds )) ) {
109                                                 SIsend( gptr, tpptr );                  //  send if clear to send
110                                         }
111
112                                         if( FD_ISSET( tpptr->fd, &gptr->execpfds ) ) {
113                                                         ;                               // sunos seems to set the except flag for unknown reasons; ignore it
114                                         } else {
115                                                 if( FD_ISSET( tpptr->fd, &gptr->readfds ) ) {                   // ready to read
116                                                         fd = tpptr->fd;
117                                                         tpptr->rcvd++;
118
119                                                         if( tpptr->flags & TPF_LISTENFD ) {                                     // new session request
120                                                                 errno=0;
121                                                                 status = SInewsession( gptr, tpptr );                   // accept connection
122                                                         } else  {                                                                                       //  data received on a regular port (we support just tcp now
123                                                                 status = RECV( fd, gptr->rbuf, MAX_RBUF, 0 );   //  read data
124                                                                 if( status > 0  &&  ! (tpptr->flags & TPF_DRAIN) ) {
125                                                                         if( (cbptr = gptr->cbtab[SI_CB_CDATA].cbrtn) != NULL ) {
126                                                                                 status = (*cbptr)( gptr->cbtab[SI_CB_CDATA].cbdata, fd, gptr->rbuf, status );
127                                                                                 SIcbstat( gptr, status, SI_CB_CDATA );  //  handle cb status
128                                                                         }
129                                                                 } else {                                                                                // no bites, but read flagged indicates disconnect
130                                                                         if( (cbptr = gptr->cbtab[SI_CB_DISC].cbrtn) != NULL ) {
131                                                                                 status = (*cbptr)( gptr->cbtab[SI_CB_DISC].cbdata, tpptr->fd );
132                                                                                 SIcbstat( gptr, status, SI_CB_DISC );   //  handle status
133                                                                         }
134                                                                         SIterm( gptr, tpptr );                  // close FD and mark block for deletion
135                                                                 }
136                                                         }
137                                                 }
138                                         }
139                                 }                                                               //  if still good fd
140
141                                 tpptr = nextone;
142                         }
143                 }
144
145                 if( SYSTEM_UNDER_TEST ) {                                // enabled only during uint testing to prevent blocking
146                         break;
147                 }
148         } while( gptr->tplist != NULL && !(gptr->flags & GIF_SHUTDOWN) );
149
150         free( ibuf );
151         if( gptr->tplist == NULL )                                      //  indicate all fds closed
152
153         if( gptr->flags & GIF_SHUTDOWN ) {                      //  we need to stop for some reason
154                 status = SI_ERROR;                                              //  status should indicate to user to die
155                 SIshutdown( gptr );                                             //  clean things up
156         } else {
157                 status = SI_OK;                                                 //  user can continue to process
158         }
159
160         return status;
161 }