settings: remove hostname handling from plugins

Remove all hostname-related code from plugins since this functionality
has been moved to the core.
This commit is contained in:
Beniamino Galvani 2015-03-23 09:15:51 +01:00
parent 6c3d71c431
commit d385a2a57a
16 changed files with 28 additions and 940 deletions

View file

@ -313,7 +313,10 @@ no-auto-default=*
<variablelist>
<varlistentry>
<term><varname>hostname</varname></term>
<listitem><para>Set a persistent hostname.</para></listitem>
<listitem><para>This key is deprecated and has no effect
since the hostname is now stored in /etc/hostname or other
system configuration files according to build options.
</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>unmanaged-devices</varname></term>

View file

@ -52,19 +52,11 @@ interface_init (gpointer g_iface)
(g_iface,
g_param_spec_uint (NM_SYSTEM_CONFIG_INTERFACE_CAPABILITIES, "", "",
NM_SYSTEM_CONFIG_INTERFACE_CAP_NONE,
( NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS
| NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME),
NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS,
NM_SYSTEM_CONFIG_INTERFACE_CAP_NONE,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
g_object_interface_install_property
(g_iface,
g_param_spec_string (NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME, "", "",
NULL,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/* Signals */
g_signal_new (NM_SYSTEM_CONFIG_INTERFACE_CONNECTION_ADDED,
iface_type,

View file

@ -43,7 +43,6 @@ GObject * nm_system_config_factory (void);
#define NM_SYSTEM_CONFIG_INTERFACE_NAME "name"
#define NM_SYSTEM_CONFIG_INTERFACE_INFO "info"
#define NM_SYSTEM_CONFIG_INTERFACE_CAPABILITIES "capabilities"
#define NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME "hostname"
#define NM_SYSTEM_CONFIG_INTERFACE_UNMANAGED_SPECS_CHANGED "unmanaged-specs-changed"
#define NM_SYSTEM_CONFIG_INTERFACE_UNRECOGNIZED_SPECS_CHANGED "unrecognized-specs-changed"
@ -52,7 +51,6 @@ GObject * nm_system_config_factory (void);
typedef enum {
NM_SYSTEM_CONFIG_INTERFACE_CAP_NONE = 0x00000000,
NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS = 0x00000001,
NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME = 0x00000002
/* When adding more capabilities, be sure to update the "Capabilities"
* property max value in nm-system-config-interface.c.
@ -65,7 +63,6 @@ typedef enum {
NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME = NM_SYSTEM_CONFIG_INTERFACE_PROP_FIRST,
NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO,
NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES,
NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME,
} NMSystemConfigInterfaceProp;

View file

@ -3,9 +3,8 @@ Plugins generally have three components:
1) plugin object: manages the individual "connections", which are
just objects wrapped around on-disk config data. The plugin handles requests
to add new connections via the NM D-Bus API, and also watches config
directories for changes to configuration data. It also handles reading and
writing the persistent hostname, if the plugin supports hostnames. Plugins
implement the NMSystemConfigInterface interface. See plugin.c.
directories for changes to configuration data. Plugins implement the
NMSystemConfigInterface interface. See plugin.c.
2) "connections": subclasses of NMSettingsConnection. They handle updates to
configuration data, deletion, etc. See NMExampleConnection.c.
@ -27,9 +26,8 @@ entry points into the plugin: nm_system_config_factory() and
the NMSystemConfigInterface methods.
The plugin also emits various signals (defined by NMSystemConfigInterface)
which NetworkManager listens for. These include persistent hostname changes
(if something modified the file in which the persistent hostname is stored)
and notifications of new connections if they were created via changes to
the on-disk files. The "connection" objects can also emit signals
(defined by the NMSettingsConnection and NMConnection superclasses) when the
connections' backing storage gets changed or deleted.
which NetworkManager listens for. These include notifications of new
connections if they were created via changes to the on-disk files. The
"connection" objects can also emit signals (defined by the NMSettingsConnection
and NMConnection superclasses) when the connections' backing storage gets
changed or deleted.

View file

@ -42,7 +42,6 @@
#include "common.h"
#include "nm-example-connection.h"
static char *plugin_get_hostname (SCPluginExample *plugin);
static void system_config_interface_init (NMSystemConfigInterface *system_config_interface_class);
/* GObject object definition. This actually defines the object and tells
@ -105,13 +104,6 @@ typedef struct {
GFileMonitor *conf_file_monitor;
guint conf_file_monitor_id;
/* Persistent hostname if the plugin supports hostnames. Normally used
* for distro plugins; ie Red Hat uses /etc/sysconfig/hostname while
* Debian uses /etc/hostname. Plugins can abstract the storage location
* and just tell NM what the persisten hostname is and when its backing
* file has changed. NM handles actually setting the hostname.
*/
char *hostname;
} SCPluginExamplePrivate;
static NMSettingsConnection *
@ -387,8 +379,6 @@ conf_file_changed (GFileMonitor *monitor,
gpointer data)
{
SCPluginExample *self = SC_PLUGIN_EXAMPLE (data);
SCPluginExamplePrivate *priv = SC_PLUGIN_EXAMPLE_GET_PRIVATE (self);
char *tmp;
switch (event_type) {
case G_FILE_MONITOR_EVENT_DELETED:
@ -399,18 +389,6 @@ conf_file_changed (GFileMonitor *monitor,
* updated specs we'll re-read the config file then.
*/
g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNMANAGED_SPECS_CHANGED);
/* Hostname may also have changed; read it and if it did actually
* change, notify NM.
*/
tmp = plugin_get_hostname (self);
if (g_strcmp0 (tmp, priv->hostname) != 0) {
g_free (priv->hostname);
priv->hostname = tmp;
tmp = NULL;
g_object_notify (G_OBJECT (self), NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
}
g_free (tmp);
break;
default:
break;
@ -449,8 +427,7 @@ setup_monitoring (NMSystemConfigInterface *config)
}
/* Set up a watch on our configuration file, basically just for watching
* whether the user has changed the unmanaged devices option or the
* persistent hostname.
* whether the user has changed the unmanaged devices option
*/
if (priv->conf_file) {
file = g_file_new_for_path (priv->conf_file);
@ -575,83 +552,6 @@ out:
return specs;
}
static char *
plugin_get_hostname (SCPluginExample *plugin)
{
SCPluginExamplePrivate *priv = SC_PLUGIN_EXAMPLE_GET_PRIVATE (plugin);
GKeyFile *key_file;
char *hostname = NULL;
GError *error = NULL;
if (!priv->conf_file)
return NULL;
/* Read the persistent hostname out of backing storage, which happens
* to be the NM config file. Other plugins (like distro-specific ones)
* should read it from the distro-specific location like /etc/hostname.
*/
key_file = g_key_file_new ();
if (g_key_file_load_from_file (key_file, priv->conf_file, G_KEY_FILE_NONE, &error))
hostname = g_key_file_get_value (key_file, "keyfile", "hostname", NULL);
else {
nm_log_warn (LOGD_SETTINGS, "Error parsing file '%s': %s", priv->conf_file, error->message);
g_error_free (error);
}
g_key_file_free (key_file);
return hostname;
}
static gboolean
plugin_set_hostname (SCPluginExample *plugin, const char *hostname)
{
SCPluginExamplePrivate *priv = SC_PLUGIN_EXAMPLE_GET_PRIVATE (plugin);
GKeyFile *key_file;
GError *error = NULL;
gboolean success = FALSE;
char *data;
gsize len;
if (!priv->conf_file) {
nm_log_warn (LOGD_SETTINGS, "Error saving hostname: no config file");
return FALSE;
}
/* This just saves the hostname to the NM config file in a section
* private to this plugin.
*/
key_file = g_key_file_new ();
if (!g_key_file_load_from_file (key_file, priv->conf_file, G_KEY_FILE_NONE, &error)) {
nm_log_warn (LOGD_SETTINGS, "Error parsing file '%s': %s", priv->conf_file, error->message);
g_error_free (error);
goto out;
}
g_key_file_set_string (key_file, "example", "hostname", hostname);
data = g_key_file_to_data (key_file, &len, &error);
if (data) {
/* Save updated file to disk */
g_file_set_contents (priv->conf_file, data, len, &error);
g_free (data);
/* Update internal copy of hostname */
g_free (priv->hostname);
priv->hostname = g_strdup (hostname);
success = TRUE;
}
if (error) {
nm_log_warn (LOGD_SETTINGS, "Error saving hostname: %s", error->message);
g_error_free (error);
}
out:
g_key_file_free (key_file);
return success;
}
/* GObject */
static void
@ -676,18 +576,11 @@ get_property (GObject *object, guint prop_id,
g_value_set_string (value, EXAMPLE_PLUGIN_INFO);
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES:
/* Return capabilities to NM; this plugin supports changing connections
* as well as being capable of saving the hostname to persistent storage.
/* Return capabilities to NM; this plugin supports changing connections.
* If the plugin can't write out updated configuration, then obviously
* it shouldn't advertise that capability. If it can't save hostnames
* to persistent storage, it shouldn't advertise that capability either.
* it shouldn't advertise that capability.
*/
g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS |
NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME);
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
/* Return the hostname we've read from persistent storage */
g_value_set_string (value, SC_PLUGIN_EXAMPLE_GET_PRIVATE (object)->hostname);
g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -699,19 +592,7 @@ 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:
/* We'll get here when the user has changed the hostname via NM's
* D-Bus interface and we're requested to save this hostname to
* persistent storage.
*/
hostname = g_value_get_string (value);
if (hostname && strlen (hostname) < 1)
hostname = NULL;
plugin_set_hostname (SC_PLUGIN_EXAMPLE (object), hostname);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -763,9 +644,6 @@ dispose (GObject *object)
priv->connections = NULL;
}
g_free (priv->hostname);
priv->hostname = NULL;
/* Chain up to the superclass */
G_OBJECT_CLASS (sc_plugin_example_parent_class)->dispose (object);
}
@ -809,10 +687,6 @@ sc_plugin_example_class_init (SCPluginExampleClass *req_class)
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES,
NM_SYSTEM_CONFIG_INTERFACE_CAPABILITIES);
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME,
NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
}
static void

View file

@ -182,10 +182,6 @@ sc_plugin_ibft_class_init (SCPluginIbftClass *req_class)
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES,
NM_SYSTEM_CONFIG_INTERFACE_CAPABILITIES);
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME,
NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
}
static void

View file

@ -53,7 +53,6 @@
#include "NetworkManagerUtils.h"
#include "nm-ifcfg-connection.h"
#include "nm-inotify-helper.h"
#include "shvar.h"
#include "reader.h"
#include "writer.h"
@ -110,13 +109,7 @@ G_DEFINE_TYPE_EXTENDED (SCPluginIfcfg, sc_plugin_ifcfg, G_TYPE_OBJECT, 0,
typedef struct {
GHashTable *connections; /* uuid::connection */
gboolean initialized;
gulong ih_event_id;
int sc_network_wd;
GFileMonitor *hostname_monitor;
guint hostname_monitor_id;
char *hostname;
GFileMonitor *ifcfg_monitor;
guint ifcfg_monitor_id;
@ -707,139 +700,6 @@ add_connection (NMSystemConfigInterface *config,
return NM_SETTINGS_CONNECTION (update_connection (self, connection, path, NULL, FALSE, NULL, error));
}
#define SC_NETWORK_FILE "/etc/sysconfig/network"
#define HOSTNAME_FILE "/etc/hostname"
static char *
plugin_get_hostname (SCPluginIfcfg *plugin)
{
shvarFile *network;
char *hostname;
gboolean ignore_localhost;
if (g_file_get_contents (HOSTNAME_FILE, &hostname, NULL, NULL)) {
g_strchomp (hostname);
return hostname;
}
network = svOpenFile (SC_NETWORK_FILE, NULL);
if (!network) {
_LOGW ("Could not get hostname: failed to read " SC_NETWORK_FILE);
return NULL;
}
hostname = svGetValue (network, "HOSTNAME", FALSE);
ignore_localhost = svTrueValue (network, "NM_IGNORE_HOSTNAME_LOCALHOST", FALSE);
if (ignore_localhost) {
/* Ignore a default hostname ('localhost[6]' or 'localhost[6].localdomain[6]')
* to preserve 'network' service behavior.
*/
if (hostname && !nm_utils_is_specific_hostname (hostname)) {
g_free (hostname);
hostname = NULL;
}
}
svCloseFile (network);
return hostname;
}
static gboolean
plugin_set_hostname (SCPluginIfcfg *plugin, const char *hostname)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (plugin);
shvarFile *network;
char *hostname_eol;
gboolean ret;
#if HAVE_SELINUX
security_context_t se_ctx_prev = NULL, se_ctx = NULL;
struct stat file_stat = { .st_mode = 0 };
mode_t st_mode = 0;
/* Get default context for HOSTNAME_FILE and set it for fscreate */
if (stat (HOSTNAME_FILE, &file_stat) == 0)
st_mode = file_stat.st_mode;
matchpathcon (HOSTNAME_FILE, st_mode, &se_ctx);
matchpathcon_fini ();
getfscreatecon (&se_ctx_prev);
setfscreatecon (se_ctx);
#endif
hostname_eol = g_strdup_printf ("%s\n", hostname);
ret = g_file_set_contents (HOSTNAME_FILE, hostname_eol, -1, NULL);
#if HAVE_SELINUX
/* Restore previous context and cleanup */
setfscreatecon (se_ctx_prev);
freecon (se_ctx);
freecon (se_ctx_prev);
#endif
if (!ret) {
_LOGW ("Could not save hostname: failed to create/open " HOSTNAME_FILE);
g_free (hostname_eol);
return FALSE;
}
g_free (priv->hostname);
priv->hostname = g_strdup (hostname);
g_free (hostname_eol);
/* Remove "HOSTNAME" from SC_NETWORK_FILE, if present */
network = svOpenFile (SC_NETWORK_FILE, NULL);
if (network) {
svSetValue (network, "HOSTNAME", NULL, FALSE);
svWriteFile (network, 0644, NULL);
svCloseFile (network);
}
return TRUE;
}
static void
hostname_maybe_changed (SCPluginIfcfg *plugin)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (plugin);
char *new_hostname;
new_hostname = plugin_get_hostname (plugin);
if ( (new_hostname && !priv->hostname)
|| (!new_hostname && priv->hostname)
|| (priv->hostname && new_hostname && strcmp (priv->hostname, new_hostname))) {
g_free (priv->hostname);
priv->hostname = new_hostname;
g_object_notify (G_OBJECT (plugin), NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
} else
g_free (new_hostname);
}
static void
sc_network_changed_cb (NMInotifyHelper *ih,
struct inotify_event *evt,
const char *path,
gpointer user_data)
{
SCPluginIfcfg *plugin = SC_PLUGIN_IFCFG (user_data);
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (plugin);
if (evt->wd != priv->sc_network_wd)
return;
hostname_maybe_changed (plugin);
}
static void
hostname_changed_cb (GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
gpointer user_data)
{
SCPluginIfcfg *plugin = SC_PLUGIN_IFCFG (user_data);
hostname_maybe_changed (plugin);
}
static gboolean
impl_ifcfgrh_get_ifcfg_details (SCPluginIfcfg *plugin,
const char *in_ifcfg,
@ -913,36 +773,11 @@ static void
sc_plugin_ifcfg_init (SCPluginIfcfg *plugin)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (plugin);
NMInotifyHelper *ih;
GError *error = NULL;
gboolean success = FALSE;
GFile *file;
GFileMonitor *monitor;
priv->connections = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
/* We watch SC_NETWORK_FILE via NMInotifyHelper (which doesn't track file creation but
* *does* track modifications made via other hard links), since we expect it to always
* exist. But we watch HOSTNAME_FILE via GFileMonitor (which has the opposite
* semantics), since /etc/hostname might not exist, but is unlikely to have hard
* links. bgo 532815 is the bug for being able to just use GFileMonitor for both.
*/
ih = nm_inotify_helper_get ();
priv->ih_event_id = g_signal_connect (ih, "event", G_CALLBACK (sc_network_changed_cb), plugin);
priv->sc_network_wd = nm_inotify_helper_add_watch (ih, SC_NETWORK_FILE);
file = g_file_new_for_path (HOSTNAME_FILE);
monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, NULL);
g_object_unref (file);
if (monitor) {
priv->hostname_monitor_id =
g_signal_connect (monitor, "changed", G_CALLBACK (hostname_changed_cb), plugin);
priv->hostname_monitor = monitor;
}
priv->hostname = plugin_get_hostname (plugin);
priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
if (!priv->bus) {
_LOGW ("Couldn't connect to D-Bus: %s", error->message);
@ -987,33 +822,12 @@ dispose (GObject *object)
{
SCPluginIfcfg *plugin = SC_PLUGIN_IFCFG (object);
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (plugin);
NMInotifyHelper *ih;
if (priv->bus) {
dbus_g_connection_unref (priv->bus);
priv->bus = NULL;
}
if (priv->ih_event_id) {
ih = nm_inotify_helper_get ();
g_signal_handler_disconnect (ih, priv->ih_event_id);
priv->ih_event_id = 0;
if (priv->sc_network_wd >= 0)
nm_inotify_helper_remove_watch (ih, priv->sc_network_wd);
}
if (priv->hostname_monitor) {
if (priv->hostname_monitor_id)
g_signal_handler_disconnect (priv->hostname_monitor, priv->hostname_monitor_id);
g_file_monitor_cancel (priv->hostname_monitor);
g_object_unref (priv->hostname_monitor);
}
g_free (priv->hostname);
if (priv->connections) {
g_hash_table_destroy (priv->connections);
priv->connections = NULL;
@ -1034,8 +848,6 @@ static void
get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (object);
switch (prop_id) {
case NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME:
g_value_set_string (value, IFCFG_PLUGIN_NAME);
@ -1044,10 +856,7 @@ 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_MODIFY_CONNECTIONS | NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME);
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
g_value_set_string (value, priv->hostname);
g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -1059,15 +868,7 @@ 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);
break;
@ -1097,10 +898,6 @@ sc_plugin_ifcfg_class_init (SCPluginIfcfgClass *req_class)
NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES,
NM_SYSTEM_CONFIG_INTERFACE_CAPABILITIES);
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME,
NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (req_class),
&dbus_glib_nm_ifcfg_rh_object_info);
}

View file

@ -35,9 +35,6 @@
#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);
@ -45,159 +42,9 @@ G_DEFINE_TYPE_EXTENDED (SCPluginIfcfg, sc_plugin_ifcfg, G_TYPE_OBJECT, 0,
G_IMPLEMENT_INTERFACE (NM_TYPE_SYSTEM_CONFIG_INTERFACE,
system_config_interface_init))
#define SC_PLUGIN_IFCFG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SC_TYPE_PLUGIN_IFCFG, SCPluginIfcfgPrivate))
#define IFCFG_FILE_PATH_TAG "ifcfg-file-path"
typedef struct {
GFileMonitor *hostname_monitor;
GFileMonitor *dhcp_monitor;
char *hostname;
} SCPluginIfcfgPrivate;
typedef void (*FileChangedFn) (gpointer user_data);
typedef struct {
FileChangedFn callback;
gpointer user_data;
} FileMonitorInfo;
static void
file_changed (GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
gpointer user_data)
{
FileMonitorInfo *info;
switch (event_type) {
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
case G_FILE_MONITOR_EVENT_DELETED:
info = (FileMonitorInfo *) user_data;
info->callback (info->user_data);
break;
default:
break;
}
}
static GFileMonitor *
monitor_file_changes (const char *filename,
FileChangedFn callback,
gpointer user_data)
{
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) {
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);
}
return monitor;
}
static gboolean
hostname_is_dynamic (void)
{
GIOChannel *channel;
const char *pattern = "DHCLIENT_SET_HOSTNAME=";
char *str = NULL;
int pattern_len;
gboolean dynamic = FALSE;
channel = g_io_channel_new_file (CONF_DHCP, "r", NULL);
if (!channel)
return dynamic;
pattern_len = strlen (pattern);
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);
}
g_io_channel_shutdown (channel, FALSE, NULL);
g_io_channel_unref (channel);
return dynamic;
}
static char *
hostname_read (void)
{
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
hostname_changed (gpointer data)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (data);
g_free (priv->hostname);
if (hostname_is_dynamic ())
priv->hostname = NULL;
else
priv->hostname = hostname_read ();
g_object_notify (G_OBJECT (data), NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
}
static void
plugin_set_hostname (SCPluginIfcfg *plugin, const char *hostname)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (plugin);
GIOChannel *channel;
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 = g_strdup (hostname);
}
static void
init (NMSystemConfigInterface *config)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (config);
priv->hostname_monitor = monitor_file_changes (HOSTNAME_FILE, hostname_changed, config);
priv->dhcp_monitor = monitor_file_changes (CONF_DHCP, hostname_changed, config);
if (!hostname_is_dynamic ())
priv->hostname = hostname_read ();
}
static void
@ -205,22 +52,6 @@ sc_plugin_ifcfg_init (SCPluginIfcfg *self)
{
}
static void
dispose (GObject *object)
{
SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (object);
if (priv->dhcp_monitor)
g_object_unref (priv->dhcp_monitor);
if (priv->hostname_monitor)
g_object_unref (priv->hostname_monitor);
g_free (priv->hostname);
G_OBJECT_CLASS (sc_plugin_ifcfg_parent_class)->dispose (object);
}
static void
get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
@ -233,10 +64,7 @@ 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_MODIFY_HOSTNAME);
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
g_value_set_string (value, SC_PLUGIN_IFCFG_GET_PRIVATE (object)->hostname);
g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_NONE);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -248,15 +76,7 @@ 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);
break;
@ -268,11 +88,8 @@ sc_plugin_ifcfg_class_init (SCPluginIfcfgClass *req_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (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,
NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME,
@ -285,10 +102,6 @@ sc_plugin_ifcfg_class_init (SCPluginIfcfgClass *req_class)
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES,
NM_SYSTEM_CONFIG_INTERFACE_CAPABILITIES);
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME,
NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
}
static void

