summaryrefslogtreecommitdiff
path: root/src/devices/atm/nm-atm-manager.c
blob: 886095dad3d9d121f3f544f29c545ad4329d9a89 (plain)
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
 *
 * 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.
 *
 * Copyright (C) 2009 - 2013 Red Hat, Inc.
 */

#include <config.h>

#include <string.h>
#include <gudev/gudev.h>

#include "nm-atm-manager.h"
#include "nm-device-adsl.h"
#include "nm-device-factory.h"
#include "nm-logging.h"

typedef struct {
	GUdevClient *client;
	GSList *devices;
	guint start_id;
} NMAtmManagerPrivate;

#define NM_ATM_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_ATM_MANAGER, NMAtmManagerPrivate))

static GType nm_atm_manager_get_type (void);

static void device_factory_interface_init (NMDeviceFactory *factory_iface);

G_DEFINE_TYPE_EXTENDED (NMAtmManager, nm_atm_manager, G_TYPE_OBJECT, 0,
                        G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_FACTORY, device_factory_interface_init))

enum {
	PROP_0,
	PROP_DEVICE_TYPE,
	LAST_PROP
};

/**************************************************************************/

#define PLUGIN_TYPE NM_DEVICE_TYPE_ADSL

G_MODULE_EXPORT NMDeviceFactory *
nm_device_factory_create (GError **error)
{
	return (NMDeviceFactory *) g_object_new (NM_TYPE_ATM_MANAGER, NULL);
}

G_MODULE_EXPORT NMDeviceType
nm_device_factory_get_device_type (void)
{
	return PLUGIN_TYPE;
}

/************************************************************************/

static gboolean
dev_get_attrs (GUdevDevice *udev_device,
               const char **out_path,
               char **out_driver)
{
	GUdevDevice *parent = NULL;
	const char *driver, *path;

	g_return_val_if_fail (udev_device != NULL, FALSE);
	g_return_val_if_fail (out_path != NULL, FALSE);
	g_return_val_if_fail (out_driver != NULL, FALSE);

	path = g_udev_device_get_sysfs_path (udev_device);
	if (!path) {
		nm_log_warn (LOGD_HW, "couldn't determine device path; ignoring...");
		return FALSE;
	}

	driver = g_udev_device_get_driver (udev_device);
	if (!driver) {
		/* Try the parent */
		parent = g_udev_device_get_parent (udev_device);
		if (parent)
			driver = g_udev_device_get_driver (parent);
	}

	*out_path = path;
	*out_driver = g_strdup (driver);

	g_clear_object (&parent);
	return TRUE;
}

static void
device_destroyed (gpointer user_data, GObject *dead)
{
	NMAtmManager *self = NM_ATM_MANAGER (user_data);
	NMAtmManagerPrivate *priv = NM_ATM_MANAGER_GET_PRIVATE (self);

	priv->devices = g_slist_remove (priv->devices, dead);
}

static void
adsl_add (NMAtmManager *self, GUdevDevice *udev_device)
{
	NMAtmManagerPrivate *priv = NM_ATM_MANAGER_GET_PRIVATE (self);
	const char *ifname, *sysfs_path = NULL;
	char *driver = NULL;
	NMDevice *device;

	g_return_if_fail (udev_device != NULL);

	ifname = g_udev_device_get_name (udev_device);
	if (!ifname) {
		nm_log_warn (LOGD_HW, "failed to get device's interface name");
		return;
	}

	nm_log_dbg (LOGD_HW, "(%s): found ATM device", ifname);

	if (dev_get_attrs (udev_device, &sysfs_path, &driver)) {
		g_assert (sysfs_path);

		device = nm_device_adsl_new (sysfs_path, ifname, driver);
		g_assert (device);

		priv->devices = g_slist_prepend (priv->devices, device);
		g_object_weak_ref (G_OBJECT (device), device_destroyed, self);

		g_signal_emit_by_name (self, NM_DEVICE_FACTORY_DEVICE_ADDED, device);
		g_object_unref (device);

		g_free (driver);
	}
}

static void
adsl_remove (NMAtmManager *self, GUdevDevice *udev_device)
{
	NMAtmManagerPrivate *priv = NM_ATM_MANAGER_GET_PRIVATE (self);
	const char *iface = g_udev_device_get_name (udev_device);
	GSList *iter;

	nm_log_dbg (LOGD_HW, "(%s): removing ATM device", iface);

	for (iter = priv->devices; iter; iter = iter->next) {
		NMDevice *device = iter->data;

		/* Match 'iface' not 'ip_iface' to the ATM device instead of the
		 * NAS bridge interface or PPPoE interface.
		 */
		if (g_strcmp0 (nm_device_get_iface (device), iface) != 0)
			continue;

		g_object_weak_unref (G_OBJECT (iter->data), device_destroyed, self);
		priv->devices = g_slist_remove (priv->devices, device);
		g_signal_emit_by_name (device, NM_DEVICE_REMOVED);
		break;
	}
}

static gboolean
query_devices (NMAtmManager *self)
{
	NMAtmManagerPrivate *priv = NM_ATM_MANAGER_GET_PRIVATE (self);
	GUdevEnumerator *enumerator;
	GList *devices, *iter;

	enumerator = g_udev_enumerator_new (priv->client);
	g_udev_enumerator_add_match_subsystem (enumerator, "atm");
	g_udev_enumerator_add_match_is_initialized (enumerator);
	devices = g_udev_enumerator_execute (enumerator);
	for (iter = devices; iter; iter = g_list_next (iter)) {
		adsl_add (self, G_UDEV_DEVICE (iter->data));
		g_object_unref (G_UDEV_DEVICE (iter->data));
	}
	g_list_free (devices);
	g_object_unref (enumerator);

	return G_SOURCE_REMOVE;
}

static void
handle_uevent (GUdevClient *client,
               const char *action,
               GUdevDevice *device,
               gpointer user_data)
{
	NMAtmManager *self = NM_ATM_MANAGER (user_data);
	const char *subsys;
	const char *ifindex;
	guint64 seqnum;

	g_return_if_fail (action != NULL);

	/* A bit paranoid */
	subsys = g_udev_device_get_subsystem (device);
	g_return_if_fail (!g_strcmp0 (subsys, "atm"));

	ifindex = g_udev_device_get_property (device, "IFINDEX");
	seqnum = g_udev_device_get_seqnum (device);
	nm_log_dbg (LOGD_HW, "UDEV event: action '%s' subsys '%s' device '%s' (%s); seqnum=%" G_GUINT64_FORMAT,
	            action, subsys, g_udev_device_get_name (device), ifindex ? ifindex : "unknown", seqnum);

	if (!strcmp (action, "add"))
		adsl_add (self, device);
	else if (!strcmp (action, "remove"))
		adsl_remove (self, device);
}

/*********************************************************************/

static void
nm_atm_manager_init (NMAtmManager *self)
{
	NMAtmManagerPrivate *priv = NM_ATM_MANAGER_GET_PRIVATE (self);
	const char *subsys[] = { "atm", NULL };

	priv->client = g_udev_client_new (subsys);
	g_signal_connect (priv->client, "uevent", G_CALLBACK (handle_uevent), self);

	priv->start_id = g_idle_add ((GSourceFunc) query_devices, self);
}

static void
device_factory_interface_init (NMDeviceFactory *factory_iface)
{
}

static void
get_property (GObject *object, guint prop_id,
              GValue *value, GParamSpec *pspec)
{
	switch (prop_id) {
	case PROP_DEVICE_TYPE:
		g_value_set_uint (value, PLUGIN_TYPE);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
dispose (GObject *object)
{
	NMAtmManager *self = NM_ATM_MANAGER (object);
	NMAtmManagerPrivate *priv = NM_ATM_MANAGER_GET_PRIVATE (self);
	GSList *iter;

	if (priv->client)
		g_signal_handlers_disconnect_by_func (priv->client, handle_uevent, self);
	g_clear_object (&priv->client);

	if (priv->start_id) {
		g_source_remove (priv->start_id);
		priv->start_id = 0;
	}

	for (iter = priv->devices; iter; iter = iter->next)
		g_object_weak_unref (G_OBJECT (iter->data), device_destroyed, self);
	g_clear_pointer (&priv->devices, g_slist_free);

	G_OBJECT_CLASS (nm_atm_manager_parent_class)->dispose (object);
}

static void
nm_atm_manager_class_init (NMAtmManagerClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (klass, sizeof (NMAtmManagerPrivate));

	/* virtual methods */
	object_class->dispose = dispose;
	object_class->get_property = get_property;

	g_object_class_override_property (object_class,
	                                  PROP_DEVICE_TYPE,
	                                  NM_DEVICE_FACTORY_DEVICE_TYPE);
}