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