ea0ec1065aa83fc3382552720a6c3d3926fe4cc1
[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   /* count enabled watches */
299   n_fds = 0;
300   link = _dbus_list_get_first_link (&callbacks);
301   while (link != NULL)
302     {
303       DBusList *next = _dbus_list_get_next_link (&callbacks, link);
304       Callback *cb = link->data;
305       if (cb->type == CALLBACK_WATCH)
306         {
307           WatchCallback *wcb = WATCH_CALLBACK (cb);
308
309           if (dbus_watch_get_enabled (wcb->watch))
310             ++n_fds;
311         }
312       
313       link = next;
314     }
315
316   /* fill our array of fds and watches */
317   if (n_fds > 0)
318     {
319       fds = dbus_new0 (DBusPollFD, n_fds);
320       while (fds == NULL)
321         {
322           bus_wait_for_memory ();
323           fds = dbus_new0 (DBusPollFD, n_fds);
324         }
325           
326       watches_for_fds = dbus_new (WatchCallback*, n_fds);
327       while (watches_for_fds == NULL)
328         {
329           bus_wait_for_memory ();
330           watches_for_fds = dbus_new (WatchCallback*, n_fds);
331         }
332       
333       i = 0;
334       link = _dbus_list_get_first_link (&callbacks);
335       while (link != NULL)
336         {
337           DBusList *next = _dbus_list_get_next_link (&callbacks, link);
338           Callback *cb = link->data;
339           if (cb->type == CALLBACK_WATCH)
340             {
341               unsigned int flags;
342               WatchCallback *wcb = WATCH_CALLBACK (cb);
343
344               if (dbus_watch_get_enabled (wcb->watch))
345                 {
346                   watches_for_fds[i] = wcb;
347                   
348                   flags = dbus_watch_get_flags (wcb->watch);
349                   
350                   fds[i].fd = dbus_watch_get_fd (wcb->watch);
351                   if (flags & DBUS_WATCH_READABLE)
352                     fds[i].events |= _DBUS_POLLIN;
353                   if (flags & DBUS_WATCH_WRITABLE)
354                     fds[i].events |= _DBUS_POLLOUT;
355
356                   ++i;
357                 }
358             }
359               
360           link = next;
361         }
362
363       _dbus_assert (i == n_fds);
364     }
365
366   timeout = -1;
367   if (timeout_count > 0)
368     {
369       unsigned long tv_sec;
370       unsigned long tv_usec;
371
372       retval = TRUE;
373       
374       _dbus_get_current_time (&tv_sec, &tv_usec);
375           
376       link = _dbus_list_get_first_link (&callbacks);
377       while (link != NULL)
378         {
379           DBusList *next = _dbus_list_get_next_link (&callbacks, link);
380           Callback *cb = link->data;
381
382           if (cb->type == CALLBACK_TIMEOUT &&
383               dbus_timeout_get_enabled (TIMEOUT_CALLBACK (cb)->timeout))
384             {
385               TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
386               unsigned long interval;
387               unsigned long elapsed;
388
389               if (tcb->last_tv_sec > tv_sec ||
390                   (tcb->last_tv_sec == tv_sec &&
391                    tcb->last_tv_usec > tv_usec))
392                 {
393                   /* Clock went backward, pretend timeout
394                    * was just installed.
395                    */
396                   tcb->last_tv_sec = tv_sec;
397                   tcb->last_tv_usec = tv_usec;
398                   _dbus_verbose ("System clock went backward\n");
399                 }
400                   
401               interval = dbus_timeout_get_interval (tcb->timeout);
402
403               elapsed =
404                 (tv_sec - tcb->last_tv_sec) * 1000 +
405                 (tv_usec - tcb->last_tv_usec) / 1000;
406
407               if (interval < elapsed)
408                 timeout = 0;
409               else if (timeout < 0)
410                 timeout = interval - elapsed;
411               else
412                 timeout = MIN (((unsigned long)timeout), interval - elapsed);
413
414               _dbus_assert (timeout >= 0);
415                   
416               if (timeout == 0)
417                 break; /* it's not going to get shorter... */
418             }
419               
420           link = next;
421         }
422     }
423
424   if (!block)
425     timeout = 0;
426       
427   n_ready = _dbus_poll (fds, n_fds, timeout);
428
429   initial_serial = callback_list_serial;
430
431   if (timeout_count > 0)
432     {
433       unsigned long tv_sec;
434       unsigned long tv_usec;
435
436       _dbus_get_current_time (&tv_sec, &tv_usec);
437
438       /* It'd be nice to avoid this O(n) thingy here */
439       link = _dbus_list_get_first_link (&callbacks);
440       while (link != NULL)
441         {
442           DBusList *next = _dbus_list_get_next_link (&callbacks, link);
443           Callback *cb = link->data;
444
445           if (initial_serial != callback_list_serial)
446             goto next_iteration;
447
448           if (exited)
449             goto next_iteration;
450               
451           if (cb->type == CALLBACK_TIMEOUT &&
452               dbus_timeout_get_enabled (TIMEOUT_CALLBACK (cb)->timeout))
453             {
454               TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
455               unsigned long interval;
456               unsigned long elapsed;
457                   
458               if (tcb->last_tv_sec > tv_sec ||
459                   (tcb->last_tv_sec == tv_sec &&
460                    tcb->last_tv_usec > tv_usec))
461                 {
462                   /* Clock went backward, pretend timeout
463                    * was just installed.
464                    */
465                   tcb->last_tv_sec = tv_sec;
466                   tcb->last_tv_usec = tv_usec;
467                   _dbus_verbose ("System clock went backward\n");
468                   goto next_timeout;
469                 }
470                   
471               interval = dbus_timeout_get_interval (tcb->timeout);
472
473               elapsed =
474                 (tv_sec - tcb->last_tv_sec) * 1000 +
475                 (tv_usec - tcb->last_tv_usec) / 1000;
476
477 #if 0
478               _dbus_verbose ("  interval = %lu elapsed = %lu\n",
479                              interval, elapsed);
480 #endif
481               
482               if (interval <= elapsed)
483                 {
484                   /* Save last callback time and fire this timeout */
485                   tcb->last_tv_sec = tv_sec;
486                   tcb->last_tv_usec = tv_usec;
487
488 #if 0
489                   _dbus_verbose ("  invoking timeout\n");
490 #endif
491                   
492                   (* tcb->function) (tcb->timeout,
493                                      cb->data);
494                 }
495             }
496
497         next_timeout:
498           link = next;
499         }
500     }
501       
502   if (n_ready > 0)
503     {
504       i = 0;
505       while (i < n_fds)
506         {
507           /* FIXME I think this "restart if we change the watches"
508            * approach could result in starving watches
509            * toward the end of the list.
510            */
511           if (initial_serial != callback_list_serial)
512             goto next_iteration;
513
514           if (exited)
515             goto next_iteration;
516
517           if (fds[i].revents != 0)
518             {
519               WatchCallback *wcb;
520               unsigned int condition;
521                   
522               wcb = watches_for_fds[i];
523                   
524               condition = 0;
525               if (fds[i].revents & _DBUS_POLLIN)
526                 condition |= DBUS_WATCH_READABLE;
527               if (fds[i].revents & _DBUS_POLLOUT)
528                 condition |= DBUS_WATCH_WRITABLE;
529               if (fds[i].revents & _DBUS_POLLHUP)
530                 condition |= DBUS_WATCH_HANGUP;
531               if (fds[i].revents & _DBUS_POLLERR)
532                 condition |= DBUS_WATCH_ERROR;
533
534               /* condition may still be 0 if we got some
535                * weird POLLFOO thing like POLLWRBAND
536                */
537                   
538               if (condition != 0 &&
539                   dbus_watch_get_enabled (wcb->watch))
540                 {
541                   (* wcb->function) (wcb->watch,
542                                      condition,
543                                      ((Callback*)wcb)->data);
544
545                   retval = TRUE;
546                 }
547             }
548               
549           ++i;
550         }
551     }
552       
553  next_iteration:
554   dbus_free (fds);
555   dbus_free (watches_for_fds);
556
557   return retval;
558 }
559
560
561 void
562 bus_loop_run (void)
563 {
564   while (!exited)
565     bus_loop_iterate (TRUE);
566 }
567
568 void
569 bus_loop_quit (void)
570 {
571   exited = TRUE;
572 }