utils: more flexible reading of /proc/sys/net
authorJiří Klimeš <jklimes@redhat.com>
Tue, 12 Jul 2011 07:08:04 +0000 (09:08 +0200)
committerJiří Klimeš <jklimes@redhat.com>
Mon, 27 Feb 2012 15:27:44 +0000 (16:27 +0100)
- changes nm_utils_get_proc_sys_net_value() to allow all values, not just 0,1
- adds nm_utils_get_proc_sys_net_value_with_bounds() for limiting valid values

src/NetworkManagerUtils.c
src/NetworkManagerUtils.h
src/ip6-manager/nm-ip6-manager.c
src/nm-device.c

index 34cef69..5b44cf6 100644 (file)
@@ -848,7 +848,7 @@ nm_utils_do_sysctl (const char *path, const char *value)
 gboolean
 nm_utils_get_proc_sys_net_value (const char *path,
                                  const char *iface,
-                                 guint32 *out_value)
+                                 gint32 *out_value)
 {
        GError *error = NULL;
        char *contents = NULL;
@@ -864,8 +864,8 @@ nm_utils_get_proc_sys_net_value (const char *path,
        } else {
                errno = 0;
                tmp = strtol (contents, NULL, 10);
-               if ((errno == 0) && (tmp == 0 || tmp == 1)) {
-                       *out_value = (guint32) tmp;
+               if (errno == 0) {
+                       *out_value = (gint32) tmp;
                        success = TRUE;
                }
                g_free (contents);
@@ -874,6 +874,28 @@ nm_utils_get_proc_sys_net_value (const char *path,
        return success;
 }
 
+gboolean
+nm_utils_get_proc_sys_net_value_with_bounds (const char *path,
+                                             const char *iface,
+                                             gint32 *out_value,
+                                             gint32 valid_min,
+                                             gint32 valid_max)
+{
+       gboolean result;
+       gint32 val;
+
+       result = nm_utils_get_proc_sys_net_value (path, iface, &val);
+
+       if (result) {
+               if (val >= valid_min && val <= valid_max)
+                       *out_value = val;
+               else
+                       result = FALSE;
+       }
+
+       return result;
+}
+
 static char *
 get_new_connection_name (const GSList *existing,
                          const char *format,
index bc2b08b..9c5393c 100644 (file)
@@ -15,7 +15,7 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
- * Copyright (C) 2004 - 2010 Red Hat, Inc.
+ * Copyright (C) 2004 - 2011 Red Hat, Inc.
  * Copyright (C) 2005 - 2008 Novell, Inc.
  */
 
@@ -83,7 +83,13 @@ gboolean nm_utils_do_sysctl (const char *path, const char *value);
 
 gboolean nm_utils_get_proc_sys_net_value (const char *path,
                                           const char *iface,
-                                          guint32 *out_value);
+                                          gint32 *out_value);
+
+gboolean nm_utils_get_proc_sys_net_value_with_bounds (const char *path,
+                                                      const char *iface,
+                                                      gint32 *out_value,
+                                                      gint32 valid_min,
+                                                      gint32 valid_max);
 
 void nm_utils_complete_generic (NMConnection *connection,
                                 const char *ctype,
index a427dff..40060c3 100644 (file)
@@ -91,7 +91,7 @@ typedef struct {
 
        char *disable_ip6_path;
        gboolean disable_ip6_save_valid;
-       guint32 disable_ip6_save;
+       gint32 disable_ip6_save;
 
        guint finish_addrconf_id;
        guint config_changed_id;
@@ -185,9 +185,10 @@ nm_ip6_device_new (NMIP6Manager *manager, int ifindex)
        device->disable_ip6_path = g_strdup_printf ("/proc/sys/net/ipv6/conf/%s/disable_ipv6",
                                                    device->iface);
        g_assert (device->disable_ip6_path);
-       device->disable_ip6_save_valid = nm_utils_get_proc_sys_net_value (device->disable_ip6_path,
-                                                                         device->iface,
-                                                                         &device->disable_ip6_save);
+       device->disable_ip6_save_valid = nm_utils_get_proc_sys_net_value_with_bounds (device->disable_ip6_path,
+                                                                                     device->iface,
+                                                                                     &device->disable_ip6_save,
+                                                                                     0, 1);
 
        return device;
 
index ef874eb..e59d888 100644 (file)
@@ -204,7 +204,7 @@ typedef struct {
        NMIP6Config *  ac_ip6_config;
 
        char *         ip6_accept_ra_path;
-       guint32        ip6_accept_ra_save;
+       gint32         ip6_accept_ra_save;
 
        NMDHCPClient *  dhcp6_client;
        guint32         dhcp6_mode;
@@ -281,9 +281,10 @@ update_accept_ra_save (NMDevice *self)
 
        /* Grab the original value of "accept_ra" so we can restore it when NM exits */
        priv->ip6_accept_ra_path = new_path;
-       if (!nm_utils_get_proc_sys_net_value (priv->ip6_accept_ra_path,
-                                             ip_iface,
-                                             &priv->ip6_accept_ra_save)) {
+       if (!nm_utils_get_proc_sys_net_value_with_bounds (priv->ip6_accept_ra_path,
+                                                         ip_iface,
+                                                         &priv->ip6_accept_ra_save,
+                                                         0, 1)) {
                g_free (priv->ip6_accept_ra_path);
                priv->ip6_accept_ra_path = NULL;
        }