1828
#define PLUGIN_PREFIX "libnm-device-plugin-"
1833
NMDeviceFactoryCreateFunc create_func;
1837
plugin_sort (PluginInfo *a, PluginInfo *b)
1839
/* Higher priority means sort earlier in the list (ie, return -1) */
1840
if (a->priority > b->priority)
1842
else if (a->priority < b->priority)
1848
load_device_factories (NMManager *self)
1850
NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1852
GError *error = NULL;
1855
GSList *list = NULL, *iter;
1857
dir = g_dir_open (NMPLUGINDIR, 0, &error);
1859
nm_log_warn (LOGD_HW, "Failed to open plugin directory %s: %s",
1861
(error && error->message) ? error->message : "(unknown)");
1862
g_clear_error (&error);
1866
while ((item = g_dir_read_name (dir))) {
1868
NMDeviceFactoryCreateFunc create_func;
1869
NMDeviceFactoryPriorityFunc priority_func;
1870
NMDeviceFactoryTypeFunc type_func;
1871
PluginInfo *info = NULL;
1872
NMDeviceType plugin_type;
1874
if (!g_str_has_prefix (item, PLUGIN_PREFIX))
1877
path = g_module_build_path (NMPLUGINDIR, item);
1879
plugin = g_module_open (path, G_MODULE_BIND_LOCAL);
1883
nm_log_warn (LOGD_HW, "(%s): failed to load plugin: %s", item, g_module_error ());
1887
if (!g_module_symbol (plugin, "nm_device_factory_get_type", (gpointer) (&type_func))) {
1888
nm_log_warn (LOGD_HW, "(%s): failed to find device factory: %s", item, g_module_error ());
1889
g_module_close (plugin);
1893
/* Make sure we don't double-load plugins */
1894
plugin_type = type_func ();
1895
for (iter = list; iter; iter = g_slist_next (iter)) {
1896
PluginInfo *candidate = iter->data;
1898
if (plugin_type == candidate->t) {
1904
g_module_close (plugin);
1908
if (!g_module_symbol (plugin, "nm_device_factory_create_device", (gpointer) (&create_func))) {
1909
nm_log_warn (LOGD_HW, "(%s): failed to find device creator: %s", item, g_module_error ());
1910
g_module_close (plugin);
1914
info = g_malloc0 (sizeof (*info));
1915
info->create_func = create_func;
1916
info->t = plugin_type;
1918
/* Grab priority; higher number equals higher priority */
1919
if (g_module_symbol (plugin, "nm_device_factory_get_priority", (gpointer) (&priority_func)))
1920
info->priority = priority_func ();
1922
nm_log_dbg (LOGD_HW, "(%s): failed to find device factory priority func: %s",
1923
item, g_module_error ());
1926
g_module_make_resident (plugin);
1927
list = g_slist_insert_sorted (list, info, (GCompareFunc) plugin_sort);
1929
nm_log_info (LOGD_HW, "Loaded device factory: %s", g_module_name (plugin));
1933
/* Ditch the priority info and copy the factory functions to our private data */
1934
for (iter = list; iter; iter = g_slist_next (iter)) {
1935
PluginInfo *info = iter->data;
1937
priv->factories = g_slist_append (priv->factories, info->create_func);
1940
g_slist_free (list);
1944
is_wireless (GUdevDevice *device)
1946
char phy80211_path[255];
1951
/* Check devtype, newer kernels (2.6.32+) have this */
1952
tmp = g_udev_device_get_property (device, "DEVTYPE");
1953
if (g_strcmp0 (tmp, "wlan") == 0)
1956
/* Check for nl80211 sysfs paths */
1957
path = g_udev_device_get_sysfs_path (device);
1958
snprintf (phy80211_path, sizeof (phy80211_path), "%s/phy80211", path);
1959
if ((stat (phy80211_path, &s) == 0 && (s.st_mode & S_IFDIR)))
1962
/* Otherwise hit up WEXT directly */
1963
return wifi_utils_is_wifi (g_udev_device_get_name (device));
1967
is_olpc_mesh (GUdevDevice *device)
1969
const gchar *prop = g_udev_device_get_property (device, "ID_NM_OLPC_MESH");
1970
return (prop != NULL);
1974
is_infiniband (GUdevDevice *device)
1976
gint etype = g_udev_device_get_sysfs_attr_as_int (device, "type");
1977
return etype == ARPHRD_INFINIBAND;
1832
1981
udev_device_added_cb (NMUdevManager *udev_mgr,
1833
1982
GUdevDevice *udev_device,
1834
NMDeviceCreatorFn creator_fn,
1984
const char *sysfs_path,
1835
1987
gpointer user_data)
1837
1989
NMManager *self = NM_MANAGER (user_data);
1990
NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1991
GObject *device = NULL;
1993
GError *error = NULL;
1841
1995
ifindex = g_udev_device_get_property_as_int (udev_device, "IFINDEX");
1842
1996
if (find_device_by_ifindex (self, ifindex))
1845
device = creator_fn (udev_mgr, udev_device, manager_sleeping (self));
1999
/* Try registered device factories */
2000
for (iter = priv->factories; iter; iter = g_slist_next (iter)) {
2001
NMDeviceFactoryCreateFunc create_func = iter->data;
2003
g_clear_error (&error);
2004
device = create_func (udev_device, sysfs_path, iface, driver, &error);
2006
g_assert_no_error (error);
2007
break; /* success! */
2011
nm_log_warn (LOGD_HW, "%s: factory failed to create device: (%d) %s",
2012
sysfs_path, error->code, error->message);
2013
g_clear_error (&error);
2018
if (device == NULL) {
2019
if (is_olpc_mesh (udev_device)) /* must be before is_wireless */
2020
device = (GObject *) nm_device_olpc_mesh_new (sysfs_path, iface, driver);
2021
else if (is_wireless (udev_device))
2022
device = (GObject *) nm_device_wifi_new (sysfs_path, iface, driver);
2023
else if (is_infiniband (udev_device))
2024
device = (GObject *) nm_device_infiniband_new (sysfs_path, iface, driver);
2026
device = (GObject *) nm_device_ethernet_new (sysfs_path, iface, driver);
1847
2030
add_device (self, NM_DEVICE (device));