dhcp: fall back to other clients when an invalid one is specified
authorBeniamino Galvani <bgalvani@redhat.com>
Thu, 24 Mar 2016 09:16:39 +0000 (10:16 +0100)
committerBeniamino Galvani <bgalvani@redhat.com>
Thu, 24 Mar 2016 14:03:04 +0000 (15:03 +0100)
If the user specifies an invalid 'dhcp' option in configuration,
currently we disable DHCP. Instead, fall back to other available
clients, as we do for other options.

src/dhcp-manager/nm-dhcp-manager.c

index 44c3365..de7c895 100644 (file)
@@ -113,7 +113,7 @@ find_client_desc (const char *name, GType gtype)
 }
 
 static GType
-is_client_enabled (const char *name, GError **error)
+is_client_enabled (const char *name)
 {
        ClientDesc *desc;
 
@@ -121,9 +121,6 @@ is_client_enabled (const char *name, GError **error)
        if (desc && (!desc->get_path_func || desc->get_path_func()))
                return desc->gtype;
 
-       g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED,
-                    _("'%s' support not found or not enabled."),
-                    name);
        return G_TYPE_INVALID;
 }
 
@@ -153,28 +150,6 @@ get_client_for_ifindex (NMDhcpManager *manager, int ifindex, gboolean ip6)
        return NULL;
 }
 
-static GType
-get_client_type (const char *client, GError **error)
-{
-       GType client_gtype;
-
-       if (client)
-               client_gtype = is_client_enabled (client, error);
-       else {
-               /* Fallbacks */
-               client_gtype = is_client_enabled ("dhclient", NULL);
-               if (client_gtype == G_TYPE_INVALID)
-                       client_gtype = is_client_enabled ("dhcpcd", NULL);
-               if (client_gtype == G_TYPE_INVALID)
-                       client_gtype = is_client_enabled ("internal", NULL);
-               if (client_gtype == G_TYPE_INVALID) {
-                       g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED,
-                                                _("no usable DHCP client could be found."));
-               }
-       }
-       return client_gtype;
-}
-
 static void client_state_changed (NMDhcpClient *client,
                                   NMDhcpState state,
                                   GObject *ip_config,
@@ -385,8 +360,8 @@ nm_dhcp_manager_init (NMDhcpManager *self)
        NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
        NMConfig *config = nm_config_get ();
        const char *client;
-       GError *error = NULL;
        GSList *iter;
+       GType type = G_TYPE_INVALID;
 
        for (iter = client_descs; iter; iter = iter->next) {
                ClientDesc *desc = iter->data;
@@ -403,16 +378,26 @@ nm_dhcp_manager_init (NMDhcpManager *self)
                client = "internal";
        }
 
-       priv->client_type = get_client_type (client, &error);
-       if (priv->client_type == G_TYPE_INVALID) {
-               nm_log_warn (LOGD_DHCP, "No usable DHCP client found (%s)! DHCP configurations will fail.",
-                            error->message);
-       } else {
-               nm_log_dbg (LOGD_DHCP, "Using DHCP client '%s'", find_client_desc (NULL, priv->client_type)->name);
+       if (client)
+               type = is_client_enabled (client);
+
+       if (type == G_TYPE_INVALID) {
+               if (client)
+                       nm_log_warn (LOGD_DHCP, "DHCP client '%s' not available", client);
 
+               type = is_client_enabled ("dhclient");
+               if (type == G_TYPE_INVALID)
+                       type = is_client_enabled ("dhcpcd");
+               if (type == G_TYPE_INVALID)
+                       type = is_client_enabled ("internal");
        }
-       g_clear_error (&error);
 
+       if (type == G_TYPE_INVALID)
+               nm_log_warn (LOGD_DHCP, "No usable DHCP client found! DHCP configurations will fail");
+       else
+               nm_log_info (LOGD_DHCP, "Using DHCP client '%s'", find_client_desc (NULL, type)->name);
+
+       priv->client_type = type;
        priv->clients = g_hash_table_new_full (g_direct_hash, g_direct_equal,
                                               NULL,
                                               (GDestroyNotify) g_object_unref);