diff options
| author | Michael Biebl <biebl@debian.org> | 2018-06-04 00:08:31 +0200 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2018-06-04 00:08:31 +0200 |
| commit | 0dd9df69fdbd475c48a0c8d5b0a1882550fe7321 (patch) | |
| tree | 249cf25643b1fe408e10679bb61613bc6540e894 /src/dhcp | |
| parent | 2e94a3b93171ab3fb95bf689aab1664d23988809 (diff) | |
| parent | 04bc9e1cd3544445d883ad29ea108c1645c8e7b7 (diff) | |
Update upstream source from tag 'upstream/1.11.4'
Update to upstream version '1.11.4' with Debian dir d0638aa2e32d5bae4e8daa021b9a66b7c4d6647e
Diffstat (limited to 'src/dhcp')
| -rw-r--r-- | src/dhcp/nm-dhcp-client.c | 83 | ||||
| -rw-r--r-- | src/dhcp/nm-dhcp-client.h | 3 | ||||
| -rw-r--r-- | src/dhcp/nm-dhcp-dhclient-utils.c | 1 | ||||
| -rw-r--r-- | src/dhcp/nm-dhcp-dhclient.c | 132 | ||||
| -rw-r--r-- | src/dhcp/nm-dhcp-dhcpcanon.c | 2 | ||||
| -rw-r--r-- | src/dhcp/nm-dhcp-listener.c | 2 | ||||
| -rw-r--r-- | src/dhcp/nm-dhcp-manager.c | 2 | ||||
| -rw-r--r-- | src/dhcp/nm-dhcp-systemd.c | 5 | ||||
| -rw-r--r-- | src/dhcp/tests/meson.build | 2 | ||||
| -rw-r--r-- | src/dhcp/tests/test-dhcp-dhclient.c | 6 |
10 files changed, 110 insertions, 128 deletions
diff --git a/src/dhcp/nm-dhcp-client.c b/src/dhcp/nm-dhcp-client.c index 96c02653..ba517606 100644 --- a/src/dhcp/nm-dhcp-client.c +++ b/src/dhcp/nm-dhcp-client.c @@ -334,7 +334,7 @@ nm_dhcp_client_stop_pid (pid_t pid, const char *iface) g_return_if_fail (pid > 1); - nm_utils_kill_child_sync (pid, SIGTERM, LOGD_DHCP, name ? name : "dhcp-client", NULL, + nm_utils_kill_child_sync (pid, SIGTERM, LOGD_DHCP, name ?: "dhcp-client", NULL, 1000 / 2, 1000 / 20); g_free (name); } @@ -359,26 +359,25 @@ stop (NMDhcpClient *self, gboolean release, GBytes *duid) void nm_dhcp_client_set_state (NMDhcpClient *self, NMDhcpState new_state, - GObject *ip_config, + NMIPConfig *ip_config, GHashTable *options) { NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self); gs_free char *event_id = NULL; + if (new_state == NM_DHCP_STATE_BOUND) { + g_return_if_fail (NM_IS_IP_CONFIG (ip_config, priv->addr_family)); + g_return_if_fail (options); + } else { + g_return_if_fail (!ip_config); + g_return_if_fail (!options); + } + if (new_state >= NM_DHCP_STATE_BOUND) timeout_cleanup (self); if (new_state >= NM_DHCP_STATE_TIMEOUT) watch_cleanup (self); - if (new_state == NM_DHCP_STATE_BOUND) { - g_assert ( (priv->addr_family == AF_INET && NM_IS_IP4_CONFIG (ip_config)) - || (priv->addr_family == AF_INET6 && NM_IS_IP6_CONFIG (ip_config))); - g_assert (options); - } else { - g_assert (ip_config == NULL); - g_assert (options == NULL); - } - /* The client may send same-state transitions for RENEW/REBIND events and * the lease may have changed, so handle same-state transitions for the * BOUND state. Ignore same-state transitions for other events since @@ -660,8 +659,10 @@ nm_dhcp_client_stop_existing (const char *pid_file, const char *binary_name) out: if (remove (pid_file) == -1) { - nm_log_dbg (LOGD_DHCP, "dhcp: could not remove pid file \"%s\": %d (%s)", - pid_file, errno, g_strerror (errno)); + int errsv = errno; + + nm_log_dbg (LOGD_DHCP, "dhcp: could not remove pid file \"%s\": %s (%d)", + pid_file, g_strerror (errsv), errsv); } } @@ -736,14 +737,6 @@ maybe_add_option (NMDhcpClient *self, GVariant *value) { char *str_value = NULL; - const char **p; - static const char *ignored_keys[] = { - "interface", - "pid", - "reason", - "dhcp_message_type", - NULL - }; g_return_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE_BYTESTRING)); @@ -751,10 +744,11 @@ maybe_add_option (NMDhcpClient *self, return; /* Filter out stuff that's not actually new DHCP options */ - for (p = ignored_keys; *p; p++) { - if (!strcmp (*p, key)) - return; - } + if (NM_IN_STRSET (key, "interface", + "pid", + "reason", + "dhcp_message_type")) + return; if (g_str_has_prefix (key, NEW_TAG)) key += NM_STRLEN (NEW_TAG); @@ -777,8 +771,8 @@ nm_dhcp_client_handle_event (gpointer unused, NMDhcpClientPrivate *priv; guint32 old_state; guint32 new_state; - GHashTable *str_options = NULL; - GObject *ip_config = NULL; + gs_unref_hashtable GHashTable *str_options = NULL; + gs_unref_object NMIPConfig *ip_config = NULL; NMPlatformIP6Address prefix = { 0, }; g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), FALSE); @@ -822,24 +816,24 @@ nm_dhcp_client_handle_event (gpointer unused, } /* Create the IP config */ - g_warn_if_fail (g_hash_table_size (str_options)); - if (g_hash_table_size (str_options)) { + if (g_hash_table_size (str_options) > 0) { if (priv->addr_family == AF_INET) { - ip_config = (GObject *) nm_dhcp_utils_ip4_config_from_options (nm_dhcp_client_get_multi_idx (self), - priv->ifindex, - priv->iface, - str_options, - priv->route_table, - priv->route_metric); + ip_config = NM_IP_CONFIG_CAST (nm_dhcp_utils_ip4_config_from_options (nm_dhcp_client_get_multi_idx (self), + priv->ifindex, + priv->iface, + str_options, + priv->route_table, + priv->route_metric)); } else { prefix = nm_dhcp_utils_ip6_prefix_from_options (str_options); - ip_config = (GObject *) nm_dhcp_utils_ip6_config_from_options (nm_dhcp_client_get_multi_idx (self), - priv->ifindex, - priv->iface, - str_options, - priv->info_only); + ip_config = NM_IP_CONFIG_CAST (nm_dhcp_utils_ip6_config_from_options (nm_dhcp_client_get_multi_idx (self), + priv->ifindex, + priv->iface, + str_options, + priv->info_only)); } - } + } else + g_warn_if_reached (); } if (!IN6_IS_ADDR_UNSPECIFIED (&prefix.address)) { @@ -851,7 +845,8 @@ nm_dhcp_client_handle_event (gpointer unused, &prefix); } else { /* Fail if no valid IP config was received */ - if (new_state == NM_DHCP_STATE_BOUND && ip_config == NULL) { + if ( new_state == NM_DHCP_STATE_BOUND + && !ip_config) { _LOGW ("client bound but IP config not received"); new_state = NM_DHCP_STATE_FAIL; g_clear_pointer (&str_options, g_hash_table_unref); @@ -860,10 +855,6 @@ nm_dhcp_client_handle_event (gpointer unused, nm_dhcp_client_set_state (self, new_state, ip_config, str_options); } - if (str_options) - g_hash_table_destroy (str_options); - g_clear_object (&ip_config); - return TRUE; } diff --git a/src/dhcp/nm-dhcp-client.h b/src/dhcp/nm-dhcp-client.h index 0d92d743..111b063b 100644 --- a/src/dhcp/nm-dhcp-client.h +++ b/src/dhcp/nm-dhcp-client.h @@ -48,7 +48,6 @@ #define NM_DHCP_CLIENT_SIGNAL_STATE_CHANGED "state-changed" #define NM_DHCP_CLIENT_SIGNAL_PREFIX_DELEGATED "prefix-delegated" - typedef enum { NM_DHCP_STATE_UNKNOWN = 0, NM_DHCP_STATE_BOUND, /* new lease or lease changed */ @@ -169,7 +168,7 @@ void nm_dhcp_client_watch_child (NMDhcpClient *self, pid_t pid); void nm_dhcp_client_set_state (NMDhcpClient *self, NMDhcpState new_state, - GObject *ip_config, /* NMIP4Config or NMIP6Config */ + NMIPConfig *ip_config, GHashTable *options); /* str:str hash */ gboolean nm_dhcp_client_handle_event (gpointer unused, diff --git a/src/dhcp/nm-dhcp-dhclient-utils.c b/src/dhcp/nm-dhcp-dhclient-utils.c index 52923310..16a76d23 100644 --- a/src/dhcp/nm-dhcp-dhclient-utils.c +++ b/src/dhcp/nm-dhcp-dhclient-utils.c @@ -95,7 +95,6 @@ grab_request_options (GPtrArray *store, const char* line) return end; } - static void add_hostname4 (GString *str, const char *hostname, gboolean use_fqdn) { diff --git a/src/dhcp/nm-dhcp-dhclient.c b/src/dhcp/nm-dhcp-dhclient.c index 738e9f91..93306ddd 100644 --- a/src/dhcp/nm-dhcp-dhclient.c +++ b/src/dhcp/nm-dhcp-dhclient.c @@ -127,11 +127,13 @@ get_dhclient_leasefile (int addr_family, _addr_family_to_path_part (addr_family), uuid, iface); - if (out_preferred_path) - *out_preferred_path = g_strdup (path); - if (g_file_test (path, G_FILE_TEST_EXISTS)) + if (g_file_test (path, G_FILE_TEST_EXISTS)) { + NM_SET_OUT (out_preferred_path, g_strdup (path)); return path; + } + + NM_SET_OUT (out_preferred_path, g_steal_pointer (&path)); /* If the leasefile we're looking for doesn't exist yet in the new location * (eg, /var/lib/NetworkManager) then look in old locations to maintain @@ -262,7 +264,6 @@ find_existing_config (NMDhcpDhclient *self, int addr_family, const char *iface, return NULL; } - /* NM provides interface-specific options; thus the same dhclient config * file cannot be used since DHCP transactions can happen in parallel. * Since some distros don't have default per-interface dhclient config files, @@ -308,7 +309,6 @@ create_dhclient_config (NMDhcpDhclient *self, return new; } - static gboolean dhclient_start (NMDhcpClient *client, const char *mode_opt, @@ -319,20 +319,23 @@ dhclient_start (NMDhcpClient *client, { NMDhcpDhclient *self = NM_DHCP_DHCLIENT (client); NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (self); - GPtrArray *argv = NULL; + gs_unref_ptrarray GPtrArray *argv = NULL; pid_t pid; GError *error = NULL; - const char *iface, *uuid, *system_bus_address, *dhclient_path = NULL; - char *binary_name, *cmd_str, *pid_file = NULL, *system_bus_address_env = NULL; - int addr_family; - gboolean success; - char *escaped, *preferred_leasefile_path = NULL; + const char *iface; + const char *uuid; + const char *system_bus_address; + const char *dhclient_path; + char *binary_name; + gs_free char *cmd_str = NULL; + gs_free char *pid_file = NULL; + gs_free char *system_bus_address_env = NULL; + gs_free char *preferred_leasefile_path = NULL; + const int addr_family = nm_dhcp_client_get_addr_family (client); - g_return_val_if_fail (priv->pid_file == NULL, FALSE); + g_return_val_if_fail (!priv->pid_file, FALSE); - iface = nm_dhcp_client_get_iface (client); - uuid = nm_dhcp_client_get_uuid (client); - addr_family = nm_dhcp_client_get_addr_family (client); + NM_SET_OUT (out_pid, 0); dhclient_path = nm_dhcp_dhclient_get_path (); if (!dhclient_path) { @@ -340,6 +343,9 @@ dhclient_start (NMDhcpClient *client, return FALSE; } + iface = nm_dhcp_client_get_iface (client); + uuid = nm_dhcp_client_get_uuid (client); + pid_file = g_strdup_printf (RUNSTATEDIR "/dhclient%s-%s.pid", _addr_family_to_path_part (addr_family), iface); @@ -351,18 +357,18 @@ dhclient_start (NMDhcpClient *client, if (release) { /* release doesn't use the pidfile after killing an old client */ - g_free (pid_file); - pid_file = NULL; + nm_clear_g_free (&pid_file); } g_free (priv->lease_file); priv->lease_file = get_dhclient_leasefile (addr_family, iface, uuid, &preferred_leasefile_path); + nm_assert (preferred_leasefile_path); if (!priv->lease_file) { /* No existing leasefile, dhclient will create one at the preferred path */ - priv->lease_file = g_strdup (preferred_leasefile_path); - } else if (g_strcmp0 (priv->lease_file, preferred_leasefile_path) != 0) { - GFile *src = g_file_new_for_path (priv->lease_file); - GFile *dst = g_file_new_for_path (preferred_leasefile_path); + priv->lease_file = g_steal_pointer (&preferred_leasefile_path); + } else if (!nm_streq0 (priv->lease_file, preferred_leasefile_path)) { + gs_unref_object GFile *src = g_file_new_for_path (priv->lease_file); + 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, &error)) { @@ -376,19 +382,16 @@ dhclient_start (NMDhcpClient *client, error->message); g_clear_error (&error); } - g_object_unref (src); - g_object_unref (dst); } - g_free (preferred_leasefile_path); /* Save the DUID to the leasefile dhclient will actually use */ if (addr_family == AF_INET6) { + gs_free char *escaped = NULL; + escaped = nm_dhcp_dhclient_escape_duid (duid); - success = nm_dhcp_dhclient_save_duid (priv->lease_file, escaped, &error); - g_free (escaped); - if (!success) { + 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_free (pid_file); + g_clear_error (&error); return FALSE; } } @@ -440,34 +443,29 @@ dhclient_start (NMDhcpClient *client, g_ptr_array_add (argv, (gpointer) system_bus_address_env); } - g_ptr_array_add (argv, (gpointer) iface); g_ptr_array_add (argv, NULL); - cmd_str = g_strjoinv (" ", (gchar **) argv->pdata); - _LOGD ("running: %s", cmd_str); - g_free (cmd_str); - - 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, &error)) { - g_assert (pid > 0); - _LOGI ("dhclient started with pid %d", pid); - if (release == FALSE) - nm_dhcp_client_watch_child (client, pid); - priv->pid_file = pid_file; - } else { + _LOGD ("running: %s", + (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, &error)) { _LOGW ("dhclient failed to start: '%s'", error->message); g_error_free (error); - g_free (pid_file); + return FALSE; } - if (out_pid) - *out_pid = pid; + _LOGI ("dhclient started with pid %lld", (long long int) pid); + + if (!release) + nm_dhcp_client_watch_child (client, pid); + + priv->pid_file = g_steal_pointer (&pid_file); - g_ptr_array_free (argv, TRUE); - g_free (system_bus_address_env); - return pid > 0 ? TRUE : FALSE; + NM_SET_OUT (out_pid, pid); + return TRUE; } static gboolean @@ -547,10 +545,12 @@ stop (NMDhcpClient *client, gboolean release, GBytes *duid) if (remove (priv->conf_file) == -1) _LOGD ("could not remove dhcp config file \"%s\": %d (%s)", priv->conf_file, errno, g_strerror (errno)); if (priv->pid_file) { - if (remove (priv->pid_file) == -1) - _LOGD ("could not remove dhcp pid file \"%s\": %d (%s)", priv->pid_file, errno, g_strerror (errno)); - g_free (priv->pid_file); - priv->pid_file = NULL; + if (remove (priv->pid_file) == -1) { + int errsv = errno; + + _LOGD ("could not remove dhcp pid file \"%s\": %s (%d)", priv->pid_file, g_strerror (errsv), errsv); + } + nm_clear_g_free (&priv->pid_file); } if (release) { @@ -607,7 +607,7 @@ get_duid (NMDhcpClient *client) g_free (leasefile); } - if (!duid && priv->def_leasefile) { + if (!duid) { /* Otherwise read the default machine-wide DUID */ _LOGD ("looking for default DUID in '%s'", priv->def_leasefile); duid = nm_dhcp_dhclient_read_duid (priv->def_leasefile, &error); @@ -625,31 +625,25 @@ get_duid (NMDhcpClient *client) /*****************************************************************************/ -static const char *def_leasefiles[] = { - SYSCONFDIR "/dhclient6.leases", - LOCALSTATEDIR "/lib/dhcp/dhclient6.leases", - LOCALSTATEDIR "/lib/dhclient/dhclient6.leases", - NULL -}; - static void nm_dhcp_dhclient_init (NMDhcpDhclient *self) { + static const char *const FILES[] = { + SYSCONFDIR "/dhclient6.leases", /* default */ + LOCALSTATEDIR "/lib/dhcp/dhclient6.leases", + LOCALSTATEDIR "/lib/dhclient/dhclient6.leases", + }; NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (self); - const char **iter = &def_leasefiles[0]; + int i; - while (iter && *iter) { - if (g_file_test (*iter, G_FILE_TEST_EXISTS)) { - priv->def_leasefile = *iter; + priv->def_leasefile = FILES[0]; + for (i = 0; i < G_N_ELEMENTS (FILES); i++) { + if (g_file_test (FILES[i], G_FILE_TEST_EXISTS)) { + priv->def_leasefile = FILES[i]; break; } - iter++; } - /* Fallback option */ - if (!priv->def_leasefile) - priv->def_leasefile = SYSCONFDIR "/dhclient6.leases"; - priv->dhcp_listener = g_object_ref (nm_dhcp_listener_get ()); g_signal_connect (priv->dhcp_listener, NM_DHCP_LISTENER_EVENT, diff --git a/src/dhcp/nm-dhcp-dhcpcanon.c b/src/dhcp/nm-dhcp-dhcpcanon.c index 82b3db4f..12aa57aa 100644 --- a/src/dhcp/nm-dhcp-dhcpcanon.c +++ b/src/dhcp/nm-dhcp-dhcpcanon.c @@ -76,7 +76,6 @@ nm_dhcp_dhcpcanon_get_path (void) return nm_utils_find_helper ("dhcpcanon", DHCPCANON_PATH, NULL); } - static gboolean dhcpcanon_start (NMDhcpClient *client, const char *mode_opt, @@ -142,7 +141,6 @@ dhcpcanon_start (NMDhcpClient *client, g_ptr_array_add (argv, (gpointer) system_bus_address_env); } - g_ptr_array_add (argv, (gpointer) iface); g_ptr_array_add (argv, NULL); diff --git a/src/dhcp/nm-dhcp-listener.c b/src/dhcp/nm-dhcp-listener.c index d7d38e54..1770ead3 100644 --- a/src/dhcp/nm-dhcp-listener.c +++ b/src/dhcp/nm-dhcp-listener.c @@ -160,7 +160,7 @@ _method_call_handle (NMDhcpListener *self, pid_str = get_option (options, "pid"); pid = _nm_utils_ascii_str_to_int64 (pid_str, 10, 0, G_MAXINT32, -1); if (pid == -1) { - _LOGW ("dhcp-event: couldn't convert PID '%s' to an integer", pid_str ? pid_str : "(null)"); + _LOGW ("dhcp-event: couldn't convert PID '%s' to an integer", pid_str ?: "(null)"); return; } diff --git a/src/dhcp/nm-dhcp-manager.c b/src/dhcp/nm-dhcp-manager.c index bf22872d..aa40e803 100644 --- a/src/dhcp/nm-dhcp-manager.c +++ b/src/dhcp/nm-dhcp-manager.c @@ -311,7 +311,7 @@ nm_dhcp_manager_start_ip6 (NMDhcpManager *self, if (send_hostname) { /* Always prefer the explicit dhcp-hostname if given */ - hostname = dhcp_hostname ? dhcp_hostname : priv->default_hostname; + hostname = dhcp_hostname ?: priv->default_hostname; } return client_start (self, AF_INET6, multi_idx, iface, ifindex, hwaddr, uuid, route_table, route_metric, ll_addr, diff --git a/src/dhcp/nm-dhcp-systemd.c b/src/dhcp/nm-dhcp-systemd.c index 4f37f069..2d0202bb 100644 --- a/src/dhcp/nm-dhcp-systemd.c +++ b/src/dhcp/nm-dhcp-systemd.c @@ -513,7 +513,7 @@ bound4_handle (NMDhcpSystemd *self) nm_dhcp_client_set_state (NM_DHCP_CLIENT (self), NM_DHCP_STATE_BOUND, - G_OBJECT (ip4_config), + NM_IP_CONFIG_CAST (ip4_config), options); } else { _LOGW ("%s", error->message); @@ -667,7 +667,6 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last } } - /* Add requested options */ for (i = 0; dhcp4_requests[i].name; i++) { if (dhcp4_requests[i].include) @@ -823,7 +822,7 @@ bound6_handle (NMDhcpSystemd *self) if (ip6_config) { nm_dhcp_client_set_state (NM_DHCP_CLIENT (self), NM_DHCP_STATE_BOUND, - G_OBJECT (ip6_config), + NM_IP_CONFIG_CAST (ip6_config), options); } else { _LOGW ("%s", error->message); diff --git a/src/dhcp/tests/meson.build b/src/dhcp/tests/meson.build index 32badae8..9d8be427 100644 --- a/src/dhcp/tests/meson.build +++ b/src/dhcp/tests/meson.build @@ -8,7 +8,7 @@ foreach test_unit: test_units test_unit, test_unit + '.c', dependencies: test_nm_dep, - c_args: '-DTESTDIR="@0@"'.format(meson.current_source_dir()) + c_args: nm_build_cflags, ) test( diff --git a/src/dhcp/tests/test-dhcp-dhclient.c b/src/dhcp/tests/test-dhcp-dhclient.c index 25af51a1..a8284b23 100644 --- a/src/dhcp/tests/test-dhcp-dhclient.c +++ b/src/dhcp/tests/test-dhcp-dhclient.c @@ -36,6 +36,8 @@ #include "nm-test-utils-core.h" +#define TEST_DIR NM_BUILD_SRCDIR"/src/dhcp/tests" + static void test_config (const char *orig, const char *expected, @@ -736,7 +738,7 @@ test_read_duid_from_leasefile (void) gconstpointer duid_arr; gsize duid_len; - duid = nm_dhcp_dhclient_read_duid (TESTDIR "/test-dhclient-duid.leases", &error); + duid = nm_dhcp_dhclient_read_duid (TEST_DIR"/test-dhclient-duid.leases", &error); g_assert_no_error (error); g_assert (duid); duid_arr = g_bytes_get_data (duid, &duid_len); @@ -750,7 +752,7 @@ test_read_commented_duid_from_leasefile (void) GBytes *duid; GError *error = NULL; - duid = nm_dhcp_dhclient_read_duid (TESTDIR "/test-dhclient-commented-duid.leases", &error); + duid = nm_dhcp_dhclient_read_duid (TEST_DIR"/test-dhclient-commented-duid.leases", &error); g_assert_no_error (error); g_assert (duid == NULL); } |