summary refs log tree commit diff
path: root/src/dhcp/nm-dhcp-dhclient.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dhcp/nm-dhcp-dhclient.c')
-rw-r--r--src/dhcp/nm-dhcp-dhclient.c177
1 files changed, 70 insertions, 107 deletions
diff --git a/src/dhcp/nm-dhcp-dhclient.c b/src/dhcp/nm-dhcp-dhclient.c
index 46d2339b..bf93c831 100644
--- a/src/dhcp/nm-dhcp-dhclient.c
+++ b/src/dhcp/nm-dhcp-dhclient.c
@@ -174,14 +174,13 @@ merge_dhclient_config (NMDhcpDhclient *self,
                        GBytes **out_new_client_id,
                        GError **error)
 {
-	gs_free char *orig = NULL;
-	gs_free char *new = NULL;
+	char *orig = NULL, *new;
+	gboolean success = FALSE;
 
-	g_return_val_if_fail (iface, FALSE);
-	g_return_val_if_fail (conf_file, FALSE);
+	g_return_val_if_fail (iface != NULL, FALSE);
+	g_return_val_if_fail (conf_file != NULL, FALSE);
 
-	if (   orig_path
-	    && g_file_test (orig_path, G_FILE_TEST_EXISTS)) {
+	if (orig_path && g_file_test (orig_path, G_FILE_TEST_EXISTS)) {
 		GError *read_error = NULL;
 
 		if (!g_file_get_contents (orig_path, &orig, NULL, &read_error)) {
@@ -191,22 +190,14 @@ merge_dhclient_config (NMDhcpDhclient *self,
 		}
 	}
 
-	new = nm_dhcp_dhclient_create_config (iface,
-	                                      addr_family,
-	                                      client_id,
-	                                      anycast_addr,
-	                                      hostname,
-	                                      timeout,
-	                                      use_fqdn,
-	                                      orig_path,
-	                                      orig,
-	                                      out_new_client_id);
+	new = nm_dhcp_dhclient_create_config (iface, addr_family, client_id, anycast_addr, hostname, timeout,
+	                                      use_fqdn, orig_path, orig, out_new_client_id);
 	g_assert (new);
+	success = g_file_set_contents (conf_file, new, -1, error);
+	g_free (new);
+	g_free (orig);
 
-	return g_file_set_contents (conf_file,
-	                            new,
-	                            -1,
-	                            error);
+	return success;
 }
 
 static char *
@@ -291,14 +282,13 @@ create_dhclient_config (NMDhcpDhclient *self,
                         gboolean use_fqdn,
                         GBytes **out_new_client_id)
 {
-	gs_free char *orig = NULL;
-	char *new = NULL;
+	char *orig = NULL, *new = NULL;
 	GError *error = NULL;
+	gboolean success = FALSE;
 
 	g_return_val_if_fail (iface != NULL, NULL);
 
 	new = g_strdup_printf (NMSTATEDIR "/dhclient%s-%s.conf", _addr_family_to_path_part (addr_family), iface);
-
 	_LOGD ("creating composite dhclient config %s", new);
 
 	orig = find_existing_config (self, addr_family, iface, uuid);
@@ -307,12 +297,15 @@ create_dhclient_config (NMDhcpDhclient *self,
 	else
 		_LOGD ("no existing dhclient configuration to merge");
 
-	if (!merge_dhclient_config (self, addr_family, iface, new, client_id, dhcp_anycast_addr,
-	                            hostname, timeout, use_fqdn, orig, out_new_client_id, &error)) {
+	error = NULL;
+	success = merge_dhclient_config (self, addr_family, iface, new, client_id, dhcp_anycast_addr,
+	                                 hostname, timeout, use_fqdn, orig, out_new_client_id, &error);
+	if (!success) {
 		_LOGW ("error creating dhclient configuration: %s", error->message);
-		g_clear_error (&error);
+		g_error_free (error);
 	}
 
+	g_free (orig);
 	return new;
 }
 
@@ -322,14 +315,13 @@ dhclient_start (NMDhcpClient *client,
                 GBytes *duid,
                 gboolean release,
                 pid_t *out_pid,
-                int prefixes,
-                GError **error)
+                int prefixes)
 {
 	NMDhcpDhclient *self = NM_DHCP_DHCLIENT (client);
 	NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (self);
 	gs_unref_ptrarray GPtrArray *argv = NULL;
 	pid_t pid;
-	gs_free_error GError *local = NULL;
+	GError *error = NULL;
 	const char *iface;
 	const char *uuid;
 	const char *system_bus_address;
@@ -347,7 +339,7 @@ dhclient_start (NMDhcpClient *client,
 
 	dhclient_path = nm_dhcp_dhclient_get_path ();
 	if (!dhclient_path) {
-		nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhclient binary not found");
+		_LOGW ("dhclient could not be found");
 		return FALSE;
 	}
 
@@ -379,7 +371,11 @@ dhclient_start (NMDhcpClient *client,
 		gs_unref_object GFile *dst = g_file_new_for_path (preferred_leasefile_path);
 
 		/* Try to copy the existing leasefile to the preferred location */
-		if (!g_file_copy (src, dst, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &local)) {
+		if (g_file_copy (src, dst, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error)) {
+			/* Success; use the preferred leasefile path */
+			g_free (priv->lease_file);
+			priv->lease_file = g_file_get_path (dst);
+		} else {
 			gs_free char *s_path = NULL;
 			gs_free char *d_path = NULL;
 
@@ -387,12 +383,8 @@ dhclient_start (NMDhcpClient *client,
 			_LOGW ("failed to copy leasefile %s to %s: %s",
 			       (s_path = g_file_get_path (src)),
 			       (d_path = g_file_get_path (dst)),
-			       local->message);
-			g_clear_error (&local);
-		} else {
-			/* Success; use the preferred leasefile path */
-			g_free (priv->lease_file);
-			priv->lease_file = g_file_get_path (dst);
+			       error->message);
+			g_clear_error (&error);
 		}
 	}
 
@@ -401,12 +393,9 @@ dhclient_start (NMDhcpClient *client,
 		gs_free char *escaped = NULL;
 
 		escaped = nm_dhcp_dhclient_escape_duid (duid);
-		if (!nm_dhcp_dhclient_save_duid (priv->lease_file, escaped, &local)) {
-			nm_utils_error_set (error,
-			                    NM_UTILS_ERROR_UNKNOWN,
-			                    "failed to save DUID to '%s': %s",
-			                    priv->lease_file,
-			                    local->message);
+		if (!nm_dhcp_dhclient_save_duid (priv->lease_file, escaped, &error)) {
+			_LOGW ("failed to save DUID to %s: %s", priv->lease_file, error->message);
+			g_clear_error (&error);
 			return FALSE;
 		}
 	}
@@ -462,15 +451,13 @@ dhclient_start (NMDhcpClient *client,
 	g_ptr_array_add (argv, NULL);
 
 	_LOGD ("running: %s",
-	       (cmd_str = g_strjoinv (" ", (char **) argv->pdata)));
+	       (cmd_str = g_strjoinv (" ", (gchar **) argv->pdata)));
 
 	if (!g_spawn_async (NULL, (char **) argv->pdata, NULL,
 	                    G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
-	                    nm_utils_setpgid, NULL, &pid, &local)) {
-		nm_utils_error_set (error,
-		                    NM_UTILS_ERROR_UNKNOWN,
-		                    "dhclient failed to start: %s",
-		                    local->message);
+	                    nm_utils_setpgid, NULL, &pid, &error)) {
+		_LOGW ("dhclient failed to start: '%s'", error->message);
+		g_error_free (error);
 		return FALSE;
 	}
 
@@ -486,46 +473,36 @@ dhclient_start (NMDhcpClient *client,
 }
 
 static gboolean
-ip4_start (NMDhcpClient *client,
-           const char *dhcp_anycast_addr,
-           const char *last_ip4_address,
-           GError **error)
+ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last_ip4_address)
 {
 	NMDhcpDhclient *self = NM_DHCP_DHCLIENT (client);
 	NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (self);
 	GBytes *client_id;
 	gs_unref_bytes GBytes *new_client_id = NULL;
+	const char *iface, *uuid, *hostname;
+	guint32 timeout;
+	gboolean success = FALSE;
+	gboolean use_fqdn;
 
+	iface = nm_dhcp_client_get_iface (client);
+	uuid = nm_dhcp_client_get_uuid (client);
 	client_id = nm_dhcp_client_get_client_id (client);
+	hostname = nm_dhcp_client_get_hostname (client);
+	timeout = nm_dhcp_client_get_timeout (client);
+	use_fqdn = nm_dhcp_client_get_use_fqdn (client);
 
-	priv->conf_file = create_dhclient_config (self,
-	                                          AF_INET,
-	                                          nm_dhcp_client_get_iface (client),
-	                                          nm_dhcp_client_get_uuid (client),
-	                                          client_id,
-	                                          dhcp_anycast_addr,
-	                                          nm_dhcp_client_get_hostname (client),
-	                                          nm_dhcp_client_get_timeout (client),
-	                                          nm_dhcp_client_get_use_fqdn (client),
-	                                          &new_client_id);
-	if (!priv->conf_file) {
-		nm_utils_error_set_literal (error,
-		                            NM_UTILS_ERROR_UNKNOWN,
-		                            "error creating dhclient configuration file");
-		return FALSE;
-	}
+	priv->conf_file = create_dhclient_config (self, AF_INET, iface, uuid, client_id, dhcp_anycast_addr,
+	                                          hostname, timeout, use_fqdn, &new_client_id);
+	if (priv->conf_file) {
+		if (new_client_id) {
+			nm_assert (!client_id);
+			nm_dhcp_client_set_client_id (client, new_client_id);
+		}
+		success = dhclient_start (client, NULL, NULL, FALSE, NULL, 0);
+	} else
+		_LOGW ("error creating dhclient configuration file");
 
-	if (new_client_id) {
-		nm_assert (!client_id);
-		nm_dhcp_client_set_client_id (client, new_client_id);
-	}
-	return dhclient_start (client,
-	                       NULL,
-	                       NULL,
-	                       FALSE,
-	                       NULL,
-	                       0,
-	                       error);
+	return success;
 }
 
 static gboolean
@@ -534,26 +511,22 @@ ip6_start (NMDhcpClient *client,
            const struct in6_addr *ll_addr,
            NMSettingIP6ConfigPrivacy privacy,
            GBytes *duid,
-           guint needed_prefixes,
-           GError **error)
+           guint needed_prefixes)
 {
 	NMDhcpDhclient *self = NM_DHCP_DHCLIENT (client);
 	NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (self);
+	const char *iface, *uuid, *hostname;
+	guint32 timeout;
+
+	iface = nm_dhcp_client_get_iface (client);
+	uuid = nm_dhcp_client_get_uuid (client);
+	hostname = nm_dhcp_client_get_hostname (client);
+	timeout = nm_dhcp_client_get_timeout (client);
 
-	priv->conf_file = create_dhclient_config (self,
-	                                          AF_INET6,
-	                                          nm_dhcp_client_get_iface (client),
-	                                          nm_dhcp_client_get_uuid (client),
-	                                          NULL,
-	                                          dhcp_anycast_addr,
-	                                          nm_dhcp_client_get_hostname (client),
-	                                          nm_dhcp_client_get_timeout (client),
-	                                          TRUE,
-	                                          NULL);
+	priv->conf_file = create_dhclient_config (self, AF_INET6, iface, uuid, NULL, dhcp_anycast_addr,
+	                                          hostname, timeout, TRUE, NULL);
 	if (!priv->conf_file) {
-		nm_utils_error_set_literal (error,
-		                            NM_UTILS_ERROR_UNKNOWN,
-		                            "error creating dhclient configuration file");
+		_LOGW ("error creating dhclient configuration file");
 		return FALSE;
 	}
 
@@ -561,11 +534,7 @@ ip6_start (NMDhcpClient *client,
 	                       nm_dhcp_client_get_info_only (NM_DHCP_CLIENT (self))
 	                         ? "-S"
 	                         : "-N",
-	                       duid,
-	                       FALSE,
-	                       NULL,
-	                       needed_prefixes,
-	                       error);
+	                       duid, FALSE, NULL, needed_prefixes);
 }
 
 static void
@@ -591,13 +560,7 @@ stop (NMDhcpClient *client, gboolean release, GBytes *duid)
 	if (release) {
 		pid_t rpid = -1;
 
-		if (dhclient_start (client,
-		                    NULL,
-		                    duid,
-		                    TRUE,
-		                    &rpid,
-		                    0,
-		                    NULL)) {
+		if (dhclient_start (client, NULL, duid, TRUE, &rpid, 0)) {
 			/* Wait a few seconds for the release to happen */
 			nm_dhcp_client_stop_pid (rpid, nm_dhcp_client_get_iface (client));
 		}