Enable use of yaz_poll always in event_loop.
[yaz-moved-to-github.git] / src / eventl.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: eventl.c,v 1.13 2007-11-09 18:49:19 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
29 #include <yaz/poll.h>
30
31 #include <yaz/log.h>
32 #include <yaz/comstack.h>
33 #include <yaz/xmalloc.h>
34 #include "eventl.h"
35 #include "session.h"
36 #include <yaz/statserv.h>
37
38 static int log_level=0;
39 static int log_level_initialized=0;
40
41 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, int chan_id)
42 {
43     IOCHAN new_iochan;
44
45     if (!log_level_initialized)
46     {
47         log_level=yaz_log_module_level("eventl");
48         log_level_initialized=1;
49     }
50
51     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
52         return 0;
53     new_iochan->destroyed = 0;
54     new_iochan->fd = fd;
55     new_iochan->flags = flags;
56     new_iochan->fun = cb;
57     new_iochan->force_event = 0;
58     new_iochan->last_event = new_iochan->max_idle = 0;
59     new_iochan->next = NULL;
60     new_iochan->chan_id = chan_id;
61     return new_iochan;
62 }
63
64
65 int iochan_is_alive(IOCHAN chan)
66 {
67     static struct timeval to;
68     fd_set in, out, except;
69     int res, max;
70
71     to.tv_sec = 0;
72     to.tv_usec = 0;
73
74     FD_ZERO(&in);
75     FD_ZERO(&out);
76     FD_ZERO(&except);
77
78     FD_SET(chan->fd, &in);
79
80     max = chan->fd + 1;
81
82     res = YAZ_EV_SELECT(max + 1, &in, 0, 0, &to);
83     if (res == 0)
84         return 1;
85     if (!ir_read(chan, EVENT_INPUT))
86         return 0;
87     return 1;
88 }
89
90 int event_loop(IOCHAN *iochans)
91 {
92     do /* loop as long as there are active associations to process */
93     {
94         IOCHAN p, nextp;
95         int i;
96         int tv_sec = 3600;
97         int no_fds = 0;
98         struct yaz_poll_fd *fds = 0;
99         int res;
100         time_t now = time(0);
101
102         if (statserv_must_terminate())
103         {
104             for (p = *iochans; p; p = p->next)
105                 p->force_event = EVENT_TIMEOUT;
106         }
107         for (p = *iochans; p; p = p->next)
108             no_fds++;
109         fds = xmalloc(no_fds * sizeof(*fds));
110         for (i = 0, p = *iochans; p; p = p->next, i++)
111         {
112             time_t w, ftime;
113             enum yaz_poll_mask input_mask = 0;
114             yaz_log(log_level, "fd=%d flags=%d force_event=%d",
115                     p->fd, p->flags, p->force_event);
116             if (p->force_event)
117                 tv_sec = 0;          /* polling select */
118             if (p->flags & EVENT_INPUT)
119                 input_mask += yaz_poll_read;
120             if (p->flags & EVENT_OUTPUT)
121                 input_mask += yaz_poll_write;
122             if (p->flags & EVENT_EXCEPT)
123                 input_mask += yaz_poll_except;
124             if (p->max_idle && p->last_event)
125             {
126                 ftime = p->last_event + p->max_idle;
127                 if (ftime < now)
128                     w = p->max_idle;
129                 else
130                     w = ftime - now;
131                 if (w < tv_sec)
132                     tv_sec = w;
133             }
134             fds[i].fd = p->fd;
135             fds[i].input_mask = input_mask;
136         }
137         res = yaz_poll(fds, no_fds, tv_sec);
138         if (res < 0)
139         {
140             if (yaz_errno() == EINTR)
141             {
142                 if (statserv_must_terminate())
143                 {
144                     for (p = *iochans; p; p = p->next)
145                         p->force_event = EVENT_TIMEOUT;
146                 }
147                 xfree(fds);
148                 continue;
149             }
150             else
151             {
152                 /* Destroy the first member in the chain, and try again */
153                 association *assoc = (association *)iochan_getdata(*iochans);
154                 COMSTACK conn = assoc->client_link;
155
156                 cs_close(conn);
157                 destroy_association(assoc);
158                 iochan_destroy(*iochans);
159                 yaz_log(log_level, "error select, destroying iochan %p",
160                         *iochans);
161             }
162         }
163         now = time(0);
164         for (i = 0, p = *iochans; p; p = p->next, i++)
165         {
166             int force_event = p->force_event;
167             enum yaz_poll_mask output_mask = fds[i].output_mask;
168
169             p->force_event = 0;
170             if (!p->destroyed && ((output_mask & yaz_poll_read) ||
171                                   force_event == EVENT_INPUT))
172             {
173                 p->last_event = now;
174                 (*p->fun)(p, EVENT_INPUT);
175             }
176             if (!p->destroyed && ((output_mask & yaz_poll_write) ||
177                                   force_event == EVENT_OUTPUT))
178             {
179                 p->last_event = now;
180                 (*p->fun)(p, EVENT_OUTPUT);
181             }
182             if (!p->destroyed && ((output_mask & yaz_poll_except) ||
183                 force_event == EVENT_EXCEPT))
184             {
185                 p->last_event = now;
186                 (*p->fun)(p, EVENT_EXCEPT);
187             }
188             if (!p->destroyed && ((p->max_idle && now - p->last_event >=
189                 p->max_idle) || force_event == EVENT_TIMEOUT))
190             {
191                 p->last_event = now;
192                 (*p->fun)(p, EVENT_TIMEOUT);
193             }
194         }
195         xfree(fds);
196         for (p = *iochans; p; p = nextp)
197         {
198             nextp = p->next;
199
200             if (p->destroyed)
201             {
202                 IOCHAN tmp = p, pr;
203
204                 /* We need to inform the threadlist that this channel has been destroyed */
205                 statserv_remove(p);
206
207                 /* Now reset the pointers */
208                 if (p == *iochans)
209                     *iochans = p->next;
210                 else
211                 {
212                     for (pr = *iochans; pr; pr = pr->next)
213                         if (pr->next == p)
214                             break;
215                     assert(pr); /* grave error if it weren't there */
216                     pr->next = p->next;
217                 }
218                 if (nextp == p)
219                     nextp = p->next;
220                 xfree(tmp);
221             }
222         }
223     }
224     while (*iochans);
225     return 0;
226 }
227 /*
228  * Local variables:
229  * c-basic-offset: 4
230  * indent-tabs-mode: nil
231  * End:
232  * vim: shiftwidth=4 tabstop=8 expandtab
233  */
234