Always terminate ELinks when using -remote.
[elinks.git] / src / main / select.c
blobbcaa190afde947cd00584f1c508d2e4fff8646b7
1 /* File descriptors managment and switching */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <errno.h>
8 #include <signal.h>
9 #include <string.h> /* FreeBSD FD_ZERO() macro calls bzero() */
10 #ifdef HAVE_SYS_SIGNAL_H
11 #include <sys/signal.h>
12 #endif
13 #include <sys/types.h>
14 #ifdef HAVE_SYS_WAIT_H
15 #include <sys/wait.h>
16 #endif
17 #ifdef HAVE_UNISTD_H
18 #include <unistd.h>
19 #endif
21 /* This must be here, thanks to BSD. */
22 #ifdef HAVE_INTTYPES_H
23 #include <inttypes.h> /* OMG */
24 #endif
25 #ifdef HAVE_SYS_SELECT_H
26 #include <sys/select.h>
27 #endif
29 #include "elinks.h"
31 #include "intl/gettext/libintl.h"
32 #include "main/main.h"
33 #include "main/select.h"
34 #include "main/timer.h"
35 #include "osdep/signals.h"
36 #include "terminal/terminal.h"
37 #include "util/error.h"
38 #include "util/memory.h"
39 #include "util/time.h"
42 #ifndef FD_SETSIZE
43 #define FD_SETSIZE 1024
44 #endif
46 struct thread {
47 select_handler_T read_func;
48 select_handler_T write_func;
49 select_handler_T error_func;
50 void *data;
53 static struct thread threads[FD_SETSIZE];
55 static fd_set w_read;
56 static fd_set w_write;
57 static fd_set w_error;
59 static fd_set x_read;
60 static fd_set x_write;
61 static fd_set x_error;
63 static int w_max;
65 int
66 get_file_handles_count(void)
68 int i = 0, j;
70 for (j = 0; j < FD_SETSIZE; j++)
71 if (threads[j].read_func
72 || threads[j].write_func
73 || threads[j].error_func)
74 i++;
75 return i;
78 struct bottom_half {
79 LIST_HEAD(struct bottom_half);
81 select_handler_T fn;
82 void *data;
85 static INIT_LIST_HEAD(bottom_halves);
87 int
88 register_bottom_half_do(select_handler_T fn, void *data)
90 struct bottom_half *bh;
92 foreach (bh, bottom_halves)
93 if (bh->fn == fn && bh->data == data)
94 return 0;
96 bh = mem_alloc(sizeof(*bh));
97 if (!bh) return -1;
98 bh->fn = fn;
99 bh->data = data;
100 add_to_list(bottom_halves, bh);
102 return 0;
105 void
106 check_bottom_halves(void)
108 while (!list_empty(bottom_halves)) {
109 struct bottom_half *bh = bottom_halves.prev;
110 select_handler_T fn = bh->fn;
111 void *data = bh->data;
113 del_from_list(bh);
114 mem_free(bh);
115 fn(data);
119 select_handler_T
120 get_handler(int fd, enum select_handler_type tp)
122 #ifndef CONFIG_OS_WIN32
123 assertm(fd >= 0 && fd < FD_SETSIZE,
124 "get_handler: handle %d >= FD_SETSIZE %d",
125 fd, FD_SETSIZE);
126 if_assert_failed return NULL;
127 #endif
128 switch (tp) {
129 case SELECT_HANDLER_READ: return threads[fd].read_func;
130 case SELECT_HANDLER_WRITE: return threads[fd].write_func;
131 case SELECT_HANDLER_ERROR: return threads[fd].error_func;
132 case SELECT_HANDLER_DATA: return threads[fd].data;
135 INTERNAL("get_handler: bad type %d", tp);
136 return NULL;
139 void
140 set_handlers(int fd, select_handler_T read_func, select_handler_T write_func,
141 select_handler_T error_func, void *data)
143 #ifndef CONFIG_OS_WIN32
144 assertm(fd >= 0 && fd < FD_SETSIZE,
145 "set_handlers: handle %d >= FD_SETSIZE %d",
146 fd, FD_SETSIZE);
147 if_assert_failed return;
148 #endif
149 threads[fd].read_func = read_func;
150 threads[fd].write_func = write_func;
151 threads[fd].error_func = error_func;
152 threads[fd].data = data;
154 if (read_func) {
155 FD_SET(fd, &w_read);
156 } else {
157 FD_CLR(fd, &w_read);
158 FD_CLR(fd, &x_read);
161 if (write_func) {
162 FD_SET(fd, &w_write);
163 } else {
164 FD_CLR(fd, &w_write);
165 FD_CLR(fd, &x_write);
168 if (error_func) {
169 FD_SET(fd, &w_error);
170 } else {
171 FD_CLR(fd, &w_error);
172 FD_CLR(fd, &x_error);
175 if (read_func || write_func || error_func) {
176 if (fd >= w_max) w_max = fd + 1;
177 } else if (fd == w_max - 1) {
178 int i;
180 for (i = fd - 1; i >= 0; i--)
181 if (FD_ISSET(i, &w_read)
182 || FD_ISSET(i, &w_write)
183 || FD_ISSET(i, &w_error))
184 break;
185 w_max = i + 1;
189 void
190 select_loop(void (*init)(void))
192 timeval_T last_time;
193 int select_errors = 0;
195 clear_signal_mask_and_handlers();
196 FD_ZERO(&w_read);
197 FD_ZERO(&w_write);
198 FD_ZERO(&w_error);
199 w_max = 0;
200 timeval_now(&last_time);
201 #ifdef SIGPIPE
202 signal(SIGPIPE, SIG_IGN);
203 #endif
204 init();
205 check_bottom_halves();
207 while (!program.terminate) {
208 struct timeval *timeout = NULL;
209 int n, i, has_timer;
210 timeval_T t;
212 check_signals();
213 check_timers(&last_time);
214 redraw_all_terminals();
216 memcpy(&x_read, &w_read, sizeof(fd_set));
217 memcpy(&x_write, &w_write, sizeof(fd_set));
218 memcpy(&x_error, &w_error, sizeof(fd_set));
220 if (program.terminate) break;
222 has_timer = get_next_timer_time(&t);
223 if (!w_max && !has_timer) break;
224 critical_section = 1;
226 if (check_signals()) {
227 critical_section = 0;
228 continue;
230 #if 0
232 int i;
234 printf("\nR:");
235 for (i = 0; i < 256; i++)
236 if (FD_ISSET(i, &x_read)) printf("%d,", i);
237 printf("\nW:");
238 for (i = 0; i < 256; i++)
239 if (FD_ISSET(i, &x_write)) printf("%d,", i);
240 printf("\nE:");
241 for (i = 0; i < 256; i++)
242 if (FD_ISSET(i, &x_error)) printf("%d,", i);
243 fflush(stdout);
245 #endif
246 if (has_timer) {
247 /* Be sure timeout is not negative. */
248 timeval_limit_to_zero_or_one(&t);
249 timeout = (struct timeval *) &t;
252 n = select(w_max, &x_read, &x_write, &x_error, timeout);
253 if (n < 0) {
254 critical_section = 0;
255 uninstall_alarm();
256 if (errno != EINTR) {
257 ERROR(gettext("The call to %s failed: %d (%s)"),
258 "select()", errno, (unsigned char *) strerror(errno));
259 if (++select_errors > 10) /* Infinite loop prevention. */
260 INTERNAL(gettext("%d select() failures."),
261 select_errors);
263 continue;
266 select_errors = 0;
267 critical_section = 0;
268 uninstall_alarm();
269 check_signals();
270 /*printf("sel: %d\n", n);*/
271 check_timers(&last_time);
273 i = -1;
274 while (n > 0 && ++i < w_max) {
275 int k = 0;
277 #if 0
278 printf("C %d : %d,%d,%d\n", i, FD_ISSET(i, &w_read),
279 FD_ISSET(i, &w_write), FD_ISSET(i, &w_error));
280 printf("A %d : %d,%d,%d\n", i, FD_ISSET(i, &x_read),
281 FD_ISSET(i, &x_write), FD_ISSET(i, &x_error));
282 #endif
283 if (FD_ISSET(i, &x_read)) {
284 if (threads[i].read_func) {
285 threads[i].read_func(threads[i].data);
286 check_bottom_halves();
288 k = 1;
291 if (FD_ISSET(i, &x_write)) {
292 if (threads[i].write_func) {
293 threads[i].write_func(threads[i].data);
294 check_bottom_halves();
296 k = 1;
299 if (FD_ISSET(i, &x_error)) {
300 if (threads[i].error_func) {
301 threads[i].error_func(threads[i].data);
302 check_bottom_halves();
304 k = 1;
307 n -= k;
312 static int
313 can_read_or_write(int fd, int write)
315 struct timeval tv = {0, 0};
316 fd_set fds;
317 fd_set *rfds = NULL;
318 fd_set *wfds = NULL;
320 FD_ZERO(&fds);
321 FD_SET(fd, &fds);
323 if (write)
324 wfds = &fds;
325 else
326 rfds = &fds;
328 return select(fd + 1, rfds, wfds, NULL, &tv);
332 can_read(int fd)
334 return can_read_or_write(fd, 0);
338 can_write(int fd)
340 return can_read_or_write(fd, 1);