Fixed bug #740: Handle SRU records referring to xmlns outside recordData.
[yaz-moved-to-github.git] / src / eventl.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: eventl.c,v 1.9 2005-06-25 15:46:04 adam Exp $
6  */
7
8 /**
9  * \file eventl.c
10  * \brief Implements event loop handling for GFS.
11  *
12  * This source implements the main event loop for the Generic Frontend
13  * Server. It uses select(2).
14  */
15
16 #include <assert.h>
17 #include <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #if HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25 #if HAVE_SYS_TIME_H
26 #include <sys/time.h>
27 #endif
28 #ifdef WIN32
29 #include <winsock.h>
30 #endif
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #if HAVE_SYS_SELECT_H
35 #include <sys/select.h>
36 #endif
37
38 #include <yaz/yconfig.h>
39 #include <yaz/log.h>
40 #include <yaz/comstack.h>
41 #include <yaz/xmalloc.h>
42 #include "eventl.h"
43 #include "session.h"
44 #include <yaz/statserv.h>
45
46 #if YAZ_GNU_THREADS
47 #include <pth.h>
48 #define YAZ_EV_SELECT pth_select
49 #endif
50
51 #ifndef YAZ_EV_SELECT
52 #define YAZ_EV_SELECT select
53 #endif
54
55 static int log_level=0;
56 static int log_level_initialized=0;
57
58 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, int chan_id)
59 {
60     IOCHAN new_iochan;
61
62     if (!log_level_initialized)
63     {
64         log_level=yaz_log_module_level("eventl");
65         log_level_initialized=1;
66     }
67
68     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
69         return 0;
70     new_iochan->destroyed = 0;
71     new_iochan->fd = fd;
72     new_iochan->flags = flags;
73     new_iochan->fun = cb;
74     new_iochan->force_event = 0;
75     new_iochan->last_event = new_iochan->max_idle = 0;
76     new_iochan->next = NULL;
77     new_iochan->chan_id = chan_id;
78     return new_iochan;
79 }
80
81 int event_loop(IOCHAN *iochans)
82 {
83     do /* loop as long as there are active associations to process */
84     {
85         IOCHAN p, nextp;
86         fd_set in, out, except;
87         int res, max;
88         static struct timeval to;
89         time_t now = time(0);
90
91         if (statserv_must_terminate())
92         {
93             for (p = *iochans; p; p = p->next)
94                 p->force_event = EVENT_TIMEOUT;
95         }
96         FD_ZERO(&in);
97         FD_ZERO(&out);
98         FD_ZERO(&except);
99         to.tv_sec = 3600;
100         to.tv_usec = 0;
101         max = 0;
102         for (p = *iochans; p; p = p->next)
103         {
104             time_t w, ftime;
105             yaz_log(log_level, "fd=%d flags=%d force_event=%d",
106                     p->fd, p->flags, p->force_event);
107             if (p->force_event)
108                 to.tv_sec = 0;          /* polling select */
109             if (p->flags & EVENT_INPUT)
110                 FD_SET(p->fd, &in);
111             if (p->flags & EVENT_OUTPUT)
112                 FD_SET(p->fd, &out);
113             if (p->flags & EVENT_EXCEPT)
114                 FD_SET(p->fd, &except);
115             if (p->fd > max)
116                 max = p->fd;
117             if (p->max_idle && p->last_event)
118             {
119                 ftime = p->last_event + p->max_idle;
120                 if (ftime < now)
121                     w = p->max_idle;
122                 else
123                     w = ftime - now;
124                 if (w < to.tv_sec)
125                     to.tv_sec = w;
126             }
127         }
128         yaz_log(log_level, "select start %ld", (long) to.tv_sec);
129         res = YAZ_EV_SELECT(max + 1, &in, &out, &except, &to);
130         yaz_log(log_level, "select end");
131         if (res < 0)
132         {
133             if (yaz_errno() == EINTR)
134             {
135                 if (statserv_must_terminate())
136                 {
137                     for (p = *iochans; p; p = p->next)
138                         p->force_event = EVENT_TIMEOUT;
139                 }
140                 continue;
141             }
142             else
143             {
144                 /* Destroy the first member in the chain, and try again */
145                 association *assoc = (association *)iochan_getdata(*iochans);
146                 COMSTACK conn = assoc->client_link;
147
148                 cs_close(conn);
149                 destroy_association(assoc);
150                 iochan_destroy(*iochans);
151                 yaz_log(log_level, "error select, destroying iochan %p",
152                         *iochans);
153             }
154         }
155         now = time(0);
156         for (p = *iochans; p; p = p->next)
157         {
158             int force_event = p->force_event;
159
160             p->force_event = 0;
161             if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
162                 force_event == EVENT_INPUT))
163             {
164                 p->last_event = now;
165                 (*p->fun)(p, EVENT_INPUT);
166             }
167             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
168                 force_event == EVENT_OUTPUT))
169             {
170                 p->last_event = now;
171                 (*p->fun)(p, EVENT_OUTPUT);
172             }
173             if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
174                 force_event == EVENT_EXCEPT))
175             {
176                 p->last_event = now;
177                 (*p->fun)(p, EVENT_EXCEPT);
178             }
179             if (!p->destroyed && ((p->max_idle && now - p->last_event >=
180                 p->max_idle) || force_event == EVENT_TIMEOUT))
181             {
182                 p->last_event = now;
183                 (*p->fun)(p, EVENT_TIMEOUT);
184             }
185         }
186         for (p = *iochans; p; p = nextp)
187         {
188             nextp = p->next;
189
190             if (p->destroyed)
191             {
192                 IOCHAN tmp = p, pr;
193
194                 /* We need to inform the threadlist that this channel has been destroyed */
195                 statserv_remove(p);
196
197                 /* Now reset the pointers */
198                 if (p == *iochans)
199                     *iochans = p->next;
200                 else
201                 {
202                     for (pr = *iochans; pr; pr = pr->next)
203                         if (pr->next == p)
204                             break;
205                     assert(pr); /* grave error if it weren't there */
206                     pr->next = p->next;
207                 }
208                 if (nextp == p)
209                     nextp = p->next;
210                 xfree(tmp);
211             }
212         }
213     }
214     while (*iochans);
215     return 0;
216 }
217 /*
218  * Local variables:
219  * c-basic-offset: 4
220  * indent-tabs-mode: nil
221  * End:
222  * vim: shiftwidth=4 tabstop=8 expandtab
223  */
224