D-Bus  1.6.28
dbus-watch.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-watch.c DBusWatch implementation
3  *
4  * Copyright (C) 2002, 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 "dbus-internals.h"
26 #include "dbus-watch.h"
27 #include "dbus-list.h"
28 
40 struct DBusWatch
41 {
42  int refcount;
43  int fd;
44  unsigned int flags;
47  void *handler_data;
50  void *data;
52  unsigned int enabled : 1;
53  unsigned int oom_last_time : 1;
54 };
55 
57 _dbus_watch_get_enabled (DBusWatch *watch)
58 {
59  return watch->enabled;
60 }
61 
63 _dbus_watch_get_oom_last_time (DBusWatch *watch)
64 {
65  return watch->oom_last_time;
66 }
67 
68 void
69 _dbus_watch_set_oom_last_time (DBusWatch *watch,
70  dbus_bool_t oom)
71 {
72  watch->oom_last_time = oom;
73 }
74 
87 DBusWatch*
89  unsigned int flags,
90  dbus_bool_t enabled,
91  DBusWatchHandler handler,
92  void *data,
93  DBusFreeFunction free_data_function)
94 {
95  DBusWatch *watch;
96 
97 #define VALID_WATCH_FLAGS (DBUS_WATCH_WRITABLE | DBUS_WATCH_READABLE)
98 
99  _dbus_assert ((flags & VALID_WATCH_FLAGS) == flags);
100 
101  watch = dbus_new0 (DBusWatch, 1);
102  if (watch == NULL)
103  return NULL;
104 
105  watch->refcount = 1;
106  watch->fd = fd;
107  watch->flags = flags;
108  watch->enabled = enabled;
109 
110  watch->handler = handler;
111  watch->handler_data = data;
112  watch->free_handler_data_function = free_data_function;
113 
114  return watch;
115 }
116 
123 DBusWatch *
125 {
126  watch->refcount += 1;
127 
128  return watch;
129 }
130 
137 void
139 {
140  _dbus_assert (watch != NULL);
141  _dbus_assert (watch->refcount > 0);
142 
143  watch->refcount -= 1;
144  if (watch->refcount == 0)
145  {
146  if (watch->fd != -1)
147  _dbus_warn ("this watch should have been invalidated");
148 
149  dbus_watch_set_data (watch, NULL, NULL); /* call free_data_function */
150 
151  if (watch->free_handler_data_function)
152  (* watch->free_handler_data_function) (watch->handler_data);
153 
154  dbus_free (watch);
155  }
156 }
157 
168 void
170 {
171  watch->fd = -1;
172  watch->flags = 0;
173 }
174 
184 void
186  unsigned int *condition)
187 {
188  if (!(watch->flags & DBUS_WATCH_READABLE))
189  *condition &= ~DBUS_WATCH_READABLE;
190  if (!(watch->flags & DBUS_WATCH_WRITABLE))
191  *condition &= ~DBUS_WATCH_WRITABLE;
192 }
193 
194 
215 {
221  void *watch_data;
223 };
224 
233 {
234  DBusWatchList *watch_list;
235 
236  watch_list = dbus_new0 (DBusWatchList, 1);
237  if (watch_list == NULL)
238  return NULL;
239 
240  return watch_list;
241 }
242 
248 void
250 {
251  /* free watch_data and removes watches as a side effect */
252  _dbus_watch_list_set_functions (watch_list,
253  NULL, NULL, NULL, NULL, NULL);
254  _dbus_list_foreach (&watch_list->watches,
256  NULL);
257  _dbus_list_clear (&watch_list->watches);
258 
259  dbus_free (watch_list);
260 }
261 
278  DBusAddWatchFunction add_function,
279  DBusRemoveWatchFunction remove_function,
280  DBusWatchToggledFunction toggled_function,
281  void *data,
282  DBusFreeFunction free_data_function)
283 {
284  /* Add watches with the new watch function, failing on OOM */
285  if (add_function != NULL)
286  {
287  DBusList *link;
288 
289  link = _dbus_list_get_first_link (&watch_list->watches);
290  while (link != NULL)
291  {
292  DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
293  link);
294 
295 #ifdef DBUS_ENABLE_VERBOSE_MODE
296  {
297  const char *watch_type;
298  int flags;
299 
300  flags = dbus_watch_get_flags (link->data);
301  if ((flags & DBUS_WATCH_READABLE) &&
302  (flags & DBUS_WATCH_WRITABLE))
303  watch_type = "readwrite";
304  else if (flags & DBUS_WATCH_READABLE)
305  watch_type = "read";
306  else if (flags & DBUS_WATCH_WRITABLE)
307  watch_type = "write";
308  else
309  watch_type = "not read or write";
310 
311  _dbus_verbose ("Adding a %s watch on fd %d using newly-set add watch function\n",
312  watch_type,
313  dbus_watch_get_socket (link->data));
314  }
315 #endif /* DBUS_ENABLE_VERBOSE_MODE */
316 
317  if (!(* add_function) (link->data, data))
318  {
319  /* remove it all again and return FALSE */
320  DBusList *link2;
321 
322  link2 = _dbus_list_get_first_link (&watch_list->watches);
323  while (link2 != link)
324  {
325  DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
326  link2);
327 
328  _dbus_verbose ("Removing watch on fd %d using newly-set remove function because initial add failed\n",
329  dbus_watch_get_socket (link2->data));
330 
331  (* remove_function) (link2->data, data);
332 
333  link2 = next;
334  }
335 
336  return FALSE;
337  }
338 
339  link = next;
340  }
341  }
342 
343  /* Remove all current watches from previous watch handlers */
344 
345  if (watch_list->remove_watch_function != NULL)
346  {
347  _dbus_verbose ("Removing all pre-existing watches\n");
348 
349  _dbus_list_foreach (&watch_list->watches,
351  watch_list->watch_data);
352  }
353 
354  if (watch_list->watch_free_data_function != NULL)
355  (* watch_list->watch_free_data_function) (watch_list->watch_data);
356 
357  watch_list->add_watch_function = add_function;
358  watch_list->remove_watch_function = remove_function;
359  watch_list->watch_toggled_function = toggled_function;
360  watch_list->watch_data = data;
361  watch_list->watch_free_data_function = free_data_function;
362 
363  return TRUE;
364 }
365 
376  DBusWatch *watch)
377 {
378  if (!_dbus_list_append (&watch_list->watches, watch))
379  return FALSE;
380 
381  _dbus_watch_ref (watch);
382 
383  if (watch_list->add_watch_function != NULL)
384  {
385  _dbus_verbose ("Adding watch on fd %d\n",
386  dbus_watch_get_socket (watch));
387 
388  if (!(* watch_list->add_watch_function) (watch,
389  watch_list->watch_data))
390  {
391  _dbus_list_remove_last (&watch_list->watches, watch);
392  _dbus_watch_unref (watch);
393  return FALSE;
394  }
395  }
396 
397  return TRUE;
398 }
399 
407 void
409  DBusWatch *watch)
410 {
411  if (!_dbus_list_remove (&watch_list->watches, watch))
412  _dbus_assert_not_reached ("Nonexistent watch was removed");
413 
414  if (watch_list->remove_watch_function != NULL)
415  {
416  _dbus_verbose ("Removing watch on fd %d\n",
417  dbus_watch_get_socket (watch));
418 
419  (* watch_list->remove_watch_function) (watch,
420  watch_list->watch_data);
421  }
422 
423  _dbus_watch_unref (watch);
424 }
425 
434 void
436  DBusWatch *watch,
437  dbus_bool_t enabled)
438 {
439  enabled = !!enabled;
440 
441  if (enabled == watch->enabled)
442  return;
443 
444  watch->enabled = enabled;
445 
446  if (watch_list->watch_toggled_function != NULL)
447  {
448  _dbus_verbose ("Toggling watch %p on fd %d to %d\n",
449  watch, dbus_watch_get_socket (watch), watch->enabled);
450 
451  (* watch_list->watch_toggled_function) (watch,
452  watch_list->watch_data);
453  }
454 }
455 
463 void
465  dbus_bool_t enabled)
466 {
467  DBusList *link;
468 
469  for (link = _dbus_list_get_first_link (&watch_list->watches);
470  link != NULL;
471  link = _dbus_list_get_next_link (&watch_list->watches, link))
472  {
473  _dbus_watch_list_toggle_watch (watch_list, link->data, enabled);
474  }
475 }
476 
489 void
491  DBusWatchHandler handler,
492  void *data,
493  DBusFreeFunction free_data_function)
494 {
495  if (watch->free_handler_data_function)
496  (* watch->free_handler_data_function) (watch->handler_data);
497 
498  watch->handler = handler;
499  watch->handler_data = data;
500  watch->free_handler_data_function = free_data_function;
501 }
502 
534 int
536 {
537  _dbus_return_val_if_fail (watch != NULL, -1);
538 
539  return dbus_watch_get_unix_fd(watch);
540 }
541 
555 int
557 {
558  _dbus_return_val_if_fail (watch != NULL, -1);
559 
560  /* FIXME remove #ifdef and do this on a lower level
561  * (watch should have set_socket and set_unix_fd and track
562  * which it has, and the transport should provide the
563  * appropriate watch type)
564  */
565 #ifdef DBUS_UNIX
566  return watch->fd;
567 #else
568  return dbus_watch_get_socket( watch );
569 #endif
570 }
571 
584 int
586 {
587  _dbus_return_val_if_fail (watch != NULL, -1);
588 
589  return watch->fd;
590 }
591 
605 unsigned int
607 {
608  _dbus_return_val_if_fail (watch != NULL, 0);
609  _dbus_assert ((watch->flags & VALID_WATCH_FLAGS) == watch->flags);
610 
611  return watch->flags;
612 }
613 
621 void*
623 {
624  _dbus_return_val_if_fail (watch != NULL, NULL);
625 
626  return watch->data;
627 }
628 
640 void
642  void *data,
643  DBusFreeFunction free_data_function)
644 {
645  _dbus_return_if_fail (watch != NULL);
646 
647  _dbus_verbose ("Setting watch fd %d data to data = %p function = %p from data = %p function = %p\n",
648  dbus_watch_get_socket (watch),
649  data, free_data_function, watch->data, watch->free_data_function);
650 
651  if (watch->free_data_function != NULL)
652  (* watch->free_data_function) (watch->data);
653 
654  watch->data = data;
655  watch->free_data_function = free_data_function;
656 }
657 
667 {
668  _dbus_return_val_if_fail (watch != NULL, FALSE);
669 
670  return watch->enabled;
671 }
672 
673 
698  unsigned int flags)
699 {
700  _dbus_return_val_if_fail (watch != NULL, FALSE);
701 
702 #ifndef DBUS_DISABLE_CHECKS
703  if (watch->fd < 0 || watch->flags == 0)
704  {
705  _dbus_warn_check_failed ("Watch is invalid, it should have been removed\n");
706  return TRUE;
707  }
708 #endif
709 
710  _dbus_return_val_if_fail (watch->fd >= 0 /* fails if watch was removed */, TRUE);
711 
712  _dbus_watch_sanitize_condition (watch, &flags);
713 
714  if (flags == 0)
715  {
716  _dbus_verbose ("After sanitization, watch flags on fd %d were 0\n",
717  watch->fd);
718  return TRUE;
719  }
720  else
721  return (* watch->handler) (watch, flags,
722  watch->handler_data);
723 }
724 
725