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