c4120fb1ad90389ac6d1a158bb56c87bde7f8306
[yaz-moved-to-github.git] / server / eventl.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: eventl.c,v $
7  * Revision 1.23  1997-09-01 08:52:59  adam
8  * New windows NT/95 port using MSV5.0. The test server 'ztest' was
9  * moved a separate directory. MSV5.0 project server.dsp created.
10  * As an option, the server can now operate as an NT service.
11  *
12  * Revision 1.22  1996/07/06 19:58:35  quinn
13  * System headerfiles gathered in yconfig
14  *
15  * Revision 1.21  1996/02/21  12:55:51  quinn
16  * small
17  *
18  * Revision 1.20  1996/02/21  12:52:55  quinn
19  * Test
20  *
21  * Revision 1.19  1995/12/05  11:17:30  quinn
22  * Moved some paranthesises around. Sigh.
23  *
24  * Revision 1.18  1995/11/13  09:27:41  quinn
25  * Fiddling with the variant stuff.
26  *
27  * Revision 1.17  1995/11/07  12:37:44  quinn
28  * Added support for forcing TIMEOUT event.
29  *
30  * Revision 1.16  1995/11/01  13:54:56  quinn
31  * Minor adjustments
32  *
33  * Revision 1.15  1995/09/15  14:44:15  quinn
34  * *** empty log message ***
35  *
36  * Revision 1.14  1995/08/29  14:44:50  quinn
37  * Reset timeouts.
38  *
39  * Revision 1.13  1995/08/29  11:17:56  quinn
40  * Added code to receive close
41  *
42  * Revision 1.12  1995/08/29  10:41:18  quinn
43  * Small.
44  *
45  * Revision 1.11  1995/06/19  12:39:09  quinn
46  * Fixed bug in timeout code. Added BER dumper.
47  *
48  * Revision 1.10  1995/06/16  10:31:33  quinn
49  * Added session timeout.
50  *
51  * Revision 1.9  1995/06/05  10:53:31  quinn
52  * Added a better SCAN.
53  *
54  * Revision 1.8  1995/05/16  08:51:01  quinn
55  * License, documentation, and memory fixes
56  *
57  * Revision 1.7  1995/03/27  15:02:01  quinn
58  * Added some includes for better portability
59  *
60  * Revision 1.6  1995/03/27  08:34:21  quinn
61  * Added dynamic server functionality.
62  * Released bindings to session.c (is now redundant)
63  *
64  * Revision 1.5  1995/03/15  08:37:41  quinn
65  * Now we're pretty much set for nonblocking I/O.
66  *
67  * Revision 1.4  1995/03/14  16:59:48  quinn
68  * Bug-fixes
69  *
70  * Revision 1.3  1995/03/14  11:30:14  quinn
71  * Works better now.
72  *
73  * Revision 1.2  1995/03/14  10:27:59  quinn
74  * More work on demo server.
75  *
76  * Revision 1.1  1995/03/10  18:22:44  quinn
77  * The rudiments of an asynchronous server.
78  *
79  */
80
81 #include <yconfig.h>
82 #include <stdio.h>
83 #include <assert.h>
84 #ifdef WINDOWS
85 #include <winsock.h>
86 #else
87 #include <unistd.h>
88 #endif
89 #include <stdlib.h>
90 #include <errno.h>
91 #include <string.h>
92
93 #include "eventl.h"
94 #include "log.h"
95 #include "comstack.h"
96 #include "session.h"
97 #include "statserv.h"
98 #include <xmalloc.h>
99
100 #ifndef WINDOWS
101
102 static IOCHAN iochans = 0;
103
104 IOCHAN iochan_getchan(void)
105 {
106     return iochans;
107 }
108
109 #endif /* WINDOWS */
110
111 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
112 {
113     IOCHAN new_iochan;
114
115     if (!(new_iochan = xmalloc(sizeof(*new_iochan))))
116         return 0;
117     new_iochan->destroyed = 0;
118     new_iochan->fd = fd;
119     new_iochan->flags = flags;
120     new_iochan->fun = cb;
121     new_iochan->force_event = 0;
122     new_iochan->last_event = new_iochan->max_idle = 0;
123
124 #ifdef WINDOWS
125     /* For windows we don't have a linklist of iochans */
126     new_iochan->next = NULL;
127 #else /* WINDOWS */
128     new_iochan->next = iochans;
129     iochans = new_iochan;
130 #endif  /* WINDOWS */
131
132     return new_iochan;
133 }
134
135 /* Event loop now takes an iochan as a parameter */
136 #ifdef WINDOWS
137 int __stdcall event_loop(IOCHAN iochans)
138 #else
139 int event_loop(IOCHAN dummylistener)
140 #endif
141 {
142     do /* loop as long as there are active associations to process */
143     {
144         IOCHAN p, nextp;
145             fd_set in, out, except;
146             int res, max;
147             static struct timeval nullto = {0, 0}, to;
148             struct timeval *timeout;
149
150             FD_ZERO(&in);
151             FD_ZERO(&out);
152             FD_ZERO(&except);
153             timeout = &to; /* hang on select */
154             to.tv_sec = 5*60;
155             to.tv_usec = 0;
156             max = 0;
157         for (p = iochans; p; p = p->next)
158         {
159                 if (p->force_event)
160                     timeout = &nullto;        /* polling select */
161                 if (p->flags & EVENT_INPUT)
162                     FD_SET(p->fd, &in);
163                 if (p->flags & EVENT_OUTPUT)
164                     FD_SET(p->fd, &out);
165                 if (p->flags & EVENT_EXCEPT)
166                     FD_SET(p->fd, &except);
167                 if (p->fd > max)
168                     max = p->fd;
169             }
170             if ((res = select(max + 1, &in, &out, &except, timeout)) < 0)
171             {
172                 if (errno == EINTR)
173                     continue;
174             else
175             {
176                 /* Destroy the first member in the chain, and try again */
177                 association *assoc = iochan_getdata(iochans);
178                 COMSTACK conn = assoc->client_link;
179
180                 cs_close(conn);
181                     destroy_association(assoc);
182                     iochan_destroy(iochans);
183                 logf(LOG_DEBUG, "error while selecting, destroying iochan %p", iochans);
184             }
185             }
186         for (p = iochans; p; p = p->next)
187         {
188                 int force_event = p->force_event;
189                 time_t now = time(0);
190
191                 p->force_event = 0;
192                 if (!p->destroyed && (FD_ISSET(p->fd, &in) || force_event == EVENT_INPUT))
193                 {
194                     p->last_event = now;
195                     (*p->fun)(p, EVENT_INPUT);
196                 }
197                 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
198                         force_event == EVENT_OUTPUT))
199                 {
200                     p->last_event = now;
201                     (*p->fun)(p, EVENT_OUTPUT);
202                 }
203                 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
204                     force_event == EVENT_EXCEPT))
205                 {
206                         p->last_event = now;
207                     (*p->fun)(p, EVENT_EXCEPT);
208                 }
209                 if (!p->destroyed && ((p->max_idle && now - p->last_event >
210                     p->max_idle) || force_event == EVENT_TIMEOUT))
211                 {
212                     p->last_event = now;
213                     (*p->fun)(p, EVENT_TIMEOUT);
214                 }
215             }
216             for (p = iochans; p; p = nextp)
217             {
218                 nextp = p->next;
219
220                 if (p->destroyed)
221                 {
222                     IOCHAN tmp = p, pr;
223
224                 /* We need to inform the threadlist that this channel has been destroyed */
225                 statserv_remove(p);
226
227                     /* Now reset the pointers */
228                 if (p == iochans)
229                             iochans = p->next;
230                         else
231                         {
232                             for (pr = iochans; pr; pr = pr->next)
233                                 if (pr->next == p)
234                                     break;
235                             assert(pr); /* grave error if it weren't there */
236                             pr->next = p->next;
237                         }
238                         if (nextp == p)
239                             nextp = p->next;
240                         xfree(tmp);
241                 }
242             }
243     }
244     while (iochans);
245     return 0;
246 }