10614745329c986e15bb244a2642c33cea64c3ca
[platform/upstream/dbus.git] / bus / loop.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* loop.c  Main loop for daemon
3  *
4  * Copyright (C) 2003  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "loop.h"
25 #include "utils.h"
26 #include <dbus/dbus-list.h>
27 #include <dbus/dbus-sysdeps.h>
28
29 static DBusList *callbacks = NULL;
30 static int callback_list_serial = 0;
31 static int watch_count = 0;
32 static int timeout_count = 0;
33 static dbus_bool_t exited = FALSE;
34
35 typedef enum
36 {
37   CALLBACK_WATCH,
38   CALLBACK_TIMEOUT
39 } CallbackType;
40
41 typedef struct
42 {
43   CallbackType type;
44   void *data;
45   DBusFreeFunction free_data_func;
46 } Callback;
47
48 typedef struct
49 {
50   Callback callback;
51   BusWatchFunction function;
52   DBusWatch *watch;
53 } WatchCallback;
54
55 typedef struct
56 {
57   Callback callback;
58   DBusTimeout *timeout;
59   BusTimeoutFunction function;
60   unsigned long last_tv_sec;
61   unsigned long last_tv_usec;
62 } TimeoutCallback;
63
64 #define WATCH_CALLBACK(callback)   ((WatchCallback*)callback)
65 #define TIMEOUT_CALLBACK(callback) ((TimeoutCallback*)callback)
66
67 static WatchCallback*
68 watch_callback_new (DBusWatch        *watch,
69                     BusWatchFunction  function,
70                     void             *data,
71                     DBusFreeFunction  free_data_func)
72 {
73   WatchCallback *cb;
74
75   cb = dbus_new (WatchCallback, 1);
76   if (cb == NULL)
77     return NULL;
78
79   cb->watch = watch;
80   cb->function = function;
81   cb->callback.type = CALLBACK_WATCH;
82   cb->callback.data = data;
83   cb->callback.free_data_func = free_data_func;
84
85   return cb;
86 }
87
88 static TimeoutCallback*
89 timeout_callback_new (DBusTimeout        *timeout,
90                       BusTimeoutFunction  function,
91                       void               *data,
92                       DBusFreeFunction    free_data_func)
93 {
94   TimeoutCallback *cb;
95
96   cb = dbus_new (TimeoutCallback, 1);
97   if (cb == NULL)
98     return NULL;
99
100   cb->timeout = timeout;
101   cb->function = function;
102   _dbus_get_current_time (&cb->last_tv_sec,
103                           &cb->last_tv_usec);
104   cb->callback.type = CALLBACK_TIMEOUT;
105   cb->callback.data = data;
106   cb->callback.free_data_func = free_data_func;
107   
108   return cb;
109 }
110
111 static void
112 callback_free (Callback *cb)
113 {
114   if (cb->free_data_func)
115     (* cb->free_data_func) (cb->data);
116
117   dbus_free (cb);
118 }
119
120 static dbus_bool_t
121 add_callback (Callback *cb)
122 {
123   if (!_dbus_list_append (&callbacks, cb))
124     return FALSE;
125
126   callback_list_serial += 1;
127
128   switch (cb->type)
129     {
130     case CALLBACK_WATCH:
131       watch_count += 1;
132       break;
133     case CALLBACK_TIMEOUT:
134       timeout_count += 1;
135       break;
136     }
137   
138   return TRUE;
139 }
140
141 static void
142 remove_callback (DBusList *link)
143 {
144   Callback *cb = link->data;
145   
146   switch (cb->type)
147     {
148     case CALLBACK_WATCH:
149       watch_count -= 1;
150       break;
151     case CALLBACK_TIMEOUT:
152       timeout_count -= 1;
153       break;
154     }
155   
156   callback_free (cb);
157   _dbus_list_remove_link (&callbacks, link);
158   callback_list_serial += 1;
159 }
160
161 dbus_bool_t
162 bus_loop_add_watch (DBusWatch        *watch,
163                     BusWatchFunction  function,
164                     void             *data,
165                     DBusFreeFunction  free_data_func)
166 {
167   WatchCallback *wcb;
168
169   wcb = watch_callback_new (watch, function, data, free_data_func);
170   if (wcb == NULL)
171     return FALSE;
172
173   if (!add_callback ((Callback*) wcb))
174     {
175       wcb->callback.free_data_func = NULL; /* don't want to have this side effect */
176       callback_free ((Callback*) wcb);
177       return FALSE;
178     }
179   
180   return TRUE;
181 }
182
183 void
184 bus_loop_remove_watch (DBusWatch        *watch,
185                        BusWatchFunction  function,
186                        void             *data)
187 {
188   DBusList *link;
189   
190   link = _dbus_list_get_first_link (&callbacks);
191   while (link != NULL)
192     {
193       DBusList *next = _dbus_list_get_next_link (&callbacks, link);
194       Callback *this = link->data;
195
196       if (this->type == CALLBACK_WATCH &&
197           WATCH_CALLBACK (this)->watch == watch &&
198           this->data == data &&
199           WATCH_CALLBACK (this)->function == function)
200         {
201           remove_callback (link);
202           
203           return;
204         }
205       
206       link = next;
207     }
208
209   _dbus_warn ("could not find watch %p function %p data %p to remove\n",
210               watch, function, data);
211 }
212
213 dbus_bool_t
214 bus_loop_add_timeout (DBusTimeout        *timeout,
215                       BusTimeoutFunction  function,
216                       void               *data,
217                       DBusFreeFunction    free_data_func)
218 {
219   TimeoutCallback *tcb;
220
221   tcb = timeout_callback_new (timeout, function, data, free_data_func);
222   if (tcb == NULL)
223     return FALSE;
224
225   if (!add_callback ((Callback*) tcb))
226     {
227       tcb->callback.free_data_func = NULL; /* don't want to have this side effect */
228       callback_free ((Callback*) tcb);
229       return FALSE;
230     }
231   
232   return TRUE;
233 }
234
235 void
236 bus_loop_remove_timeout (DBusTimeout        *timeout,
237                          BusTimeoutFunction  function,
238                          void               *data)
239 {
240   DBusList *link;
241   
242   link = _dbus_list_get_first_link (&callbacks);
243   while (link != NULL)
244     {
245       DBusList *next = _dbus_list_get_next_link (&callbacks, link);
246       Callback *this = link->data;
247
248       if (this->type == CALLBACK_TIMEOUT &&
249           TIMEOUT_CALLBACK (this)->timeout == timeout &&
250           this->data == data &&
251           TIMEOUT_CALLBACK (this)->function == function)
252         {
253           remove_callback (link);
254           
255           return;
256         }
257       
258       link = next;
259     }
260
261   _dbus_warn ("could not find timeout %p function %p data %p to remove\n",
262               timeout, function, data);
263 }
264
265 /* Returns TRUE if we have any timeouts or ready file descriptors,
266  * which is just used in test code as a debug hack
267  */
268
269 dbus_bool_t
270 bus_loop_iterate (dbus_bool_t block)
271 {
272   dbus_bool_t retval;
273   DBusPollFD *fds;
274   int n_fds;
275   WatchCallback **watches_for_fds;
276   int i;
277   DBusList *link;
278   int n_ready;
279   int initial_serial;
280   long timeout;
281
282   retval = FALSE;
283       
284   fds = NULL;
285   watches_for_fds = NULL;
286
287 #if 0
288   _dbus_verbose (" iterate %d timeouts %d watches\n",
289                  timeout_count, watch_count);
290 #endif
291   
292   if (callbacks == NULL)
293     {
294       bus_loop_quit ();
295       goto next_iteration;
296     }
297       
298   n_fds = watch_count;
299
300   if (n_fds > 0)
301     {
302       fds = dbus_new0 (DBusPollFD, n_fds);
303       while (fds == NULL)
304         {
305           bus_wait_for_memory ();
306           fds = dbus_new0 (DBusPollFD, n_fds);
307         }
308           
309       watches_for_fds = dbus_new (WatchCallback*, n_fds);
310       while (watches_for_fds == NULL)
311         {
312           bus_wait_for_memory ();
313           watches_for_fds = dbus_new (WatchCallback*, n_fds);
314         }
315       
316       i = 0;
317       link = _dbus_list_get_first_link (&callbacks);
318       while (link != NULL)
319         {
320           DBusList *next = _dbus_list_get_next_link (&callbacks, link);
321           Callback *cb = link->data;
322           if (cb->type == CALLBACK_WATCH)
323             {
324               unsigned int flags;
325               WatchCallback *wcb = WATCH_CALLBACK (cb);
326                   
327               watches_for_fds[i] = wcb;
328                   
329               flags = dbus_watch_get_flags (wcb->watch);
330                   
331               fds[i].fd = dbus_watch_get_fd (wcb->watch);
332               if (flags & DBUS_WATCH_READABLE)
333                 fds[i].events |= _DBUS_POLLIN;
334               if (flags & DBUS_WATCH_WRITABLE)
335                 fds[i].events |= _DBUS_POLLOUT;
336
337               ++i;
338             }
339               
340           link = next;
341         }
342
343       _dbus_assert (i == n_fds);
344     }
345
346   timeout = -1;
347   if (timeout_count > 0)
348     {
349       unsigned long tv_sec;
350       unsigned long tv_usec;
351
352       retval = TRUE;
353       
354       _dbus_get_current_time (&tv_sec, &tv_usec);
355           
356       link = _dbus_list_get_first_link (&callbacks);
357       while (link != NULL)
358         {
359           DBusList *next = _dbus_list_get_next_link (&callbacks, link);
360           Callback *cb = link->data;
361
362           if (cb->type == CALLBACK_TIMEOUT)
363             {
364               TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
365               unsigned long interval;
366               unsigned long elapsed;
367
368               if (tcb->last_tv_sec > tv_sec ||
369                   (tcb->last_tv_sec == tv_sec &&
370                    tcb->last_tv_usec > tv_usec))
371                 {
372                   /* Clock went backward, pretend timeout
373                    * was just installed.
374                    */
375                   tcb->last_tv_sec = tv_sec;
376                   tcb->last_tv_usec = tv_usec;
377                   _dbus_verbose ("System clock went backward\n");
378                 }
379                   
380               interval = dbus_timeout_get_interval (tcb->timeout);
381
382               elapsed =
383                 (tv_sec - tcb->last_tv_sec) * 1000 +
384                 (tv_usec - tcb->last_tv_usec) / 1000;
385
386               if (interval < elapsed)
387                 timeout = 0;
388               else if (timeout < 0)
389                 timeout = interval - elapsed;
390               else
391                 timeout = MIN (((unsigned long)timeout), interval - elapsed);
392
393               _dbus_assert (timeout >= 0);
394                   
395               if (timeout == 0)
396                 break; /* it's not going to get shorter... */
397             }
398               
399           link = next;
400         }
401     }
402
403   if (!block)
404     timeout = 0;
405       
406   n_ready = _dbus_poll (fds, n_fds, timeout);
407
408   initial_serial = callback_list_serial;
409
410   if (timeout_count > 0)
411     {
412       unsigned long tv_sec;
413       unsigned long tv_usec;
414
415       _dbus_get_current_time (&tv_sec, &tv_usec);
416
417       /* It'd be nice to avoid this O(n) thingy here */
418       link = _dbus_list_get_first_link (&callbacks);
419       while (link != NULL)
420         {
421           DBusList *next = _dbus_list_get_next_link (&callbacks, link);
422           Callback *cb = link->data;
423
424           if (initial_serial != callback_list_serial)
425             goto next_iteration;
426
427           if (exited)
428             goto next_iteration;
429               
430           if (cb->type == CALLBACK_TIMEOUT)
431             {
432               TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
433               unsigned long interval;
434               unsigned long elapsed;
435                   
436               if (tcb->last_tv_sec > tv_sec ||
437                   (tcb->last_tv_sec == tv_sec &&
438                    tcb->last_tv_usec > tv_usec))
439                 {
440                   /* Clock went backward, pretend timeout
441                    * was just installed.
442                    */
443                   tcb->last_tv_sec = tv_sec;
444                   tcb->last_tv_usec = tv_usec;
445                   _dbus_verbose ("System clock went backward\n");
446                   goto next_timeout;
447                 }
448                   
449               interval = dbus_timeout_get_interval (tcb->timeout);
450
451               elapsed =
452                 (tv_sec - tcb->last_tv_sec) * 1000 +
453                 (tv_usec - tcb->last_tv_usec) / 1000;
454
455 #if 0
456               _dbus_verbose ("  interval = %lu elapsed = %lu\n",
457                              interval, elapsed);
458 #endif
459               
460               if (interval <= elapsed)
461                 {
462                   /* Save last callback time and fire this timeout */
463                   tcb->last_tv_sec = tv_sec;
464                   tcb->last_tv_usec = tv_usec;
465
466 #if 0
467                   _dbus_verbose ("  invoking timeout\n");
468 #endif
469                   
470                   (* tcb->function) (tcb->timeout,
471                                      cb->data);
472                 }
473             }
474
475         next_timeout:
476           link = next;
477         }
478     }
479       
480   if (n_ready > 0)
481     {
482       i = 0;
483       while (i < n_fds)
484         {
485           /* FIXME I think this "restart if we change the watches"
486            * approach could result in starving watches
487            * toward the end of the list.
488            */
489           if (initial_serial != callback_list_serial)
490             goto next_iteration;
491
492           if (exited)
493             goto next_iteration;
494
495           if (fds[i].revents != 0)
496             {
497               WatchCallback *wcb;
498               unsigned int condition;
499                   
500               wcb = watches_for_fds[i];
501                   
502               condition = 0;
503               if (fds[i].revents & _DBUS_POLLIN)
504                 condition |= DBUS_WATCH_READABLE;
505               if (fds[i].revents & _DBUS_POLLOUT)
506                 condition |= DBUS_WATCH_WRITABLE;
507               if (fds[i].revents & _DBUS_POLLHUP)
508                 condition |= DBUS_WATCH_HANGUP;
509               if (fds[i].revents & _DBUS_POLLERR)
510                 condition |= DBUS_WATCH_ERROR;
511
512               /* condition may still be 0 if we got some
513                * weird POLLFOO thing like POLLWRBAND
514                */
515                   
516               if (condition != 0)
517                 {
518                   (* wcb->function) (wcb->watch,
519                                      condition,
520                                      ((Callback*)wcb)->data);
521
522                   retval = TRUE;
523                 }
524             }
525               
526           ++i;
527         }
528     }
529       
530  next_iteration:
531   dbus_free (fds);
532   dbus_free (watches_for_fds);
533
534   return retval;
535 }
536
537
538 void
539 bus_loop_run (void)
540 {
541   while (!exited)
542     bus_loop_iterate (TRUE);
543 }
544
545 void
546 bus_loop_quit (void)
547 {
548   exited = TRUE;
549 }