TCPD libs only used in libyaz's LIBADD
[yaz-moved-to-github.git] / src / eventl.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file eventl.c
8  * \brief Implements event loop handling for GFS.
9  *
10  * This source implements the main event loop for the Generic Frontend
11  * Server.
12  */
13
14 #include <assert.h>
15 #include <errno.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #if HAVE_SYS_TYPES_H
21 #include <sys/types.h>
22 #endif
23 #if HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #endif
26
27 #include <yaz/poll.h>
28
29 #include <yaz/log.h>
30 #include <yaz/comstack.h>
31 #include <yaz/xmalloc.h>
32 #include <yaz/errno.h>
33 #include "eventl.h"
34 #include "session.h"
35 #include <yaz/statserv.h>
36
37 static int log_level=0;
38 static int log_level_initialized=0;
39
40 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, int chan_id)
41 {
42     IOCHAN new_iochan;
43
44     if (!log_level_initialized)
45     {
46         log_level=yaz_log_module_level("eventl");
47         log_level_initialized=1;
48     }
49
50     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
51         return 0;
52     new_iochan->destroyed = 0;
53     new_iochan->fd = fd;
54     new_iochan->flags = flags;
55     new_iochan->fun = cb;
56     new_iochan->force_event = 0;
57     new_iochan->last_event = new_iochan->max_idle = 0;
58     new_iochan->next = NULL;
59     new_iochan->chan_id = chan_id;
60     return new_iochan;
61 }
62
63
64 int iochan_is_alive(IOCHAN chan)
65 {
66     struct yaz_poll_fd fds;
67     int res;
68
69     fds.fd = chan->fd;
70     fds.input_mask = yaz_poll_read;
71     res = yaz_poll(&fds, 1, 0, 0);
72     if (res == 0)
73         return 1;
74     if (!ir_read(chan, EVENT_INPUT))
75         return 0;
76     return 1;
77 }
78
79 int iochan_event_loop(IOCHAN *iochans)
80 {
81     do /* loop as long as there are active associations to process */
82     {
83         IOCHAN p, nextp;
84         int i;
85         int tv_sec = 3600;
86         int no_fds = 0;
87         struct yaz_poll_fd *fds = 0;
88         int res;
89         time_t now = time(0);
90
91         if (statserv_must_terminate())
92         {
93             for (p = *iochans; p; p = p->next)
94                 p->force_event = EVENT_TIMEOUT;
95         }
96         for (p = *iochans; p; p = p->next)
97             no_fds++;
98         fds = (struct yaz_poll_fd *) xmalloc(no_fds * sizeof(*fds));
99         for (i = 0, p = *iochans; p; p = p->next, i++)
100         {
101             time_t w, ftime;
102             enum yaz_poll_mask input_mask = yaz_poll_none;
103             yaz_log(log_level, "fd=%d flags=%d force_event=%d",
104                     p->fd, p->flags, p->force_event);
105             if (p->force_event)
106                 tv_sec = 0;          /* polling select */
107             if (p->flags & EVENT_INPUT)
108                 yaz_poll_add(input_mask, yaz_poll_read);
109             if (p->flags & EVENT_OUTPUT)
110                 yaz_poll_add(input_mask, yaz_poll_write);
111             if (p->flags & EVENT_EXCEPT)
112                 yaz_poll_add(input_mask, yaz_poll_except);
113             if (p->max_idle && p->last_event)
114             {
115                 ftime = p->last_event + p->max_idle;
116                 if (ftime < now)
117                     w = p->max_idle;
118                 else
119                     w = ftime - now;
120                 /* tv_sec will be minimum wait.. */
121                 if (w < tv_sec)
122                     tv_sec = (int) w; /* can hold it because w < tv_sec */
123             }
124             fds[i].fd = p->fd;
125             fds[i].input_mask = input_mask;
126         }
127         res = yaz_poll(fds, no_fds, tv_sec, 0);
128         if (res < 0)
129         {
130             if (yaz_errno() == EINTR)
131             {
132                 if (statserv_must_terminate())
133                 {
134                     for (p = *iochans; p; p = p->next)
135                         p->force_event = EVENT_TIMEOUT;
136                 }
137                 xfree(fds);
138                 continue;
139             }
140             else
141             {
142                 yaz_log(YLOG_WARN|YLOG_ERRNO, "yaz_poll");
143                 xfree(fds);
144                 continue;
145             }
146         }
147         now = time(0);
148         for (i = 0, p = *iochans; p; p = p->next, i++)
149         {
150             int force_event = p->force_event;
151             enum yaz_poll_mask output_mask = fds[i].output_mask;
152
153             p->force_event = 0;
154             if (!p->destroyed && ((output_mask & yaz_poll_read) ||
155                                   force_event == EVENT_INPUT))
156             {
157                 p->last_event = now;
158                 (*p->fun)(p, EVENT_INPUT);
159             }
160             if (!p->destroyed && ((output_mask & yaz_poll_write) ||
161                                   force_event == EVENT_OUTPUT))
162             {
163                 p->last_event = now;
164                 (*p->fun)(p, EVENT_OUTPUT);
165             }
166             if (!p->destroyed && ((output_mask & yaz_poll_except) ||
167                 force_event == EVENT_EXCEPT))
168             {
169                 p->last_event = now;
170                 (*p->fun)(p, EVENT_EXCEPT);
171             }
172             if (!p->destroyed && ((p->max_idle && now - p->last_event >=
173                 p->max_idle) || force_event == EVENT_TIMEOUT))
174             {
175                 p->last_event = now;
176                 (*p->fun)(p, EVENT_TIMEOUT);
177             }
178         }
179         xfree(fds);
180         for (p = *iochans; p; p = nextp)
181         {
182             nextp = p->next;
183
184             if (p->destroyed)
185             {
186                 IOCHAN tmp = p, pr;
187
188                 /* We need to inform the threadlist that this channel has been destroyed */
189                 statserv_remove(p);
190
191                 /* Now reset the pointers */
192                 if (p == *iochans)
193                     *iochans = p->next;
194                 else
195                 {
196                     for (pr = *iochans; pr; pr = pr->next)
197                         if (pr->next == p)
198                             break;
199                     assert(pr); /* grave error if it weren't there */
200                     pr->next = p->next;
201                 }
202                 if (nextp == p)
203                     nextp = p->next;
204                 xfree(tmp);
205             }
206         }
207     }
208     while (*iochans);
209     return 0;
210 }
211 /*
212  * Local variables:
213  * c-basic-offset: 4
214  * c-file-style: "Stroustrup"
215  * indent-tabs-mode: nil
216  * End:
217  * vim: shiftwidth=4 tabstop=8 expandtab
218  */
219