platform: setter/getter of /proc/sys and /sys options
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
#define debug(format, ...) nm_log_dbg (LOGD_PLATFORM, format, __VA_ARGS__)
|
||||
|
||||
typedef struct {
|
||||
GHashTable *options;
|
||||
GArray *links;
|
||||
GArray *ip4_addresses;
|
||||
GArray *ip6_addresses;
|
||||
@@ -50,6 +51,24 @@ nm_fake_platform_setup (void)
|
||||
|
||||
/******************************************************************/
|
||||
|
||||
static gboolean
|
||||
sysctl_set (NMPlatform *platform, const char *path, const char *value)
|
||||
{
|
||||
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform);
|
||||
|
||||
g_hash_table_insert (priv->options, g_strdup (path), g_strdup (value));
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static char *
|
||||
sysctl_get (NMPlatform *platform, const char *path)
|
||||
{
|
||||
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform);
|
||||
|
||||
return g_strdup (g_hash_table_lookup (priv->options, path));
|
||||
}
|
||||
|
||||
static void
|
||||
link_init (NMPlatformLink *device, int ifindex, int type, const char *name)
|
||||
{
|
||||
@@ -735,6 +754,7 @@ nm_fake_platform_init (NMFakePlatform *fake_platform)
|
||||
{
|
||||
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (fake_platform);
|
||||
|
||||
priv->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
|
||||
priv->links = g_array_new (TRUE, TRUE, sizeof (NMPlatformLink));
|
||||
priv->ip4_addresses = g_array_new (TRUE, TRUE, sizeof (NMPlatformIP4Address));
|
||||
priv->ip6_addresses = g_array_new (TRUE, TRUE, sizeof (NMPlatformIP6Address));
|
||||
@@ -764,6 +784,7 @@ nm_fake_platform_finalize (GObject *object)
|
||||
{
|
||||
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (object);
|
||||
|
||||
g_hash_table_unref (priv->options);
|
||||
g_array_unref (priv->links);
|
||||
g_array_unref (priv->ip4_addresses);
|
||||
g_array_unref (priv->ip6_addresses);
|
||||
@@ -786,6 +807,9 @@ nm_fake_platform_class_init (NMFakePlatformClass *klass)
|
||||
|
||||
platform_class->setup = setup;
|
||||
|
||||
platform_class->sysctl_set = sysctl_set;
|
||||
platform_class->sysctl_get = sysctl_get;
|
||||
|
||||
platform_class->link_get_all = link_get_all;
|
||||
platform_class->link_add = link_add;
|
||||
platform_class->link_delete = link_delete;
|
||||
|
@@ -22,6 +22,7 @@
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <fcntl.h>
|
||||
#include <netinet/icmp6.h>
|
||||
#include <netinet/in.h>
|
||||
#include <linux/if_arp.h>
|
||||
@@ -725,6 +726,70 @@ event_notification (struct nl_msg *msg, gpointer user_data)
|
||||
|
||||
/******************************************************************/
|
||||
|
||||
static gboolean
|
||||
sysctl_set (NMPlatform *platform, const char *path, const char *value)
|
||||
{
|
||||
int fd, len, nwrote, tries;
|
||||
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) {
|
||||
error ("sysctl: failed to open '%s': (%d) %s",
|
||||
path, errno, strerror (errno));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
debug ("sysctl: setting '%s' to '%s'", path, value);
|
||||
|
||||
/* Most sysfs and sysctl options don't care about a trailing LF, while some
|
||||
* (like infiniband) do. So always add the LF. Also, neither sysfs nor
|
||||
* sysctl support partial writes so the LF 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) {
|
||||
error ("sysctl: interrupted, will try again");
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (nwrote != len && errno != EEXIST) {
|
||||
error ("sysctl: failed to set '%s' to '%s': (%d) %s",
|
||||
path, value, errno, strerror (errno));
|
||||
}
|
||||
|
||||
g_free (actual);
|
||||
close (fd);
|
||||
return (nwrote == len);
|
||||
}
|
||||
|
||||
static char *
|
||||
sysctl_get (NMPlatform *platform, const char *path)
|
||||
{
|
||||
GError *error = NULL;
|
||||
char *contents;
|
||||
|
||||
if (!g_file_get_contents (path, &contents, NULL, &error)) {
|
||||
error ("error reading %s: %s", path, error->message);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return g_strstrip (contents);
|
||||
}
|
||||
|
||||
/******************************************************************/
|
||||
|
||||
static GArray *
|
||||
link_get_all (NMPlatform *platform)
|
||||
{
|
||||
@@ -1450,6 +1515,9 @@ nm_linux_platform_class_init (NMLinuxPlatformClass *klass)
|
||||
|
||||
platform_class->setup = setup;
|
||||
|
||||
platform_class->sysctl_set = sysctl_set;
|
||||
platform_class->sysctl_get = sysctl_get;
|
||||
|
||||
platform_class->link_get_all = link_get_all;
|
||||
platform_class->link_add = link_add;
|
||||
platform_class->link_delete = link_delete;
|
||||
|
@@ -23,6 +23,7 @@
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "nm-platform.h"
|
||||
#include "nm-logging.h"
|
||||
@@ -175,6 +176,54 @@ reset_error (void)
|
||||
|
||||
/******************************************************************/
|
||||
|
||||
/**
|
||||
* nm_platform_sysctl_set:
|
||||
* @path: Absolute option path
|
||||
* @value: Value to write
|
||||
*
|
||||
* This function is intended to be used for writing values to sysctl-style
|
||||
* virtual runtime configuration files. This includes not only /proc/sys
|
||||
* but also for example /sys/class.
|
||||
*
|
||||
* Returns: %TRUE on success.
|
||||
*/
|
||||
gboolean
|
||||
nm_platform_sysctl_set (const char *path, const char *value)
|
||||
{
|
||||
reset_error ();
|
||||
|
||||
g_return_val_if_fail (path, FALSE);
|
||||
g_return_val_if_fail (value, FALSE);
|
||||
g_return_val_if_fail (klass->sysctl_set, FALSE);
|
||||
|
||||
/* Don't write outside known locations */
|
||||
g_assert (g_str_has_prefix (path, "/proc/sys")
|
||||
|| g_str_has_prefix (path, "/sys"));
|
||||
/* Don't write to suspicious locations */
|
||||
g_assert (!strstr (path, ".."));
|
||||
|
||||
return klass->sysctl_set (platform, path, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* nm_platform_sysctl_get:
|
||||
* @path: Absolute path to sysctl
|
||||
*
|
||||
* Returns: (transfer full): Contents of the virtual sysctl file.
|
||||
*/
|
||||
char *
|
||||
nm_platform_sysctl_get (const char *path)
|
||||
{
|
||||
reset_error ();
|
||||
|
||||
g_return_val_if_fail (path, NULL);
|
||||
g_return_val_if_fail (klass->sysctl_get, NULL);
|
||||
|
||||
return klass->sysctl_get (platform, path);
|
||||
}
|
||||
|
||||
/******************************************************************/
|
||||
|
||||
/**
|
||||
* nm_platform_link_get_all:
|
||||
*
|
||||
|
@@ -125,6 +125,9 @@ typedef struct {
|
||||
|
||||
gboolean (*setup) (NMPlatform *);
|
||||
|
||||
gboolean (*sysctl_set) (NMPlatform *, const char *path, const char *value);
|
||||
char * (*sysctl_get) (NMPlatform *, const char *path);
|
||||
|
||||
GArray *(*link_get_all) (NMPlatform *);
|
||||
gboolean (*link_add) (NMPlatform *, const char *name, NMLinkType type);
|
||||
gboolean (*link_delete) (NMPlatform *, int ifindex);
|
||||
@@ -224,6 +227,9 @@ void nm_platform_free (void);
|
||||
int nm_platform_get_error (void);
|
||||
const char *nm_platform_get_error_msg (void);
|
||||
|
||||
gboolean nm_platform_sysctl_set (const char *path, const char *value);
|
||||
char *nm_platform_sysctl_get (const char *path);
|
||||
|
||||
GArray *nm_platform_link_get_all (void);
|
||||
gboolean nm_platform_dummy_add (const char *name);
|
||||
gboolean nm_platform_bridge_add (const char *name);
|
||||
|
Reference in New Issue
Block a user