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