From 8f7a3cbbdd0c0a48277c341dd3a8ec8743ae9735 Mon Sep 17 00:00:00 2001 From: Michael Biebl Date: Sat, 8 Sep 2018 17:44:06 +0200 Subject: New upstream version 1.13.90 --- src/dhcp/meson.build | 1 - src/dhcp/nm-dhcp-client.c | 37 ++++++++-- src/dhcp/nm-dhcp-client.h | 6 +- src/dhcp/nm-dhcp-dhclient-utils.c | 30 +++++--- src/dhcp/nm-dhcp-dhclient.c | 2 +- src/dhcp/nm-dhcp-dhcpcanon.c | 2 +- src/dhcp/nm-dhcp-dhcpcd.c | 2 +- src/dhcp/nm-dhcp-helper.c | 2 +- src/dhcp/nm-dhcp-utils.c | 2 +- src/dhcp/tests/meson.build | 1 - src/dhcp/tests/test-dhcp-dhclient.c | 135 ++++++++++++++++++++++++++++-------- 11 files changed, 170 insertions(+), 50 deletions(-) (limited to 'src/dhcp') diff --git a/src/dhcp/meson.build b/src/dhcp/meson.build index 289a16ca..76707bca 100644 --- a/src/dhcp/meson.build +++ b/src/dhcp/meson.build @@ -3,7 +3,6 @@ name = 'nm-dhcp-helper' cflags = [ '-DG_LOG_DOMAIN="@0@"'.format(name), '-DNETWORKMANAGER_COMPILATION=NM_NETWORKMANAGER_COMPILATION_GLIB', - '-DNMRUNDIR="@0@"'.format(nm_pkgrundir), ] executable( diff --git a/src/dhcp/nm-dhcp-client.c b/src/dhcp/nm-dhcp-client.c index 360bd367..9fc7d2c1 100644 --- a/src/dhcp/nm-dhcp-client.c +++ b/src/dhcp/nm-dhcp-client.c @@ -51,7 +51,7 @@ enum { static guint signals[LAST_SIGNAL] = { 0 }; -NM_GOBJECT_PROPERTIES_DEFINE_BASE ( +NM_GOBJECT_PROPERTIES_DEFINE (NMDhcpClient, PROP_ADDR_FAMILY, PROP_FLAGS, PROP_HWADDR, @@ -163,6 +163,17 @@ nm_dhcp_client_get_route_table (NMDhcpClient *self) return NM_DHCP_CLIENT_GET_PRIVATE (self)->route_table; } +void +nm_dhcp_client_set_route_table (NMDhcpClient *self, guint32 route_table) +{ + NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self); + + if (route_table != priv->route_table) { + priv->route_table = route_table; + _notify (self, PROP_ROUTE_TABLE); + } +} + guint32 nm_dhcp_client_get_route_metric (NMDhcpClient *self) { @@ -171,6 +182,17 @@ nm_dhcp_client_get_route_metric (NMDhcpClient *self) return NM_DHCP_CLIENT_GET_PRIVATE (self)->route_metric; } +void +nm_dhcp_client_set_route_metric (NMDhcpClient *self, guint32 route_metric) +{ + NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self); + + if (route_metric != priv->route_metric) { + priv->route_metric = route_metric; + _notify (self, PROP_ROUTE_METRIC); + } +} + guint32 nm_dhcp_client_get_timeout (NMDhcpClient *self) { @@ -423,7 +445,7 @@ transaction_timeout (gpointer user_data) } static void -daemon_watch_cb (GPid pid, gint status, gpointer user_data) +daemon_watch_cb (GPid pid, int status, gpointer user_data) { NMDhcpClient *self = NM_DHCP_CLIENT (user_data); NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self); @@ -712,7 +734,7 @@ maybe_add_option (NMDhcpClient *self, gboolean nm_dhcp_client_handle_event (gpointer unused, const char *iface, - gint pid, + int pid, GVariant *options, const char *reason, NMDhcpClient *self) @@ -834,6 +856,9 @@ get_property (GObject *object, guint prop_id, case PROP_ROUTE_METRIC: g_value_set_uint (value, priv->route_metric); break; + case PROP_ROUTE_TABLE: + g_value_set_uint (value, priv->route_table); + break; case PROP_TIMEOUT: g_value_set_uint (value, priv->timeout); break; @@ -889,11 +914,9 @@ set_property (GObject *object, guint prop_id, priv->uuid = g_value_dup_string (value); break; case PROP_ROUTE_TABLE: - /* construct-only */ priv->route_table = g_value_get_uint (value); break; case PROP_ROUTE_METRIC: - /* construct-only */ priv->route_metric = g_value_get_uint (value); break; case PROP_TIMEOUT: @@ -1002,13 +1025,13 @@ nm_dhcp_client_class_init (NMDhcpClientClass *client_class) obj_properties[PROP_ROUTE_TABLE] = g_param_spec_uint (NM_DHCP_CLIENT_ROUTE_TABLE, "", "", 0, G_MAXUINT32, RT_TABLE_MAIN, - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); obj_properties[PROP_ROUTE_METRIC] = g_param_spec_uint (NM_DHCP_CLIENT_ROUTE_METRIC, "", "", 0, G_MAXUINT32, 0, - G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); obj_properties[PROP_TIMEOUT] = diff --git a/src/dhcp/nm-dhcp-client.h b/src/dhcp/nm-dhcp-client.h index f3d0b7d1..b50ea515 100644 --- a/src/dhcp/nm-dhcp-client.h +++ b/src/dhcp/nm-dhcp-client.h @@ -131,8 +131,12 @@ GBytes *nm_dhcp_client_get_hw_addr (NMDhcpClient *self); guint32 nm_dhcp_client_get_route_table (NMDhcpClient *self); +void nm_dhcp_client_set_route_table (NMDhcpClient *self, guint32 route_table); + guint32 nm_dhcp_client_get_route_metric (NMDhcpClient *self); +void nm_dhcp_client_set_route_metric (NMDhcpClient *self, guint32 route_metric); + guint32 nm_dhcp_client_get_timeout (NMDhcpClient *self); GBytes *nm_dhcp_client_get_client_id (NMDhcpClient *self); @@ -176,7 +180,7 @@ void nm_dhcp_client_set_state (NMDhcpClient *self, gboolean nm_dhcp_client_handle_event (gpointer unused, const char *iface, - gint pid, + int pid, GVariant *options, const char *reason, NMDhcpClient *self); diff --git a/src/dhcp/nm-dhcp-dhclient-utils.c b/src/dhcp/nm-dhcp-dhclient-utils.c index 3290dd65..d8f8dd98 100644 --- a/src/dhcp/nm-dhcp-dhclient-utils.c +++ b/src/dhcp/nm-dhcp-dhclient-utils.c @@ -316,6 +316,7 @@ nm_dhcp_dhclient_create_config (const char *interface, if (orig_contents) { char **lines, **line; + int nest = 0; gboolean in_alsoreq = FALSE; gboolean in_req = FALSE; char intf[IFNAMSIZ]; @@ -330,18 +331,24 @@ nm_dhcp_dhclient_create_config (const char *interface, if (!strlen (g_strstrip (p))) continue; - if ( !intf[0] - && g_str_has_prefix (p, "interface") - && !in_req) { - if (read_interface (p, intf, sizeof (intf))) - continue; - } - - if (intf[0] && strchr (p, '}')) { + if (in_req) { + /* pass */ + } else if (strchr (p, '{')) { + nest++; + if ( !intf[0] + && g_str_has_prefix (p, "interface")) + if (read_interface (p, intf, sizeof (intf))) + continue; + } else if (strchr (p, '}')) { + if (nest) + nest--; intf[0] = '\0'; continue; } + if (nest && !intf[0]) + continue; + if (intf[0] && !nm_streq (intf, interface)) continue; @@ -523,6 +530,10 @@ nm_dhcp_dhclient_unescape_duid (const char *duid) guint i, len; guint8 octal; + /* FIXME: it's wrong to have an "unescape-duid" function. dhclient + * defines a file format with escaping. So we need a general unescape + * function that can handle dhclient syntax. */ + len = strlen (duid); unescaped = g_byte_array_sized_new (len); for (i = 0; i < len; i++) { @@ -536,6 +547,9 @@ nm_dhcp_dhclient_unescape_duid (const char *duid) g_byte_array_append (unescaped, &octal, 1); i += 2; } else { + /* FIXME: don't warn on untrusted data. Either signal an error, or accept + * it silently. */ + /* One of ", ', $, `, \, |, or & */ g_warn_if_fail (p[i] == '"' || p[i] == '\'' || p[i] == '$' || p[i] == '`' || p[i] == '\\' || p[i] == '|' || diff --git a/src/dhcp/nm-dhcp-dhclient.c b/src/dhcp/nm-dhcp-dhclient.c index 3bd14ebe..8408e3f9 100644 --- a/src/dhcp/nm-dhcp-dhclient.c +++ b/src/dhcp/nm-dhcp-dhclient.c @@ -447,7 +447,7 @@ dhclient_start (NMDhcpClient *client, g_ptr_array_add (argv, NULL); _LOGD ("running: %s", - (cmd_str = g_strjoinv (" ", (gchar **) argv->pdata))); + (cmd_str = g_strjoinv (" ", (char **) 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, diff --git a/src/dhcp/nm-dhcp-dhcpcanon.c b/src/dhcp/nm-dhcp-dhcpcanon.c index 12aa57aa..92aa7f8c 100644 --- a/src/dhcp/nm-dhcp-dhcpcanon.c +++ b/src/dhcp/nm-dhcp-dhcpcanon.c @@ -144,7 +144,7 @@ dhcpcanon_start (NMDhcpClient *client, g_ptr_array_add (argv, (gpointer) iface); g_ptr_array_add (argv, NULL); - cmd_str = g_strjoinv (" ", (gchar **) argv->pdata); + cmd_str = g_strjoinv (" ", (char **) argv->pdata); g_free (cmd_str); if (g_spawn_async (NULL, (char **) argv->pdata, NULL, diff --git a/src/dhcp/nm-dhcp-dhcpcd.c b/src/dhcp/nm-dhcp-dhcpcd.c index c4bcb084..10094e5c 100644 --- a/src/dhcp/nm-dhcp-dhcpcd.c +++ b/src/dhcp/nm-dhcp-dhcpcd.c @@ -153,7 +153,7 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last g_ptr_array_add (argv, (gpointer) iface); g_ptr_array_add (argv, NULL); - cmd_str = g_strjoinv (" ", (gchar **) argv->pdata); + cmd_str = g_strjoinv (" ", (char **) argv->pdata); _LOGD ("running: %s", cmd_str); g_free (cmd_str); diff --git a/src/dhcp/nm-dhcp-helper.c b/src/dhcp/nm-dhcp-helper.c index 8ea55061..2b48d7a5 100644 --- a/src/dhcp/nm-dhcp-helper.c +++ b/src/dhcp/nm-dhcp-helper.c @@ -31,7 +31,7 @@ /*****************************************************************************/ -#ifdef NM_MORE_LOGGING +#if NM_MORE_LOGGING #define _NMLOG_ENABLED(level) TRUE #else #define _NMLOG_ENABLED(level) ((level) <= LOG_ERR) diff --git a/src/dhcp/nm-dhcp-utils.c b/src/dhcp/nm-dhcp-utils.c index 9185a135..6bbc670b 100644 --- a/src/dhcp/nm-dhcp-utils.c +++ b/src/dhcp/nm-dhcp-utils.c @@ -594,7 +594,7 @@ ip6_add_domain_search (gpointer data, gpointer user_data) NMPlatformIP6Address nm_dhcp_utils_ip6_prefix_from_options (GHashTable *options) { - gs_strfreev gchar **split_addr = NULL; + gs_strfreev char **split_addr = NULL; NMPlatformIP6Address address = { 0, }; struct in6_addr tmp_addr; char *str = NULL; diff --git a/src/dhcp/tests/meson.build b/src/dhcp/tests/meson.build index 9d8be427..0fee26b2 100644 --- a/src/dhcp/tests/meson.build +++ b/src/dhcp/tests/meson.build @@ -8,7 +8,6 @@ foreach test_unit: test_units test_unit, test_unit + '.c', dependencies: test_nm_dep, - 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 2f369aac..14a1c786 100644 --- a/src/dhcp/tests/test-dhcp-dhclient.c +++ b/src/dhcp/tests/test-dhcp-dhclient.c @@ -86,7 +86,7 @@ test_config (const char *orig, if (expected_new_client_id) { g_assert (new_client_id); g_assert (g_bytes_equal (new_client_id, expected_new_client_id)); - } else + } else g_assert (new_client_id == NULL); } @@ -675,27 +675,18 @@ test_existing_multiline_alsoreq (void) static void test_one_duid (const char *escaped, const guint8 *unescaped, guint len) { - GBytes *t; - char *w; - gsize t_len; - gconstpointer t_arr; - - t = nm_dhcp_dhclient_unescape_duid (escaped); - g_assert (t); - t_arr = g_bytes_get_data (t, &t_len); - g_assert (t_arr); - g_assert_cmpint (t_len, ==, len); - g_assert_cmpint (memcmp (t_arr, unescaped, len), ==, 0); - g_bytes_unref (t); - - t = g_bytes_new_static (unescaped, len); - w = nm_dhcp_dhclient_escape_duid (t); + gs_unref_bytes GBytes *t1 = NULL; + gs_unref_bytes GBytes *t2 = NULL; + gs_free char *w = NULL; + + t1 = nm_dhcp_dhclient_unescape_duid (escaped); + g_assert (t1); + g_assert (nm_utils_gbytes_equal_mem (t1, unescaped, len)); + + t2 = g_bytes_new (unescaped, len); + w = nm_dhcp_dhclient_escape_duid (t2); g_assert (w); - g_assert_cmpint (strlen (escaped), ==, strlen (w)); g_assert_cmpstr (escaped, ==, w); - - g_bytes_unref (t); - g_free (w); } static void @@ -735,15 +726,11 @@ test_read_duid_from_leasefile (void) 0x13, 0x60, 0x67, 0x20, 0xec, 0x4c, 0x70 }; gs_unref_bytes GBytes *duid = NULL; GError *error = NULL; - gconstpointer duid_arr; - gsize duid_len; 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); - g_assert_cmpint (duid_len, ==, sizeof (expected)); - g_assert_cmpint (memcmp (duid_arr, expected, duid_len), ==, 0); + nmtst_assert_success (duid, error); + + g_assert (nm_utils_gbytes_equal_mem (duid, expected, G_N_ELEMENTS (expected))); } static void @@ -961,6 +948,99 @@ test_interface2 (void) NULL); } +static void +test_structured (void) +{ + gs_unref_bytes GBytes *new_client_id = NULL; + const guint8 bytes[] = "sad-and-useless"; + + static const char *const orig = \ + "interface \"eth0\" { \n" + " send host-name \"useless.example.com\";\n" + " hardware ethernet de:ad:80:86:ba:be;\n" + " send dhcp-client-identifier \"sad-and-useless\";\n" + " script \"/bin/useless\";\n" + " send dhcp-lease-time 8086;\n" + " request subnet-mask, broadcast-address, time-offset, routers,\n" + " domain-search, domain-name, host-name;\n" + " require subnet-mask;\n" + "} \n" + "\n" + " interface \"eth1\" { \n" + " send host-name \"sad.example.com\";\n" + " hardware ethernet de:ca:f6:66:ca:fe;\n" + " send dhcp-client-identifier \"useless-and-miserable\";\n" + " script \"/bin/miserable\";\n" + " send dhcp-lease-time 1337;\n" + " request subnet-mask, broadcast-address, time-offset, routers,\n" + " domain-search, domain-name, domain-name-servers, host-name;\n" + " require subnet-mask, domain-name-servers;\n" + " } \n" + "\n" + "pseudo \"secondary\" \"eth0\" { \n" + " send dhcp-client-identifier \"sad-useless-and-secondary\";\n" + " script \"/bin/secondary\";\n" + " send host-name \"secondary.useless.example.com\";\n" + " send dhcp-lease-time 666;\n" + " request routers;\n" + " require routers;\n" + " } \n" + "\n" + " pseudo \"tertiary\" \"eth0\" { \n" + " send dhcp-client-identifier \"sad-useless-and-tertiary\";\n" + " script \"/bin/tertiary\";\n" + " send host-name \"tertiary.useless.example.com\";\n" + "} \n" + "\n" + " alias{ \n" + " interface \"eth0\";\n" + " fixed-address 192.0.2.1;\n" + " option subnet-mask 255.255.255.0;\n" + " } \n" + " lease { \n" + " interface \"eth0\";\n" + " fixed-address 192.0.2.2;\n" + " option subnet-mask 255.255.255.0;\n" + " } \n"; + + static const char *const expected = \ + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\n" + "\n" + "send host-name \"useless.example.com\";\n" + "hardware ethernet de:ad:80:86:ba:be;\n" + "send dhcp-client-identifier \"sad-and-useless\";\n" + "send dhcp-lease-time 8086;\n" + "require subnet-mask;\n" + "\n" + "option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;\n" + "option ms-classless-static-routes code 249 = array of unsigned integer 8;\n" + "option wpad code 252 = string;\n" + "\n" + "request; # override dhclient defaults\n" + "also request subnet-mask;\n" + "also request broadcast-address;\n" + "also request time-offset;\n" + "also request routers;\n" + "also request domain-search;\n" + "also request domain-name;\n" + "also request host-name;\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "\n"; + + new_client_id = g_bytes_new (bytes, sizeof (bytes) - 1); + test_config (orig, expected, + AF_INET, NULL, 0, FALSE, + NULL, + new_client_id, + "eth0", + NULL); +} + static void test_config_req_intf (void) { @@ -1046,6 +1126,7 @@ main (int argc, char **argv) g_test_add_func ("/dhcp/dhclient/interface/1", test_interface1); g_test_add_func ("/dhcp/dhclient/interface/2", test_interface2); g_test_add_func ("/dhcp/dhclient/config/req_intf", test_config_req_intf); + g_test_add_func ("/dhcp/dhclient/structured", test_structured); g_test_add_func ("/dhcp/dhclient/read_duid_from_leasefile", test_read_duid_from_leasefile); g_test_add_func ("/dhcp/dhclient/read_commented_duid_from_leasefile", test_read_commented_duid_from_leasefile); -- cgit 1.3.0-6-gf8a5 From c240974325c552cad177c457d6ff04e381fd77a3 Mon Sep 17 00:00:00 2001 From: Michael Biebl Date: Sat, 15 Sep 2018 01:04:32 +0200 Subject: New upstream version 1.14.0 --- src/dhcp/nm-dhcp-client.c | 14 +++- src/dhcp/nm-dhcp-client.h | 14 ++-- src/dhcp/nm-dhcp-dhclient.c | 181 ++++++++++++++++++++++++++----------------- src/dhcp/nm-dhcp-dhcpcanon.c | 90 ++++++++++++--------- src/dhcp/nm-dhcp-dhcpcd.c | 66 +++++++++------- src/dhcp/nm-dhcp-manager.c | 59 ++++++++++---- src/dhcp/nm-dhcp-manager.h | 6 +- src/dhcp/nm-dhcp-systemd.c | 79 ++++++++++--------- 8 files changed, 309 insertions(+), 200 deletions(-) (limited to 'src/dhcp') diff --git a/src/dhcp/nm-dhcp-client.c b/src/dhcp/nm-dhcp-client.c index 9fc7d2c1..16db8306 100644 --- a/src/dhcp/nm-dhcp-client.c +++ b/src/dhcp/nm-dhcp-client.c @@ -510,7 +510,8 @@ nm_dhcp_client_start_ip4 (NMDhcpClient *self, GBytes *client_id, const char *dhcp_anycast_addr, const char *hostname, - const char *last_ip4_address) + const char *last_ip4_address, + GError **error) { NMDhcpClientPrivate *priv; @@ -531,7 +532,10 @@ nm_dhcp_client_start_ip4 (NMDhcpClient *self, g_clear_pointer (&priv->hostname, g_free); priv->hostname = g_strdup (hostname); - return NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, dhcp_anycast_addr, last_ip4_address); + return NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, + dhcp_anycast_addr, + last_ip4_address, + error); } static GBytes * @@ -548,7 +552,8 @@ nm_dhcp_client_start_ip6 (NMDhcpClient *self, const struct in6_addr *ll_addr, const char *hostname, NMSettingIP6ConfigPrivacy privacy, - guint needed_prefixes) + guint needed_prefixes, + GError **error) { NMDhcpClientPrivate *priv; gs_free char *str = NULL; @@ -584,7 +589,8 @@ nm_dhcp_client_start_ip6 (NMDhcpClient *self, ll_addr, privacy, priv->duid, - needed_prefixes); + needed_prefixes, + error); } void diff --git a/src/dhcp/nm-dhcp-client.h b/src/dhcp/nm-dhcp-client.h index b50ea515..86d60e38 100644 --- a/src/dhcp/nm-dhcp-client.h +++ b/src/dhcp/nm-dhcp-client.h @@ -76,18 +76,18 @@ typedef enum { typedef struct { GObjectClass parent; - /* Methods */ - gboolean (*ip4_start) (NMDhcpClient *self, const char *anycast_addr, - const char *last_ip4_address); + const char *last_ip4_address, + GError **error); gboolean (*ip6_start) (NMDhcpClient *self, const char *anycast_addr, const struct in6_addr *ll_addr, NMSettingIP6ConfigPrivacy privacy, GBytes *duid, - guint needed_prefixes); + guint needed_prefixes, + GError **error); void (*stop) (NMDhcpClient *self, gboolean release, @@ -151,7 +151,8 @@ gboolean nm_dhcp_client_start_ip4 (NMDhcpClient *self, GBytes *client_id, const char *dhcp_anycast_addr, const char *hostname, - const char *last_ip4_address); + const char *last_ip4_address, + GError **error); gboolean nm_dhcp_client_start_ip6 (NMDhcpClient *self, GBytes *client_id, @@ -160,7 +161,8 @@ gboolean nm_dhcp_client_start_ip6 (NMDhcpClient *self, const struct in6_addr *ll_addr, const char *hostname, NMSettingIP6ConfigPrivacy privacy, - guint needed_prefixes); + guint needed_prefixes, + GError **error); void nm_dhcp_client_stop (NMDhcpClient *self, gboolean release); diff --git a/src/dhcp/nm-dhcp-dhclient.c b/src/dhcp/nm-dhcp-dhclient.c index 8408e3f9..46d2339b 100644 --- a/src/dhcp/nm-dhcp-dhclient.c +++ b/src/dhcp/nm-dhcp-dhclient.c @@ -174,13 +174,14 @@ merge_dhclient_config (NMDhcpDhclient *self, GBytes **out_new_client_id, GError **error) { - char *orig = NULL, *new; - gboolean success = FALSE; + gs_free char *orig = NULL; + gs_free char *new = NULL; - g_return_val_if_fail (iface != NULL, FALSE); - g_return_val_if_fail (conf_file != NULL, FALSE); + g_return_val_if_fail (iface, FALSE); + g_return_val_if_fail (conf_file, 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)) { @@ -190,14 +191,22 @@ 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 success; + return g_file_set_contents (conf_file, + new, + -1, + error); } static char * @@ -282,13 +291,14 @@ create_dhclient_config (NMDhcpDhclient *self, gboolean use_fqdn, GBytes **out_new_client_id) { - char *orig = NULL, *new = NULL; + gs_free char *orig = NULL; + char *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); @@ -297,15 +307,12 @@ create_dhclient_config (NMDhcpDhclient *self, else _LOGD ("no existing dhclient configuration to merge"); - 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) { + if (!merge_dhclient_config (self, addr_family, iface, new, client_id, dhcp_anycast_addr, + hostname, timeout, use_fqdn, orig, out_new_client_id, &error)) { _LOGW ("error creating dhclient configuration: %s", error->message); - g_error_free (error); + g_clear_error (&error); } - g_free (orig); return new; } @@ -315,13 +322,14 @@ dhclient_start (NMDhcpClient *client, GBytes *duid, gboolean release, pid_t *out_pid, - int prefixes) + int prefixes, + GError **error) { NMDhcpDhclient *self = NM_DHCP_DHCLIENT (client); NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (self); gs_unref_ptrarray GPtrArray *argv = NULL; pid_t pid; - GError *error = NULL; + gs_free_error GError *local = NULL; const char *iface; const char *uuid; const char *system_bus_address; @@ -339,7 +347,7 @@ dhclient_start (NMDhcpClient *client, dhclient_path = nm_dhcp_dhclient_get_path (); if (!dhclient_path) { - _LOGW ("dhclient could not be found"); + nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhclient binary not found"); return FALSE; } @@ -371,16 +379,20 @@ 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, &error)) { - /* Success; use the preferred leasefile path */ - g_free (priv->lease_file); - priv->lease_file = g_strdup (g_file_get_path (dst)); - } else { + if (!g_file_copy (src, dst, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &local)) { + gs_free char *s_path = NULL; + gs_free char *d_path = NULL; + /* Failure; just use the existing leasefile */ _LOGW ("failed to copy leasefile %s to %s: %s", - g_file_get_path (src), g_file_get_path (dst), - error->message); - g_clear_error (&error); + (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); } } @@ -389,9 +401,12 @@ 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, &error)) { - _LOGW ("failed to save DUID to %s: %s", priv->lease_file, error->message); - g_clear_error (&error); + 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); return FALSE; } } @@ -451,9 +466,11 @@ dhclient_start (NMDhcpClient *client, 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); + nm_utils_setpgid, NULL, &pid, &local)) { + nm_utils_error_set (error, + NM_UTILS_ERROR_UNKNOWN, + "dhclient failed to start: %s", + local->message); return FALSE; } @@ -469,36 +486,46 @@ dhclient_start (NMDhcpClient *client, } static gboolean -ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last_ip4_address) +ip4_start (NMDhcpClient *client, + const char *dhcp_anycast_addr, + const char *last_ip4_address, + GError **error) { 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, 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"); + 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; + } - return success; + 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); } static gboolean @@ -507,22 +534,26 @@ ip6_start (NMDhcpClient *client, const struct in6_addr *ll_addr, NMSettingIP6ConfigPrivacy privacy, GBytes *duid, - guint needed_prefixes) + guint needed_prefixes, + GError **error) { 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, iface, uuid, NULL, dhcp_anycast_addr, - hostname, timeout, TRUE, NULL); + 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); if (!priv->conf_file) { - _LOGW ("error creating dhclient configuration file"); + nm_utils_error_set_literal (error, + NM_UTILS_ERROR_UNKNOWN, + "error creating dhclient configuration file"); return FALSE; } @@ -530,7 +561,11 @@ ip6_start (NMDhcpClient *client, nm_dhcp_client_get_info_only (NM_DHCP_CLIENT (self)) ? "-S" : "-N", - duid, FALSE, NULL, needed_prefixes); + duid, + FALSE, + NULL, + needed_prefixes, + error); } static void @@ -556,7 +591,13 @@ stop (NMDhcpClient *client, gboolean release, GBytes *duid) if (release) { pid_t rpid = -1; - if (dhclient_start (client, NULL, duid, TRUE, &rpid, 0)) { + if (dhclient_start (client, + NULL, + duid, + TRUE, + &rpid, + 0, + NULL)) { /* Wait a few seconds for the release to happen */ nm_dhcp_client_stop_pid (rpid, nm_dhcp_client_get_iface (client)); } diff --git a/src/dhcp/nm-dhcp-dhcpcanon.c b/src/dhcp/nm-dhcp-dhcpcanon.c index 92aa7f8c..de403020 100644 --- a/src/dhcp/nm-dhcp-dhcpcanon.c +++ b/src/dhcp/nm-dhcp-dhcpcanon.c @@ -82,28 +82,36 @@ dhcpcanon_start (NMDhcpClient *client, GBytes *duid, gboolean release, pid_t *out_pid, - int prefixes) + guint needed_prefixes, + GError **error) { NMDhcpDhcpcanon *self = NM_DHCP_DHCPCANON (client); NMDhcpDhcpcanonPrivate *priv = NM_DHCP_DHCPCANON_GET_PRIVATE (self); - GPtrArray *argv = NULL; + gs_unref_ptrarray GPtrArray *argv = NULL; pid_t pid; - GError *error = NULL; - const char *iface, *system_bus_address, *dhcpcanon_path = NULL; - char *binary_name, *cmd_str, *pid_file = NULL, *system_bus_address_env = NULL; + gs_free_error GError *local = NULL; + const char *iface; + const char *system_bus_address; + const char *dhcpcanon_path; + gs_free char *binary_name = NULL; + gs_free char *pid_file = NULL; + gs_free char *system_bus_address_env = NULL; int addr_family; - 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); + addr_family = nm_dhcp_client_get_addr_family (client); + dhcpcanon_path = nm_dhcp_dhcpcanon_get_path (); - _LOGD ("dhcpcanon_path: %s", dhcpcanon_path); if (!dhcpcanon_path) { - _LOGW ("dhcpcanon could not be found"); + nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhcpcanon binary not found"); return FALSE; } + _LOGD ("dhcpcanon_path: %s", dhcpcanon_path); + pid_file = g_strdup_printf (RUNSTATEDIR "/dhcpcanon%c-%s.pid", nm_utils_addr_family_to_char (addr_family), iface); @@ -112,7 +120,6 @@ dhcpcanon_start (NMDhcpClient *client, /* Kill any existing dhcpcanon from the pidfile */ binary_name = g_path_get_basename (dhcpcanon_path); nm_dhcp_client_stop_existing (pid_file, binary_name); - g_free (binary_name); argv = g_ptr_array_new (); g_ptr_array_add (argv, (gpointer) dhcpcanon_path); @@ -120,10 +127,8 @@ dhcpcanon_start (NMDhcpClient *client, g_ptr_array_add (argv, (gpointer) "-sf"); /* Set script file */ g_ptr_array_add (argv, (gpointer) nm_dhcp_helper_path); - if (pid_file) { - g_ptr_array_add (argv, (gpointer) "-pf"); /* Set pid file */ - g_ptr_array_add (argv, (gpointer) pid_file); - } + g_ptr_array_add (argv, (gpointer) "-pf"); /* Set pid file */ + g_ptr_array_add (argv, (gpointer) pid_file); if (priv->conf_file) { g_ptr_array_add (argv, (gpointer) "-cf"); /* Set interface config file */ @@ -144,33 +149,43 @@ dhcpcanon_start (NMDhcpClient *client, g_ptr_array_add (argv, (gpointer) iface); g_ptr_array_add (argv, NULL); - cmd_str = g_strjoinv (" ", (char **) argv->pdata); - 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 ("dhcpcanon started with pid %d", pid); - nm_dhcp_client_watch_child (client, pid); - priv->pid_file = pid_file; - } else { - _LOGW ("dhcpcanon failed to start: '%s'", error->message); - g_error_free (error); - g_free (pid_file); + 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, + "dhcpcanon failed to start: %s", + local->message); + return FALSE; } - g_ptr_array_free (argv, TRUE); - g_free (system_bus_address_env); - return pid > 0 ? TRUE : FALSE; + nm_assert (pid > 0); + _LOGI ("dhcpcanon started with pid %d", pid); + nm_dhcp_client_watch_child (client, pid); + priv->pid_file = g_steal_pointer (&pid_file); + return TRUE; } static gboolean -ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last_ip4_address) +ip4_start (NMDhcpClient *client, + const char *dhcp_anycast_addr, + const char *last_ip4_address, + GError **error) { - gboolean success = FALSE; - success = dhcpcanon_start (client, NULL, NULL, FALSE, NULL, 0); - return success; + return dhcpcanon_start (client, + NULL, + NULL, + FALSE, + NULL, + 0, + error); } static gboolean @@ -179,11 +194,10 @@ ip6_start (NMDhcpClient *client, const struct in6_addr *ll_addr, NMSettingIP6ConfigPrivacy privacy, GBytes *duid, - guint needed_prefixes) + guint needed_prefixes, + GError **error) { - NMDhcpDhcpcanon *self = NM_DHCP_DHCPCANON (client); - - _LOGW ("the dhcpcd backend does not support IPv6"); + nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhcpcanon plugin does not support IPv6"); return FALSE; } static void diff --git a/src/dhcp/nm-dhcp-dhcpcd.c b/src/dhcp/nm-dhcp-dhcpcd.c index 10094e5c..98ab5342 100644 --- a/src/dhcp/nm-dhcp-dhcpcd.c +++ b/src/dhcp/nm-dhcp-dhcpcd.c @@ -81,15 +81,21 @@ nm_dhcp_dhcpcd_get_path (void) } static gboolean -ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last_ip4_address) +ip4_start (NMDhcpClient *client, + const char *dhcp_anycast_addr, + const char *last_ip4_address, + GError **error) { NMDhcpDhcpcd *self = NM_DHCP_DHCPCD (client); NMDhcpDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE (self); - GPtrArray *argv = NULL; + gs_unref_ptrarray GPtrArray *argv = NULL; pid_t pid = -1; - GError *error = NULL; - char *pid_contents = NULL, *binary_name, *cmd_str; - const char *iface, *dhcpcd_path, *hostname; + GError *local = NULL; + gs_free char *cmd_str = NULL; + gs_free char *binary_name = NULL; + const char *iface; + const char *dhcpcd_path; + const char *hostname; g_return_val_if_fail (priv->pid_file == NULL, FALSE); @@ -102,14 +108,13 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last dhcpcd_path = nm_dhcp_dhcpcd_get_path (); if (!dhcpcd_path) { - _LOGW ("dhcpcd could not be found"); + nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhcpcd binary not found"); return FALSE; } /* Kill any existing dhcpcd from the pidfile */ binary_name = g_path_get_basename (dhcpcd_path); nm_dhcp_client_stop_existing (priv->pid_file, binary_name); - g_free (binary_name); argv = g_ptr_array_new (); g_ptr_array_add (argv, (gpointer) dhcpcd_path); @@ -128,13 +133,11 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last g_ptr_array_add (argv, (gpointer) "-c"); /* Set script file */ g_ptr_array_add (argv, (gpointer) nm_dhcp_helper_path); -#ifdef DHCPCD_SUPPORTS_IPV6 /* IPv4-only for now. NetworkManager knows better than dhcpcd when to * run IPv6, and dhcpcd's automatic Router Solicitations cause problems * with devices that don't expect them. */ g_ptr_array_add (argv, (gpointer) "-4"); -#endif hostname = nm_dhcp_client_get_hostname (client); @@ -153,24 +156,30 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last g_ptr_array_add (argv, (gpointer) iface); g_ptr_array_add (argv, NULL); - cmd_str = g_strjoinv (" ", (char **) 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 ("dhcpcd started with pid %d", pid); - nm_dhcp_client_watch_child (client, pid); - } else { - _LOGW ("dhcpcd failed to start, error: '%s'", error->message); - g_error_free (error); + _LOGD ("running: %s", + (cmd_str = g_strjoinv (" ", (char **) 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, + "dhcpcd failed to start: %s", + local->message); + g_error_free (local); + return FALSE; } - g_free (pid_contents); - g_ptr_array_free (argv, TRUE); - return pid > 0 ? TRUE : FALSE; + nm_assert (pid > 0); + _LOGI ("dhcpcd started with pid %d", pid); + nm_dhcp_client_watch_child (client, pid); + return TRUE; } static gboolean @@ -179,11 +188,10 @@ ip6_start (NMDhcpClient *client, const struct in6_addr *ll_addr, NMSettingIP6ConfigPrivacy privacy, GBytes *duid, - guint needed_prefixes) + guint needed_prefixes, + GError **error) { - NMDhcpDhcpcd *self = NM_DHCP_DHCPCD (client); - - _LOGW ("the dhcpcd backend does not support IPv6"); + nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhcpcd plugin does not support IPv6"); return FALSE; } diff --git a/src/dhcp/nm-dhcp-manager.c b/src/dhcp/nm-dhcp-manager.c index 67254323..6c71af9d 100644 --- a/src/dhcp/nm-dhcp-manager.c +++ b/src/dhcp/nm-dhcp-manager.c @@ -172,22 +172,39 @@ client_start (NMDhcpManager *self, gboolean info_only, NMSettingIP6ConfigPrivacy privacy, const char *last_ip4_address, - guint needed_prefixes) + guint needed_prefixes, + GError **error) { NMDhcpManagerPrivate *priv; NMDhcpClient *client; gboolean success = FALSE; + gsize hwaddr_len; - g_return_val_if_fail (self, NULL); g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL); g_return_val_if_fail (ifindex > 0, NULL); g_return_val_if_fail (uuid != NULL, NULL); g_return_val_if_fail (!dhcp_client_id || g_bytes_get_size (dhcp_client_id) >= 2, NULL); + g_return_val_if_fail (!error || !*error, NULL); + + if (!hwaddr) { + nm_utils_error_set (error, + NM_UTILS_ERROR_UNKNOWN, + "missing MAC address"); + return NULL; + } + + hwaddr_len = g_bytes_get_size (hwaddr); + if ( hwaddr_len == 0 + || hwaddr_len > NM_UTILS_HWADDR_LEN_MAX) { + nm_utils_error_set (error, + NM_UTILS_ERROR_UNKNOWN, + "invalid MAC address"); + g_return_val_if_reached (NULL) ; + } priv = NM_DHCP_MANAGER_GET_PRIVATE (self); - if (!priv->client_factory) - return NULL; + nm_assert (priv->client_factory); /* Kill any old client instance */ client = get_client_for_ifindex (self, addr_family, ifindex); @@ -216,10 +233,24 @@ client_start (NMDhcpManager *self, c_list_link_tail (&priv->dhcp_client_lst_head, &client->dhcp_client_lst); g_signal_connect (client, NM_DHCP_CLIENT_SIGNAL_STATE_CHANGED, G_CALLBACK (client_state_changed), self); - if (addr_family == AF_INET) - success = nm_dhcp_client_start_ip4 (client, dhcp_client_id, dhcp_anycast_addr, hostname, last_ip4_address); - else - success = nm_dhcp_client_start_ip6 (client, dhcp_client_id, enforce_duid, dhcp_anycast_addr, ipv6_ll_addr, hostname, privacy, needed_prefixes); + if (addr_family == AF_INET) { + success = nm_dhcp_client_start_ip4 (client, + dhcp_client_id, + dhcp_anycast_addr, + hostname, + last_ip4_address, + error); + } else { + success = nm_dhcp_client_start_ip6 (client, + dhcp_client_id, + enforce_duid, + dhcp_anycast_addr, + ipv6_ll_addr, + hostname, + privacy, + needed_prefixes, + error); + } if (!success) { remove_client_unref (self, client); @@ -245,7 +276,8 @@ nm_dhcp_manager_start_ip4 (NMDhcpManager *self, GBytes *dhcp_client_id, guint32 timeout, const char *dhcp_anycast_addr, - const char *last_ip_address) + const char *last_ip_address, + GError **error) { NMDhcpManagerPrivate *priv; const char *hostname = NULL; @@ -282,7 +314,7 @@ nm_dhcp_manager_start_ip4 (NMDhcpManager *self, return client_start (self, AF_INET, multi_idx, iface, ifindex, hwaddr, uuid, route_table, route_metric, NULL, dhcp_client_id, 0, timeout, dhcp_anycast_addr, hostname, - use_fqdn, FALSE, 0, last_ip_address, 0); + use_fqdn, FALSE, 0, last_ip_address, 0, error); } /* Caller owns a reference to the NMDhcpClient on return */ @@ -304,7 +336,8 @@ nm_dhcp_manager_start_ip6 (NMDhcpManager *self, const char *dhcp_anycast_addr, gboolean info_only, NMSettingIP6ConfigPrivacy privacy, - guint needed_prefixes) + guint needed_prefixes, + GError **error) { NMDhcpManagerPrivate *priv; const char *hostname = NULL; @@ -319,7 +352,7 @@ nm_dhcp_manager_start_ip6 (NMDhcpManager *self, return client_start (self, AF_INET6, multi_idx, iface, ifindex, hwaddr, uuid, route_table, route_metric, ll_addr, duid, enforce_duid, timeout, dhcp_anycast_addr, hostname, TRUE, info_only, - privacy, NULL, needed_prefixes); + privacy, NULL, needed_prefixes, error); } void @@ -409,7 +442,7 @@ nm_dhcp_manager_init (NMDhcpManager *self) } } - nm_assert (client_factory); + g_return_if_fail (client_factory); nm_log_info (LOGD_DHCP, "dhcp-init: Using DHCP client '%s'", client_factory->name); diff --git a/src/dhcp/nm-dhcp-manager.h b/src/dhcp/nm-dhcp-manager.h index 7eb32c37..1d9e5c21 100644 --- a/src/dhcp/nm-dhcp-manager.h +++ b/src/dhcp/nm-dhcp-manager.h @@ -59,7 +59,8 @@ NMDhcpClient * nm_dhcp_manager_start_ip4 (NMDhcpManager *manager, GBytes *dhcp_client_id, guint32 timeout, const char *dhcp_anycast_addr, - const char *last_ip_address); + const char *last_ip_address, + GError **error); NMDhcpClient * nm_dhcp_manager_start_ip6 (NMDhcpManager *manager, struct _NMDedupMultiIndex *multi_idx, @@ -78,7 +79,8 @@ NMDhcpClient * nm_dhcp_manager_start_ip6 (NMDhcpManager *manager, const char *dhcp_anycast_addr, gboolean info_only, NMSettingIP6ConfigPrivacy privacy, - guint needed_prefixes); + guint needed_prefixes, + GError **error); /* For testing only */ extern const char* nm_dhcp_helper_path; diff --git a/src/dhcp/nm-dhcp-systemd.c b/src/dhcp/nm-dhcp-systemd.c index 2d0202bb..b51c6e7b 100644 --- a/src/dhcp/nm-dhcp-systemd.c +++ b/src/dhcp/nm-dhcp-systemd.c @@ -567,7 +567,10 @@ get_arp_type (GBytes *hwaddr) } static gboolean -ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last_ip4_address) +ip4_start (NMDhcpClient *client, + const char *dhcp_anycast_addr, + const char *last_ip4_address, + GError **error) { NMDhcpSystemd *self = NM_DHCP_SYSTEMD (client); NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (self); @@ -590,7 +593,7 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last r = sd_dhcp_client_new (&priv->client4, FALSE); if (r < 0) { - _LOGW ("failed to create client (%d)", r); + nm_utils_error_set_errno (error, r, "failed to create dhcp-client: %s"); return FALSE; } @@ -598,8 +601,8 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last r = sd_dhcp_client_attach_event (priv->client4, NULL, 0); if (r < 0) { - _LOGW ("failed to attach event (%d)", r); - goto error; + nm_utils_error_set_errno (error, r, "failed to attach event: %s"); + goto errout; } hwaddr = nm_dhcp_client_get_hw_addr (client); @@ -613,21 +616,21 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last len, get_arp_type (hwaddr)); if (r < 0) { - _LOGW ("failed to set MAC address (%d)", r); - goto error; + nm_utils_error_set_errno (error, r, "failed to set MAC address: %s"); + goto errout; } } r = sd_dhcp_client_set_ifindex (priv->client4, nm_dhcp_client_get_ifindex (client)); if (r < 0) { - _LOGW ("failed to set ififindex (%d)", r); - goto error; + nm_utils_error_set_errno (error, r, "failed to set ifindex: %s"); + goto errout; } r = sd_dhcp_client_set_callback (priv->client4, dhcp_event_cb, client); if (r < 0) { - _LOGW ("failed to set callback (%d)", r); - goto error; + nm_utils_error_set_errno (error, r, "failed to set callback: %s"); + goto errout; } dhcp_lease_load (&lease, priv->lease_file); @@ -640,8 +643,8 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last if (last_addr.s_addr) { r = sd_dhcp_client_set_request_address (priv->client4, &last_addr); if (r < 0) { - _LOGW ("failed to set last IPv4 address (%d)", r); - goto error; + nm_utils_error_set_errno (error, r, "failed to set last IPv4 address: %s"); + goto errout; } } @@ -681,25 +684,25 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last */ r = sd_dhcp_client_set_hostname (priv->client4, hostname); if (r < 0) { - _LOGW ("failed to set DHCP hostname to '%s' (%d)", hostname, r); - goto error; + nm_utils_error_set_errno (error, r, "failed to set DHCP hostname: %s"); + goto errout; } } r = sd_dhcp_client_start (priv->client4); if (r < 0) { - _LOGW ("failed to start client (%d)", r); - goto error; + nm_utils_error_set_errno (error, r, "failed to start DHCP client: %s"); + goto errout; } nm_dhcp_client_start_timeout (client); success = TRUE; -error: +errout: sd_dhcp_lease_unref (lease); if (!success) - priv->client4 = sd_dhcp_client_unref (priv->client4); + sd_dhcp_client_unref (g_steal_pointer (&priv->client4)); return success; } @@ -864,7 +867,8 @@ ip6_start (NMDhcpClient *client, const struct in6_addr *ll_addr, NMSettingIP6ConfigPrivacy privacy, GBytes *duid, - guint needed_prefixes) + guint needed_prefixes, + GError **error) { NMDhcpSystemd *self = NM_DHCP_SYSTEMD (client); NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (self); @@ -888,7 +892,7 @@ ip6_start (NMDhcpClient *client, r = sd_dhcp6_client_new (&priv->client6); if (r < 0) { - _LOGW ("failed to create client (%d)", r); + nm_utils_error_set_errno (error, r, "failed to create dhcp-client: %s"); return FALSE; } @@ -907,14 +911,14 @@ ip6_start (NMDhcpClient *client, &duid_arr[2], duid_len - 2); if (r < 0) { - _LOGW ("failed to set DUID (%d)", r); + nm_utils_error_set_errno (error, r, "failed to set DUID: %s"); return FALSE; } r = sd_dhcp6_client_attach_event (priv->client6, NULL, 0); if (r < 0) { - _LOGW ("failed to attach event (%d)", r); - goto error; + nm_utils_error_set_errno (error, r, "failed to attach event: %s"); + goto errout; } hwaddr = nm_dhcp_client_get_hw_addr (client); @@ -928,21 +932,21 @@ ip6_start (NMDhcpClient *client, len, get_arp_type (hwaddr)); if (r < 0) { - _LOGW ("failed to set MAC address (%d)", r); - goto error; + nm_utils_error_set_errno (error, r, "failed to set MAC address: %s"); + goto errout; } } r = sd_dhcp6_client_set_ifindex (priv->client6, nm_dhcp_client_get_ifindex (client)); if (r < 0) { - _LOGW ("failed to set ifindex (%d)", r); - goto error; + nm_utils_error_set_errno (error, r, "failed to set ifindex: %s"); + goto errout; } r = sd_dhcp6_client_set_callback (priv->client6, dhcp6_event_cb, client); if (r < 0) { - _LOGW ("failed to set callback (%d)", r); - goto error; + nm_utils_error_set_errno (error, r, "failed to set callback: %s"); + goto errout; } /* Add requested options */ @@ -953,30 +957,29 @@ ip6_start (NMDhcpClient *client, r = sd_dhcp6_client_set_local_address (priv->client6, ll_addr); if (r < 0) { - _LOGW ("failed to set local address (%d)", r); - goto error; + nm_utils_error_set_errno (error, r, "failed to set local address: %s"); + goto errout; } hostname = nm_dhcp_client_get_hostname (client); r = sd_dhcp6_client_set_fqdn (priv->client6, hostname); if (r < 0) { - _LOGW ("failed to set DHCP hostname to '%s' (%d)", hostname, r); - goto error; + nm_utils_error_set_errno (error, r, "failed to set DHCP hostname: %s"); + goto errout; } r = sd_dhcp6_client_start (priv->client6); if (r < 0) { - _LOGW ("failed to start client (%d)", r); - goto error; + nm_utils_error_set_errno (error, r, "failed to start client: %s"); + goto errout; } nm_dhcp_client_start_timeout (client); return TRUE; -error: - sd_dhcp6_client_unref (priv->client6); - priv->client6 = NULL; +errout: + sd_dhcp6_client_unref (g_steal_pointer (&priv->client6)); return FALSE; } -- cgit 1.3.0-6-gf8a5 From e126f3e804c35480c4f075777430419d6ece23da Mon Sep 17 00:00:00 2001 From: Michael Biebl Date: Sun, 23 Sep 2018 10:10:27 +0200 Subject: New upstream version 1.12.4 --- src/dhcp/meson.build | 1 + src/dhcp/nm-dhcp-client.c | 18 ++-- src/dhcp/nm-dhcp-client.h | 16 ++-- src/dhcp/nm-dhcp-dhclient-utils.c | 30 ++---- src/dhcp/nm-dhcp-dhclient.c | 177 ++++++++++++++---------------------- src/dhcp/nm-dhcp-dhcpcanon.c | 90 ++++++++---------- src/dhcp/nm-dhcp-dhcpcd.c | 66 ++++++-------- src/dhcp/nm-dhcp-helper.c | 2 +- src/dhcp/nm-dhcp-manager.c | 59 +++--------- src/dhcp/nm-dhcp-manager.h | 6 +- src/dhcp/nm-dhcp-systemd.c | 79 ++++++++-------- src/dhcp/nm-dhcp-utils.c | 2 +- src/dhcp/tests/meson.build | 1 + src/dhcp/tests/test-dhcp-dhclient.c | 135 ++++++--------------------- 14 files changed, 242 insertions(+), 440 deletions(-) (limited to 'src/dhcp') diff --git a/src/dhcp/meson.build b/src/dhcp/meson.build index 76707bca..289a16ca 100644 --- a/src/dhcp/meson.build +++ b/src/dhcp/meson.build @@ -3,6 +3,7 @@ name = 'nm-dhcp-helper' cflags = [ '-DG_LOG_DOMAIN="@0@"'.format(name), '-DNETWORKMANAGER_COMPILATION=NM_NETWORKMANAGER_COMPILATION_GLIB', + '-DNMRUNDIR="@0@"'.format(nm_pkgrundir), ] executable( diff --git a/src/dhcp/nm-dhcp-client.c b/src/dhcp/nm-dhcp-client.c index 16db8306..e53c8d87 100644 --- a/src/dhcp/nm-dhcp-client.c +++ b/src/dhcp/nm-dhcp-client.c @@ -445,7 +445,7 @@ transaction_timeout (gpointer user_data) } static void -daemon_watch_cb (GPid pid, int status, gpointer user_data) +daemon_watch_cb (GPid pid, gint status, gpointer user_data) { NMDhcpClient *self = NM_DHCP_CLIENT (user_data); NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self); @@ -510,8 +510,7 @@ nm_dhcp_client_start_ip4 (NMDhcpClient *self, GBytes *client_id, const char *dhcp_anycast_addr, const char *hostname, - const char *last_ip4_address, - GError **error) + const char *last_ip4_address) { NMDhcpClientPrivate *priv; @@ -532,10 +531,7 @@ nm_dhcp_client_start_ip4 (NMDhcpClient *self, g_clear_pointer (&priv->hostname, g_free); priv->hostname = g_strdup (hostname); - return NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, - dhcp_anycast_addr, - last_ip4_address, - error); + return NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, dhcp_anycast_addr, last_ip4_address); } static GBytes * @@ -552,8 +548,7 @@ nm_dhcp_client_start_ip6 (NMDhcpClient *self, const struct in6_addr *ll_addr, const char *hostname, NMSettingIP6ConfigPrivacy privacy, - guint needed_prefixes, - GError **error) + guint needed_prefixes) { NMDhcpClientPrivate *priv; gs_free char *str = NULL; @@ -589,8 +584,7 @@ nm_dhcp_client_start_ip6 (NMDhcpClient *self, ll_addr, privacy, priv->duid, - needed_prefixes, - error); + needed_prefixes); } void @@ -740,7 +734,7 @@ maybe_add_option (NMDhcpClient *self, gboolean nm_dhcp_client_handle_event (gpointer unused, const char *iface, - int pid, + gint pid, GVariant *options, const char *reason, NMDhcpClient *self) diff --git a/src/dhcp/nm-dhcp-client.h b/src/dhcp/nm-dhcp-client.h index 86d60e38..4c196045 100644 --- a/src/dhcp/nm-dhcp-client.h +++ b/src/dhcp/nm-dhcp-client.h @@ -76,18 +76,18 @@ typedef enum { typedef struct { GObjectClass parent; + /* Methods */ + gboolean (*ip4_start) (NMDhcpClient *self, const char *anycast_addr, - const char *last_ip4_address, - GError **error); + const char *last_ip4_address); gboolean (*ip6_start) (NMDhcpClient *self, const char *anycast_addr, const struct in6_addr *ll_addr, NMSettingIP6ConfigPrivacy privacy, GBytes *duid, - guint needed_prefixes, - GError **error); + guint needed_prefixes); void (*stop) (NMDhcpClient *self, gboolean release, @@ -151,8 +151,7 @@ gboolean nm_dhcp_client_start_ip4 (NMDhcpClient *self, GBytes *client_id, const char *dhcp_anycast_addr, const char *hostname, - const char *last_ip4_address, - GError **error); + const char *last_ip4_address); gboolean nm_dhcp_client_start_ip6 (NMDhcpClient *self, GBytes *client_id, @@ -161,8 +160,7 @@ gboolean nm_dhcp_client_start_ip6 (NMDhcpClient *self, const struct in6_addr *ll_addr, const char *hostname, NMSettingIP6ConfigPrivacy privacy, - guint needed_prefixes, - GError **error); + guint needed_prefixes); void nm_dhcp_client_stop (NMDhcpClient *self, gboolean release); @@ -182,7 +180,7 @@ void nm_dhcp_client_set_state (NMDhcpClient *self, gboolean nm_dhcp_client_handle_event (gpointer unused, const char *iface, - int pid, + gint pid, GVariant *options, const char *reason, NMDhcpClient *self); diff --git a/src/dhcp/nm-dhcp-dhclient-utils.c b/src/dhcp/nm-dhcp-dhclient-utils.c index d8f8dd98..3290dd65 100644 --- a/src/dhcp/nm-dhcp-dhclient-utils.c +++ b/src/dhcp/nm-dhcp-dhclient-utils.c @@ -316,7 +316,6 @@ nm_dhcp_dhclient_create_config (const char *interface, if (orig_contents) { char **lines, **line; - int nest = 0; gboolean in_alsoreq = FALSE; gboolean in_req = FALSE; char intf[IFNAMSIZ]; @@ -331,23 +330,17 @@ nm_dhcp_dhclient_create_config (const char *interface, if (!strlen (g_strstrip (p))) continue; - if (in_req) { - /* pass */ - } else if (strchr (p, '{')) { - nest++; - if ( !intf[0] - && g_str_has_prefix (p, "interface")) - if (read_interface (p, intf, sizeof (intf))) - continue; - } else if (strchr (p, '}')) { - if (nest) - nest--; - intf[0] = '\0'; - continue; + if ( !intf[0] + && g_str_has_prefix (p, "interface") + && !in_req) { + if (read_interface (p, intf, sizeof (intf))) + continue; } - if (nest && !intf[0]) + if (intf[0] && strchr (p, '}')) { + intf[0] = '\0'; continue; + } if (intf[0] && !nm_streq (intf, interface)) continue; @@ -530,10 +523,6 @@ nm_dhcp_dhclient_unescape_duid (const char *duid) guint i, len; guint8 octal; - /* FIXME: it's wrong to have an "unescape-duid" function. dhclient - * defines a file format with escaping. So we need a general unescape - * function that can handle dhclient syntax. */ - len = strlen (duid); unescaped = g_byte_array_sized_new (len); for (i = 0; i < len; i++) { @@ -547,9 +536,6 @@ nm_dhcp_dhclient_unescape_duid (const char *duid) g_byte_array_append (unescaped, &octal, 1); i += 2; } else { - /* FIXME: don't warn on untrusted data. Either signal an error, or accept - * it silently. */ - /* One of ", ', $, `, \, |, or & */ g_warn_if_fail (p[i] == '"' || p[i] == '\'' || p[i] == '$' || p[i] == '`' || p[i] == '\\' || p[i] == '|' || 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)); } diff --git a/src/dhcp/nm-dhcp-dhcpcanon.c b/src/dhcp/nm-dhcp-dhcpcanon.c index de403020..12aa57aa 100644 --- a/src/dhcp/nm-dhcp-dhcpcanon.c +++ b/src/dhcp/nm-dhcp-dhcpcanon.c @@ -82,36 +82,28 @@ dhcpcanon_start (NMDhcpClient *client, GBytes *duid, gboolean release, pid_t *out_pid, - guint needed_prefixes, - GError **error) + int prefixes) { NMDhcpDhcpcanon *self = NM_DHCP_DHCPCANON (client); NMDhcpDhcpcanonPrivate *priv = NM_DHCP_DHCPCANON_GET_PRIVATE (self); - gs_unref_ptrarray GPtrArray *argv = NULL; + GPtrArray *argv = NULL; pid_t pid; - gs_free_error GError *local = NULL; - const char *iface; - const char *system_bus_address; - const char *dhcpcanon_path; - gs_free char *binary_name = NULL; - gs_free char *pid_file = NULL; - gs_free char *system_bus_address_env = NULL; + GError *error = NULL; + const char *iface, *system_bus_address, *dhcpcanon_path = NULL; + char *binary_name, *cmd_str, *pid_file = NULL, *system_bus_address_env = NULL; int addr_family; - g_return_val_if_fail (!priv->pid_file, FALSE); + g_return_val_if_fail (priv->pid_file == NULL, FALSE); iface = nm_dhcp_client_get_iface (client); - addr_family = nm_dhcp_client_get_addr_family (client); - dhcpcanon_path = nm_dhcp_dhcpcanon_get_path (); + _LOGD ("dhcpcanon_path: %s", dhcpcanon_path); if (!dhcpcanon_path) { - nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhcpcanon binary not found"); + _LOGW ("dhcpcanon could not be found"); return FALSE; } - _LOGD ("dhcpcanon_path: %s", dhcpcanon_path); - pid_file = g_strdup_printf (RUNSTATEDIR "/dhcpcanon%c-%s.pid", nm_utils_addr_family_to_char (addr_family), iface); @@ -120,6 +112,7 @@ dhcpcanon_start (NMDhcpClient *client, /* Kill any existing dhcpcanon from the pidfile */ binary_name = g_path_get_basename (dhcpcanon_path); nm_dhcp_client_stop_existing (pid_file, binary_name); + g_free (binary_name); argv = g_ptr_array_new (); g_ptr_array_add (argv, (gpointer) dhcpcanon_path); @@ -127,8 +120,10 @@ dhcpcanon_start (NMDhcpClient *client, g_ptr_array_add (argv, (gpointer) "-sf"); /* Set script file */ g_ptr_array_add (argv, (gpointer) nm_dhcp_helper_path); - g_ptr_array_add (argv, (gpointer) "-pf"); /* Set pid file */ - g_ptr_array_add (argv, (gpointer) pid_file); + if (pid_file) { + g_ptr_array_add (argv, (gpointer) "-pf"); /* Set pid file */ + g_ptr_array_add (argv, (gpointer) pid_file); + } if (priv->conf_file) { g_ptr_array_add (argv, (gpointer) "-cf"); /* Set interface config file */ @@ -149,43 +144,33 @@ dhcpcanon_start (NMDhcpClient *client, g_ptr_array_add (argv, (gpointer) iface); g_ptr_array_add (argv, NULL); - 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, - "dhcpcanon failed to start: %s", - local->message); - return FALSE; + cmd_str = g_strjoinv (" ", (gchar **) argv->pdata); + 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 ("dhcpcanon started with pid %d", pid); + nm_dhcp_client_watch_child (client, pid); + priv->pid_file = pid_file; + } else { + _LOGW ("dhcpcanon failed to start: '%s'", error->message); + g_error_free (error); + g_free (pid_file); } - nm_assert (pid > 0); - _LOGI ("dhcpcanon started with pid %d", pid); - nm_dhcp_client_watch_child (client, pid); - priv->pid_file = g_steal_pointer (&pid_file); - return TRUE; + g_ptr_array_free (argv, TRUE); + g_free (system_bus_address_env); + return pid > 0 ? TRUE : FALSE; } 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) { - return dhcpcanon_start (client, - NULL, - NULL, - FALSE, - NULL, - 0, - error); + gboolean success = FALSE; + success = dhcpcanon_start (client, NULL, NULL, FALSE, NULL, 0); + return success; } static gboolean @@ -194,10 +179,11 @@ ip6_start (NMDhcpClient *client, const struct in6_addr *ll_addr, NMSettingIP6ConfigPrivacy privacy, GBytes *duid, - guint needed_prefixes, - GError **error) + guint needed_prefixes) { - nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhcpcanon plugin does not support IPv6"); + NMDhcpDhcpcanon *self = NM_DHCP_DHCPCANON (client); + + _LOGW ("the dhcpcd backend does not support IPv6"); return FALSE; } static void diff --git a/src/dhcp/nm-dhcp-dhcpcd.c b/src/dhcp/nm-dhcp-dhcpcd.c index 98ab5342..c4bcb084 100644 --- a/src/dhcp/nm-dhcp-dhcpcd.c +++ b/src/dhcp/nm-dhcp-dhcpcd.c @@ -81,21 +81,15 @@ nm_dhcp_dhcpcd_get_path (void) } 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) { NMDhcpDhcpcd *self = NM_DHCP_DHCPCD (client); NMDhcpDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE (self); - gs_unref_ptrarray GPtrArray *argv = NULL; + GPtrArray *argv = NULL; pid_t pid = -1; - GError *local = NULL; - gs_free char *cmd_str = NULL; - gs_free char *binary_name = NULL; - const char *iface; - const char *dhcpcd_path; - const char *hostname; + GError *error = NULL; + char *pid_contents = NULL, *binary_name, *cmd_str; + const char *iface, *dhcpcd_path, *hostname; g_return_val_if_fail (priv->pid_file == NULL, FALSE); @@ -108,13 +102,14 @@ ip4_start (NMDhcpClient *client, dhcpcd_path = nm_dhcp_dhcpcd_get_path (); if (!dhcpcd_path) { - nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhcpcd binary not found"); + _LOGW ("dhcpcd could not be found"); return FALSE; } /* Kill any existing dhcpcd from the pidfile */ binary_name = g_path_get_basename (dhcpcd_path); nm_dhcp_client_stop_existing (priv->pid_file, binary_name); + g_free (binary_name); argv = g_ptr_array_new (); g_ptr_array_add (argv, (gpointer) dhcpcd_path); @@ -133,11 +128,13 @@ ip4_start (NMDhcpClient *client, g_ptr_array_add (argv, (gpointer) "-c"); /* Set script file */ g_ptr_array_add (argv, (gpointer) nm_dhcp_helper_path); +#ifdef DHCPCD_SUPPORTS_IPV6 /* IPv4-only for now. NetworkManager knows better than dhcpcd when to * run IPv6, and dhcpcd's automatic Router Solicitations cause problems * with devices that don't expect them. */ g_ptr_array_add (argv, (gpointer) "-4"); +#endif hostname = nm_dhcp_client_get_hostname (client); @@ -156,30 +153,24 @@ ip4_start (NMDhcpClient *client, g_ptr_array_add (argv, (gpointer) iface); g_ptr_array_add (argv, NULL); - _LOGD ("running: %s", - (cmd_str = g_strjoinv (" ", (char **) 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, - "dhcpcd failed to start: %s", - local->message); - g_error_free (local); - return FALSE; + 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 ("dhcpcd started with pid %d", pid); + nm_dhcp_client_watch_child (client, pid); + } else { + _LOGW ("dhcpcd failed to start, error: '%s'", error->message); + g_error_free (error); } - nm_assert (pid > 0); - _LOGI ("dhcpcd started with pid %d", pid); - nm_dhcp_client_watch_child (client, pid); - return TRUE; + g_free (pid_contents); + g_ptr_array_free (argv, TRUE); + return pid > 0 ? TRUE : FALSE; } static gboolean @@ -188,10 +179,11 @@ ip6_start (NMDhcpClient *client, const struct in6_addr *ll_addr, NMSettingIP6ConfigPrivacy privacy, GBytes *duid, - guint needed_prefixes, - GError **error) + guint needed_prefixes) { - nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhcpcd plugin does not support IPv6"); + NMDhcpDhcpcd *self = NM_DHCP_DHCPCD (client); + + _LOGW ("the dhcpcd backend does not support IPv6"); return FALSE; } diff --git a/src/dhcp/nm-dhcp-helper.c b/src/dhcp/nm-dhcp-helper.c index 2b48d7a5..8ea55061 100644 --- a/src/dhcp/nm-dhcp-helper.c +++ b/src/dhcp/nm-dhcp-helper.c @@ -31,7 +31,7 @@ /*****************************************************************************/ -#if NM_MORE_LOGGING +#ifdef NM_MORE_LOGGING #define _NMLOG_ENABLED(level) TRUE #else #define _NMLOG_ENABLED(level) ((level) <= LOG_ERR) diff --git a/src/dhcp/nm-dhcp-manager.c b/src/dhcp/nm-dhcp-manager.c index 6c71af9d..67254323 100644 --- a/src/dhcp/nm-dhcp-manager.c +++ b/src/dhcp/nm-dhcp-manager.c @@ -172,39 +172,22 @@ client_start (NMDhcpManager *self, gboolean info_only, NMSettingIP6ConfigPrivacy privacy, const char *last_ip4_address, - guint needed_prefixes, - GError **error) + guint needed_prefixes) { NMDhcpManagerPrivate *priv; NMDhcpClient *client; gboolean success = FALSE; - gsize hwaddr_len; + g_return_val_if_fail (self, NULL); g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL); g_return_val_if_fail (ifindex > 0, NULL); g_return_val_if_fail (uuid != NULL, NULL); g_return_val_if_fail (!dhcp_client_id || g_bytes_get_size (dhcp_client_id) >= 2, NULL); - g_return_val_if_fail (!error || !*error, NULL); - - if (!hwaddr) { - nm_utils_error_set (error, - NM_UTILS_ERROR_UNKNOWN, - "missing MAC address"); - return NULL; - } - - hwaddr_len = g_bytes_get_size (hwaddr); - if ( hwaddr_len == 0 - || hwaddr_len > NM_UTILS_HWADDR_LEN_MAX) { - nm_utils_error_set (error, - NM_UTILS_ERROR_UNKNOWN, - "invalid MAC address"); - g_return_val_if_reached (NULL) ; - } priv = NM_DHCP_MANAGER_GET_PRIVATE (self); - nm_assert (priv->client_factory); + if (!priv->client_factory) + return NULL; /* Kill any old client instance */ client = get_client_for_ifindex (self, addr_family, ifindex); @@ -233,24 +216,10 @@ client_start (NMDhcpManager *self, c_list_link_tail (&priv->dhcp_client_lst_head, &client->dhcp_client_lst); g_signal_connect (client, NM_DHCP_CLIENT_SIGNAL_STATE_CHANGED, G_CALLBACK (client_state_changed), self); - if (addr_family == AF_INET) { - success = nm_dhcp_client_start_ip4 (client, - dhcp_client_id, - dhcp_anycast_addr, - hostname, - last_ip4_address, - error); - } else { - success = nm_dhcp_client_start_ip6 (client, - dhcp_client_id, - enforce_duid, - dhcp_anycast_addr, - ipv6_ll_addr, - hostname, - privacy, - needed_prefixes, - error); - } + if (addr_family == AF_INET) + success = nm_dhcp_client_start_ip4 (client, dhcp_client_id, dhcp_anycast_addr, hostname, last_ip4_address); + else + success = nm_dhcp_client_start_ip6 (client, dhcp_client_id, enforce_duid, dhcp_anycast_addr, ipv6_ll_addr, hostname, privacy, needed_prefixes); if (!success) { remove_client_unref (self, client); @@ -276,8 +245,7 @@ nm_dhcp_manager_start_ip4 (NMDhcpManager *self, GBytes *dhcp_client_id, guint32 timeout, const char *dhcp_anycast_addr, - const char *last_ip_address, - GError **error) + const char *last_ip_address) { NMDhcpManagerPrivate *priv; const char *hostname = NULL; @@ -314,7 +282,7 @@ nm_dhcp_manager_start_ip4 (NMDhcpManager *self, return client_start (self, AF_INET, multi_idx, iface, ifindex, hwaddr, uuid, route_table, route_metric, NULL, dhcp_client_id, 0, timeout, dhcp_anycast_addr, hostname, - use_fqdn, FALSE, 0, last_ip_address, 0, error); + use_fqdn, FALSE, 0, last_ip_address, 0); } /* Caller owns a reference to the NMDhcpClient on return */ @@ -336,8 +304,7 @@ nm_dhcp_manager_start_ip6 (NMDhcpManager *self, const char *dhcp_anycast_addr, gboolean info_only, NMSettingIP6ConfigPrivacy privacy, - guint needed_prefixes, - GError **error) + guint needed_prefixes) { NMDhcpManagerPrivate *priv; const char *hostname = NULL; @@ -352,7 +319,7 @@ nm_dhcp_manager_start_ip6 (NMDhcpManager *self, return client_start (self, AF_INET6, multi_idx, iface, ifindex, hwaddr, uuid, route_table, route_metric, ll_addr, duid, enforce_duid, timeout, dhcp_anycast_addr, hostname, TRUE, info_only, - privacy, NULL, needed_prefixes, error); + privacy, NULL, needed_prefixes); } void @@ -442,7 +409,7 @@ nm_dhcp_manager_init (NMDhcpManager *self) } } - g_return_if_fail (client_factory); + nm_assert (client_factory); nm_log_info (LOGD_DHCP, "dhcp-init: Using DHCP client '%s'", client_factory->name); diff --git a/src/dhcp/nm-dhcp-manager.h b/src/dhcp/nm-dhcp-manager.h index 1d9e5c21..7eb32c37 100644 --- a/src/dhcp/nm-dhcp-manager.h +++ b/src/dhcp/nm-dhcp-manager.h @@ -59,8 +59,7 @@ NMDhcpClient * nm_dhcp_manager_start_ip4 (NMDhcpManager *manager, GBytes *dhcp_client_id, guint32 timeout, const char *dhcp_anycast_addr, - const char *last_ip_address, - GError **error); + const char *last_ip_address); NMDhcpClient * nm_dhcp_manager_start_ip6 (NMDhcpManager *manager, struct _NMDedupMultiIndex *multi_idx, @@ -79,8 +78,7 @@ NMDhcpClient * nm_dhcp_manager_start_ip6 (NMDhcpManager *manager, const char *dhcp_anycast_addr, gboolean info_only, NMSettingIP6ConfigPrivacy privacy, - guint needed_prefixes, - GError **error); + guint needed_prefixes); /* For testing only */ extern const char* nm_dhcp_helper_path; diff --git a/src/dhcp/nm-dhcp-systemd.c b/src/dhcp/nm-dhcp-systemd.c index b51c6e7b..2d0202bb 100644 --- a/src/dhcp/nm-dhcp-systemd.c +++ b/src/dhcp/nm-dhcp-systemd.c @@ -567,10 +567,7 @@ get_arp_type (GBytes *hwaddr) } 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) { NMDhcpSystemd *self = NM_DHCP_SYSTEMD (client); NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (self); @@ -593,7 +590,7 @@ ip4_start (NMDhcpClient *client, r = sd_dhcp_client_new (&priv->client4, FALSE); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to create dhcp-client: %s"); + _LOGW ("failed to create client (%d)", r); return FALSE; } @@ -601,8 +598,8 @@ ip4_start (NMDhcpClient *client, r = sd_dhcp_client_attach_event (priv->client4, NULL, 0); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to attach event: %s"); - goto errout; + _LOGW ("failed to attach event (%d)", r); + goto error; } hwaddr = nm_dhcp_client_get_hw_addr (client); @@ -616,21 +613,21 @@ ip4_start (NMDhcpClient *client, len, get_arp_type (hwaddr)); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to set MAC address: %s"); - goto errout; + _LOGW ("failed to set MAC address (%d)", r); + goto error; } } r = sd_dhcp_client_set_ifindex (priv->client4, nm_dhcp_client_get_ifindex (client)); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to set ifindex: %s"); - goto errout; + _LOGW ("failed to set ififindex (%d)", r); + goto error; } r = sd_dhcp_client_set_callback (priv->client4, dhcp_event_cb, client); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to set callback: %s"); - goto errout; + _LOGW ("failed to set callback (%d)", r); + goto error; } dhcp_lease_load (&lease, priv->lease_file); @@ -643,8 +640,8 @@ ip4_start (NMDhcpClient *client, if (last_addr.s_addr) { r = sd_dhcp_client_set_request_address (priv->client4, &last_addr); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to set last IPv4 address: %s"); - goto errout; + _LOGW ("failed to set last IPv4 address (%d)", r); + goto error; } } @@ -684,25 +681,25 @@ ip4_start (NMDhcpClient *client, */ r = sd_dhcp_client_set_hostname (priv->client4, hostname); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to set DHCP hostname: %s"); - goto errout; + _LOGW ("failed to set DHCP hostname to '%s' (%d)", hostname, r); + goto error; } } r = sd_dhcp_client_start (priv->client4); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to start DHCP client: %s"); - goto errout; + _LOGW ("failed to start client (%d)", r); + goto error; } nm_dhcp_client_start_timeout (client); success = TRUE; -errout: +error: sd_dhcp_lease_unref (lease); if (!success) - sd_dhcp_client_unref (g_steal_pointer (&priv->client4)); + priv->client4 = sd_dhcp_client_unref (priv->client4); return success; } @@ -867,8 +864,7 @@ ip6_start (NMDhcpClient *client, const struct in6_addr *ll_addr, NMSettingIP6ConfigPrivacy privacy, GBytes *duid, - guint needed_prefixes, - GError **error) + guint needed_prefixes) { NMDhcpSystemd *self = NM_DHCP_SYSTEMD (client); NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (self); @@ -892,7 +888,7 @@ ip6_start (NMDhcpClient *client, r = sd_dhcp6_client_new (&priv->client6); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to create dhcp-client: %s"); + _LOGW ("failed to create client (%d)", r); return FALSE; } @@ -911,14 +907,14 @@ ip6_start (NMDhcpClient *client, &duid_arr[2], duid_len - 2); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to set DUID: %s"); + _LOGW ("failed to set DUID (%d)", r); return FALSE; } r = sd_dhcp6_client_attach_event (priv->client6, NULL, 0); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to attach event: %s"); - goto errout; + _LOGW ("failed to attach event (%d)", r); + goto error; } hwaddr = nm_dhcp_client_get_hw_addr (client); @@ -932,21 +928,21 @@ ip6_start (NMDhcpClient *client, len, get_arp_type (hwaddr)); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to set MAC address: %s"); - goto errout; + _LOGW ("failed to set MAC address (%d)", r); + goto error; } } r = sd_dhcp6_client_set_ifindex (priv->client6, nm_dhcp_client_get_ifindex (client)); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to set ifindex: %s"); - goto errout; + _LOGW ("failed to set ifindex (%d)", r); + goto error; } r = sd_dhcp6_client_set_callback (priv->client6, dhcp6_event_cb, client); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to set callback: %s"); - goto errout; + _LOGW ("failed to set callback (%d)", r); + goto error; } /* Add requested options */ @@ -957,29 +953,30 @@ ip6_start (NMDhcpClient *client, r = sd_dhcp6_client_set_local_address (priv->client6, ll_addr); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to set local address: %s"); - goto errout; + _LOGW ("failed to set local address (%d)", r); + goto error; } hostname = nm_dhcp_client_get_hostname (client); r = sd_dhcp6_client_set_fqdn (priv->client6, hostname); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to set DHCP hostname: %s"); - goto errout; + _LOGW ("failed to set DHCP hostname to '%s' (%d)", hostname, r); + goto error; } r = sd_dhcp6_client_start (priv->client6); if (r < 0) { - nm_utils_error_set_errno (error, r, "failed to start client: %s"); - goto errout; + _LOGW ("failed to start client (%d)", r); + goto error; } nm_dhcp_client_start_timeout (client); return TRUE; -errout: - sd_dhcp6_client_unref (g_steal_pointer (&priv->client6)); +error: + sd_dhcp6_client_unref (priv->client6); + priv->client6 = NULL; return FALSE; } diff --git a/src/dhcp/nm-dhcp-utils.c b/src/dhcp/nm-dhcp-utils.c index 6bbc670b..9185a135 100644 --- a/src/dhcp/nm-dhcp-utils.c +++ b/src/dhcp/nm-dhcp-utils.c @@ -594,7 +594,7 @@ ip6_add_domain_search (gpointer data, gpointer user_data) NMPlatformIP6Address nm_dhcp_utils_ip6_prefix_from_options (GHashTable *options) { - gs_strfreev char **split_addr = NULL; + gs_strfreev gchar **split_addr = NULL; NMPlatformIP6Address address = { 0, }; struct in6_addr tmp_addr; char *str = NULL; diff --git a/src/dhcp/tests/meson.build b/src/dhcp/tests/meson.build index 0fee26b2..9d8be427 100644 --- a/src/dhcp/tests/meson.build +++ b/src/dhcp/tests/meson.build @@ -8,6 +8,7 @@ foreach test_unit: test_units test_unit, test_unit + '.c', dependencies: test_nm_dep, + 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 14a1c786..2f369aac 100644 --- a/src/dhcp/tests/test-dhcp-dhclient.c +++ b/src/dhcp/tests/test-dhcp-dhclient.c @@ -86,7 +86,7 @@ test_config (const char *orig, if (expected_new_client_id) { g_assert (new_client_id); g_assert (g_bytes_equal (new_client_id, expected_new_client_id)); - } else + } else g_assert (new_client_id == NULL); } @@ -675,18 +675,27 @@ test_existing_multiline_alsoreq (void) static void test_one_duid (const char *escaped, const guint8 *unescaped, guint len) { - gs_unref_bytes GBytes *t1 = NULL; - gs_unref_bytes GBytes *t2 = NULL; - gs_free char *w = NULL; - - t1 = nm_dhcp_dhclient_unescape_duid (escaped); - g_assert (t1); - g_assert (nm_utils_gbytes_equal_mem (t1, unescaped, len)); - - t2 = g_bytes_new (unescaped, len); - w = nm_dhcp_dhclient_escape_duid (t2); + GBytes *t; + char *w; + gsize t_len; + gconstpointer t_arr; + + t = nm_dhcp_dhclient_unescape_duid (escaped); + g_assert (t); + t_arr = g_bytes_get_data (t, &t_len); + g_assert (t_arr); + g_assert_cmpint (t_len, ==, len); + g_assert_cmpint (memcmp (t_arr, unescaped, len), ==, 0); + g_bytes_unref (t); + + t = g_bytes_new_static (unescaped, len); + w = nm_dhcp_dhclient_escape_duid (t); g_assert (w); + g_assert_cmpint (strlen (escaped), ==, strlen (w)); g_assert_cmpstr (escaped, ==, w); + + g_bytes_unref (t); + g_free (w); } static void @@ -726,11 +735,15 @@ test_read_duid_from_leasefile (void) 0x13, 0x60, 0x67, 0x20, 0xec, 0x4c, 0x70 }; gs_unref_bytes GBytes *duid = NULL; GError *error = NULL; + gconstpointer duid_arr; + gsize duid_len; duid = nm_dhcp_dhclient_read_duid (TEST_DIR"/test-dhclient-duid.leases", &error); - nmtst_assert_success (duid, error); - - g_assert (nm_utils_gbytes_equal_mem (duid, expected, G_N_ELEMENTS (expected))); + g_assert_no_error (error); + g_assert (duid); + duid_arr = g_bytes_get_data (duid, &duid_len); + g_assert_cmpint (duid_len, ==, sizeof (expected)); + g_assert_cmpint (memcmp (duid_arr, expected, duid_len), ==, 0); } static void @@ -948,99 +961,6 @@ test_interface2 (void) NULL); } -static void -test_structured (void) -{ - gs_unref_bytes GBytes *new_client_id = NULL; - const guint8 bytes[] = "sad-and-useless"; - - static const char *const orig = \ - "interface \"eth0\" { \n" - " send host-name \"useless.example.com\";\n" - " hardware ethernet de:ad:80:86:ba:be;\n" - " send dhcp-client-identifier \"sad-and-useless\";\n" - " script \"/bin/useless\";\n" - " send dhcp-lease-time 8086;\n" - " request subnet-mask, broadcast-address, time-offset, routers,\n" - " domain-search, domain-name, host-name;\n" - " require subnet-mask;\n" - "} \n" - "\n" - " interface \"eth1\" { \n" - " send host-name \"sad.example.com\";\n" - " hardware ethernet de:ca:f6:66:ca:fe;\n" - " send dhcp-client-identifier \"useless-and-miserable\";\n" - " script \"/bin/miserable\";\n" - " send dhcp-lease-time 1337;\n" - " request subnet-mask, broadcast-address, time-offset, routers,\n" - " domain-search, domain-name, domain-name-servers, host-name;\n" - " require subnet-mask, domain-name-servers;\n" - " } \n" - "\n" - "pseudo \"secondary\" \"eth0\" { \n" - " send dhcp-client-identifier \"sad-useless-and-secondary\";\n" - " script \"/bin/secondary\";\n" - " send host-name \"secondary.useless.example.com\";\n" - " send dhcp-lease-time 666;\n" - " request routers;\n" - " require routers;\n" - " } \n" - "\n" - " pseudo \"tertiary\" \"eth0\" { \n" - " send dhcp-client-identifier \"sad-useless-and-tertiary\";\n" - " script \"/bin/tertiary\";\n" - " send host-name \"tertiary.useless.example.com\";\n" - "} \n" - "\n" - " alias{ \n" - " interface \"eth0\";\n" - " fixed-address 192.0.2.1;\n" - " option subnet-mask 255.255.255.0;\n" - " } \n" - " lease { \n" - " interface \"eth0\";\n" - " fixed-address 192.0.2.2;\n" - " option subnet-mask 255.255.255.0;\n" - " } \n"; - - static const char *const expected = \ - "# Created by NetworkManager\n" - "# Merged from /path/to/dhclient.conf\n" - "\n" - "send host-name \"useless.example.com\";\n" - "hardware ethernet de:ad:80:86:ba:be;\n" - "send dhcp-client-identifier \"sad-and-useless\";\n" - "send dhcp-lease-time 8086;\n" - "require subnet-mask;\n" - "\n" - "option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;\n" - "option ms-classless-static-routes code 249 = array of unsigned integer 8;\n" - "option wpad code 252 = string;\n" - "\n" - "request; # override dhclient defaults\n" - "also request subnet-mask;\n" - "also request broadcast-address;\n" - "also request time-offset;\n" - "also request routers;\n" - "also request domain-search;\n" - "also request domain-name;\n" - "also request host-name;\n" - "also request rfc3442-classless-static-routes;\n" - "also request ms-classless-static-routes;\n" - "also request static-routes;\n" - "also request wpad;\n" - "also request ntp-servers;\n" - "\n"; - - new_client_id = g_bytes_new (bytes, sizeof (bytes) - 1); - test_config (orig, expected, - AF_INET, NULL, 0, FALSE, - NULL, - new_client_id, - "eth0", - NULL); -} - static void test_config_req_intf (void) { @@ -1126,7 +1046,6 @@ main (int argc, char **argv) g_test_add_func ("/dhcp/dhclient/interface/1", test_interface1); g_test_add_func ("/dhcp/dhclient/interface/2", test_interface2); g_test_add_func ("/dhcp/dhclient/config/req_intf", test_config_req_intf); - g_test_add_func ("/dhcp/dhclient/structured", test_structured); g_test_add_func ("/dhcp/dhclient/read_duid_from_leasefile", test_read_duid_from_leasefile); g_test_add_func ("/dhcp/dhclient/read_commented_duid_from_leasefile", test_read_commented_duid_from_leasefile); -- cgit 1.3.0-6-gf8a5