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