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