1 | #include "owl.h" |
---|
2 | |
---|
3 | static const char fileIdent[] = "$Id: select.c 894 2008-01-17 07:13:44Z asedeno $"; |
---|
4 | |
---|
5 | static int dispatch_active = 0; |
---|
6 | |
---|
7 | int _owl_select_timer_cmp(owl_timer *t1, owl_timer *t2) { |
---|
8 | return t1->time - t2->time; |
---|
9 | } |
---|
10 | |
---|
11 | int _owl_select_timer_eq(owl_timer *t1, owl_timer *t2) { |
---|
12 | return t1 == t2; |
---|
13 | } |
---|
14 | |
---|
15 | owl_timer *owl_select_add_timer(int after, int interval, void (*cb)(owl_timer *, void *), void (*destroy)(owl_timer*), void *data) |
---|
16 | { |
---|
17 | owl_timer *t = owl_malloc(sizeof(owl_timer)); |
---|
18 | GList **timers = owl_global_get_timerlist(&g); |
---|
19 | |
---|
20 | t->time = time(NULL) + after; |
---|
21 | t->interval = interval; |
---|
22 | t->callback = cb; |
---|
23 | t->destroy = destroy; |
---|
24 | t->data = data; |
---|
25 | |
---|
26 | *timers = g_list_insert_sorted(*timers, t, |
---|
27 | (GCompareFunc)_owl_select_timer_cmp); |
---|
28 | return t; |
---|
29 | } |
---|
30 | |
---|
31 | void owl_select_remove_timer(owl_timer *t) |
---|
32 | { |
---|
33 | GList **timers = owl_global_get_timerlist(&g); |
---|
34 | if (t && g_list_find(*timers, t)) { |
---|
35 | *timers = g_list_remove(*timers, t); |
---|
36 | if(t->destroy) { |
---|
37 | t->destroy(t); |
---|
38 | } |
---|
39 | owl_free(t); |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | void owl_select_process_timers(struct timespec *timeout) |
---|
44 | { |
---|
45 | time_t now = time(NULL); |
---|
46 | GList **timers = owl_global_get_timerlist(&g); |
---|
47 | |
---|
48 | while(*timers) { |
---|
49 | owl_timer *t = (*timers)->data; |
---|
50 | int remove = 0; |
---|
51 | |
---|
52 | if(t->time > now) |
---|
53 | break; |
---|
54 | |
---|
55 | /* Reschedule if appropriate */ |
---|
56 | if(t->interval > 0) { |
---|
57 | t->time = now + t->interval; |
---|
58 | *timers = g_list_remove(*timers, t); |
---|
59 | *timers = g_list_insert_sorted(*timers, t, |
---|
60 | (GCompareFunc)_owl_select_timer_cmp); |
---|
61 | } else { |
---|
62 | remove = 1; |
---|
63 | } |
---|
64 | |
---|
65 | /* Do the callback */ |
---|
66 | t->callback(t, t->data); |
---|
67 | if(remove) { |
---|
68 | owl_select_remove_timer(t); |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | if(*timers) { |
---|
73 | owl_timer *t = (*timers)->data; |
---|
74 | timeout->tv_sec = t->time - now; |
---|
75 | if (timeout->tv_sec > 60) |
---|
76 | timeout->tv_sec = 60; |
---|
77 | } else { |
---|
78 | timeout->tv_sec = 60; |
---|
79 | } |
---|
80 | |
---|
81 | timeout->tv_nsec = 0; |
---|
82 | } |
---|
83 | |
---|
84 | /* Returns the index of the dispatch for the file descriptor. */ |
---|
85 | int owl_select_find_dispatch(int fd) |
---|
86 | { |
---|
87 | int i, len; |
---|
88 | owl_list *dl; |
---|
89 | owl_dispatch *d; |
---|
90 | |
---|
91 | dl = owl_global_get_dispatchlist(&g); |
---|
92 | len = owl_list_get_size(dl); |
---|
93 | for(i = 0; i < len; i++) { |
---|
94 | d = (owl_dispatch*)owl_list_get_element(dl, i); |
---|
95 | if (d->fd == fd) return i; |
---|
96 | } |
---|
97 | return -1; |
---|
98 | } |
---|
99 | |
---|
100 | void owl_select_remove_dispatch_at(int elt) /* noproto */ |
---|
101 | { |
---|
102 | owl_list *dl; |
---|
103 | owl_dispatch *d; |
---|
104 | |
---|
105 | dl = owl_global_get_dispatchlist(&g); |
---|
106 | d = (owl_dispatch*)owl_list_get_element(dl, elt); |
---|
107 | owl_list_remove_element(dl, elt); |
---|
108 | if (d->destroy) { |
---|
109 | d->destroy(d); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | /* Adds a new owl_dispatch to the list, replacing existing ones if needed. */ |
---|
114 | void owl_select_add_dispatch(owl_dispatch *d) |
---|
115 | { |
---|
116 | int elt; |
---|
117 | owl_list *dl; |
---|
118 | |
---|
119 | d->needs_gc = 0; |
---|
120 | |
---|
121 | elt = owl_select_find_dispatch(d->fd); |
---|
122 | dl = owl_global_get_dispatchlist(&g); |
---|
123 | |
---|
124 | if (elt != -1) { /* If we have a dispatch for this FD */ |
---|
125 | owl_dispatch *d_old; |
---|
126 | d_old = (owl_dispatch*)owl_list_get_element(dl, elt); |
---|
127 | /* Ignore if we're adding the same dispatch again. Otherwise |
---|
128 | replace the old dispatch. */ |
---|
129 | if (d_old != d) { |
---|
130 | owl_select_remove_dispatch_at(elt); |
---|
131 | } |
---|
132 | } |
---|
133 | owl_list_append_element(dl, d); |
---|
134 | } |
---|
135 | |
---|
136 | /* Removes an owl_dispatch to the list, based on it's file descriptor. */ |
---|
137 | void owl_select_remove_dispatch(int fd) |
---|
138 | { |
---|
139 | int elt; |
---|
140 | owl_list *dl; |
---|
141 | owl_dispatch *d; |
---|
142 | |
---|
143 | elt = owl_select_find_dispatch(fd); |
---|
144 | if(elt == -1) { |
---|
145 | return; |
---|
146 | } else if(dispatch_active) { |
---|
147 | /* Defer the removal until dispatch is done walking the list */ |
---|
148 | dl = owl_global_get_dispatchlist(&g); |
---|
149 | d = (owl_dispatch*)owl_list_get_element(dl, elt); |
---|
150 | d->needs_gc = 1; |
---|
151 | } else { |
---|
152 | owl_select_remove_dispatch_at(elt); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | int owl_select_dispatch_count() |
---|
157 | { |
---|
158 | return owl_list_get_size(owl_global_get_dispatchlist(&g)); |
---|
159 | } |
---|
160 | |
---|
161 | int owl_select_add_perl_dispatch(int fd, SV *cb) |
---|
162 | { |
---|
163 | int elt; |
---|
164 | owl_dispatch *d; |
---|
165 | elt = owl_select_find_dispatch(fd); |
---|
166 | if (elt != -1) { |
---|
167 | d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt); |
---|
168 | if (d->cfunc != owl_perlconfig_dispatch) { |
---|
169 | /* don't mess with non-perl dispatch functions from here. */ |
---|
170 | return 1; |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | d = malloc(sizeof(owl_dispatch)); |
---|
175 | d->fd = fd; |
---|
176 | d->cfunc = owl_perlconfig_dispatch; |
---|
177 | d->destroy = owl_perlconfig_dispatch_free; |
---|
178 | d->data = cb; |
---|
179 | owl_select_add_dispatch(d); |
---|
180 | return 0; |
---|
181 | } |
---|
182 | |
---|
183 | int owl_select_remove_perl_dispatch(int fd) |
---|
184 | { |
---|
185 | int elt; |
---|
186 | owl_dispatch *d; |
---|
187 | |
---|
188 | elt = owl_select_find_dispatch(fd); |
---|
189 | if (elt != -1) { |
---|
190 | d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt); |
---|
191 | if (d->cfunc == owl_perlconfig_dispatch) { |
---|
192 | owl_select_remove_dispatch_at(elt); |
---|
193 | return 0; |
---|
194 | } |
---|
195 | } |
---|
196 | return 1; |
---|
197 | } |
---|
198 | |
---|
199 | int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e) |
---|
200 | { |
---|
201 | int i, len, max_fd; |
---|
202 | owl_dispatch *d; |
---|
203 | owl_list *dl; |
---|
204 | |
---|
205 | dl = owl_global_get_dispatchlist(&g); |
---|
206 | FD_ZERO(r); |
---|
207 | FD_ZERO(e); |
---|
208 | max_fd = 0; |
---|
209 | len = owl_select_dispatch_count(g); |
---|
210 | for(i = 0; i < len; i++) { |
---|
211 | d = (owl_dispatch*)owl_list_get_element(dl, i); |
---|
212 | FD_SET(d->fd, r); |
---|
213 | FD_SET(d->fd, e); |
---|
214 | if (max_fd < d->fd) max_fd = d->fd; |
---|
215 | } |
---|
216 | return max_fd + 1; |
---|
217 | } |
---|
218 | |
---|
219 | void owl_select_gc() |
---|
220 | { |
---|
221 | int i; |
---|
222 | owl_list *dl; |
---|
223 | |
---|
224 | dl = owl_global_get_dispatchlist(&g); |
---|
225 | /* |
---|
226 | * Count down so we aren't set off by removing items from the list |
---|
227 | * during the iteration. |
---|
228 | */ |
---|
229 | for(i = owl_list_get_size(dl) - 1; i >= 0; i--) { |
---|
230 | owl_dispatch *d = owl_list_get_element(dl, i); |
---|
231 | if(d->needs_gc) { |
---|
232 | owl_select_remove_dispatch_at(i); |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | void owl_select_dispatch(fd_set *fds, int max_fd) |
---|
238 | { |
---|
239 | int i, len; |
---|
240 | owl_dispatch *d; |
---|
241 | owl_list *dl; |
---|
242 | |
---|
243 | dl = owl_global_get_dispatchlist(&g); |
---|
244 | len = owl_select_dispatch_count(); |
---|
245 | |
---|
246 | dispatch_active = 1; |
---|
247 | |
---|
248 | for(i = 0; i < len; i++) { |
---|
249 | d = (owl_dispatch*)owl_list_get_element(dl, i); |
---|
250 | /* While d shouldn't normally be null, the list may be altered by |
---|
251 | * functions we dispatch to. */ |
---|
252 | if (d != NULL && !d->needs_gc && FD_ISSET(d->fd, fds)) { |
---|
253 | if (d->cfunc != NULL) { |
---|
254 | d->cfunc(d); |
---|
255 | } |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | dispatch_active = 0; |
---|
260 | owl_select_gc(); |
---|
261 | } |
---|
262 | |
---|
263 | int owl_select_aim_hack(fd_set *rfds, fd_set *wfds) |
---|
264 | { |
---|
265 | aim_conn_t *cur; |
---|
266 | aim_session_t *sess; |
---|
267 | int max_fd; |
---|
268 | |
---|
269 | FD_ZERO(rfds); |
---|
270 | FD_ZERO(wfds); |
---|
271 | max_fd = 0; |
---|
272 | sess = owl_global_get_aimsess(&g); |
---|
273 | for (cur = sess->connlist, max_fd = 0; cur; cur = cur->next) { |
---|
274 | if (cur->fd != -1) { |
---|
275 | FD_SET(cur->fd, rfds); |
---|
276 | if (cur->status & AIM_CONN_STATUS_INPROGRESS) { |
---|
277 | /* Yes, we're checking writable sockets here. Without it, AIM |
---|
278 | login is really slow. */ |
---|
279 | FD_SET(cur->fd, wfds); |
---|
280 | } |
---|
281 | |
---|
282 | if (cur->fd > max_fd) |
---|
283 | max_fd = cur->fd; |
---|
284 | } |
---|
285 | } |
---|
286 | return max_fd; |
---|
287 | } |
---|
288 | |
---|
289 | void owl_select_handle_intr() |
---|
290 | { |
---|
291 | owl_input in; |
---|
292 | in.ch = in.uch = owl_global_get_startup_tio(&g)->c_cc[VINTR]; |
---|
293 | owl_process_input_char(in); |
---|
294 | } |
---|
295 | |
---|
296 | void owl_select() |
---|
297 | { |
---|
298 | int i, max_fd, aim_max_fd, aim_done, ret; |
---|
299 | fd_set r; |
---|
300 | fd_set e; |
---|
301 | fd_set aim_rfds, aim_wfds; |
---|
302 | struct timespec timeout; |
---|
303 | sigset_t mask, intr; |
---|
304 | |
---|
305 | sigemptyset(&intr); |
---|
306 | sigaddset(&intr, SIGINT); |
---|
307 | |
---|
308 | owl_select_process_timers(&timeout); |
---|
309 | |
---|
310 | sigprocmask(SIG_BLOCK, &intr, &mask); |
---|
311 | if(owl_global_is_interrupted(&g)) { |
---|
312 | owl_select_handle_intr(); |
---|
313 | return; |
---|
314 | } |
---|
315 | |
---|
316 | max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e); |
---|
317 | |
---|
318 | /* AIM HACK: |
---|
319 | * |
---|
320 | * The problem - I'm not sure where to hook into the owl/faim |
---|
321 | * interface to keep track of when the AIM socket(s) open and |
---|
322 | * close. In particular, the bosconn thing throws me off. So, |
---|
323 | * rather than register particular dispatchers for AIM, I look up |
---|
324 | * the relevant FDs and add them to select's watch lists, then |
---|
325 | * check for them individually before moving on to the other |
---|
326 | * dispatchers. --asedeno |
---|
327 | */ |
---|
328 | aim_done = 1; |
---|
329 | FD_ZERO(&aim_rfds); |
---|
330 | FD_ZERO(&aim_wfds); |
---|
331 | if (owl_global_is_doaimevents(&g)) { |
---|
332 | aim_done = 0; |
---|
333 | aim_max_fd = owl_select_aim_hack(&aim_rfds, &aim_wfds); |
---|
334 | if (max_fd < aim_max_fd) max_fd = aim_max_fd; |
---|
335 | for(i = 0; i <= aim_max_fd; i++) { |
---|
336 | if (FD_ISSET(i, &aim_rfds)) { |
---|
337 | FD_SET(i, &r); |
---|
338 | FD_SET(i, &e); |
---|
339 | } |
---|
340 | } |
---|
341 | } |
---|
342 | /* END AIM HACK */ |
---|
343 | |
---|
344 | |
---|
345 | ret = pselect(max_fd+1, &r, &aim_wfds, &e, &timeout, &mask); |
---|
346 | |
---|
347 | if(ret < 0 && errno == EINTR) { |
---|
348 | if(owl_global_is_interrupted(&g)) { |
---|
349 | owl_select_handle_intr(); |
---|
350 | } |
---|
351 | return; |
---|
352 | } |
---|
353 | |
---|
354 | sigprocmask(SIG_UNBLOCK, &intr, NULL); |
---|
355 | |
---|
356 | if(ret > 0) { |
---|
357 | /* Merge fd_sets and clear AIM FDs. */ |
---|
358 | for(i = 0; i <= max_fd; i++) { |
---|
359 | /* Merge all interesting FDs into one set, since we have a |
---|
360 | single dispatch per FD. */ |
---|
361 | if (FD_ISSET(i, &r) || FD_ISSET(i, &aim_wfds) || FD_ISSET(i, &e)) { |
---|
362 | /* AIM HACK: no separate dispatch, just process here if |
---|
363 | needed, and only once per run through. */ |
---|
364 | if (!aim_done && (FD_ISSET(i, &aim_rfds) || FD_ISSET(i, &aim_wfds))) { |
---|
365 | owl_process_aim(); |
---|
366 | aim_done = 1; |
---|
367 | } |
---|
368 | else { |
---|
369 | FD_SET(i, &r); |
---|
370 | } |
---|
371 | } |
---|
372 | } |
---|
373 | /* NOTE: the same dispatch function is called for both exceptional |
---|
374 | and read ready FDs. */ |
---|
375 | owl_select_dispatch(&r, max_fd); |
---|
376 | } |
---|
377 | } |
---|