ce38bd8ae78900d986874f990f03c98c4010e39c
[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.34 2002-11-26 16:56:21 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             for (p = *iochans; p; p = p->next)
66                 p->force_event = EVENT_TIMEOUT;
67         }
68         FD_ZERO(&in);
69         FD_ZERO(&out);
70         FD_ZERO(&except);
71         timeout = &to; /* hang on select */
72         to.tv_sec = 5*60;
73         to.tv_usec = 0;
74         max = 0;
75         for (p = *iochans; p; p = p->next)
76         {
77             if (p->force_event)
78                 timeout = &nullto;        /* polling select */
79             if (p->flags & EVENT_INPUT)
80                 FD_SET(p->fd, &in);
81             if (p->flags & EVENT_OUTPUT)
82                 FD_SET(p->fd, &out);
83             if (p->flags & EVENT_EXCEPT)
84                 FD_SET(p->fd, &except);
85             if (p->fd > max)
86                 max = p->fd;
87         }
88         res = YAZ_EV_SELECT(max + 1, &in, &out, &except, timeout);
89         if (res < 0)
90         {
91             if (yaz_errno() == EINTR)
92             {
93                 if (statserv_must_terminate())
94                 {
95                     for (p = *iochans; p; p = p->next)
96                         p->force_event = EVENT_TIMEOUT;
97                 }
98                 continue;
99             }
100             else
101             {
102                 /* Destroy the first member in the chain, and try again */
103                 association *assoc = (association *)iochan_getdata(*iochans);
104                 COMSTACK conn = assoc->client_link;
105
106                 cs_close(conn);
107                 destroy_association(assoc);
108                 iochan_destroy(*iochans);
109                 yaz_log(LOG_DEBUG, "error select, destroying iochan %p",
110                         *iochans);
111             }
112         }
113         for (p = *iochans; p; p = p->next)
114         {
115             int force_event = p->force_event;
116             time_t now = time(0);
117
118             p->force_event = 0;
119             if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
120                 force_event == EVENT_INPUT))
121             {
122                 p->last_event = now;
123                 (*p->fun)(p, EVENT_INPUT);
124             }
125             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
126                 force_event == EVENT_OUTPUT))
127             {
128                 p->last_event = now;
129                 (*p->fun)(p, EVENT_OUTPUT);
130             }
131             if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
132                 force_event == EVENT_EXCEPT))
133             {
134                 p->last_event = now;
135                 (*p->fun)(p, EVENT_EXCEPT);
136             }
137             if (!p->destroyed && ((p->max_idle && now - p->last_event >
138                 p->max_idle) || force_event == EVENT_TIMEOUT))
139             {
140                 p->last_event = now;
141                 (*p->fun)(p, EVENT_TIMEOUT);
142             }
143         }
144         for (p = *iochans; p; p = nextp)
145         {
146             nextp = p->next;
147
148             if (p->destroyed)
149             {
150                 IOCHAN tmp = p, pr;
151
152                 /* We need to inform the threadlist that this channel has been destroyed */
153                 statserv_remove(p);
154
155                 /* Now reset the pointers */
156                 if (p == *iochans)
157                     *iochans = p->next;
158                 else
159                 {
160                     for (pr = *iochans; pr; pr = pr->next)
161                         if (pr->next == p)
162                             break;
163                     assert(pr); /* grave error if it weren't there */
164                     pr->next = p->next;
165                 }
166                 if (nextp == p)
167                     nextp = p->next;
168                 xfree(tmp);
169             }
170         }
171     }
172     while (*iochans);
173     return 0;
174 }