sd-event.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef foosdeventhfoo
  2. #define foosdeventhfoo
  3. /***
  4. This file is part of systemd.
  5. Copyright 2013 Lennart Poettering
  6. systemd is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published by
  8. the Free Software Foundation; either version 2.1 of the License, or
  9. (at your option) any later version.
  10. systemd is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with systemd; If not, see <http://www.gnu.org/licenses/>.
  16. ***/
  17. #include <inttypes.h>
  18. #include <signal.h>
  19. #include <sys/epoll.h>
  20. #include <sys/signalfd.h>
  21. #include <sys/types.h>
  22. #include "_sd-common.h"
  23. /*
  24. Why is this better than pure epoll?
  25. - Supports event source prioritization
  26. - Scales better with a large number of time events because it does not require one timerfd each
  27. - Automatically tries to coalesce timer events system-wide
  28. - Handles signals and child PIDs
  29. */
  30. _SD_BEGIN_DECLARATIONS;
  31. typedef struct sd_event sd_event;
  32. typedef struct sd_event_source sd_event_source;
  33. enum {
  34. SD_EVENT_OFF = 0,
  35. SD_EVENT_ON = 1,
  36. SD_EVENT_ONESHOT = -1
  37. };
  38. enum {
  39. SD_EVENT_INITIAL,
  40. SD_EVENT_ARMED,
  41. SD_EVENT_PENDING,
  42. SD_EVENT_RUNNING,
  43. SD_EVENT_EXITING,
  44. SD_EVENT_FINISHED,
  45. SD_EVENT_PREPARING
  46. };
  47. enum {
  48. /* And everything in-between and outside is good too */
  49. SD_EVENT_PRIORITY_IMPORTANT = -100,
  50. SD_EVENT_PRIORITY_NORMAL = 0,
  51. SD_EVENT_PRIORITY_IDLE = 100
  52. };
  53. typedef int (*sd_event_handler_t)(sd_event_source *s, void *userdata);
  54. typedef int (*sd_event_io_handler_t)(sd_event_source *s, int fd, uint32_t revents, void *userdata);
  55. typedef int (*sd_event_time_handler_t)(sd_event_source *s, uint64_t usec, void *userdata);
  56. typedef int (*sd_event_signal_handler_t)(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata);
  57. #if defined __USE_POSIX199309 || defined __USE_XOPEN_EXTENDED
  58. typedef int (*sd_event_child_handler_t)(sd_event_source *s, const siginfo_t *si, void *userdata);
  59. #else
  60. typedef void* sd_event_child_handler_t;
  61. #endif
  62. int sd_event_default(sd_event **e);
  63. int sd_event_new(sd_event **e);
  64. sd_event* sd_event_ref(sd_event *e);
  65. sd_event* sd_event_unref(sd_event *e);
  66. int sd_event_add_io(sd_event *e, sd_event_source **s, int fd, uint32_t events, sd_event_io_handler_t callback, void *userdata);
  67. int sd_event_add_time(sd_event *e, sd_event_source **s, clockid_t clock, uint64_t usec, uint64_t accuracy, sd_event_time_handler_t callback, void *userdata);
  68. int sd_event_add_signal(sd_event *e, sd_event_source **s, int sig, sd_event_signal_handler_t callback, void *userdata);
  69. int sd_event_add_child(sd_event *e, sd_event_source **s, pid_t pid, int options, sd_event_child_handler_t callback, void *userdata);
  70. int sd_event_add_defer(sd_event *e, sd_event_source **s, sd_event_handler_t callback, void *userdata);
  71. int sd_event_add_post(sd_event *e, sd_event_source **s, sd_event_handler_t callback, void *userdata);
  72. int sd_event_add_exit(sd_event *e, sd_event_source **s, sd_event_handler_t callback, void *userdata);
  73. int sd_event_prepare(sd_event *e);
  74. int sd_event_wait(sd_event *e, uint64_t usec);
  75. int sd_event_dispatch(sd_event *e);
  76. int sd_event_run(sd_event *e, uint64_t usec);
  77. int sd_event_loop(sd_event *e);
  78. int sd_event_exit(sd_event *e, int code);
  79. int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec);
  80. int sd_event_get_fd(sd_event *e);
  81. int sd_event_get_state(sd_event *e);
  82. int sd_event_get_tid(sd_event *e, pid_t *tid);
  83. int sd_event_get_exit_code(sd_event *e, int *code);
  84. int sd_event_set_watchdog(sd_event *e, int b);
  85. int sd_event_get_watchdog(sd_event *e);
  86. sd_event_source* sd_event_source_ref(sd_event_source *s);
  87. sd_event_source* sd_event_source_unref(sd_event_source *s);
  88. sd_event *sd_event_source_get_event(sd_event_source *s);
  89. void* sd_event_source_get_userdata(sd_event_source *s);
  90. void* sd_event_source_set_userdata(sd_event_source *s, void *userdata);
  91. int sd_event_source_set_description(sd_event_source *s, const char *description);
  92. int sd_event_source_get_description(sd_event_source *s, const char **description);
  93. int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback);
  94. int sd_event_source_get_pending(sd_event_source *s);
  95. int sd_event_source_get_priority(sd_event_source *s, int64_t *priority);
  96. int sd_event_source_set_priority(sd_event_source *s, int64_t priority);
  97. int sd_event_source_get_enabled(sd_event_source *s, int *enabled);
  98. int sd_event_source_set_enabled(sd_event_source *s, int enabled);
  99. int sd_event_source_get_io_fd(sd_event_source *s);
  100. int sd_event_source_set_io_fd(sd_event_source *s, int fd);
  101. int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events);
  102. int sd_event_source_set_io_events(sd_event_source *s, uint32_t events);
  103. int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents);
  104. int sd_event_source_get_time(sd_event_source *s, uint64_t *usec);
  105. int sd_event_source_set_time(sd_event_source *s, uint64_t usec);
  106. int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec);
  107. int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec);
  108. int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock);
  109. int sd_event_source_get_signal(sd_event_source *s);
  110. int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid);
  111. /* Define helpers so that __attribute__((cleanup(sd_event_unrefp))) and similar may be used. */
  112. _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_event, sd_event_unref);
  113. _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_event_source, sd_event_source_unref);
  114. _SD_END_DECLARATIONS;
  115. #endif