summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Winship <danw@gnome.org>2013-11-08 08:49:06 -0500
committerDan Winship <danw@gnome.org>2013-11-15 10:49:43 -0500
commit7bc7da83ec46143392d8657e1923adcc6a94794e (patch)
treeddea0bb2e66810f5b8bb898653508d4918431937
parent1981323b19f1ad22e53ef9ac1cd3783346e7c121 (diff)
downloadNetworkManager-7bc7da83ec46143392d8657e1923adcc6a94794e.tar.gz
core: remove redundant sysctl utilities
NMDevice was still using the old sysctl functions from NetworkManagerUtils rather than the new NMPlatform ones. Fix it, and remove the old functions.
-rw-r--r--src/NetworkManagerUtils.c110
-rw-r--r--src/NetworkManagerUtils.h12
-rw-r--r--src/devices/nm-device-infiniband.c2
-rw-r--r--src/devices/nm-device.c35
-rw-r--r--src/platform/nm-platform.c25
-rw-r--r--src/platform/nm-platform.h1
6 files changed, 43 insertions, 142 deletions
diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c
index e09b94acf8..c2cf5e7a50 100644
--- a/src/NetworkManagerUtils.c
+++ b/src/NetworkManagerUtils.c
@@ -408,116 +408,6 @@ value_hash_add_object_property (GHashTable *hash,
value_hash_add (hash, key, value);
}
-/**
- * nm_utils_do_sysctl:
- * @path: path to write @value to
- * @value: value to write to @path
- *
- * Writes @value to the file at @path, trying 3 times on failure.
- *
- * Returns: %TRUE on success. On failure, returns %FALSE and sets errno.
- */
-gboolean
-nm_utils_do_sysctl (const char *path, const char *value)
-{
- int fd, len, nwrote, tries, saved_errno = 0;
- char *actual;
-
- g_return_val_if_fail (path != NULL, FALSE);
- g_return_val_if_fail (value != NULL, FALSE);
-
- fd = open (path, O_WRONLY | O_TRUNC);
- if (fd == -1) {
- saved_errno = errno;
- nm_log_warn (LOGD_CORE, "sysctl: failed to open '%s': (%d) %s",
- path, saved_errno, strerror (saved_errno));
- errno = saved_errno;
- return FALSE;
- }
-
- nm_log_dbg (LOGD_CORE, "sysctl: setting '%s' to '%s'", path, value);
-
- /* 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++) {
- errno = 0;
- nwrote = write (fd, actual, len);
- if (nwrote == -1) {
- if (errno == EINTR)
- continue;
- saved_errno = errno;
- break;
- }
- }
- g_free (actual);
- close (fd);
-
- if (nwrote != len && saved_errno != EEXIST) {
- nm_log_warn (LOGD_CORE, "sysctl: failed to set '%s' to '%s': (%d) %s",
- path, value, saved_errno, strerror (saved_errno));
- }
-
- errno = saved_errno;
- return (nwrote == len);
-}
-
-gboolean
-nm_utils_get_proc_sys_net_value (const char *path,
- const char *iface,
- gint32 *out_value)
-{
- GError *error = NULL;
- char *contents = NULL;
- gboolean success = FALSE;
- long int tmp;
-
- if (!g_file_get_contents (path, &contents, NULL, &error)) {
- nm_log_dbg (LOGD_DEVICE, "(%s): error reading %s: (%d) %s",
- iface, path,
- error ? error->code : -1,
- error && error->message ? error->message : "(unknown)");
- g_clear_error (&error);
- } else {
- errno = 0;
- tmp = strtol (contents, NULL, 10);
- if (errno == 0) {
- *out_value = (gint32) tmp;
- success = TRUE;
- }
- g_free (contents);
- }
-
- 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,
diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h
index 2d8844d8ed..93ec11134d 100644
--- a/src/NetworkManagerUtils.h
+++ b/src/NetworkManagerUtils.h
@@ -70,18 +70,6 @@ void value_hash_add_object_property (GHashTable *hash,
const char *prop,
GType val_type);
-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,
- 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_normalize_connection (NMConnection *connection,
gboolean default_enable_ipv6);
const char *nm_utils_get_ip_config_method (NMConnection *connection,
diff --git a/src/devices/nm-device-infiniband.c b/src/devices/nm-device-infiniband.c
index 2d1f142e02..be79094c07 100644
--- a/src/devices/nm-device-infiniband.c
+++ b/src/devices/nm-device-infiniband.c
@@ -180,7 +180,7 @@ act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason)
}
}
- ok = nm_utils_do_sysctl (mode_path, transport_mode);
+ ok = nm_platform_sysctl_set (mode_path, transport_mode);
g_free (mode_path);
if (!ok) {
diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c
index 7b939ac508..a5158caa66 100644
--- a/src/devices/nm-device.c
+++ b/src/devices/nm-device.c
@@ -419,10 +419,8 @@ 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_with_bounds (priv->ip6_accept_ra_path,
- ip_iface,
- &priv->ip6_accept_ra_save,
- 0, 2)) {
+ priv->ip6_accept_ra_save = nm_platform_sysctl_get_uint (priv->ip6_accept_ra_path);
+ if (priv->ip6_accept_ra_save < 0 || priv->ip6_accept_ra_save > 2) {
g_free (priv->ip6_accept_ra_path);
priv->ip6_accept_ra_path = NULL;
}
@@ -454,9 +452,8 @@ update_ip6_privacy_save (NMDevice *self)
/* Grab the original value of "use_tempaddr" so we can restore it when NM exits */
priv->ip6_privacy_tempaddr_path = new_path;
- if (!nm_utils_get_proc_sys_net_value (priv->ip6_privacy_tempaddr_path,
- ip_iface,
- &priv->ip6_privacy_tempaddr_save)) {
+ priv->ip6_privacy_tempaddr_save = nm_platform_sysctl_get_uint (priv->ip6_privacy_tempaddr_path);
+ if (priv->ip6_privacy_tempaddr_save < 0 || priv->ip6_privacy_tempaddr_save > 2) {
g_free (priv->ip6_privacy_tempaddr_path);
priv->ip6_privacy_tempaddr_path = NULL;
}
@@ -3528,15 +3525,15 @@ act_stage3_ip6_config_start (NMDevice *self,
} else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP) == 0) {
/* Router advertisements shouldn't be used in pure DHCP mode */
if (priv->ip6_accept_ra_path)
- nm_utils_do_sysctl (priv->ip6_accept_ra_path, "0");
+ nm_platform_sysctl_set (priv->ip6_accept_ra_path, "0");
priv->dhcp6_mode = NM_RDISC_DHCP_LEVEL_MANAGED;
ret = dhcp6_start (self, connection, priv->dhcp6_mode, reason);
} else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) == 0) {
/* reset the saved RA value when ipv6 is ignored */
if (priv->ip6_accept_ra_path) {
- nm_utils_do_sysctl (priv->ip6_accept_ra_path,
- priv->ip6_accept_ra_save ? "1" : "0");
+ nm_platform_sysctl_set (priv->ip6_accept_ra_path,
+ priv->ip6_accept_ra_save ? "1" : "0");
}
ret = NM_ACT_STAGE_RETURN_STOP;
} else if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL) == 0) {
@@ -3546,7 +3543,7 @@ act_stage3_ip6_config_start (NMDevice *self,
/* Router advertisements shouldn't be used in manual mode */
if (priv->ip6_accept_ra_path)
- nm_utils_do_sysctl (priv->ip6_accept_ra_path, "0");
+ nm_platform_sysctl_set (priv->ip6_accept_ra_path, "0");
ret = NM_ACT_STAGE_RETURN_SUCCESS;
} else {
nm_log_warn (LOGD_IP6, "(%s): unhandled IPv6 config method '%s'; will fail",
@@ -3583,7 +3580,7 @@ act_stage3_ip6_config_start (NMDevice *self,
break;
}
if (priv->ip6_privacy_tempaddr_path)
- nm_utils_do_sysctl (priv->ip6_privacy_tempaddr_path, ip6_privacy_str);
+ nm_platform_sysctl_set (priv->ip6_privacy_tempaddr_path, ip6_privacy_str);
return ret;
}
@@ -3971,13 +3968,13 @@ share_init (void)
NULL };
char **iter;
- if (!nm_utils_do_sysctl ("/proc/sys/net/ipv4/ip_forward", "1")) {
+ if (!nm_platform_sysctl_set ("/proc/sys/net/ipv4/ip_forward", "1")) {
nm_log_err (LOGD_SHARING, "Error starting IP forwarding: (%d) %s",
errno, strerror (errno));
return FALSE;
}
- if (!nm_utils_do_sysctl ("/proc/sys/net/ipv4/ip_dynaddr", "1")) {
+ if (!nm_platform_sysctl_set ("/proc/sys/net/ipv4/ip_dynaddr", "1")) {
nm_log_err (LOGD_SHARING, "error starting IP forwarding: (%d) %s",
errno, strerror (errno));
}
@@ -4508,11 +4505,11 @@ nm_device_deactivate (NMDevice *self, NMDeviceStateReason reason)
/* Turn off router advertisements until they are needed */
if (priv->ip6_accept_ra_path)
- nm_utils_do_sysctl (priv->ip6_accept_ra_path, "0");
+ nm_platform_sysctl_set (priv->ip6_accept_ra_path, "0");
/* Turn off IPv6 privacy extensions */
if (priv->ip6_privacy_tempaddr_path)
- nm_utils_do_sysctl (priv->ip6_privacy_tempaddr_path, "0");
+ nm_platform_sysctl_set (priv->ip6_privacy_tempaddr_path, "0");
/* Call device type-specific deactivation */
if (NM_DEVICE_GET_CLASS (self)->deactivate)
@@ -5267,8 +5264,8 @@ dispose (GObject *object)
/* reset the saved RA value */
if ( priv->ip6_accept_ra_path
&& g_file_test (priv->ip6_accept_ra_path, G_FILE_TEST_EXISTS)) {
- nm_utils_do_sysctl (priv->ip6_accept_ra_path,
- priv->ip6_accept_ra_save ? "1" : "0");
+ nm_platform_sysctl_set (priv->ip6_accept_ra_path,
+ priv->ip6_accept_ra_save ? "1" : "0");
}
/* reset the saved use_tempaddr value */
@@ -5277,7 +5274,7 @@ dispose (GObject *object)
char tmp[16];
snprintf (tmp, sizeof (tmp), "%d", priv->ip6_privacy_tempaddr_save);
- nm_utils_do_sysctl (priv->ip6_privacy_tempaddr_path, tmp);
+ nm_platform_sysctl_set (priv->ip6_privacy_tempaddr_path, tmp);
}
}
g_free (priv->ip6_accept_ra_path);
diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c
index 788e695081..3f7f05442a 100644
--- a/src/platform/nm-platform.c
+++ b/src/platform/nm-platform.c
@@ -243,6 +243,31 @@ nm_platform_sysctl_get (const char *path)
return klass->sysctl_get (platform, path);
}
+/**
+ * nm_platform_sysctl_get_uint:
+ * @path: Absolute path to sysctl
+ *
+ * Returns: (unsigned integer) contents of the sysctl file, or -1 on error
+ */
+int
+nm_platform_sysctl_get_uint (const char *path)
+{
+ char *value, *end;
+ long tmp;
+ int ret = -1;
+
+ value = nm_platform_sysctl_get (path);
+ if (!value)
+ return ret;
+
+ tmp = strtoul (value, &end, 10);
+ if (!*end)
+ ret = tmp;
+ g_free (value);
+
+ return ret;
+}
+
/******************************************************************/
/**
diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h
index 9cafbc7be7..ff338fc0c2 100644
--- a/src/platform/nm-platform.h
+++ b/src/platform/nm-platform.h
@@ -346,6 +346,7 @@ void nm_platform_query_devices (void);
gboolean nm_platform_sysctl_set (const char *path, const char *value);
char *nm_platform_sysctl_get (const char *path);
+int nm_platform_sysctl_get_uint (const char *path);
GArray *nm_platform_link_get_all (void);
gboolean nm_platform_dummy_add (const char *name);