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