udev: only create monitor in NMUdevClient when needed

GUdevClient always creates a monitor instance, even if there are no subsystems
or handlers defined. Hence the first iteration of NMUdevClient did that as
well.

I think that can be avoided however. We only need a monitor when there is
a event handler subscribed. Contrary to GUdevClient, we know that from the
very beginning.
This commit is contained in:
Thomas Haller 2017-03-20 12:37:53 +01:00
parent e32839838e
commit c48c13b8e5

View file

@ -134,9 +134,6 @@ monitor_event (GIOChannel *source,
if (!self->monitor)
goto out;
if (!self->event_handler)
goto out;
udevice = udev_monitor_receive_device (self->monitor);
if (udevice == NULL)
goto out;
@ -181,32 +178,34 @@ nm_udev_client_new (const char *const*subsystems,
goto fail;
/* connect to event source */
self->monitor = udev_monitor_new_from_netlink (self->udev, "udev");
if (!self->monitor)
goto fail;
if (self->event_handler) {
self->monitor = udev_monitor_new_from_netlink (self->udev, "udev");
if (!self->monitor)
goto fail;
if (self->subsystems) {
/* install subsystem filters to only wake up for certain events */
for (n = 0; self->subsystems[n]; n++) {
if (self->monitor) {
gs_free char *to_free;
const char *subsystem;
const char *devtype;
if (self->subsystems) {
/* install subsystem filters to only wake up for certain events */
for (n = 0; self->subsystems[n]; n++) {
if (self->monitor) {
gs_free char *to_free;
const char *subsystem;
const char *devtype;
_subsystem_split (self->subsystems[n], &subsystem, &devtype, &to_free);
udev_monitor_filter_add_match_subsystem_devtype (self->monitor, subsystem, devtype);
_subsystem_split (self->subsystems[n], &subsystem, &devtype, &to_free);
udev_monitor_filter_add_match_subsystem_devtype (self->monitor, subsystem, devtype);
}
}
}
/* listen to events, and buffer them */
if (self->monitor) {
udev_monitor_enable_receiving (self->monitor);
channel = g_io_channel_unix_new (udev_monitor_get_fd (self->monitor));
self->watch_source = g_io_create_watch (channel, G_IO_IN);
g_io_channel_unref (channel);
g_source_set_callback (self->watch_source, (GSourceFunc) monitor_event, self, NULL);
g_source_attach (self->watch_source, g_main_context_get_thread_default ());
g_source_unref (self->watch_source);
/* listen to events, and buffer them */
if (self->monitor) {
udev_monitor_enable_receiving (self->monitor);
channel = g_io_channel_unix_new (udev_monitor_get_fd (self->monitor));
self->watch_source = g_io_create_watch (channel, G_IO_IN);
g_io_channel_unref (channel);
g_source_set_callback (self->watch_source, (GSourceFunc) monitor_event, self, NULL);
g_source_attach (self->watch_source, g_main_context_get_thread_default ());
g_source_unref (self->watch_source);
}
}
}