diff options
Diffstat (limited to 'src/systemd/nm-sd-adapt.c')
-rw-r--r-- | src/systemd/nm-sd-adapt.c | 259 |
1 files changed, 67 insertions, 192 deletions
diff --git a/src/systemd/nm-sd-adapt.c b/src/systemd/nm-sd-adapt.c index 40d52e76b4..8e2de17ee9 100644 --- a/src/systemd/nm-sd-adapt.c +++ b/src/systemd/nm-sd-adapt.c @@ -20,233 +20,108 @@ #include "nm-sd-adapt.h" -#include <unistd.h> -#include <errno.h> - #include "sd-event.h" #include "fd-util.h" -#include "time-util.h" -struct sd_event_source { - guint refcount; - guint id; - gpointer user_data; - - GIOChannel *channel; - - union { - struct { - sd_event_io_handler_t cb; - } io; - struct { - sd_event_time_handler_t cb; - uint64_t usec; - } time; - }; -}; - -static struct sd_event_source * -source_new (void) -{ - struct sd_event_source *source; - - source = g_slice_new0 (struct sd_event_source); - source->refcount = 1; - return source; -} +/*****************************************************************************/ int -sd_event_source_set_priority (sd_event_source *s, int64_t priority) -{ - return 0; +asynchronous_close (int fd) { + safe_close (fd); + return -1; } -int -sd_event_source_set_enabled (sd_event_source *s, int m) -{ - /* TODO */ - g_return_val_if_reached (-EINVAL); -} +/***************************************************************************** + * Integrating sd_event into glib. Taken and adjusted from + * https://www.freedesktop.org/software/systemd/man/sd_event_get_fd.html + *****************************************************************************/ -int -sd_event_source_set_time (sd_event_source *s, uint64_t usec) -{ - /* TODO */ - g_return_val_if_reached (-EINVAL); -} +typedef struct SDEventSource { + GSource source; + GPollFD pollfd; + sd_event *event; +} SDEventSource; -sd_event_source* -sd_event_source_unref (sd_event_source *s) +static gboolean +event_prepare (GSource *source, gint *timeout_) { - - if (!s) - return NULL; - - g_return_val_if_fail (s->refcount, NULL); - - s->refcount--; - if (s->refcount == 0) { - if (s->id) - g_source_remove (s->id); - if (s->channel) { - /* Don't shut down the channel since systemd will soon close - * the file descriptor itself, which would cause -EBADF. - */ - g_io_channel_unref (s->channel); - } - g_slice_free (struct sd_event_source, s); - } - return NULL; + return sd_event_prepare (((SDEventSource *) source)->event) > 0; } -int -sd_event_source_set_description(sd_event_source *s, const char *description) +static gboolean +event_check (GSource *source) { - if (!s) - return -EINVAL; - - g_source_set_name_by_id (s->id, description); - return 0; + return sd_event_wait (((SDEventSource *) source)->event, 0) > 0; } static gboolean -io_ready (GIOChannel *channel, GIOCondition condition, struct sd_event_source *source) +event_dispatch (GSource *source, GSourceFunc callback, gpointer user_data) { - int r, revents = 0; - gboolean result; - - if (condition & G_IO_IN) - revents |= EPOLLIN; - if (condition & G_IO_OUT) - revents |= EPOLLOUT; - if (condition & G_IO_PRI) - revents |= EPOLLPRI; - if (condition & G_IO_ERR) - revents |= EPOLLERR; - if (condition & G_IO_HUP) - revents |= EPOLLHUP; - - source->refcount++; - - r = source->io.cb (source, g_io_channel_unix_get_fd (channel), revents, source->user_data); - if (r < 0 || source->refcount <= 1) { - source->id = 0; - result = G_SOURCE_REMOVE; - } else - result = G_SOURCE_CONTINUE; - - sd_event_source_unref (source); - - return result; + return sd_event_dispatch (((SDEventSource *)source)->event) > 0; } -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) +static void +event_finalize (GSource *source) { - struct sd_event_source *source; - GIOChannel *channel; - GIOCondition condition = 0; - - /* systemd supports floating sd_event_source by omitting the @s argument. - * We don't have such users and don't implement floating references. */ - g_return_val_if_fail (s, -EINVAL); - - channel = g_io_channel_unix_new (fd); - if (!channel) - return -EINVAL; - - source = source_new (); - source->io.cb = callback; - source->user_data = userdata; - source->channel = channel; - - if (events & EPOLLIN) - condition |= G_IO_IN; - if (events & EPOLLOUT) - condition |= G_IO_OUT; - if (events & EPOLLPRI) - condition |= G_IO_PRI; - if (events & EPOLLERR) - condition |= G_IO_ERR; - if (events & EPOLLHUP) - condition |= G_IO_HUP; - - g_io_channel_set_encoding (source->channel, NULL, NULL); - g_io_channel_set_buffered (source->channel, FALSE); - source->id = g_io_add_watch (source->channel, condition, (GIOFunc) io_ready, source); - - *s = source; - return 0; + sd_event_unref (((SDEventSource *) source)->event); } -static gboolean -time_ready (struct sd_event_source *source) +static SDEventSource * +event_create_source (sd_event *event) { - source->refcount++; + static GSourceFuncs event_funcs = { + .prepare = event_prepare, + .check = event_check, + .dispatch = event_dispatch, + .finalize = event_finalize, + }; + SDEventSource *source; - source->time.cb (source, source->time.usec, source->user_data); - source->id = 0; + g_return_val_if_fail (event, NULL); - sd_event_source_unref (source); + source = (SDEventSource *) g_source_new (&event_funcs, sizeof (SDEventSource)); - return G_SOURCE_REMOVE; -} + source->event = sd_event_ref (event); + source->pollfd.fd = sd_event_get_fd (event); + source->pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR; -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) -{ - struct sd_event_source *source; - uint64_t n = now (clock); - - /* systemd supports floating sd_event_source by omitting the @s argument. - * We don't have such users and don't implement floating references. */ - g_return_val_if_fail (s, -EINVAL); + g_source_add_poll ((GSource *) source, &source->pollfd); - source = source_new (); - source->time.cb = callback; - source->user_data = userdata; - source->time.usec = usec; + return source; +} - if (usec > 1000) - usec = n < usec - 1000 ? usec - n : 1000; - source->id = g_timeout_add (usec / 1000, (GSourceFunc) time_ready, source); +guint +nm_sd_event_attach (sd_event *event, GMainContext *context) +{ + SDEventSource *source; + guint id; + int r; + sd_event *e = event; - *s = source; - return 0; -} + if (!e) { -/* sd_event is basically a GMainContext; but since we only - * ever use the default context, nothing to do here. - */ + if (sd_event_default (NULL)) { + /* we *must* create a new default-event, otherwise it's unclear whether the + * existing event is already registered or whether we should re-register. + * Just don't allow that. */ + g_return_val_if_reached (0); + } -int -sd_event_default (sd_event **e) -{ - *e = GUINT_TO_POINTER (1); - return 0; -} + r = sd_event_default (&e); + if (r < 0) + g_return_val_if_reached (0); + } -sd_event* -sd_event_ref (sd_event *e) -{ - return e; -} + source = event_create_source (e); + id = g_source_attach ((GSource *) source, context); + g_source_unref ((GSource *) source); -sd_event* -sd_event_unref (sd_event *e) -{ - return NULL; -} + if (!event) + sd_event_unref (e); -int -sd_event_now (sd_event *e, clockid_t clock, uint64_t *usec) -{ - *usec = now (clock); - return 0; + g_return_val_if_fail (id, 0); + return id; } -int asynchronous_close(int fd) { - safe_close(fd); - return -1; -} +/*****************************************************************************/ |