1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* expirelist.c  List of items that expire
3  *
4  * Copyright (C) 2003  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23 
24 #include <config.h>
25 #include "expirelist.h"
26 #include "test.h"
27 #include <dbus/dbus-internals.h>
28 #include <dbus/dbus-mainloop.h>
29 #include <dbus/dbus-timeout.h>
30 
31 struct BusExpireList
32 {
33   DBusList      *items; /**< List of BusExpireItem */
34   DBusTimeout   *timeout;
35   DBusLoop      *loop;
36   BusExpireFunc  expire_func;
37   void          *data;
38   int            expire_after; /**< Expire after milliseconds (thousandths) */
39 };
40 
41 static dbus_bool_t expire_timeout_handler (void *data);
42 
43 BusExpireList*
bus_expire_list_new(DBusLoop * loop,int expire_after,BusExpireFunc expire_func,void * data)44 bus_expire_list_new (DBusLoop      *loop,
45                      int            expire_after,
46                      BusExpireFunc  expire_func,
47                      void          *data)
48 {
49   BusExpireList *list;
50 
51   list = dbus_new0 (BusExpireList, 1);
52   if (list == NULL)
53     return NULL;
54 
55   list->expire_func = expire_func;
56   list->data = data;
57   list->loop = loop;
58   list->expire_after = expire_after;
59 
60   list->timeout = _dbus_timeout_new (100, /* irrelevant */
61                                      expire_timeout_handler,
62                                      list, NULL);
63   if (list->timeout == NULL)
64     goto failed;
65 
66   _dbus_timeout_disable (list->timeout);
67 
68   if (!_dbus_loop_add_timeout (list->loop, list->timeout))
69     goto failed;
70 
71   return list;
72 
73  failed:
74   if (list->timeout)
75     _dbus_timeout_unref (list->timeout);
76 
77   dbus_free (list);
78 
79   return NULL;
80 }
81 
82 void
bus_expire_list_free(BusExpireList * list)83 bus_expire_list_free (BusExpireList *list)
84 {
85   _dbus_assert (list->items == NULL);
86 
87   _dbus_loop_remove_timeout (list->loop, list->timeout);
88 
89   _dbus_timeout_unref (list->timeout);
90 
91   dbus_free (list);
92 }
93 
94 void
bus_expire_timeout_set_interval(DBusTimeout * timeout,int next_interval)95 bus_expire_timeout_set_interval (DBusTimeout   *timeout,
96                                  int            next_interval)
97 {
98   if (next_interval >= 0)
99     {
100       _dbus_timeout_restart (timeout, next_interval);
101 
102       _dbus_verbose ("Enabled an expire timeout with interval %d\n",
103                      next_interval);
104     }
105   else if (dbus_timeout_get_enabled (timeout))
106     {
107       _dbus_timeout_disable (timeout);
108 
109       _dbus_verbose ("Disabled an expire timeout\n");
110     }
111   else
112     _dbus_verbose ("No need to disable this expire timeout\n");
113 }
114 
115 void
bus_expire_list_recheck_immediately(BusExpireList * list)116 bus_expire_list_recheck_immediately (BusExpireList *list)
117 {
118   _dbus_verbose ("setting interval on expire list to 0 for immediate recheck\n");
119 
120   bus_expire_timeout_set_interval (list->timeout, 0);
121 }
122 
123 static int
do_expiration_with_monotonic_time(BusExpireList * list,long tv_sec,long tv_usec)124 do_expiration_with_monotonic_time (BusExpireList *list,
125                                    long           tv_sec,
126                                    long           tv_usec)
127 {
128   DBusList *link;
129   int next_interval, min_wait_time, items_to_expire;
130 
131   next_interval = -1;
132   min_wait_time = 3600 * 1000; /* this is reset anyway if used */
133   items_to_expire = 0;
134 
135   link = _dbus_list_get_first_link (&list->items);
136   while (link != NULL)
137     {
138       DBusList *next = _dbus_list_get_next_link (&list->items, link);
139       double elapsed;
140       BusExpireItem *item;
141 
142       item = link->data;
143 
144       elapsed = ELAPSED_MILLISECONDS_SINCE (item->added_tv_sec,
145                                             item->added_tv_usec,
146                                             tv_sec, tv_usec);
147 
148       if (((item->added_tv_sec == 0) && (item->added_tv_usec == 0)) ||
149           ((list->expire_after > 0) && (elapsed >= (double) list->expire_after)))
150         {
151           _dbus_verbose ("Expiring an item %p\n", item);
152 
153           /* If the expire function fails, we just end up expiring
154            * this item next time we walk through the list. This would
155            * be an indeterminate time normally, so we set up the
156            * next_interval to be "shortly" (just enough to avoid
157            * a busy loop)
158            */
159           if (!(* list->expire_func) (list, link, list->data))
160             {
161               next_interval = _dbus_get_oom_wait ();
162               break;
163             }
164         }
165       else if (list->expire_after > 0)
166         {
167           double to_wait;
168 
169           items_to_expire = 1;
170           to_wait = (double) list->expire_after - elapsed;
171           if (min_wait_time > to_wait)
172             min_wait_time = to_wait;
173         }
174 
175       link = next;
176     }
177 
178   if (next_interval < 0 && items_to_expire)
179     next_interval = min_wait_time;
180 
181   return next_interval;
182 }
183 
184 static void
bus_expirelist_expire(BusExpireList * list)185 bus_expirelist_expire (BusExpireList *list)
186 {
187   int next_interval;
188 
189   next_interval = -1;
190 
191   if (list->items != NULL)
192     {
193       long tv_sec, tv_usec;
194 
195       _dbus_get_monotonic_time (&tv_sec, &tv_usec);
196 
197       next_interval = do_expiration_with_monotonic_time (list, tv_sec, tv_usec);
198     }
199 
200   bus_expire_timeout_set_interval (list->timeout, next_interval);
201 }
202 
203 static dbus_bool_t
expire_timeout_handler(void * data)204 expire_timeout_handler (void *data)
205 {
206   BusExpireList *list = data;
207 
208   _dbus_verbose ("Running\n");
209 
210   /* note that this may remove the timeout */
211   bus_expirelist_expire (list);
212 
213   return TRUE;
214 }
215 
216 void
bus_expire_list_remove_link(BusExpireList * list,DBusList * link)217 bus_expire_list_remove_link (BusExpireList *list,
218                              DBusList      *link)
219 {
220   _dbus_list_remove_link (&list->items, link);
221 }
222 
223 dbus_bool_t
bus_expire_list_remove(BusExpireList * list,BusExpireItem * item)224 bus_expire_list_remove (BusExpireList *list,
225                         BusExpireItem *item)
226 {
227   return _dbus_list_remove (&list->items, item);
228 }
229 
230 void
bus_expire_list_unlink(BusExpireList * list,DBusList * link)231 bus_expire_list_unlink (BusExpireList *list,
232                         DBusList      *link)
233 {
234   _dbus_list_unlink (&list->items, link);
235 }
236 
237 dbus_bool_t
bus_expire_list_add(BusExpireList * list,BusExpireItem * item)238 bus_expire_list_add (BusExpireList *list,
239                      BusExpireItem *item)
240 {
241   dbus_bool_t ret;
242 
243   ret = _dbus_list_prepend (&list->items, item);
244   if (ret && !dbus_timeout_get_enabled (list->timeout))
245     bus_expire_timeout_set_interval (list->timeout, 0);
246 
247   return ret;
248 }
249 
250 void
bus_expire_list_add_link(BusExpireList * list,DBusList * link)251 bus_expire_list_add_link (BusExpireList *list,
252                           DBusList      *link)
253 {
254   _dbus_assert (link->data != NULL);
255 
256   _dbus_list_prepend_link (&list->items, link);
257 
258   if (!dbus_timeout_get_enabled (list->timeout))
259     bus_expire_timeout_set_interval (list->timeout, 0);
260 }
261 
262 DBusList*
bus_expire_list_get_first_link(BusExpireList * list)263 bus_expire_list_get_first_link (BusExpireList *list)
264 {
265   return _dbus_list_get_first_link (&list->items);
266 }
267 
268 DBusList*
bus_expire_list_get_next_link(BusExpireList * list,DBusList * link)269 bus_expire_list_get_next_link (BusExpireList *list,
270                                DBusList      *link)
271 {
272   return _dbus_list_get_next_link (&list->items, link);
273 }
274 
275 dbus_bool_t
bus_expire_list_contains_item(BusExpireList * list,BusExpireItem * item)276 bus_expire_list_contains_item (BusExpireList *list,
277                                BusExpireItem *item)
278 {
279   return _dbus_list_find_last (&list->items, item) != NULL;
280 }
281 
282 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
283 
284 typedef struct
285 {
286   BusExpireItem item;
287   int expire_count;
288 } TestExpireItem;
289 
290 static dbus_bool_t
test_expire_func(BusExpireList * list,DBusList * link,void * data)291 test_expire_func (BusExpireList *list,
292                   DBusList      *link,
293                   void          *data)
294 {
295   TestExpireItem *t;
296 
297   t = (TestExpireItem*) link->data;
298 
299   t->expire_count += 1;
300 
301   return TRUE;
302 }
303 
304 static void
time_add_milliseconds(long * tv_sec,long * tv_usec,int milliseconds)305 time_add_milliseconds (long *tv_sec,
306                        long *tv_usec,
307                        int   milliseconds)
308 {
309   *tv_sec = *tv_sec + milliseconds / 1000;
310   *tv_usec = *tv_usec + milliseconds * 1000;
311   if (*tv_usec >= 1000000)
312     {
313       *tv_usec -= 1000000;
314       *tv_sec += 1;
315     }
316 }
317 
318 dbus_bool_t
bus_expire_list_test(const DBusString * test_data_dir)319 bus_expire_list_test (const DBusString *test_data_dir)
320 {
321   DBusLoop *loop;
322   BusExpireList *list;
323   long tv_sec, tv_usec;
324   long tv_sec_not_expired, tv_usec_not_expired;
325   long tv_sec_expired, tv_usec_expired;
326   long tv_sec_past, tv_usec_past;
327   TestExpireItem *item;
328   int next_interval;
329   dbus_bool_t result = FALSE;
330 
331 
332   loop = _dbus_loop_new ();
333   _dbus_assert (loop != NULL);
334 
335 #define EXPIRE_AFTER 100
336 
337   list = bus_expire_list_new (loop, EXPIRE_AFTER,
338                               test_expire_func, NULL);
339   _dbus_assert (list != NULL);
340 
341   _dbus_get_monotonic_time (&tv_sec, &tv_usec);
342 
343   tv_sec_not_expired = tv_sec;
344   tv_usec_not_expired = tv_usec;
345   time_add_milliseconds (&tv_sec_not_expired,
346                          &tv_usec_not_expired, EXPIRE_AFTER - 1);
347 
348   tv_sec_expired = tv_sec;
349   tv_usec_expired = tv_usec;
350   time_add_milliseconds (&tv_sec_expired,
351                          &tv_usec_expired, EXPIRE_AFTER);
352 
353 
354   tv_sec_past = tv_sec - 1;
355   tv_usec_past = tv_usec;
356 
357   item = dbus_new0 (TestExpireItem, 1);
358 
359   if (item == NULL)
360     goto oom;
361 
362   item->item.added_tv_sec = tv_sec;
363   item->item.added_tv_usec = tv_usec;
364   if (!bus_expire_list_add (list, &item->item))
365     _dbus_assert_not_reached ("out of memory");
366 
367   next_interval =
368     do_expiration_with_monotonic_time (list, tv_sec_not_expired,
369                                        tv_usec_not_expired);
370   _dbus_assert (item->expire_count == 0);
371   _dbus_verbose ("next_interval = %d\n", next_interval);
372   _dbus_assert (next_interval == 1);
373 
374   next_interval =
375     do_expiration_with_monotonic_time (list, tv_sec_expired,
376                                        tv_usec_expired);
377   _dbus_assert (item->expire_count == 1);
378   _dbus_verbose ("next_interval = %d\n", next_interval);
379   _dbus_assert (next_interval == -1);
380 
381   next_interval =
382     do_expiration_with_monotonic_time (list, tv_sec_past,
383                                        tv_usec_past);
384   _dbus_assert (item->expire_count == 1);
385   _dbus_verbose ("next_interval = %d\n", next_interval);
386   _dbus_assert (next_interval == 1000 + EXPIRE_AFTER);
387 
388   bus_expire_list_remove (list, &item->item);
389   dbus_free (item);
390 
391   bus_expire_list_free (list);
392   _dbus_loop_unref (loop);
393 
394   result = TRUE;
395 
396  oom:
397   return result;
398 }
399 
400 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
401