2ed53c6491ded373bc326eae0a194df53a44e8a5
[yaz-moved-to-github.git] / server / eventl.c
1 /*
2  * Copyright (c) 1995-2001, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: eventl.c,v 1.33 2002-11-26 13:15:42 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             fprintf (stderr, "must terminate 1\n");
66
67             for (p = *iochans; p; p = p->next)
68                 p->force_event = EVENT_TIMEOUT;
69         }
70         FD_ZERO(&in);
71         FD_ZERO(&out);
72         FD_ZERO(&except);
73         timeout = &to; /* hang on select */
74         to.tv_sec = 5*60;
75         to.tv_usec = 0;
76         max = 0;
77         for (p = *iochans; p; p = p->next)
78         {
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         res = YAZ_EV_SELECT(max + 1, &in, &out, &except, timeout);
91         if (res < 0)
92         {
93             if (yaz_errno() == EINTR)
94             {
95                 if (statserv_must_terminate())
96                 {
97                     for (p = *iochans; p; p = p->next)
98                         p->force_event = EVENT_TIMEOUT;
99                 }
100                 continue;
101             }
102             else
103             {
104                 /* Destroy the first member in the chain, and try again */
105                 association *assoc = (association *)iochan_getdata(*iochans);
106                 COMSTACK conn = assoc->client_link;
107
108                 cs_close(conn);
109                 destroy_association(assoc);
110                 iochan_destroy(*iochans);
111                 yaz_log(LOG_DEBUG, "error select, destroying iochan %p",
112                         *iochans);
113             }
114         }
115         for (p = *iochans; p; p = p->next)
116         {
117             int force_event = p->force_event;
118             time_t now = time(0);
119
120             p->force_event = 0;
121             if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
122                 force_event == EVENT_INPUT))
123             {
124                 p->last_event = now;
125                 (*p->fun)(p, EVENT_INPUT);
126             }
127             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
128                 force_event == EVENT_OUTPUT))
129             {
130                 p->last_event = now;
131                 (*p->fun)(p, EVENT_OUTPUT);
132             }
133             if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
134                 force_event == EVENT_EXCEPT))
135             {
136                 p->last_event = now;
137                 (*p->fun)(p, EVENT_EXCEPT);
138             }
139             if (!p->destroyed && ((p->max_idle && now - p->last_event >
140                 p->max_idle) || force_event == EVENT_TIMEOUT))
141             {
142                 p->last_event = now;
143                 (*p->fun)(p, EVENT_TIMEOUT);
144             }
145         }
146         for (p = *iochans; p; p = nextp)
147         {
148             nextp = p->next;
149
150             if (p->destroyed)
151             {
152                 IOCHAN tmp = p, pr;
153
154                 /* We need to inform the threadlist that this channel has been destroyed */
155                 statserv_remove(p);
156
157                 /* Now reset the pointers */
158                 if (p == *iochans)
159                     *iochans = p->next;
160                 else
161                 {
162                     for (pr = *iochans; pr; pr = pr->next)
163                         if (pr->next == p)
164                             break;
165                     assert(pr); /* grave error if it weren't there */
166                     pr->next = p->next;
167                 }
168                 if (nextp == p)
169                     nextp = p->next;
170                 xfree(tmp);
171             }
172         }
173     }
174     while (*iochans);
175     return 0;
176 }