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.
This commit is contained in:
Dan Winship
2013-11-08 08:49:06 -05:00
parent 1981323b19
commit 7bc7da83ec
6 changed files with 43 additions and 142 deletions

View File

@@ -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,

View File

@@ -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,

View File

@@ -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) {

View File

@@ -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,14 +3525,14 @@ 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,
nm_platform_sysctl_set (priv->ip6_accept_ra_path,
priv->ip6_accept_ra_save ? "1" : "0");
}
ret = NM_ACT_STAGE_RETURN_STOP;
@@ -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,7 +5264,7 @@ 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,
nm_platform_sysctl_set (priv->ip6_accept_ra_path,
priv->ip6_accept_ra_save ? "1" : "0");
}
@@ -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);

View File

@@ -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;
}
/******************************************************************/
/**

View File

@@ -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);