Add basic support for monitoring udev events
authorMarcel Holtmann <marcel@holtmann.org>
Thu, 1 Jan 2009 20:06:15 +0000 (21:06 +0100)
committerMarcel Holtmann <marcel@holtmann.org>
Thu, 1 Jan 2009 20:06:15 +0000 (21:06 +0100)
src/connman.h
src/main.c
src/udev.c

index 4f781bd..5dafe76 100644 (file)
@@ -116,6 +116,9 @@ static inline void __connman_element_unlock(struct connman_element *element)
 int __connman_detect_init(void);
 void __connman_detect_cleanup(void);
 
+int __connman_udev_init(void);
+void __connman_udev_cleanup(void);
+
 #include <connman/device.h>
 
 int __connman_device_init(void);
index 8af1d6d..65cc9d4 100644 (file)
@@ -163,6 +163,7 @@ int main(int argc, char *argv[])
        __connman_profile_init(conn);
 
        __connman_rtnl_init();
+       __connman_udev_init();
 
        __connman_plugin_init();
 
@@ -181,6 +182,7 @@ int main(int argc, char *argv[])
 
        __connman_plugin_cleanup();
 
+       __connman_udev_cleanup();
        __connman_rtnl_cleanup();
 
        __connman_profile_cleanup();
index 4896a1d..fcb0c4c 100644 (file)
 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
 #include <libudev.h>
 
+#include <glib.h>
+
 #include "connman.h"
+
+static gboolean udev_event(GIOChannel *channel,
+                               GIOCondition condition, gpointer user_data)
+{
+       struct udev_monitor *monitor = user_data;
+       struct udev_device *device;
+       const char *action, *sysname;
+
+       device = udev_monitor_receive_device(monitor);
+       if (device == NULL)
+               return TRUE;
+
+       action = udev_device_get_action(device);
+       if (action == NULL)
+               return TRUE;
+
+       sysname = udev_device_get_sysname(device);
+
+       DBG("%s %s", action, sysname);
+
+       return TRUE;
+}
+
+static struct udev *udev_ctx;
+static struct udev_monitor *udev_mon;
+static guint udev_watch = 0;
+
+int __connman_udev_init(void)
+{
+       GIOChannel *channel;
+       int fd;
+
+       DBG("");
+
+       udev_ctx = udev_new();
+       if (udev_ctx == NULL) {
+               connman_error("Failed to create udev context");
+               return -1;
+       }
+
+       udev_mon = udev_monitor_new_from_socket(udev_ctx,
+                                               "@/org/moblin/connman/udev");
+       if (udev_mon == NULL) {
+               connman_error("Failed to create udev monitor");
+               udev_unref(udev_ctx);
+               udev_ctx = NULL;
+               return -1;
+       }
+
+       if (udev_monitor_enable_receiving(udev_mon) < 0) {
+               connman_error("Failed to enable udev monitor");
+               udev_unref(udev_ctx);
+               udev_ctx = NULL;
+               udev_monitor_unref(udev_mon);
+               return -1;
+       }
+
+       fd = udev_monitor_get_fd(udev_mon);
+
+       channel = g_io_channel_unix_new(fd);
+       if (channel == NULL)
+               return 0;
+
+       udev_watch = g_io_add_watch(channel, G_IO_IN, udev_event, udev_mon);
+
+       g_io_channel_unref(channel);
+
+       return 0;
+}
+
+void __connman_udev_cleanup(void)
+{
+       DBG("");
+
+       if (udev_watch > 0)
+               g_source_remove(udev_watch);
+
+       if (udev_ctx == NULL)
+               return;
+
+       udev_monitor_unref(udev_mon);
+       udev_unref(udev_ctx);
+}