View file

@ -136,47 +136,6 @@ reload_parsers (void)
return TRUE;
}
gchar *
read_hostname (const char *path)
{
gchar *contents = NULL, *result = NULL, *tmp;
gchar **all_lines = NULL;
guint line_num, i;
if (!g_file_get_contents (path, &contents, NULL, NULL))
return NULL;
all_lines = g_strsplit (contents, "\n", 0);
line_num = g_strv_length (all_lines);
for (i = 0; i < line_num; i++) {
g_strstrip (all_lines[i]);
if (all_lines[i][0] == '#' || all_lines[i][0] == '\0')
continue;
if (g_str_has_prefix (all_lines[i], "hostname")) {
tmp = strstr (all_lines[i], "=");
tmp++;
result = g_shell_unquote (tmp, NULL);
break;
}
}
g_strfreev (all_lines);
g_free (contents);
return result;
}
gboolean
write_hostname (const char *path, const gchar *hostname)
{
gboolean result;
char *contents;
contents = g_strdup_printf ("#Generated by NetworkManager\n"
"hostname=\"%s\"\n", hostname);
result = g_file_set_contents (path, contents, -1, NULL);
g_free (contents);
return result;
}
gboolean
is_static_ip4 (const char *conn_name)
{

View file

@ -37,8 +37,6 @@ typedef struct _ip_block {
struct _ip_block *next;
} ip_block;
gchar *read_hostname (const char *path);
gboolean write_hostname (const char *path, const char *hostname);
gboolean is_static_ip4 (const char *conn_name);
gboolean is_static_ip6 (const char *conn_name);
gboolean is_ip4_address (const char *in_address);

View file

@ -45,16 +45,13 @@
#define IFNET_PLUGIN_NAME_PRINT "ifnet"
#define IFNET_PLUGIN_INFO "(C) 1999-2010 Gentoo Foundation, Inc. To report bugs please use bugs.gentoo.org with [networkmanager] or [qiaomuf] prefix."
#define IFNET_SYSTEM_HOSTNAME_FILE "/etc/conf.d/hostname"
#define IFNET_MANAGE_WELL_KNOWN_DEFAULT TRUE
#define IFNET_KEY_FILE_KEY_MANAGED "managed"
typedef struct {
GHashTable *connections; /* uuid::connection */
gchar *hostname;
gboolean unmanaged_well_known;
GFileMonitor *hostname_monitor;
GFileMonitor *net_monitor;
GFileMonitor *wpa_monitor;
@ -81,42 +78,6 @@ ignore_cb(NMSettingsConnectionInterface * connection,
{
}
*/
static const char *
get_hostname (NMSystemConfigInterface * config)
{
return SC_PLUGIN_IFNET_GET_PRIVATE (config)->hostname;
}
static void
update_system_hostname (gpointer config)
{
SCPluginIfnetPrivate *priv = SC_PLUGIN_IFNET_GET_PRIVATE (config);
if (priv->hostname)
g_free (priv->hostname);
priv->hostname = read_hostname (IFNET_SYSTEM_HOSTNAME_FILE);
g_object_notify (G_OBJECT (config),
NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
nm_log_info (LOGD_SETTINGS, "Hostname updated to: %s", priv->hostname);
}
static void
write_system_hostname (NMSystemConfigInterface * config,
const gchar * newhostname)
{
SCPluginIfnetPrivate *priv = SC_PLUGIN_IFNET_GET_PRIVATE (config);
g_return_if_fail (newhostname);
nm_log_info (LOGD_SETTINGS, "Write system hostname: %s", newhostname);
if (write_hostname (IFNET_SYSTEM_HOSTNAME_FILE, newhostname)) {
g_free (priv->hostname);
priv->hostname = g_strdup (newhostname);
g_object_notify (G_OBJECT (config),
NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
} else
nm_log_warn (LOGD_SETTINGS, "Write system hostname: %s failed", newhostname);
}
static gboolean
is_managed_plugin (void)
@ -189,9 +150,6 @@ setup_monitors (NMIfnetConnection * connection, gpointer user_data)
SCPluginIfnet *self = SC_PLUGIN_IFNET (user_data);
SCPluginIfnetPrivate *priv = SC_PLUGIN_IFNET_GET_PRIVATE (self);
priv->hostname_monitor =
monitor_file_changes (IFNET_SYSTEM_HOSTNAME_FILE,
update_system_hostname, user_data);
if (nm_config_get_monitor_connection_files (nm_config_get ())) {
priv->net_monitor =
monitor_file_changes (CONF_NET_FILE, (FileChangedFn) reload_connections,
@ -208,10 +166,6 @@ cancel_monitors (NMIfnetConnection * connection, gpointer user_data)
SCPluginIfnet *self = SC_PLUGIN_IFNET (user_data);
SCPluginIfnetPrivate *priv = SC_PLUGIN_IFNET_GET_PRIVATE (self);
if (priv->hostname_monitor) {
g_file_monitor_cancel (priv->hostname_monitor);
g_object_unref (priv->hostname_monitor);
}
if (priv->net_monitor) {
g_file_monitor_cancel (priv->net_monitor);
g_object_unref (priv->net_monitor);
@ -444,7 +398,6 @@ init (NMSystemConfigInterface *config)
setup_monitors (NULL, config);
reload_connections (config);
update_system_hostname (self);
nm_log_info (LOGD_SETTINGS, "Initialzation complete!");
}
@ -490,8 +443,6 @@ static void
get_property (GObject * object, guint prop_id, GValue * value,
GParamSpec * pspec)
{
NMSystemConfigInterface *self = NM_SYSTEM_CONFIG_INTERFACE (object);
switch (prop_id) {
case NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME:
g_value_set_string (value, IFNET_PLUGIN_NAME_PRINT);
@ -501,12 +452,7 @@ get_property (GObject * object, guint prop_id, GValue * value,
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES:
g_value_set_uint (value,
NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS
|
NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME);
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
g_value_set_string (value, get_hostname (self));
NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -519,15 +465,6 @@ set_property (GObject * object, guint prop_id, const GValue * value,
GParamSpec * pspec)
{
switch (prop_id) {
case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:{
const gchar *hostname = g_value_get_string (value);
if (hostname && strlen (hostname) < 1)
hostname = NULL;
write_system_hostname (NM_SYSTEM_CONFIG_INTERFACE
(object), hostname);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -546,9 +483,6 @@ dispose (GObject * object)
priv->connections = NULL;
}
g_free (priv->hostname);
priv->hostname = NULL;
ifnet_destroy ();
wpa_parser_destroy ();
G_OBJECT_CLASS (sc_plugin_ifnet_parent_class)->dispose (object);
@ -576,10 +510,6 @@ sc_plugin_ifnet_class_init (SCPluginIfnetClass * req_class)
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES,
NM_SYSTEM_CONFIG_INTERFACE_CAPABILITIES);
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME,
NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
}
G_MODULE_EXPORT GObject *

View file

@ -40,7 +40,6 @@ TESTS = test-ifnet
endif
EXTRA_DIST = \
hostname \
net \
net.all \
nm-system-settings.conf \

View file

@ -1,2 +0,0 @@
#Generated by NetworkManager
hostname="gentoo"

View file

@ -69,32 +69,6 @@ test_getdata (void)
strcmp ("!wpa_supplicant", ifnet_get_global_data ("modules")) == 0);
}
static void
test_read_hostname (void)
{
char *hostname;
hostname = read_hostname (TEST_IFNET_DIR "/hostname");
g_assert_cmpstr (hostname, ==, "gentoo");
g_free (hostname);
}
static void
test_write_hostname (void)
{
char *hostname_path = TEST_SCRATCH_DIR "/hostname-test";
char *hostname;
write_hostname (hostname_path, "gentoo-nm");
hostname = read_hostname (hostname_path);
g_assert_cmpstr (hostname, ==, "gentoo-nm");
g_free (hostname);
unlink (hostname_path);
}
static void
test_is_static (void)
{
@ -404,8 +378,6 @@ main (int argc, char **argv)
g_test_add_func (TPATH "has-ip6-address", test_has_ip6_address);
g_test_add_func (TPATH "has-default-route", test_has_default_route);
g_test_add_func (TPATH "get-data", test_getdata);
g_test_add_func (TPATH "read-hostname", test_read_hostname);
g_test_add_func (TPATH "write-hostname", test_write_hostname);
g_test_add_func (TPATH "is-ip4-address", test_is_ip4_address);
g_test_add_func (TPATH "is-ip6-address", test_is_ip6_address);
g_test_add_func (TPATH "convert-ip4-config", test_convert_ipv4_config_block);

View file

@ -25,8 +25,6 @@
#include "config.h"
#include <string.h>
#include <sys/inotify.h>
#include <gmodule.h>
#include <glib-object.h>
@ -48,7 +46,6 @@
#include "nm-ifupdown-connection.h"
#include "plugin.h"
#include "parser.h"
#include "nm-inotify-helper.h"
#include "nm-logging.h"
#include "nm-config.h"
@ -61,7 +58,6 @@
#define IFUPDOWN_PLUGIN_NAME "ifupdown"
#define IFUPDOWN_PLUGIN_INFO "(C) 2008 Canonical Ltd. To report bugs please use the NetworkManager mailing list."
#define IFUPDOWN_SYSTEM_HOSTNAME_FILE "/etc/hostname"
#define IFUPDOWN_KEY_FILE_GROUP "ifupdown"
#define IFUPDOWN_KEY_FILE_KEY_MANAGED "managed"
@ -76,7 +72,6 @@ typedef struct {
GUdevClient *client;
GHashTable *connections; /* /e/n/i block name :: NMIfupdownConnection */
gchar* hostname;
/* Stores all blocks/interfaces read from /e/n/i regardless of whether
* there is an NMIfupdownConnection for block.
@ -87,9 +82,6 @@ typedef struct {
GHashTable *kernel_ifaces;
gboolean unmanage_well_known;
gulong inotify_event_id;
int inotify_system_hostname_wd;
} SCPluginIfupdownPrivate;
static void
@ -134,18 +126,6 @@ GObject__set_property (GObject *object, guint prop_id,
static void
GObject__dispose (GObject *object);
/* other helpers */
static const char *
get_hostname (NMSystemConfigInterface *config);
static void
update_system_hostname(NMInotifyHelper *inotify_helper,
struct inotify_event *evt,
const char *path,
NMSystemConfigInterface *config);
static void
system_config_interface_init (NMSystemConfigInterface *system_config_interface_class)
{
@ -176,10 +156,6 @@ sc_plugin_ifupdown_class_init (SCPluginIfupdownClass *req_class)
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES,
NM_SYSTEM_CONFIG_INTERFACE_CAPABILITIES);
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME,
NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
}
static void
@ -326,7 +302,6 @@ SCPluginIfupdown_init (NMSystemConfigInterface *config)
SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (self);
GHashTable *auto_ifaces;
if_block *block = NULL;
NMInotifyHelper *inotify_helper;
char *value;
GError *error = NULL;
GList *keys, *iter;
@ -355,17 +330,6 @@ SCPluginIfupdown_init (NMSystemConfigInterface *config)
g_signal_connect (priv->client, "uevent", G_CALLBACK (handle_uevent), self);
priv->unmanage_well_known = IFUPDOWN_UNMANAGE_WELL_KNOWN_DEFAULT;
inotify_helper = nm_inotify_helper_get ();
priv->inotify_event_id = g_signal_connect (inotify_helper,
"event",
G_CALLBACK (update_system_hostname),
config);
priv->inotify_system_hostname_wd =
nm_inotify_helper_add_watch (inotify_helper, IFUPDOWN_SYSTEM_HOSTNAME_FILE);
update_system_hostname (inotify_helper, NULL, NULL, config);
/* Read in all the interfaces */
ifparser_init (ENI_INTERFACES_FILE, 0);
@ -553,76 +517,6 @@ SCPluginIfupdown_get_unmanaged_specs (NMSystemConfigInterface *config)
return specs;
}
static const char *
get_hostname (NMSystemConfigInterface *config)
{
SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config);
return priv->hostname;
}
static void
update_system_hostname(NMInotifyHelper *inotify_helper,
struct inotify_event *evt,
const char *path,
NMSystemConfigInterface *config)
{
SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config);
gchar *hostname_file = NULL;
gsize hostname_file_len = 0;
GError *error = NULL;
nm_log_info (LOGD_SETTINGS, "update_system_hostname");
if (evt && evt->wd != priv->inotify_system_hostname_wd)
return;
if(!g_file_get_contents ( IFUPDOWN_SYSTEM_HOSTNAME_FILE,
&hostname_file,
&hostname_file_len,
&error)) {
nm_log_warn (LOGD_SETTINGS, "update_system_hostname() - couldn't read "
IFUPDOWN_SYSTEM_HOSTNAME_FILE " (%d/%s)",
error->code, error->message);
return;
}
g_free(priv->hostname);
priv->hostname = g_strstrip(hostname_file);
/* We shouldn't return a zero-length hostname, but NULL */
if (priv->hostname && !strlen (priv->hostname)) {
g_free (priv->hostname);
priv->hostname = NULL;
}
g_object_notify (G_OBJECT (config), NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
}
static void
write_system_hostname(NMSystemConfigInterface *config,
const char *newhostname)
{
GError *error = NULL;
SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config);
nm_log_info (LOGD_SETTINGS, "write_system_hostname: %s", newhostname);
g_return_if_fail (newhostname);
if(!g_file_set_contents ( IFUPDOWN_SYSTEM_HOSTNAME_FILE,
newhostname,
-1,
&error)) {
nm_log_warn (LOGD_SETTINGS, "update_system_hostname() - couldn't write hostname (%s) to "
IFUPDOWN_SYSTEM_HOSTNAME_FILE " (%d/%s)",
newhostname, error->code, error->message);
} else {
priv->hostname = g_strdup (newhostname);
}
g_object_notify (G_OBJECT (config), NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
}
static void
sc_plugin_ifupdown_init (SCPluginIfupdown *plugin)
{
@ -632,8 +526,6 @@ static void
GObject__get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
{
NMSystemConfigInterface *self = NM_SYSTEM_CONFIG_INTERFACE (object);
switch (prop_id) {
case NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME:
g_value_set_string (value, IFUPDOWN_PLUGIN_NAME);
@ -642,13 +534,8 @@ GObject__get_property (GObject *object, guint prop_id,
g_value_set_string (value, IFUPDOWN_PLUGIN_INFO);
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES:
g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME);
g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_NONE);
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
{
g_value_set_string (value, get_hostname(self));
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -660,15 +547,6 @@ GObject__set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
switch (prop_id) {
case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
{
const gchar *hostname = g_value_get_string (value);
if (hostname && strlen (hostname) < 1)
hostname = NULL;
write_system_hostname(NM_SYSTEM_CONFIG_INTERFACE(object),
hostname);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -680,12 +558,6 @@ GObject__dispose (GObject *object)
{
SCPluginIfupdown *plugin = SC_PLUGIN_IFUPDOWN (object);
SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (plugin);
NMInotifyHelper *inotify_helper = nm_inotify_helper_get ();
g_signal_handler_disconnect (inotify_helper, priv->inotify_event_id);
if (priv->inotify_system_hostname_wd >= 0)
nm_inotify_helper_remove_watch (inotify_helper, priv->inotify_system_hostname_wd);
if (priv->kernel_ifaces)
g_hash_table_destroy(priv->kernel_ifaces);

View file

@ -47,7 +47,6 @@
#include "utils.h"
#include "gsystem-local-alloc.h"
static char *plugin_get_hostname (SCPluginKeyfile *plugin);
static void system_config_interface_init (NMSystemConfigInterface *system_config_interface_class);
G_DEFINE_TYPE_EXTENDED (SCPluginKeyfile, sc_plugin_keyfile, G_TYPE_OBJECT, 0,
@ -67,8 +66,6 @@ typedef struct {
GFileMonitor *conf_file_monitor;
guint conf_file_monitor_id;
char *hostname;
gboolean disposed;
} SCPluginKeyfilePrivate;
@ -329,29 +326,12 @@ conf_file_changed (GFileMonitor *monitor,
gpointer data)
{
SCPluginKeyfile *self = SC_PLUGIN_KEYFILE (data);
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (self);
char *tmp;
switch (event_type) {
case G_FILE_MONITOR_EVENT_DELETED:
case G_FILE_MONITOR_EVENT_CREATED:
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNMANAGED_SPECS_CHANGED);
/* hostname */
tmp = plugin_get_hostname (self);
if ((tmp && !priv->hostname)
|| (!tmp && priv->hostname)
|| (priv->hostname && tmp && strcmp (priv->hostname, tmp))) {
g_free (priv->hostname);
priv->hostname = tmp;
tmp = NULL;
g_object_notify (G_OBJECT (self), NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
}
g_free (tmp);
break;
default:
break;
@ -602,82 +582,6 @@ get_unmanaged_specs (NMSystemConfigInterface *config)
return specs;
}
static char *
plugin_get_hostname (SCPluginKeyfile *plugin)
{
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (plugin);
GKeyFile *key_file;
char *hostname = NULL;
GError *error = NULL;
if (!priv->conf_file)
return NULL;
key_file = g_key_file_new ();
if (!parse_key_file_allow_none (priv, key_file, &error))
goto out;
hostname = g_key_file_get_value (key_file, "keyfile", "hostname", NULL);
out:
if (error) {
nm_log_warn (LOGD_SETTINGS, "keyfile: error getting hostname: %s", error->message);
g_error_free (error);
}
if (key_file)
g_key_file_free (key_file);
return hostname;
}
static gboolean
plugin_set_hostname (SCPluginKeyfile *plugin, const char *hostname)
{
gboolean ret = FALSE;
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (plugin);
GKeyFile *key_file = NULL;
GError *error = NULL;
char *data = NULL;
gsize len;
if (!priv->conf_file) {
g_set_error (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Error saving hostname: no config file");
goto out;
}
g_free (priv->hostname);
priv->hostname = g_strdup (hostname);
key_file = g_key_file_new ();
if (!parse_key_file_allow_none (priv, key_file, &error))
goto out;
g_key_file_set_string (key_file, "keyfile", "hostname", hostname);
data = g_key_file_to_data (key_file, &len, &error);
if (!data)
goto out;
if (!g_file_set_contents (priv->conf_file, data, len, &error)) {
g_prefix_error (&error, "Error saving hostname: ");
goto out;
}
ret = TRUE;
out:
if (error) {
nm_log_warn (LOGD_SETTINGS, "keyfile: error setting hostname: %s", error->message);
g_error_free (error);
}
g_free (data);
if (key_file)
g_key_file_free (key_file);
return ret;
}
/* GObject */
static void
@ -700,11 +604,7 @@ get_property (GObject *object, guint prop_id,
g_value_set_string (value, KEYFILE_PLUGIN_INFO);
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES:
g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS |
NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME);
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
g_value_set_string (value, SC_PLUGIN_KEYFILE_GET_PRIVATE (object)->hostname);
g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -716,15 +616,7 @@ 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_KEYFILE (object), hostname);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -757,8 +649,6 @@ dispose (GObject *object)
g_object_unref (priv->conf_file_monitor);
}
g_free (priv->hostname);
if (priv->connections) {
g_hash_table_destroy (priv->connections);
priv->connections = NULL;
@ -790,10 +680,6 @@ sc_plugin_keyfile_class_init (SCPluginKeyfileClass *req_class)
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES,
NM_SYSTEM_CONFIG_INTERFACE_CAPABILITIES);
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME,
NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
}
static void
@ -812,15 +698,19 @@ nm_settings_keyfile_plugin_new (void)
{
static SCPluginKeyfile *singleton = NULL;
SCPluginKeyfilePrivate *priv;
char *value;
if (!singleton) {
singleton = SC_PLUGIN_KEYFILE (g_object_new (SC_TYPE_PLUGIN_KEYFILE, NULL));
priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (singleton);
priv->conf_file = nm_config_data_get_config_main_file (nm_config_get_data (nm_config_get ()));
/* plugin_set_hostname() has to be called *after* priv->conf_file is set */
priv->hostname = plugin_get_hostname (singleton);
value = nm_config_data_get_value (nm_config_get_data (nm_config_get ()),
"keyfile", "hostname", NULL);
if (value) {
nm_log_warn (LOGD_SETTINGS, "keyfile: 'hostname' option is deprecated and has no effect");
g_free (value);
}
} else
g_object_ref (singleton);