SOAP, SRW codecs and HTTP transport for YAZ using libxml2.
[yaz-moved-to-github.git] / server / eventl.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: eventl.c,v 1.35 2003-02-12 15:06:43 adam Exp $
7  */
8
9 #include <stdio.h>
10 #include <assert.h>
11 #ifdef WIN32
12 #include <winsock.h>
13 #else
14 #include <unistd.h>
15 #endif
16 #include <stdlib.h>
17 #include <errno.h>
18 #include <string.h>
19
20 #include <yaz/yconfig.h>
21 #include <yaz/log.h>
22 #include <yaz/comstack.h>
23 #include <yaz/xmalloc.h>
24 #include "eventl.h"
25 #include "session.h"
26 #include <yaz/statserv.h>
27
28 #if YAZ_GNU_THREADS
29 #include <pth.h>
30 #define YAZ_EV_SELECT pth_select
31 #endif
32
33 #ifndef YAZ_EV_SELECT
34 #define YAZ_EV_SELECT select
35 #endif
36
37 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
38 {
39     IOCHAN new_iochan;
40
41     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
42         return 0;
43     new_iochan->destroyed = 0;
44     new_iochan->fd = fd;
45     new_iochan->flags = flags;
46     new_iochan->fun = cb;
47     new_iochan->force_event = 0;
48     new_iochan->last_event = new_iochan->max_idle = 0;
49     new_iochan->next = NULL;
50     return new_iochan;
51 }
52
53 int event_loop(IOCHAN *iochans)
54 {
55     do /* loop as long as there are active associations to process */
56     {
57         IOCHAN p, nextp;
58         fd_set in, out, except;
59         int res, max;
60         static struct timeval nullto = {0, 0}, to;
61         struct timeval *timeout;
62
63         if (statserv_must_terminate())
64         {
65             for (p = *iochans; p; p = p->next)
66                 p->force_event = EVENT_TIMEOUT;
67         }
68         FD_ZERO(&in);
69         FD_ZERO(&out);
70         FD_ZERO(&except);
71         timeout = &to; /* hang on select */
72         to.tv_sec = 5*60;
73         to.tv_usec = 0;
74         max = 0;
75         for (p = *iochans; p; p = p->next)
76         {
77             yaz_log(LOG_LOG, "fd=%d flags=%d force_event=%d",
78                     p->fd, p->flags, p->force_event);
79             if (p->force_event)
80                 timeout = &nullto;        /* polling select */
81             if (p->flags & EVENT_INPUT)
82                 FD_SET(p->fd, &in);
83             if (p->flags & EVENT_OUTPUT)
84                 FD_SET(p->fd, &out);
85             if (p->flags & EVENT_EXCEPT)
86                 FD_SET(p->fd, &except);
87             if (p->fd > max)
88                 max = p->fd;
89         }
90         yaz_log(LOG_LOG, "select start");
91         res = YAZ_EV_SELECT(max + 1, &in, &out, &except, timeout);
92         yaz_log(LOG_LOG, "select end");
93         if (res < 0)
94         {
95             if (yaz_errno() == EINTR)
96             {
97                 if (statserv_must_terminate())
98                 {
99                     for (p = *iochans; p; p = p->next)
100                         p->force_event = EVENT_TIMEOUT;
101                 }
102                 continue;
103             }
104             else
105             {
106                 /* Destroy the first member in the chain, and try again */
107                 association *assoc = (association *)iochan_getdata(*iochans);
108                 COMSTACK conn = assoc->client_link;
109
110                 cs_close(conn);
111                 destroy_association(assoc);
112                 iochan_destroy(*iochans);
113                 yaz_log(LOG_DEBUG, "error select, destroying iochan %p",
114                         *iochans);
115             }
116         }
117         for (p = *iochans; p; p = p->next)
118         {
119             int force_event = p->force_event;
120             time_t now = time(0);
121
122             p->force_event = 0;
123             if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
124                 force_event == EVENT_INPUT))
125             {
126                 p->last_event = now;
127                 (*p->fun)(p, EVENT_INPUT);
128             }
129             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
130                 force_event == EVENT_OUTPUT))
131             {
132                 p->last_event = now;
133                 (*p->fun)(p, EVENT_OUTPUT);
134             }
135             if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
136                 force_event == EVENT_EXCEPT))
137             {
138                 p->last_event = now;
139                 (*p->fun)(p, EVENT_EXCEPT);
140             }
141             if (!p->destroyed && ((p->max_idle && now - p->last_event >
142                 p->max_idle) || force_event == EVENT_TIMEOUT))
143             {
144                 p->last_event = now;
145                 (*p->fun)(p, EVENT_TIMEOUT);
146             }
147         }
148         for (p = *iochans; p; p = nextp)
149         {
150             nextp = p->next;
151
152             if (p->destroyed)
153             {
154                 IOCHAN tmp = p, pr;
155
156                 /* We need to inform the threadlist that this channel has been destroyed */
157                 statserv_remove(p);
158
159                 /* Now reset the pointers */
160                 if (p == *iochans)
161                     *iochans = p->next;
162                 else
163                 {
164                     for (pr = *iochans; pr; pr = pr->next)
165                         if (pr->next == p)
166                             break;
167                     assert(pr); /* grave error if it weren't there */
168                     pr->next = p->next;
169                 }
170                 if (nextp == p)
171                     nextp = p->next;
172                 xfree(tmp);
173             }
174         }
175     }
176     while (*iochans);
177     return 0;
178 }