[PATCH] Reduce direct D-Bus calls in isys/iface.c.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The first patch in a two (possibly three) patch series that replaces
some direct D-Bus code with libnm-glib calls.  Much less verbose,
easier to read, etc, etc.

Tested this patch locally and all seems well.  Calling it a day for
now.

[rawhide]
---
 isys/Makefile   |    8 ++--
 isys/iface.c    |   88 ++++++++++++-------------------------------------------
 isys/iface.h    |    4 +--
 loader/loader.c |   21 +------------
 4 files changed, 26 insertions(+), 95 deletions(-)

diff --git a/isys/Makefile b/isys/Makefile
index 595f4b8..14c7505 100644
--- a/isys/Makefile
+++ b/isys/Makefile
@@ -34,10 +34,6 @@ endif
 PYMODULES = _isys.so
 SUBDIRS = 
 
-# D-Bus
-LOADLIBES += $(shell pkg-config --libs dbus-1)
-CFLAGS    += $(shell pkg-config --cflags dbus-1)
-
 # NetworkManager
 LOADLIBES += $(shell pkg-config --libs NetworkManager)
 CFLAGS    += $(shell pkg-config --cflags NetworkManager)
@@ -46,6 +42,10 @@ CFLAGS    += $(shell pkg-config --cflags NetworkManager)
 LOADLIBES += $(shell pkg-config --libs libnl-1)
 CFLAGS    += $(shell pkg-config --cflags libnl-1)
 
+# libnm-glib
+LOADLIBES += $(shell pkg-config --libs libnm_glib)
+CFLAGS    += $(shell pkg-config --cflags libnm_glib)
+
 ifeq ($(ARCH),ppc)
 OBJECTS += minifind.o
 SOURCES += minifind.c
diff --git a/isys/iface.c b/isys/iface.c
index d0613e2..3e101fd 100644
--- a/isys/iface.c
+++ b/isys/iface.c
@@ -43,8 +43,10 @@
 #include <netlink/route/addr.h>
 #include <netlink/route/link.h>
 
+#include <glib.h>
 #include <dbus/dbus.h>
 #include <NetworkManager.h>
+#include <nm-client.h>
 
 #include "iface.h"
 #include "str.h"
@@ -446,99 +448,47 @@ int iface_have_in6_addr(struct in6_addr *addr6) {
 }
 
 /* Check if NM is already running */
