Change timeout parameter for yaz_poll.
[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.15 2007-11-09 22:08:14 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     struct yaz_poll_fd fds;
68     int res;
69
70     fds.fd = chan->fd;
71     fds.input_mask = yaz_poll_read;
72     res = yaz_poll(&fds, 1, 0, 0);
73     if (res == 0)
74         return 1;
75     if (!ir_read(chan, EVENT_INPUT))
76         return 0;
77     return 1;
78 }
79
80 int event_loop(IOCHAN *iochans)
81 {
82     do /* loop as long as there are active associations to process */
83     {
84         IOCHAN p, nextp;
85         int i;
86         int tv_sec = 3600;
87         int no_fds = 0;
88         struct yaz_poll_fd *fds = 0;
89         int res;
90         time_t now = time(0);
91
92         if (statserv_must_terminate())
93         {
94             for (p = *iochans; p; p = p->next)
95                 p->force_event = EVENT_TIMEOUT;
96         }
97         for (p = *iochans; p; p = p->next)
98             no_fds++;
99         fds = xmalloc(no_fds * sizeof(*fds));
100         for (i = 0, p = *iochans; p; p = p->next, i++)
101         {
102             time_t w, ftime;
103             enum yaz_poll_mask input_mask = 0;
104             yaz_log(log_level, "fd=%d flags=%d force_event=%d",
105                     p->fd, p->flags, p->force_event);
106             if (p->force_event)
107                 tv_sec = 0;          /* polling select */
108             if (p->flags & EVENT_INPUT)
109                 input_mask += yaz_poll_read;
110             if (p->flags & EVENT_OUTPUT)
111                 input_mask += yaz_poll_write;
112             if (p->flags & EVENT_EXCEPT)
113                 input_mask += yaz_poll_except;
114             if (p->max_idle && p->last_event)
115             {
116                 ftime = p->last_event + p->max_idle;
117                 if (ftime < now)
118                     w = p->max_idle;
119                 else
120                     w = ftime - now;
121                 if (w < tv_sec)
122                     tv_sec = w;
123             }
124             fds[i].fd = p->fd;
125             fds[i].input_mask = input_mask;
126         }
127         res = yaz_poll(fds, no_fds, tv_sec, 0);
128         if (res < 0)
129         {
130             if (yaz_errno() == EINTR)
131             {
132                 if (statserv_must_terminate())
133                 {
134                     for (p = *iochans; p; p = p->next)
135                         p->force_event = EVENT_TIMEOUT;
136                 }
137                 xfree(fds);
138                 continue;
139             }
140             else
141             {
142                 /* Destroy the first member in the chain, and try again */
143                 association *assoc = (association *)iochan_getdata(*iochans);
144                 COMSTACK conn = assoc->client_link;
145
146                 cs_close(conn);
147                 destroy_association(assoc);
148                 iochan_destroy(*iochans);
149                 yaz_log(log_level, "error select, destroying iochan %p",
150                         *iochans);
151             }
152         }
153         now = time(0);
154         for (i = 0, p = *iochans; p; p = p->next, i++)
155         {
156             int force_event = p->force_event;
157             enum yaz_poll_mask output_mask = fds[i].output_mask;
158
159             p->force_event = 0;
160             if (!p->destroyed && ((output_mask & yaz_poll_read) ||
161                                   force_event == EVENT_INPUT))
162             {
163                 p->last_event = now;
164                 (*p->fun)(p, EVENT_INPUT);
165             }
166             if (!p->destroyed && ((output_mask & yaz_poll_write) ||
167                                   force_event == EVENT_OUTPUT))
168             {
169                 p->last_event = now;
170                 (*p->fun)(p, EVENT_OUTPUT);
171             }
172             if (!p->destroyed && ((output_mask & yaz_poll_except) ||
173                 force_event == EVENT_EXCEPT))
174             {
175                 p->last_event = now;
176                 (*p->fun)(p, EVENT_EXCEPT);
177             }
178             if (!p->destroyed && ((p->max_idle && now - p->last_event >=
179                 p->max_idle) || force_event == EVENT_TIMEOUT))
180             {
181                 p->last_event = now;
182                 (*p->fun)(p, EVENT_TIMEOUT);
183             }
184         }
185         xfree(fds);
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