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