-int is_nm_running(DBusConnection *connection, int *running, char **error_str)
-{
-    DBusError error;
-    DBusMessage *message, *reply;
-    const char *nm_service = NM_DBUS_SERVICE;
-    dbus_bool_t alive = FALSE;
-
-    message = dbus_message_new_method_call("org.freedesktop.DBus",
-                                           "/org/freedesktop/DBus",
-                                           "org.freedesktop.DBus",
-                                           "NameHasOwner");
-    if (!message)
-        return 33;
-
-    if (!dbus_message_append_args(message,
-                                  DBUS_TYPE_STRING, &nm_service,
-                                  DBUS_TYPE_INVALID)) {
-        dbus_message_unref(message);
-        return 34;
-    }
+gboolean is_nm_running(void) {
+    gboolean running;
+    NMClient *client = NULL;
 
-    dbus_error_init(&error);
-    reply = dbus_connection_send_with_reply_and_block(connection,
-                                                        message, 2000,
-                                                        &error);
-    if (!reply) {
-        if (dbus_error_is_set(&error)) {
-            *error_str = strdup(error.message);
-            dbus_error_free(&error);
-        }
+    g_type_init();
 
-        dbus_message_unref(message);
-        return 35;
-    }
+    client = nm_client_new();
+    if (!client)
+        return FALSE;
 
-    dbus_error_init(&error);
-    if (!dbus_message_get_args(reply, &error,
-                                DBUS_TYPE_BOOLEAN, &alive,
-                                DBUS_TYPE_INVALID)) {
-        if (dbus_error_is_set(&error)) {
-            *error_str = strdup(error.message);
-            dbus_error_free(&error);
-        }
-
-        dbus_message_unref(message);
-        dbus_message_unref(reply);
-        return 36;
-    }
-
-    *running = alive;
-
-    dbus_message_unref(message);
-    dbus_message_unref(reply);
-    return 0;
+    running = nm_client_get_manager_running(client);
+    g_object_unref(client);
+    return running;
 }
 
 /*
  * Wait for NetworkManager to appear on the system bus
  */
-int wait_for_nm(DBusConnection *connection, char **error_str) {
+int wait_for_nm(void) {
     int count = 0;
 
     /* send message and block until a reply or error comes back */
     while (count < 45) {
-        int running = 0, ret;
-
-        ret = is_nm_running(connection, &running, error_str);
-        if (ret != 0)
-            return ret;  /* error */
-        if (running)
-            return 0;  /* nm is alive */
+        if (is_nm_running())
+            return 0;
 
         sleep(1);
         count++;
     }
 
-    return 37;
+    return 1;
 }
 
 /*
  * Start NetworkManager -- requires that you have already written out the
  * control files in /etc/sysconfig for the interface.
  */
-int iface_start_NetworkManager(DBusConnection *connection, char **error) {
+int iface_start_NetworkManager(void) {
     pid_t pid;
-    int ret, running = 0;
-    char *ignore = NULL;
-
-    ret = is_nm_running(connection, &running, &ignore);
-    if (ignore)
-        free(ignore);
 
-    if (ret == 0 && running)
+    if (is_nm_running())
         return 0;  /* already running */
 
     /* Start NetworkManager */
@@ -562,7 +512,7 @@ int iface_start_NetworkManager(DBusConnection *connection, char **error) {
     } else if (pid == -1) {
         return 1;
     } else {
-        return wait_for_nm(connection, error);
+        return wait_for_nm();
     }
 
     return 0;
diff --git a/isys/iface.h b/isys/iface.h
index 497feaa..5cd8614 100644
--- a/isys/iface.h
+++ b/isys/iface.h
@@ -27,8 +27,6 @@
 #include <netlink/cache.h>
 #include <netlink/socket.h>
 
-#include <dbus/dbus.h>
-
 /* Enumerated types used in iface.c as well as loader's network code */
 enum { IPUNUSED, IPV4, IPV6 };
 
@@ -150,7 +148,7 @@ int iface_have_in6_addr(struct in6_addr *addr6);
 /*
  * Start NetworkManager
  */
-int iface_start_NetworkManager(DBusConnection *connection, char **error);
+int iface_start_NetworkManager(void);
 
 /*
  * Set Maximum Transfer Unit (MTU) on specified interface
diff --git a/loader/loader.c b/loader/loader.c
index a028f1c..ec54d6f 100644
--- a/loader/loader.c
+++ b/loader/loader.c
@@ -1781,10 +1781,6 @@ int main(int argc, char ** argv) {
 
     struct loaderData_s loaderData;
 
-    char *error_str = NULL;
-    DBusError error;
-    DBusConnection *connection = NULL;
-
     char *path;
     char * cmdLine = NULL;
     char * ksFile = NULL;
@@ -1950,21 +1946,8 @@ int main(int argc, char ** argv) {
     }
 
     /* Start NetworkManager now so it's always available to talk to. */
-    dbus_error_init(&error);
-    connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
-    if (connection == NULL) {
-        if (dbus_error_is_set(&error)) {
-            logMessage(DEBUGLVL, "%s (%d): %s: %s", __func__,
-                       __LINE__, error.name, error.message);
-            dbus_error_free(&error);
-        }
-    } else {
-        rc = iface_start_NetworkManager(connection, &error_str);
-        if (rc != 0) {
-            logMessage(INFO, "failed to start NetworkManager (%d): error %d (%s)",
-                       __LINE__, rc, error_str ? error_str : "unknown");
-        }
-    }
+    if (iface_start_NetworkManager())
+        logMessage(INFO, "failed to start NetworkManager");
 
     if (!FL_CMDLINE(flags))
         startNewt();
-- 
1.6.0.3

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/anaconda-devel-list

[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux