1825
#define PLUGIN_PREFIX "libnm-device-plugin-"
1830
NMDeviceFactoryCreateFunc create_func;
1834
plugin_sort (PluginInfo *a, PluginInfo *b)
1836
/* Higher priority means sort earlier in the list (ie, return -1) */
1837
if (a->priority > b->priority)
1839
else if (a->priority < b->priority)
1845
load_device_factories (NMManager *self)
1847
NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1849
GError *error = NULL;
1852
GSList *list = NULL, *iter;
1854
dir = g_dir_open (NMPLUGINDIR, 0, &error);
1856
nm_log_warn (LOGD_HW, "Failed to open plugin directory %s: %s",
1858
(error && error->message) ? error->message : "(unknown)");
1859
g_clear_error (&error);
1863
while ((item = g_dir_read_name (dir))) {
1865
NMDeviceFactoryCreateFunc create_func;
1866
NMDeviceFactoryPriorityFunc priority_func;
1867
NMDeviceFactoryTypeFunc type_func;
1868
PluginInfo *info = NULL;
1869
NMDeviceType plugin_type;
1871
if (!g_str_has_prefix (item, PLUGIN_PREFIX))
1874
path = g_module_build_path (NMPLUGINDIR, item);
1876
plugin = g_module_open (path, G_MODULE_BIND_LOCAL);
1880
nm_log_warn (LOGD_HW, "(%s): failed to load plugin: %s", item, g_module_error ());
1884
if (!g_module_symbol (plugin, "nm_device_factory_get_type", (gpointer) (&type_func))) {
1885
nm_log_warn (LOGD_HW, "(%s): failed to find device factory: %s", item, g_module_error ());
1886
g_module_close (plugin);
1890
/* Make sure we don't double-load plugins */
1891
plugin_type = type_func ();
1892
for (iter = list; iter; iter = g_slist_next (iter)) {
1893
PluginInfo *candidate = iter->data;
1895
if (plugin_type == candidate->t) {
1901
g_module_close (plugin);
1905
if (!g_module_symbol (plugin, "nm_device_factory_create_device", (gpointer) (&create_func))) {
1906
nm_log_warn (LOGD_HW, "(%s): failed to find device creator: %s", item, g_module_error ());
1907
g_module_close (plugin);
1911
info = g_malloc0 (sizeof (*info));
1912
info->create_func = create_func;
1913
info->t = plugin_type;
1915
/* Grab priority; higher number equals higher priority */
1916
if (g_module_symbol (plugin, "nm_device_factory_get_priority", (gpointer) (&priority_func)))
1917
info->priority = priority_func ();
1919
nm_log_dbg (LOGD_HW, "(%s): failed to find device factory priority func: %s",
1920
item, g_module_error ());
1923
g_module_make_resident (plugin);
1924
list = g_slist_insert_sorted (list, info, (GCompareFunc) plugin_sort);
1926
nm_log_info (LOGD_HW, "Loaded device factory: %s", g_module_name (plugin));
1930
/* Ditch the priority info and copy the factory functions to our private data */
1931
for (iter = list; iter; iter = g_slist_next (iter)) {
1932
PluginInfo *info = iter->data;
1934
priv->factories = g_slist_append (priv->factories, info->create_func);
1937
g_slist_free (list);
1941
is_wireless (GUdevDevice *device)
1943
char phy80211_path[255];
1948
/* Check devtype, newer kernels (2.6.32+) have this */
1949
tmp = g_udev_device_get_property (device, "DEVTYPE");
1950
if (g_strcmp0 (tmp, "wlan") == 0)
1953
/* Check for nl80211 sysfs paths */
1954
path = g_udev_device_get_sysfs_path (device);
1955
snprintf (phy80211_path, sizeof (phy80211_path), "%s/phy80211", path);
1956
if ((stat (phy80211_path, &s) == 0 && (s.st_mode & S_IFDIR)))
1959
/* Otherwise hit up WEXT directly */
1960
return wifi_utils_is_wifi (g_udev_device_get_name (device));
1964
is_olpc_mesh (GUdevDevice *device)
1966
const gchar *prop = g_udev_device_get_property (device, "ID_NM_OLPC_MESH");
1967
return (prop != NULL);
1971
is_infiniband (GUdevDevice *device)
1973
gint etype = g_udev_device_get_sysfs_attr_as_int (device, "type");
1974
return etype == ARPHRD_INFINIBAND;
1829
1978
udev_device_added_cb (NMUdevManager *udev_mgr,
1830
1979
GUdevDevice *udev_device,
1831
NMDeviceCreatorFn creator_fn,
1981
const char *sysfs_path,
1832
1984
gpointer user_data)
1834
1986
NMManager *self = NM_MANAGER (user_data);
1987
NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
1988
GObject *device = NULL;
1990
GError *error = NULL;
1838
1992
ifindex = g_udev_device_get_property_as_int (udev_device, "IFINDEX");
1839
1993
if (find_device_by_ifindex (self, ifindex))
1842
device = creator_fn (udev_mgr, udev_device, manager_sleeping (self));
1996
/* Try registered device factories */
1997
for (iter = priv->factories; iter; iter = g_slist_next (iter)) {
1998
NMDeviceFactoryCreateFunc create_func = iter->data;
2000
g_clear_error (&error);
2001
device = create_func (udev_device, sysfs_path, iface, driver, &error);
2003
g_assert_no_error (error);
2004
break; /* success! */
2008
nm_log_warn (LOGD_HW, "%s: factory failed to create device: (%d) %s",
2009
sysfs_path, error->code, error->message);
2010
g_clear_error (&error);
2015
if (device == NULL) {
2016
if (is_olpc_mesh (udev_device)) /* must be before is_wireless */
2017
device = (GObject *) nm_device_olpc_mesh_new (sysfs_path, iface, driver);
2018
else if (is_wireless (udev_device))
2019
device = (GObject *) nm_device_wifi_new (sysfs_path, iface, driver);
2020
else if (is_infiniband (udev_device))
2021
device = (GObject *) nm_device_infiniband_new (sysfs_path, iface, driver);
2023
device = (GObject *) nm_device_ethernet_new (sysfs_path, iface, driver);
1844
2027
add_device (self, NM_DEVICE (device));