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