diff options
Diffstat (limited to 'src/NetworkManagerUtils.c')
-rw-r--r-- | src/NetworkManagerUtils.c | 284 |
1 files changed, 34 insertions, 250 deletions
diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 45d867d96..db627091e 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -22,19 +22,14 @@ #include <glib.h> #include <errno.h> #include <fcntl.h> -#include <stdio.h> #include <string.h> #include <unistd.h> -#include <ctype.h> #include <stdlib.h> #include "NetworkManagerUtils.h" #include "nm-utils.h" #include "nm-logging.h" #include "nm-device.h" -#include "nm-dbus-manager.h" -#include "nm-dispatcher-action.h" -#include "nm-dbus-glib-types.h" #include "nm-setting-connection.h" #include "nm-setting-ip4-config.h" #include "nm-setting-ip6-config.h" @@ -368,221 +363,6 @@ nm_utils_merge_ip6_config (NMIP6Config *ip6_config, NMSettingIP6Config *setting) nm_ip6_config_set_never_default (ip6_config, TRUE); } -static void -dump_object_to_props (GObject *object, GHashTable *hash) -{ - GParamSpec **pspecs; - guint len = 0, i; - - pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &len); - for (i = 0; i < len; i++) { - value_hash_add_object_property (hash, - pspecs[i]->name, - object, - pspecs[i]->name, - pspecs[i]->value_type); - } - g_free (pspecs); -} - -static void -dump_dhcp4_to_props (NMDHCP4Config *config, GHashTable *hash) -{ - GSList *options, *iter; - - options = nm_dhcp4_config_list_options (config); - for (iter = options; iter; iter = g_slist_next (iter)) { - const char *option = (const char *) iter->data; - const char *val; - - val = nm_dhcp4_config_get_option (config, option); - value_hash_add_str (hash, option, val); - } - g_slist_free (options); -} - -static void -dump_dhcp6_to_props (NMDHCP6Config *config, GHashTable *hash) -{ - GSList *options, *iter; - - options = nm_dhcp6_config_list_options (config); - for (iter = options; iter; iter = g_slist_next (iter)) { - const char *option = (const char *) iter->data; - const char *val; - - val = nm_dhcp6_config_get_option (config, option); - value_hash_add_str (hash, option, val); - } - g_slist_free (options); -} - -static void -fill_device_props (NMDevice *device, - GHashTable *dev_hash, - GHashTable *ip4_hash, - GHashTable *ip6_hash, - GHashTable *dhcp4_hash, - GHashTable *dhcp6_hash) -{ - NMIP4Config *ip4_config; - NMIP6Config *ip6_config; - NMDHCP4Config *dhcp4_config; - NMDHCP6Config *dhcp6_config; - - /* If the action is for a VPN, send the VPN's IP interface instead of the device's */ - value_hash_add_str (dev_hash, NMD_DEVICE_PROPS_IP_INTERFACE, nm_device_get_ip_iface (device)); - value_hash_add_str (dev_hash, NMD_DEVICE_PROPS_INTERFACE, nm_device_get_iface (device)); - value_hash_add_uint (dev_hash, NMD_DEVICE_PROPS_TYPE, nm_device_get_device_type (device)); - value_hash_add_uint (dev_hash, NMD_DEVICE_PROPS_STATE, nm_device_get_state (device)); - value_hash_add_object_path (dev_hash, NMD_DEVICE_PROPS_PATH, nm_device_get_path (device)); - - ip4_config = nm_device_get_ip4_config (device); - if (ip4_config) - dump_object_to_props (G_OBJECT (ip4_config), ip4_hash); - - ip6_config = nm_device_get_ip6_config (device); - if (ip6_config) - dump_object_to_props (G_OBJECT (ip6_config), ip6_hash); - - dhcp4_config = nm_device_get_dhcp4_config (device); - if (dhcp4_config) - dump_dhcp4_to_props (dhcp4_config, dhcp4_hash); - - dhcp6_config = nm_device_get_dhcp6_config (device); - if (dhcp6_config) - dump_dhcp6_to_props (dhcp6_config, dhcp6_hash); -} - -static void -fill_vpn_props (NMIP4Config *ip4_config, - NMIP6Config *ip6_config, - GHashTable *ip4_hash, - GHashTable *ip6_hash) -{ - if (ip4_config) - dump_object_to_props (G_OBJECT (ip4_config), ip4_hash); - if (ip6_config) - dump_object_to_props (G_OBJECT (ip6_config), ip6_hash); -} - -static void -dispatcher_done_cb (DBusGProxy *proxy, DBusGProxyCall *call, gpointer user_data) -{ - dbus_g_proxy_end_call (proxy, call, NULL, G_TYPE_INVALID); - g_object_unref (proxy); -} - -void -nm_utils_call_dispatcher (const char *action, - NMConnection *connection, - NMDevice *device, - const char *vpn_iface, - NMIP4Config *vpn_ip4_config, - NMIP6Config *vpn_ip6_config) -{ - NMDBusManager *dbus_mgr; - DBusGProxy *proxy; - DBusGConnection *g_connection; - GHashTable *connection_hash; - GHashTable *connection_props; - GHashTable *device_props; - GHashTable *device_ip4_props; - GHashTable *device_ip6_props; - GHashTable *device_dhcp4_props; - GHashTable *device_dhcp6_props; - GHashTable *vpn_ip4_props; - GHashTable *vpn_ip6_props; - - g_return_if_fail (action != NULL); - - /* All actions except 'hostname' require a device */ - if (strcmp (action, "hostname") != 0) - g_return_if_fail (NM_IS_DEVICE (device)); - /* VPN actions require at least an IPv4 config (for now) */ - if (strcmp (action, "vpn-up") == 0) - g_return_if_fail (vpn_ip4_config != NULL); - - dbus_mgr = nm_dbus_manager_get (); - g_connection = nm_dbus_manager_get_connection (dbus_mgr); - proxy = dbus_g_proxy_new_for_name (g_connection, - NM_DISPATCHER_DBUS_SERVICE, - NM_DISPATCHER_DBUS_PATH, - NM_DISPATCHER_DBUS_IFACE); - if (!proxy) { - nm_log_err (LOGD_CORE, "could not get dispatcher proxy!"); - g_object_unref (dbus_mgr); - return; - } - - if (connection) { - connection_hash = nm_connection_to_hash (connection, NM_SETTING_HASH_FLAG_NO_SECRETS); - - connection_props = value_hash_create (); - - /* path */ - value_hash_add_object_path (connection_props, - NMD_CONNECTION_PROPS_PATH, - nm_connection_get_path (connection)); - } else { - connection_hash = value_hash_create (); - connection_props = value_hash_create (); - } - - device_props = value_hash_create (); - device_ip4_props = value_hash_create (); - device_ip6_props = value_hash_create (); - device_dhcp4_props = value_hash_create (); - device_dhcp6_props = value_hash_create (); - vpn_ip4_props = value_hash_create (); - vpn_ip6_props = value_hash_create (); - - /* hostname actions only send the hostname */ - if (strcmp (action, "hostname") != 0) { - fill_device_props (device, - device_props, - device_ip4_props, - device_ip6_props, - device_dhcp4_props, - device_dhcp6_props); - if (vpn_iface) - fill_vpn_props (vpn_ip4_config, NULL, vpn_ip4_props, vpn_ip6_props); - } - - /* Do a non-blocking call, but wait for the reply, because dbus-glib - * sometimes needs time to complete internal housekeeping. If we use - * dbus_g_proxy_call_no_reply(), that housekeeping (specifically the - * GetNameOwner response) doesn't complete and we run into an assert - * on unreffing the proxy. - */ - dbus_g_proxy_begin_call_with_timeout (proxy, "Action", - dispatcher_done_cb, - dbus_mgr, /* automatically unref the dbus mgr when call is done */ - g_object_unref, - 5000, - G_TYPE_STRING, action, - DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, connection_hash, - DBUS_TYPE_G_MAP_OF_VARIANT, connection_props, - DBUS_TYPE_G_MAP_OF_VARIANT, device_props, - DBUS_TYPE_G_MAP_OF_VARIANT, device_ip4_props, - DBUS_TYPE_G_MAP_OF_VARIANT, device_ip6_props, - DBUS_TYPE_G_MAP_OF_VARIANT, device_dhcp4_props, - DBUS_TYPE_G_MAP_OF_VARIANT, device_dhcp6_props, - G_TYPE_STRING, vpn_iface ? vpn_iface : "", - DBUS_TYPE_G_MAP_OF_VARIANT, vpn_ip4_props, - DBUS_TYPE_G_MAP_OF_VARIANT, vpn_ip6_props, - G_TYPE_INVALID); - g_hash_table_destroy (connection_hash); - g_hash_table_destroy (connection_props); - g_hash_table_destroy (device_props); - g_hash_table_destroy (device_ip4_props); - g_hash_table_destroy (device_ip6_props); - g_hash_table_destroy (device_dhcp4_props); - g_hash_table_destroy (device_dhcp6_props); - g_hash_table_destroy (vpn_ip4_props); - g_hash_table_destroy (vpn_ip6_props); -} - gboolean nm_match_spec_hwaddr (const GSList *specs, const char *hwaddr) { @@ -629,12 +409,12 @@ parse_subchannels (const char *subchannels, guint32 *a, guint32 *b, guint32 *c) g_return_val_if_fail (*c == 0, FALSE); /* sanity check */ - if (!isxdigit (subchannels[0])) + if (!g_ascii_isxdigit (subchannels[0])) return FALSE; /* Get the first channel */ while (*p && (*p != ',')) { - if (!isxdigit (*p) && (*p != '.')) + if (!g_ascii_isxdigit (*p) && (*p != '.')) return FALSE; /* Invalid chars */ if (i >= BUFSIZE) return FALSE; /* Too long to be a subchannel */ @@ -830,27 +610,48 @@ value_hash_add_object_property (GHashTable *hash, gboolean nm_utils_do_sysctl (const char *path, const char *value) { - int fd, len, nwrote, total; + int fd, len, nwrote, tries; + char *actual; + + g_return_val_if_fail (path != NULL, FALSE); + g_return_val_if_fail (value != NULL, FALSE); + g_return_val_if_fail (value[0], FALSE); fd = open (path, O_WRONLY | O_TRUNC); - if (fd == -1) + if (fd == -1) { + nm_log_warn (LOGD_CORE, "sysctl: failed to open '%s': (%d) %s", + path, errno, strerror (errno)); return FALSE; + } + + nm_log_dbg (LOGD_CORE, "sysctl: setting '%s' to '%s'", path, value); - len = strlen (value); - total = 0; - do { - nwrote = write (fd, value + total, len - total); + /* Most sysfs and sysctl options don't care about a trailing CR, while some + * (like infiniband) do. So always add the CR. Also, neither sysfs nor + * sysctl support partial writes so the CR must be added to the string we're + * about to write. + */ + actual = g_strdup_printf ("%s\n", value); + + /* Try to write the entire value three times if a partial write occurs */ + len = strlen (actual); + for (tries = 0, nwrote = 0; tries < 3 && nwrote != len; tries++) { + nwrote = write (fd, actual, len); if (nwrote == -1) { if (errno == EINTR) continue; - close (fd); - return FALSE; + break; } - total += nwrote; - } while (total < len); + } + g_free (actual); + + if (nwrote != len) { + nm_log_warn (LOGD_CORE, "sysctl: failed to set '%s' to '%s': (%d) %s", + path, value, errno, strerror (errno)); + } close (fd); - return TRUE; + return (nwrote == len); } gboolean @@ -1018,23 +819,6 @@ nm_utils_complete_generic (NMConnection *connection, } } -gboolean -nm_utils_is_uuid (const char *str) -{ - const char *p = str; - int num_dashes = 0; - - while (*p) { - if (*p == '-') - num_dashes++; - else if (!isxdigit (*p)) - return FALSE; - p++; - } - - return (num_dashes == 4) && (p - str == 36); -} - char * nm_utils_new_vlan_name (const char *parent_iface, guint32 vlan_id) { |