dispatcher: return error reason from nm_dispatcher_utils_construct_envp()
authorThomas Haller <thaller@redhat.com>
Tue, 4 Aug 2015 08:51:32 +0000 (10:51 +0200)
committerBeniamino Galvani <bgalvani@redhat.com>
Tue, 25 Aug 2015 13:27:19 +0000 (15:27 +0200)
Also, error out in handle_action() when nm_dispatcher_utils_construct_envp()
indicates a failure.

callouts/nm-dispatcher-utils.c
callouts/nm-dispatcher-utils.h
callouts/nm-dispatcher.c
callouts/tests/test-dispatcher-envp.c

index bbd0e49..6079d01 100644 (file)
@@ -331,7 +331,8 @@ nm_dispatcher_utils_construct_envp (const char *action,
                                     const char *vpn_ip_iface,
                                     GVariant *vpn_ip4_props,
                                     GVariant *vpn_ip6_props,
-                                    char **out_iface)
+                                    char **out_iface,
+                                    const char **out_error_message)
 {
        const char *iface = NULL, *ip_iface = NULL;
        const char *uuid = NULL, *id = NULL, *path = NULL;
@@ -343,6 +344,10 @@ nm_dispatcher_utils_construct_envp (const char *action,
        GSList *items = NULL, *iter;
        guint i;
        GVariant *con_setting;
+       const char *error_message_backup;
+
+       if (!out_error_message)
+               out_error_message = &error_message_backup;
 
        g_return_val_if_fail (action != NULL, NULL);
        g_return_val_if_fail (out_iface != NULL, NULL);
@@ -354,7 +359,7 @@ nm_dispatcher_utils_construct_envp (const char *action,
 
        /* Connection properties */
        if (!g_variant_lookup (connection_props, NMD_CONNECTION_PROPS_PATH, "&o", &path)) {
-               g_warning ("Missing or invalid required value " NMD_CONNECTION_PROPS_PATH "!");
+               *out_error_message = "Missing or invalid required value " NMD_CONNECTION_PROPS_PATH "!";
                return NULL;
        }
        items = g_slist_prepend (items, g_strdup_printf ("CONNECTION_DBUS_PATH=%s", path));
@@ -374,7 +379,7 @@ nm_dispatcher_utils_construct_envp (const char *action,
 
        /* interface name */
        if (!g_variant_lookup (device_props, NMD_DEVICE_PROPS_INTERFACE, "&s", &iface)) {
-               g_warning ("Missing or invalid required value " NMD_DEVICE_PROPS_INTERFACE "!");
+               *out_error_message = "Missing or invalid required value " NMD_DEVICE_PROPS_INTERFACE "!";
                return NULL;
        }
        if (!*iface)
@@ -384,7 +389,7 @@ nm_dispatcher_utils_construct_envp (const char *action,
        value = g_variant_lookup_value (device_props, NMD_DEVICE_PROPS_IP_INTERFACE, NULL);
        if (value) {
                if (!g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)) {
-                       g_warning ("Invalid value " NMD_DEVICE_PROPS_IP_INTERFACE "!");
+                       *out_error_message = "Invalid value " NMD_DEVICE_PROPS_IP_INTERFACE "!";
                        return NULL;
                }
                g_variant_unref (value);
@@ -393,14 +398,14 @@ nm_dispatcher_utils_construct_envp (const char *action,
 
        /* Device type */
        if (!g_variant_lookup (device_props, NMD_DEVICE_PROPS_TYPE, "u", NULL)) {
-               g_warning ("Missing or invalid required value " NMD_DEVICE_PROPS_TYPE "!");
+               *out_error_message = "Missing or invalid required value " NMD_DEVICE_PROPS_TYPE "!";
                return NULL;
        }
 
        /* Device state */
        value = g_variant_lookup_value (device_props, NMD_DEVICE_PROPS_STATE, G_VARIANT_TYPE_UINT32);
        if (!value) {
-               g_warning ("Missing or invalid required value " NMD_DEVICE_PROPS_STATE "!");
+               *out_error_message = "Missing or invalid required value " NMD_DEVICE_PROPS_STATE "!";
                return NULL;
        }
        dev_state = g_variant_get_uint32 (value);
@@ -408,25 +413,25 @@ nm_dispatcher_utils_construct_envp (const char *action,
 
        /* device itself */
        if (!g_variant_lookup (device_props, NMD_DEVICE_PROPS_PATH, "o", NULL)) {
-               g_warning ("Missing or invalid required value " NMD_DEVICE_PROPS_PATH "!");
+               *out_error_message = "Missing or invalid required value " NMD_DEVICE_PROPS_PATH "!";
                return NULL;
        }
 
        /* UUID and ID */
        con_setting = g_variant_lookup_value (connection_dict, NM_SETTING_CONNECTION_SETTING_NAME, NM_VARIANT_TYPE_SETTING);
        if (!con_setting) {
-               g_warning ("Failed to read connection setting");
+               *out_error_message = "Failed to read connection setting";
                return NULL;
        }
 
        if (!g_variant_lookup (con_setting, NM_SETTING_CONNECTION_UUID, "&s", &uuid)) {
-               g_warning ("Connection hash did not contain the UUID");
+               *out_error_message = "Connection hash did not contain the UUID";
                g_variant_unref (con_setting);
                return NULL;
        }
 
        if (!g_variant_lookup (con_setting, NM_SETTING_CONNECTION_ID, "&s", &id)) {
-               g_warning ("Connection hash did not contain the ID");
+               *out_error_message = "Connection hash did not contain the ID";
                g_variant_unref (con_setting);
                return NULL;
        }
@@ -473,6 +478,7 @@ nm_dispatcher_utils_construct_envp (const char *action,
                envp[i] = (char *) iter->data;
        g_slist_free (items);
 
+       *out_error_message = NULL;
        return envp;
 }
 
index f68b3ea..40a0d41 100644 (file)
@@ -35,7 +35,8 @@ nm_dispatcher_utils_construct_envp (const char *action,
                                     const char *vpn_ip_iface,
                                     GVariant *vpn_ip4_props,
                                     GVariant *vpn_ip6_props,
-                                    char **out_iface);
+                                    char **out_iface,
+                                    const char **out_error_message);
 
 #endif  /* __NETWORKMANAGER_DISPATCHER_UTILS_H__ */
 
index c769a80..6b665f4 100644 (file)
@@ -623,6 +623,7 @@ handle_action (NMDBusDispatcher *dbus_dispatcher,
        Request *request;
        char **p;
        guint i, num_nowait = 0;
+       const char *error_message = NULL;
 
        sorted_scripts = find_scripts (str_action);
 
@@ -651,7 +652,11 @@ handle_action (NMDBusDispatcher *dbus_dispatcher,
                                                            vpn_ip_iface,
                                                            vpn_ip4_props,
                                                            vpn_ip6_props,
-                                                           &request->iface);
+                                                           &request->iface,
+                                                           &error_message);
+
+       if (error_message)
+               g_warning (error_message);
 
        if (request->debug) {
                g_message ("------------ Action ID %p '%s' Interface %s Environment ------------",
@@ -673,6 +678,15 @@ handle_action (NMDBusDispatcher *dbus_dispatcher,
        }
        g_slist_free (sorted_scripts);
 
+       if (error_message) {
+               GVariant *results;
+
+               results = g_variant_new_array (G_VARIANT_TYPE ("(sus)"), NULL, 0);
+               g_dbus_method_invocation_return_value (context, g_variant_new ("(@a(sus))", results));
+               request_free (request);
+               return TRUE;
+       }
+
        nm_clear_g_source (&quit_id);
 
        h->num_requests_pending++;
index faa6c08..420f507 100644 (file)
@@ -458,6 +458,7 @@ test_generic (const char *file, const char *override_vpn_ip_iface)
        char *expected_iface = NULL;
        char *action = NULL;
        char *out_iface = NULL;
+       const char *error_message = NULL;
        GHashTable *expected_env = NULL;
        GError *error = NULL;
        gboolean success;
@@ -497,7 +498,13 @@ test_generic (const char *file, const char *override_vpn_ip_iface)
                                                   override_vpn_ip_iface ? override_vpn_ip_iface : vpn_ip_iface,
                                                   vpn_ip4_props,
                                                   vpn_ip6_props,
-                                                  &out_iface);
+                                                  &out_iface,
+                                                  &error_message);
+
+       g_assert ((!denv && error_message) || (denv && !error_message));
+
+       if (error_message)
+               g_warning (error_message);
 
        /* Print out environment for now */
 #ifdef DEBUG