2019-09-10 11:19:01 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2019-09-25 13:13:40 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2014 - 2016 Red Hat, Inc.
|
2016-03-11 10:25:40 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "nm-default.h"
|
|
|
|
|
|
|
|
|
|
#include "nm-sd.h"
|
|
|
|
|
|
|
|
|
|
#include "sd-event.h"
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
|
* Integrating sd_event into glib. Taken and adjusted from
|
|
|
|
|
* https://www.freedesktop.org/software/systemd/man/sd_event_get_fd.html
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
typedef struct SDEventSource {
|
|
|
|
|
GSource source;
|
|
|
|
|
GPollFD pollfd;
|
|
|
|
|
sd_event *event;
|
|
|
|
|
} SDEventSource;
|
|
|
|
|
|
|
|
|
|
static gboolean
|
all: don't use gchar/gshort/gint/glong but C types
We commonly don't use the glib typedefs for char/short/int/long,
but their C types directly.
$ git grep '\<g\(char\|short\|int\|long\|float\|double\)\>' | wc -l
587
$ git grep '\<\(char\|short\|int\|long\|float\|double\)\>' | wc -l
21114
One could argue that using the glib typedefs is preferable in
public API (of our glib based libnm library) or where it clearly
is related to glib, like during
g_object_set (obj, PROPERTY, (gint) value, NULL);
However, that argument does not seem strong, because in practice we don't
follow that argument today, and seldomly use the glib typedefs.
Also, the style guide for this would be hard to formalize, because
"using them where clearly related to a glib" is a very loose suggestion.
Also note that glib typedefs will always just be typedefs of the
underlying C types. There is no danger of glib changing the meaning
of these typedefs (because that would be a major API break of glib).
A simple style guide is instead: don't use these typedefs.
No manual actions, I only ran the bash script:
FILES=($(git ls-files '*.[hc]'))
sed -i \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>\( [^ ]\)/\1\2/g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\> /\1 /g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>/\1/g' \
"${FILES[@]}"
2018-07-11 07:40:19 +02:00
|
|
|
event_prepare (GSource *source, int *timeout_)
|
2016-03-11 10:25:40 +01:00
|
|
|
{
|
|
|
|
|
return sd_event_prepare (((SDEventSource *) source)->event) > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
event_check (GSource *source)
|
|
|
|
|
{
|
|
|
|
|
return sd_event_wait (((SDEventSource *) source)->event, 0) > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
event_dispatch (GSource *source, GSourceFunc callback, gpointer user_data)
|
|
|
|
|
{
|
2019-11-08 10:51:31 +01:00
|
|
|
return sd_event_dispatch (((SDEventSource *) source)->event) > 0;
|
2016-03-11 10:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
event_finalize (GSource *source)
|
|
|
|
|
{
|
2019-11-08 10:51:31 +01:00
|
|
|
SDEventSource *s = (SDEventSource *) source;
|
2016-03-11 10:25:40 +01:00
|
|
|
|
|
|
|
|
sd_event_unref (s->event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static SDEventSource *
|
2019-11-08 10:51:31 +01:00
|
|
|
event_create_source (sd_event *event)
|
2016-03-11 10:25:40 +01:00
|
|
|
{
|
2019-11-08 10:51:31 +01:00
|
|
|
static const GSourceFuncs event_funcs = {
|
2016-03-11 10:25:40 +01:00
|
|
|
.prepare = event_prepare,
|
|
|
|
|
.check = event_check,
|
|
|
|
|
.dispatch = event_dispatch,
|
|
|
|
|
.finalize = event_finalize,
|
|
|
|
|
};
|
|
|
|
|
SDEventSource *source;
|
2019-11-08 10:51:31 +01:00
|
|
|
gboolean is_default_event = FALSE;
|
|
|
|
|
int r;
|
2016-03-11 10:25:40 +01:00
|
|
|
|
2019-11-08 10:51:31 +01:00
|
|
|
if (!event) {
|
|
|
|
|
is_default_event = TRUE;
|
|
|
|
|
r = sd_event_default (&event);
|
|
|
|
|
if (r < 0)
|
|
|
|
|
g_return_val_if_reached (NULL);
|
|
|
|
|
}
|
2016-03-11 10:25:40 +01:00
|
|
|
|
2019-11-08 10:51:31 +01:00
|
|
|
source = (SDEventSource *) g_source_new ((GSourceFuncs *) &event_funcs, sizeof (SDEventSource));
|
2016-03-11 10:25:40 +01:00
|
|
|
|
2019-11-08 10:51:31 +01:00
|
|
|
source->event = is_default_event
|
|
|
|
|
? g_steal_pointer (&event)
|
|
|
|
|
: sd_event_ref (event);
|
2016-03-11 10:25:40 +01:00
|
|
|
|
2019-11-08 10:51:31 +01:00
|
|
|
source->pollfd = (GPollFD) {
|
|
|
|
|
.fd = sd_event_get_fd (source->event),
|
|
|
|
|
.events = G_IO_IN | G_IO_HUP | G_IO_ERR,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
g_source_add_poll (&source->source, &source->pollfd);
|
2016-03-11 10:25:40 +01:00
|
|
|
|
|
|
|
|
return source;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static guint
|
|
|
|
|
event_attach (sd_event *event, GMainContext *context)
|
|
|
|
|
{
|
|
|
|
|
SDEventSource *source;
|
|
|
|
|
guint id;
|
|
|
|
|
|
2019-11-08 10:51:31 +01:00
|
|
|
source = event_create_source (event);
|
2016-03-11 10:25:40 +01:00
|
|
|
|
2019-11-08 10:51:31 +01:00
|
|
|
g_return_val_if_fail (source, 0);
|
2016-03-11 10:25:40 +01:00
|
|
|
|
|
|
|
|
id = g_source_attach ((GSource *) source, context);
|
|
|
|
|
g_source_unref ((GSource *) source);
|
|
|
|
|
|
2019-11-08 10:51:31 +01:00
|
|
|
nm_assert (id != 0);
|
2016-03-11 10:25:40 +01:00
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guint
|
|
|
|
|
nm_sd_event_attach_default (void)
|
|
|
|
|
{
|
|
|
|
|
return event_attach (NULL, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2016-08-11 17:33:12 +02:00
|
|
|
/* ensure that defines in nm-sd.h correspond to the internal defines. */
|
|
|
|
|
|
2018-12-28 18:11:16 +01:00
|
|
|
#include "nm-sd-adapt-core.h"
|
2016-08-11 17:33:12 +02:00
|
|
|
#include "dhcp-lease-internal.h"
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|