ifcfg-suse: Clean up, upstream the patch which has been in use for a while

Since openSUSE 11.1 NetworkManager does not support reading yast network
setup. It's for your own good - you either want to use static configuration
(yast) or dynamic (NetworkManager). Mixing the two has never worked very well
and has caused a lot of confusion. The only exception to this is hostname
handling, which is handled by ifcfg-suse plugin.
This commit is contained in:
Tambet Ingo
2009-12-21 12:07:18 +02:00
parent 1e1be1cd57
commit b9ca266d94
9 changed files with 126 additions and 1743 deletions

View File

@@ -24,32 +24,19 @@
#include <config.h>
#include <string.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <gmodule.h>
#include <glib-object.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <nm-setting-connection.h>
#include <nm-setting-ip4-config.h>
#define G_UDEV_API_IS_SUBJECT_TO_CHANGE
#include <gudev/gudev.h>
#include "plugin.h"
#include "parser.h"
#include "nm-suse-connection.h"
#include "nm-system-config-interface.h"
#include "wireless-helper.h"
#define IFCFG_PLUGIN_NAME "ifcfg-suse"
#define IFCFG_PLUGIN_INFO "(C) 2008 Novell, Inc. To report bugs please use the NetworkManager mailing list."
#define IFCFG_DIR SYSCONFDIR "/sysconfig/network"
#define CONF_DHCP IFCFG_DIR "/dhcp"
#define HOSTNAME_FILE "/etc/HOSTNAME"
static void system_config_interface_init (NMSystemConfigInterface *system_config_interface_class);
@@ -63,15 +50,9 @@ G_DEFINE_TYPE_EXTENDED (SCPluginIfcfg, sc_plugin_ifcfg, G_TYPE_OBJECT, 0,
#define IFCFG_FILE_PATH_TAG "ifcfg-file-path"
typedef struct {
GUdevClient *client;
gboolean initialized;
GHashTable *connections;
GHashTable *unmanaged_specs;
guint32 default_gw;
GFileMonitor *default_gw_monitor;
guint default_gw_monitor_id;
GFileMonitor *hostname_monitor;
GFileMonitor *dhcp_monitor;
char *hostname;
} SCPluginIfcfgPrivate;
GQuark
@@ -85,285 +66,153 @@ ifcfg_plugin_error_quark (void)
return error_quark;
}
static void
ignore_cb (NMSettingsConnectionInterface *connection,
GError *error,
gpointer user_data)
{
}
typedef void (*FileChangedFn) (gpointer user_data);
typedef struct {
FileChangedFn callback;
gpointer user_data;
} FileMonitorInfo;
static void
update_connections (SCPluginIfcfg *self)
file_changed (GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
gpointer user_data)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init (&iter, priv->connections);
while (g_hash_table_iter_next (&iter, NULL, &value)) {
NMSuseConnection *exported = NM_SUSE_CONNECTION (value);
NMSettingIP4Config *ip4_config;
ip4_config = (NMSettingIP4Config *) nm_connection_get_setting (NM_CONNECTION (exported), NM_TYPE_SETTING_IP4_CONFIG);
if (!ip4_config)
continue;
if (nm_setting_ip4_config_get_num_addresses (ip4_config)) {
/* suse only has one address per device */
NMIP4Address *ip4_address;
ip4_address = nm_setting_ip4_config_get_address (ip4_config, 0);
if (nm_ip4_address_get_gateway (ip4_address) != priv->default_gw) {
nm_ip4_address_set_gateway (ip4_address, priv->default_gw);
nm_settings_connection_interface_update (NM_SETTINGS_CONNECTION_INTERFACE (exported),
ignore_cb,
NULL);
}
}
}
}
static void
routes_changed (GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
gpointer user_data)
{
SCPluginIfcfg *self = SC_PLUGIN_IFCFG (user_data);
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
char *filename;
guint32 new_gw;
FileMonitorInfo *info;
switch (event_type) {
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
case G_FILE_MONITOR_EVENT_DELETED:
filename = g_file_get_path (file);
new_gw = parser_parse_routes (filename);
g_free (filename);
if (priv->default_gw != new_gw) {
priv->default_gw = new_gw;
update_connections (self);
}
info = (FileMonitorInfo *) user_data;
info->callback (info->user_data);
break;
default:
break;
}
}
static void
monitor_routes (SCPluginIfcfg *self, const char *filename)
static GFileMonitor *
monitor_file_changes (const char *filename,
FileChangedFn callback,
gpointer user_data)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
GFile *file;
GFileMonitor *monitor;
FileMonitorInfo *info;
file = g_file_new_for_path (filename);
monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, NULL);
g_object_unref (file);
if (monitor) {
priv->default_gw_monitor_id = g_signal_connect (monitor, "changed", G_CALLBACK (routes_changed), self);
priv->default_gw_monitor = monitor;
info = g_new0 (FileMonitorInfo, 1);
info->callback = callback;
info->user_data = user_data;
g_object_weak_ref (G_OBJECT (monitor), (GWeakNotify) g_free, info);
g_signal_connect (monitor, "changed", G_CALLBACK (file_changed), info);
}
}
static void
read_connection (SCPluginIfcfg *self, GUdevDevice *device, NMDeviceType dev_type)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
const char *iface, *address;
guint32 ifindex;
iface = g_udev_device_get_name (device);
if (!iface)
return;
ifindex = (guint32) g_udev_device_get_property_as_uint64 (device, "IFINDEX");
if (parser_ignore_device (iface)) {
char *spec;
address = g_udev_device_get_sysfs_attr (device, "address");
if (address && (strlen (address) == 17)) {
spec = g_strdup_printf ("mac:%s", address);
g_hash_table_insert (priv->unmanaged_specs, GUINT_TO_POINTER (ifindex), spec);
g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNMANAGED_SPECS_CHANGED);
} else
PLUGIN_WARN (IFCFG_PLUGIN_NAME, " (%s) error getting hardware address", iface);
} else {
NMSuseConnection *connection;
connection = nm_suse_connection_new (iface, dev_type);
if (connection) {
g_hash_table_insert (priv->connections,
GUINT_TO_POINTER (ifindex),
connection);
g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_CONNECTION_ADDED, connection);
}
}
}
static void
read_connections_by_type (SCPluginIfcfg *self, NMDeviceType dev_type)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
GList *devices, *iter;
if ( (dev_type != NM_DEVICE_TYPE_ETHERNET)
&& (dev_type != NM_DEVICE_TYPE_WIFI))
return;
devices = g_udev_client_query_by_subsystem (priv->client, "net");
for (iter = devices; iter; iter = g_list_next (iter)) {
read_connection (self, G_UDEV_DEVICE (iter->data), dev_type);
g_object_unref (G_UDEV_DEVICE (iter->data));
}
g_list_free (devices);
return monitor;
}
static gboolean
is_wireless (GUdevDevice *device)
hostname_is_dynamic (void)
{
char phy80211_path[255];
struct stat s;
int fd;
struct iwreq iwr;
const char *ifname, *path;
gboolean is_wifi = FALSE;
GIOChannel *channel;
const char *pattern = "DHCLIENT_SET_HOSTNAME=";
char *str = NULL;
int pattern_len;
gboolean dynamic = FALSE;
ifname = g_udev_device_get_name (device);
g_assert (ifname);
channel = g_io_channel_new_file (CONF_DHCP, "r", NULL);
if (!channel)
return dynamic;
fd = socket (PF_INET, SOCK_DGRAM, 0);
strncpy (iwr.ifr_ifrn.ifrn_name, ifname, IFNAMSIZ);
pattern_len = strlen (pattern);
path = g_udev_device_get_sysfs_path (device);
snprintf (phy80211_path, sizeof (phy80211_path), "%s/phy80211", path);
while (g_io_channel_read_line (channel, &str, NULL, NULL, NULL) != G_IO_STATUS_EOF) {
if (!strncmp (str, pattern, pattern_len)) {
if (!strncmp (str + pattern_len, "\"yes\"", 5))
dynamic = TRUE;
break;
}
g_free (str);
}
if ( (ioctl (fd, SIOCGIWNAME, &iwr) == 0)
|| (stat (phy80211_path, &s) == 0 && (s.st_mode & S_IFDIR)))
is_wifi = TRUE;
g_io_channel_shutdown (channel, FALSE, NULL);
g_io_channel_unref (channel);
close (fd);
return is_wifi;
return dynamic;
}
static char *
hostname_read ()
{
GIOChannel *channel;
char *hostname = NULL;
channel = g_io_channel_new_file (HOSTNAME_FILE, "r", NULL);
if (channel) {
g_io_channel_read_line (channel, &hostname, NULL, NULL, NULL);
g_io_channel_shutdown (channel, FALSE, NULL);
g_io_channel_unref (channel);
if (hostname)
hostname = g_strchomp (hostname);
}
return hostname;
}
static void
handle_uevent (GUdevClient *client,
const char *action,
GUdevDevice *device,
gpointer user_data)
hostname_changed (gpointer data)
{
SCPluginIfcfg *self = SC_PLUGIN_IFCFG (user_data);
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
const char *subsys;
gboolean wifi;
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (data);
g_return_if_fail (action != NULL);
g_free (priv->hostname);
if (hostname_is_dynamic ())
priv->hostname = NULL;
else
priv->hostname = hostname_read ();
/* A bit paranoid */
subsys = g_udev_device_get_subsystem (device);
g_return_if_fail (subsys != NULL);
g_return_if_fail (strcmp (subsys, "net") == 0);
g_object_notify (G_OBJECT (data), NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
}
wifi = is_wireless (device);
static void
plugin_set_hostname (SCPluginIfcfg *plugin, const char *hostname)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (plugin);
GIOChannel *channel;
if (!strcmp (action, "add")) {
read_connection (self,
device,
wifi ? NM_DEVICE_TYPE_WIFI : NM_DEVICE_TYPE_ETHERNET);
} else if (!strcmp (action, "remove")) {
NMExportedConnection *exported;
guint32 ifindex;
ifindex = (guint32) g_udev_device_get_property_as_uint64 (device, "IFINDEX");
if (g_hash_table_remove (priv->unmanaged_specs, GUINT_TO_POINTER (ifindex)))
g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNMANAGED_SPECS_CHANGED);
exported = (NMExportedConnection *) g_hash_table_lookup (priv->connections,
GUINT_TO_POINTER (ifindex));
if (exported) {
nm_settings_connection_interface_delete (NM_SETTINGS_CONNECTION_INTERFACE (exported),
ignore_cb,
NULL);
g_hash_table_remove (priv->connections, GUINT_TO_POINTER (ifindex));
}
channel = g_io_channel_new_file (HOSTNAME_FILE, "w", NULL);
if (channel) {
g_io_channel_write_chars (channel, hostname, -1, NULL, NULL);
g_io_channel_write_chars (channel, "\n", -1, NULL, NULL);
g_io_channel_shutdown (channel, TRUE, NULL);
g_io_channel_unref (channel);
}
g_free (priv->hostname);
priv->hostname = hostname ? g_strdup (hostname) : NULL;
}
static void
init (NMSystemConfigInterface *config)
{
SCPluginIfcfg *self = SC_PLUGIN_IFCFG (config);
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
const char *subsys[2] = { "net", NULL };
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (config);
priv->client = g_udev_client_new (subsys);
if (!priv->client) {
PLUGIN_WARN (IFCFG_PLUGIN_NAME, " error initializing libgudev");
} else
g_signal_connect (priv->client, "uevent", G_CALLBACK (handle_uevent), self);
}
priv->hostname_monitor = monitor_file_changes (HOSTNAME_FILE, hostname_changed, config);
priv->dhcp_monitor = monitor_file_changes (CONF_DHCP, hostname_changed, config);
static GSList *
get_connections (NMSystemConfigInterface *config)
{
SCPluginIfcfg *self = SC_PLUGIN_IFCFG (config);
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
GSList *list = NULL;
GHashTableIter iter;
gpointer value;
if (!priv->initialized) {
const char *filename;
read_connections_by_type (self, NM_DEVICE_TYPE_ETHERNET);
read_connections_by_type (self, NM_DEVICE_TYPE_WIFI);
filename = SYSCONFDIR"/sysconfig/network/routes";
monitor_routes (self, filename);
priv->default_gw = parser_parse_routes (filename);
if (priv->default_gw)
update_connections (self);
priv->initialized = TRUE;
}
g_hash_table_iter_init (&iter, priv->connections);
while (g_hash_table_iter_next (&iter, NULL, &value))
list = g_slist_prepend (list, value);
return list;
}
static void
add_one_unmanaged_spec (gpointer key, gpointer val, gpointer user_data)
{
GSList **list = (GSList **) key;
*list = g_slist_prepend (*list, g_strdup ((const char *) val));
}
static GSList *
get_unmanaged_specs (NMSystemConfigInterface *config)
{
SCPluginIfcfg *self = SC_PLUGIN_IFCFG (config);
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
GSList *list = NULL;
g_hash_table_foreach (priv->unmanaged_specs, add_one_unmanaged_spec, &list);
return list;
if (!hostname_is_dynamic ())
priv->hostname = hostname_read ();
}
static void
sc_plugin_ifcfg_init (SCPluginIfcfg *self)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref);
priv->unmanaged_specs = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
}
static void
@@ -371,19 +220,13 @@ dispose (GObject *object)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (object);
g_hash_table_destroy (priv->connections);
g_hash_table_destroy (priv->unmanaged_specs);
if (priv->dhcp_monitor)
g_object_unref (priv->dhcp_monitor);
if (priv->default_gw_monitor) {
if (priv->default_gw_monitor_id)
g_signal_handler_disconnect (priv->default_gw_monitor, priv->default_gw_monitor_id);
if (priv->hostname_monitor)
g_object_unref (priv->hostname_monitor);
g_file_monitor_cancel (priv->default_gw_monitor);
g_object_unref (priv->default_gw_monitor);
}
if (priv->client)
g_object_unref (priv->client);
g_free (priv->hostname);
G_OBJECT_CLASS (sc_plugin_ifcfg_parent_class)->dispose (object);
}
@@ -400,10 +243,29 @@ get_property (GObject *object, guint prop_id,
g_value_set_string (value, IFCFG_PLUGIN_INFO);
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES:
g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_NONE);
g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME);
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
g_value_set_string (value, "");
g_value_set_string (value, SC_PLUGIN_IFCFG_GET_PRIVATE (object)->hostname);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
const char *hostname;
switch (prop_id) {
case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
hostname = g_value_get_string (value);
if (hostname && strlen (hostname) < 1)
hostname = NULL;
plugin_set_hostname (SC_PLUGIN_IFCFG (object), hostname);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -419,6 +281,7 @@ sc_plugin_ifcfg_class_init (SCPluginIfcfgClass *req_class)
g_type_class_add_private (req_class, sizeof (SCPluginIfcfgPrivate));
object_class->get_property = get_property;
object_class->set_property = set_property;
object_class->dispose = dispose;
g_object_class_override_property (object_class,
@@ -442,8 +305,6 @@ static void
system_config_interface_init (NMSystemConfigInterface *system_config_interface_class)
{
/* interface implementation */
system_config_interface_class->get_connections = get_connections;
system_config_interface_class->get_unmanaged_specs = get_unmanaged_specs;
system_config_interface_class->init = init;
}