- * Internal callback for evloop management.
- * The effect of this function is hidden: it exits
- * the waiting poll if any. Then it wakes up a thread
- * awaiting the evloop using signal.
- */
-static int on_evloop_efd(sd_event_source *s, int fd, uint32_t revents, void *userdata)
-{
- evloop_on_efd_event();
- return 1;
-}
-
-/* temporary hack */
-#if !defined(REMOVE_SYSTEMD_EVENT)
-__attribute__((unused))
-#endif
-static void evloop_callback(void *arg, uint32_t event, struct fdev *fdev)
-{
- sig_monitor(0, evloop_run, arg);
-}
-
-/**
- * Gets a sd_event item for the current thread.
- * @return a sd_event or NULL in case of error
- */
-static struct sd_event *get_sd_event_locked()
-{
- int rc;
-
- /* creates the evloop on need */
- if (!evloop.sdev) {
- /* start the creation */
- evloop.state = 0;
- /* creates the eventfd for waking up polls */
- evloop.efd = eventfd(0, EFD_CLOEXEC|EFD_SEMAPHORE);
- if (evloop.efd < 0) {
- ERROR("can't make eventfd for events");
- goto error1;
- }
- /* create the systemd event loop */
- rc = sd_event_new(&evloop.sdev);
- if (rc < 0) {
- ERROR("can't make new event loop");
- goto error2;
- }
- /* put the eventfd in the event loop */
- rc = sd_event_add_io(evloop.sdev, NULL, evloop.efd, EPOLLIN, on_evloop_efd, NULL);
- if (rc < 0) {
- ERROR("can't register eventfd");
-#if !defined(REMOVE_SYSTEMD_EVENT)
- sd_event_unref(evloop.sdev);
- evloop.sdev = NULL;
-error2:
- close(evloop.efd);
-error1:
- return NULL;
- }
-#else
- goto error3;
- }
- /* handle the event loop */
- evloop.fdev = fdev_epoll_add(get_fdevepoll(), sd_event_get_fd(evloop.sdev));
- if (!evloop.fdev) {
- ERROR("can't create fdev");
-error3:
- sd_event_unref(evloop.sdev);
-error2:
- close(evloop.efd);
-error1:
- memset(&evloop, 0, sizeof evloop);
- return NULL;
- }
- fdev_set_autoclose(evloop.fdev, 0);
- fdev_set_events(evloop.fdev, EPOLLIN);
- fdev_set_callback(evloop.fdev, evloop_callback, NULL);
-#endif
- }
-
- /* acquire the event loop */
- evloop_acquire();
-
- return evloop.sdev;
-}
-
-/**
- * Gets a sd_event item for the current thread.
- * @return a sd_event or NULL in case of error