Fix MAC compilation
[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.6 2005-01-17 12:53: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)
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     return new_iochan;
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         fd_set in, out, except;
86         int res, max;
87         static struct timeval to;
88         time_t now = time(0);
89
90         if (statserv_must_terminate())
91         {
92             for (p = *iochans; p; p = p->next)
93                 p->force_event = EVENT_TIMEOUT;
94         }
95         FD_ZERO(&in);
96         FD_ZERO(&out);
97         FD_ZERO(&except);
98         to.tv_sec = 3600;
99         to.tv_usec = 0;
100         max = 0;
101         for (p = *iochans; p; p = p->next)
102         {
103             time_t w, ftime;
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                 to.tv_sec = 0;          /* polling select */
108             if (p->flags & EVENT_INPUT)
109                 FD_SET(p->fd, &in);
110             if (p->flags & EVENT_OUTPUT)
111                 FD_SET(p->fd, &out);
112             if (p->flags & EVENT_EXCEPT)
113                 FD_SET(p->fd, &except);
114             if (p->fd > max)
115                 max = p->fd;
116             if (p->max_idle && p->last_event)
117             {
118                 ftime = p->last_event + p->max_idle;
119                 if (ftime < now)
120                     w = p->max_idle;
121                 else
122                     w = ftime - now;
123                 if (w < to.tv_sec)
124                     to.tv_sec = w;
125             }
126         }
127         yaz_log(log_level, "select start %ld", (long) to.tv_sec);
128         res = YAZ_EV_SELECT(max + 1, &in, &out, &except, &to);
129         yaz_log(log_level, "select end");
130         if (res < 0)
131         {
132             if (yaz_errno() == EINTR)
133             {
134                 if (statserv_must_terminate())
135                 {
136                     for (p = *iochans; p; p = p->next)
137                         p->force_event = EVENT_TIMEOUT;
138                 }
139                 continue;
140             }
141             else
142             {
143                 /* Destroy the first member in the chain, and try again */
144                 association *assoc = (association *)iochan_getdata(*iochans);
145                 COMSTACK conn = assoc->client_link;
146
147                 cs_close(conn);
148                 destroy_association(assoc);
149                 iochan_destroy(*iochans);
150                 yaz_log(log_level, "error select, destroying iochan %p",
151                         *iochans);
152             }
153         }
154         now = time(0);
155         for (p = *iochans; p; p = p->next)
156         {
157             int force_event = p->force_event;
158
159             p->force_event = 0;
160             if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
161                 force_event == EVENT_INPUT))
162             {
163                 p->last_event = now;
164                 (*p->fun)(p, EVENT_INPUT);
165             }
166             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
167                 force_event == EVENT_OUTPUT))
168             {
169                 p->last_event = now;
170                 (*p->fun)(p, EVENT_OUTPUT);
171             }
172             if (!p->destroyed && (FD_ISSET(p->fd, &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         for (p = *iochans; p; p = nextp)
186         {
187             nextp = p->next;
188
189             if (p->destroyed)
190             {
191                 IOCHAN tmp = p, pr;
192
193                 /* We need to inform the threadlist that this channel has been destroyed */
194                 statserv_remove(p);
195
196                 /* Now reset the pointers */
197                 if (p == *iochans)
198                     *iochans = p->next;
199                 else
200                 {
201                     for (pr = *iochans; pr; pr = pr->next)
202                         if (pr->next == p)
203                             break;
204                     assert(pr); /* grave error if it weren't there */
205                     pr->next = p->next;
206                 }
207                 if (nextp == p)
208                     nextp = p->next;
209                 xfree(tmp);
210             }
211         }
212     }
213     while (*iochans);
214     return 0;
215 }