|
Packit Service |
b23acc |
// SPDX-License-Identifier: GPL-2.0+
|
|
Packit Service |
b23acc |
/*
|
|
Packit Service |
b23acc |
* Copyright (C) 2014 - 2016 Red Hat, Inc.
|
|
Packit Service |
b23acc |
*/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
#include "nm-default.h"
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
#include "nm-sd.h"
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
#include "sd-event.h"
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/*****************************************************************************
|
|
Packit Service |
b23acc |
* Integrating sd_event into glib. Taken and adjusted from
|
|
Packit Service |
b23acc |
* https://www.freedesktop.org/software/systemd/man/sd_event_get_fd.html
|
|
Packit Service |
b23acc |
*****************************************************************************/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
typedef struct SDEventSource {
|
|
Packit Service |
b23acc |
GSource source;
|
|
Packit Service |
b23acc |
GPollFD pollfd;
|
|
Packit Service |
b23acc |
sd_event *event;
|
|
Packit Service |
b23acc |
} SDEventSource;
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
static gboolean
|
|
Packit Service |
b23acc |
event_prepare (GSource *source, int *timeout_)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
return sd_event_prepare (((SDEventSource *) source)->event) > 0;
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
static gboolean
|
|
Packit Service |
b23acc |
event_check (GSource *source)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
return sd_event_wait (((SDEventSource *) source)->event, 0) > 0;
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
static gboolean
|
|
Packit Service |
b23acc |
event_dispatch (GSource *source, GSourceFunc callback, gpointer user_data)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
return sd_event_dispatch (((SDEventSource *) source)->event) > 0;
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
static void
|
|
Packit Service |
b23acc |
event_finalize (GSource *source)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
SDEventSource *s = (SDEventSource *) source;
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
sd_event_unref (s->event);
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
static SDEventSource *
|
|
Packit Service |
b23acc |
event_create_source (sd_event *event)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
static const GSourceFuncs event_funcs = {
|
|
Packit Service |
b23acc |
.prepare = event_prepare,
|
|
Packit Service |
b23acc |
.check = event_check,
|
|
Packit Service |
b23acc |
.dispatch = event_dispatch,
|
|
Packit Service |
b23acc |
.finalize = event_finalize,
|
|
Packit Service |
b23acc |
};
|
|
Packit Service |
b23acc |
SDEventSource *source;
|
|
Packit Service |
b23acc |
gboolean is_default_event = FALSE;
|
|
Packit Service |
b23acc |
int r;
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
if (!event) {
|
|
Packit Service |
b23acc |
is_default_event = TRUE;
|
|
Packit Service |
b23acc |
r = sd_event_default (&event);
|
|
Packit Service |
b23acc |
if (r < 0)
|
|
Packit Service |
b23acc |
g_return_val_if_reached (NULL);
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
source = (SDEventSource *) g_source_new ((GSourceFuncs *) &event_funcs, sizeof (SDEventSource));
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
source->event = is_default_event
|
|
Packit Service |
b23acc |
? g_steal_pointer (&event)
|
|
Packit Service |
b23acc |
: sd_event_ref (event);
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
source->pollfd = (GPollFD) {
|
|
Packit Service |
b23acc |
.fd = sd_event_get_fd (source->event),
|
|
Packit Service |
b23acc |
.events = G_IO_IN | G_IO_HUP | G_IO_ERR,
|
|
Packit Service |
b23acc |
};
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
g_source_add_poll (&source->source, &source->pollfd);
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
return source;
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
static guint
|
|
Packit Service |
b23acc |
event_attach (sd_event *event, GMainContext *context)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
SDEventSource *source;
|
|
Packit Service |
b23acc |
guint id;
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
source = event_create_source (event);
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
g_return_val_if_fail (source, 0);
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
id = g_source_attach ((GSource *) source, context);
|
|
Packit Service |
b23acc |
g_source_unref ((GSource *) source);
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
nm_assert (id != 0);
|
|
Packit Service |
b23acc |
return id;
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
guint
|
|
Packit Service |
b23acc |
nm_sd_event_attach_default (void)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
return event_attach (NULL, NULL);
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/*****************************************************************************/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/* ensure that defines in nm-sd.h correspond to the internal defines. */
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
#include "nm-sd-adapt-core.h"
|
|
Packit Service |
b23acc |
#include "dhcp-lease-internal.h"
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/*****************************************************************************/
|
|
Packit Service |
b23acc |
|