1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
/* NetworkManager system settings service
*
* Søren Sandmann <sandmann@daimi.au.dk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (C) Copyright 2007 - 2009 Red Hat, Inc.
* (C) Copyright 2007 - 2008 Novell, Inc.
*/
#include <config.h>
#include <string.h>
#include <gmodule.h>
#include <glib-object.h>
#include <gio/gio.h>
#include "plugin.h"
#include "nm-system-config-interface.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);
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;
GQuark
ifcfg_plugin_error_quark (void)
{
static GQuark error_quark = 0;
if (G_UNLIKELY (error_quark == 0))
error_quark = g_quark_from_static_string ("ifcfg-plugin-error-quark");
return error_quark;
}
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 ()
{
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 = hostname ? g_strdup (hostname) : NULL;
}
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
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)
{
switch (prop_id) {
case NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME:
g_value_set_string (value, IFCFG_PLUGIN_NAME);
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO:
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);
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);
break;
}
}
static void
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,
NM_SYSTEM_CONFIG_INTERFACE_NAME);
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO,
NM_SYSTEM_CONFIG_INTERFACE_INFO);
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
system_config_interface_init (NMSystemConfigInterface *system_config_interface_class)
{
/* interface implementation */
system_config_interface_class->init = init;
}
G_MODULE_EXPORT GObject *
nm_system_config_factory (void)
{
static SCPluginIfcfg *singleton = NULL;
if (!singleton)
singleton = SC_PLUGIN_IFCFG (g_object_new (SC_TYPE_PLUGIN_IFCFG, NULL));
else
g_object_ref (singleton);
return G_OBJECT (singleton);
}
|