~ubuntu-branches/ubuntu/precise/network-manager/precise

« back to all changes in this revision

Viewing changes to src/nm-manager.c

  • Committer: Package Import Robot
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2012-02-09 16:45:41 UTC
  • mfrom: (1.1.53)
  • Revision ID: package-import@ubuntu.com-20120209164541-4h90zknlsfdb7x35
Tags: 0.9.2.0+git201202091925.c721477-0ubuntu1
* upstream snapshot 2012-02-09 19:25:59 (GMT)
  + c721477d11d4fe144111d6d2eec8f93f2e9186c9
* debian/patches/avoid-periodic-disk-wakeups.patch: refreshed.
* debian/patches/nl3-default-ip6-route.patch: refreshed.
* debian/libnm-glib4.symbols: add symbols:
  + nm_active_connection_get_master@Base
  + nm_client_new_async@Base
  + nm_client_new_finish@Base
  + nm_remote_settings_new_async@Base
  + nm_remote_settings_new_finish@Base
  + nm_device_get_state_reason@Base
* debian/libnm-util2.symbols: add symbols:
  + nm_setting_802_1x_get_pac_file@Base
  + nm_setting_infiniband_get_transport_mode@Base

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
17
 *
18
18
 * Copyright (C) 2007 - 2009 Novell, Inc.
19
 
 * Copyright (C) 2007 - 2011 Red Hat, Inc.
 
19
 * Copyright (C) 2007 - 2012 Red Hat, Inc.
20
20
 */
21
21
 
22
22
#include <config.h>
23
23
 
24
24
#include <netinet/ether.h>
25
 
#include <unistd.h>
26
25
#include <fcntl.h>
27
26
#include <errno.h>
28
27
#include <string.h>
 
28
#include <sys/stat.h>
29
29
#include <dbus/dbus-glib-lowlevel.h>
30
30
#include <dbus/dbus-glib.h>
31
31
#include <gio/gio.h>
43
43
#include "nm-device-wifi.h"
44
44
#include "nm-device-olpc-mesh.h"
45
45
#include "nm-device-modem.h"
 
46
#include "nm-device-infiniband.h"
46
47
#include "nm-system.h"
47
48
#include "nm-properties-changed-signal.h"
48
49
#include "nm-setting-bluetooth.h"
60
61
#include "nm-manager-auth.h"
61
62
#include "NetworkManagerUtils.h"
62
63
#include "nm-utils.h"
 
64
#include "nm-device-factory.h"
 
65
#include "wifi-utils.h"
63
66
 
64
67
#define NM_AUTOIP_DBUS_SERVICE "org.freedesktop.nm_avahi_autoipd"
65
68
#define NM_AUTOIP_DBUS_IFACE   "org.freedesktop.nm_avahi_autoipd"
113
116
 
114
117
#include "nm-manager-glue.h"
115
118
 
116
 
static void udev_device_added_cb (NMUdevManager *udev_mgr,
117
 
                                  GUdevDevice *device,
118
 
                                  NMDeviceCreatorFn creator_fn,
119
 
                                  gpointer user_data);
120
 
 
121
 
static void udev_device_removed_cb (NMUdevManager *udev_mgr,
122
 
                                    GUdevDevice *device,
123
 
                                    gpointer user_data);
124
 
 
125
119
static void bluez_manager_bdaddr_added_cb (NMBluezManager *bluez_mgr,
126
120
                                           const char *bdaddr,
127
121
                                           const char *name,
201
195
        NMUdevManager *udev_mgr;
202
196
        NMBluezManager *bluez_mgr;
203
197
 
 
198
        /* List of NMDeviceFactoryFunc pointers sorted in priority order */
 
199
        GSList *factories;
 
200
 
204
201
        NMSettings *settings;
205
202
        char *hostname;
206
203
 
1828
1825
        return NULL;
1829
1826
}
1830
1827
 
 
1828
#define PLUGIN_PREFIX "libnm-device-plugin-"
 
1829
 
 
1830
typedef struct {
 
1831
        NMDeviceType t;
 
1832
        guint priority;
 
1833
        NMDeviceFactoryCreateFunc create_func;
 
1834
} PluginInfo;
 
1835
 
 
1836
static gint
 
1837
plugin_sort (PluginInfo *a, PluginInfo *b)
 
1838
{
 
1839
        /* Higher priority means sort earlier in the list (ie, return -1) */
 
1840
        if (a->priority > b->priority)
 
1841
                return -1;
 
1842
        else if (a->priority < b->priority)
 
1843
                return 1;
 
1844
        return 0;
 
1845
}
 
1846
 
 
1847
static void
 
1848
load_device_factories (NMManager *self)
 
1849
{
 
1850
        NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
 
1851
        GDir *dir;
 
1852
        GError *error = NULL;
 
1853
        const char *item;
 
1854
        char *path;
 
1855
        GSList *list = NULL, *iter;
 
1856
 
 
1857
        dir = g_dir_open (NMPLUGINDIR, 0, &error);
 
1858
        if (!dir) {
 
1859
                nm_log_warn (LOGD_HW, "Failed to open plugin directory %s: %s",
 
1860
                             NMPLUGINDIR,
 
1861
                             (error && error->message) ? error->message : "(unknown)");
 
1862
                g_clear_error (&error);
 
1863
                return;
 
1864
        }
 
1865
 
 
1866
        while ((item = g_dir_read_name (dir))) {
 
1867
                GModule *plugin;
 
1868
                NMDeviceFactoryCreateFunc create_func;
 
1869
                NMDeviceFactoryPriorityFunc priority_func;
 
1870
                NMDeviceFactoryTypeFunc type_func;
 
1871
                PluginInfo *info = NULL;
 
1872
                NMDeviceType plugin_type;
 
1873
 
 
1874
                if (!g_str_has_prefix (item, PLUGIN_PREFIX))
 
1875
                        continue;
 
1876
 
 
1877
                path = g_module_build_path (NMPLUGINDIR, item);
 
1878
                g_assert (path);
 
1879
                plugin = g_module_open (path, G_MODULE_BIND_LOCAL);
 
1880
                g_free (path);
 
1881
 
 
1882
                if (!plugin) {
 
1883
                        nm_log_warn (LOGD_HW, "(%s): failed to load plugin: %s", item, g_module_error ());
 
1884
                        continue;
 
1885
                }
 
1886
 
 
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);
 
1890
                        continue;
 
1891
                }
 
1892
 
 
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;
 
1897
 
 
1898
                        if (plugin_type == candidate->t) {
 
1899
                                info = candidate;
 
1900
                                break;
 
1901
                        }
 
1902
                }
 
1903
                if (info) {
 
1904
                        g_module_close (plugin);
 
1905
                        continue;
 
1906
                }
 
1907
 
 
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);
 
1911
                        continue;
 
1912
                }
 
1913
 
 
1914
                info = g_malloc0 (sizeof (*info));
 
1915
                info->create_func = create_func;
 
1916
                info->t = plugin_type;
 
1917
 
 
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 ();
 
1921
                else {
 
1922
                        nm_log_dbg (LOGD_HW, "(%s): failed to find device factory priority func: %s",
 
1923
                                    item, g_module_error ());
 
1924
                }
 
1925
 
 
1926
                g_module_make_resident (plugin);
 
1927
                list = g_slist_insert_sorted (list, info, (GCompareFunc) plugin_sort);
 
1928
 
 
1929
                nm_log_info (LOGD_HW, "Loaded device factory: %s", g_module_name (plugin));
 
1930
        };
 
1931
        g_dir_close (dir);
 
1932
 
 
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;
 
1936
 
 
1937
                priv->factories = g_slist_append (priv->factories, info->create_func);
 
1938
                g_free (info);
 
1939
        }
 
1940
        g_slist_free (list);
 
1941
}
 
1942
 
 
1943
static gboolean
 
1944
is_wireless (GUdevDevice *device)
 
1945
{
 
1946
        char phy80211_path[255];
 
1947
        struct stat s;
 
1948
        const char *path;
 
1949
        const char *tmp;
 
1950
 
 
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)
 
1954
                return TRUE;
 
1955
 
 
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)))
 
1960
                return TRUE;
 
1961
 
 
1962
        /* Otherwise hit up WEXT directly */
 
1963
        return wifi_utils_is_wifi (g_udev_device_get_name (device));
 
1964
}
 
1965
 
 
1966
static gboolean
 
1967
is_olpc_mesh (GUdevDevice *device)
 
1968
{
 
1969
        const gchar *prop = g_udev_device_get_property (device, "ID_NM_OLPC_MESH");
 
1970
        return (prop != NULL);
 
1971
}
 
1972
 
 
1973
static gboolean
 
1974
is_infiniband (GUdevDevice *device)
 
1975
{
 
1976
        gint etype = g_udev_device_get_sysfs_attr_as_int (device, "type");
 
1977
        return etype == ARPHRD_INFINIBAND;
 
1978
}
 
1979
 
1831
1980
static void
1832
1981
udev_device_added_cb (NMUdevManager *udev_mgr,
1833
1982
                      GUdevDevice *udev_device,
1834
 
                      NMDeviceCreatorFn creator_fn,
 
1983
                      const char *iface,
 
1984
                      const char *sysfs_path,
 
1985
                      const char *driver,
 
1986
                      int ifindex,
1835
1987
                      gpointer user_data)
1836
1988
{
1837
1989
        NMManager *self = NM_MANAGER (user_data);
1838
 
        GObject *device;
1839
 
        guint32 ifindex;
 
1990
        NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
 
1991
        GObject *device = NULL;
 
1992
        GSList *iter;
 
1993
        GError *error = NULL;
1840
1994
 
1841
1995
        ifindex = g_udev_device_get_property_as_int (udev_device, "IFINDEX");
1842
1996
        if (find_device_by_ifindex (self, ifindex))
1843
1997
                return;
1844
1998
 
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;
 
2002
 
 
2003
                g_clear_error (&error);
 
2004
                device = create_func (udev_device, sysfs_path, iface, driver, &error);
 
2005
                if (device) {
 
2006
                        g_assert_no_error (error);
 
2007
                        break;  /* success! */
 
2008
                }
 
2009
 
 
2010
                if (error) {
 
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);
 
2014
                        return;
 
2015
                }
 
2016
        }
 
2017
 
 
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);
 
2025
                else
 
2026
                        device = (GObject *) nm_device_ethernet_new (sysfs_path, iface, driver);
 
2027
        }
 
2028
 
1846
2029
        if (device)
1847
2030
                add_device (self, NM_DEVICE (device));
1848
2031
}
3319
3502
                g_object_unref (priv->fw_monitor);
3320
3503
        }
3321
3504
 
 
3505
        g_slist_free (priv->factories);
 
3506
 
3322
3507
        if (priv->timestamp_update_id) {
3323
3508
                g_source_remove (priv->timestamp_update_id);
3324
3509
                priv->timestamp_update_id = 0;
3631
3816
                nm_log_warn (LOGD_CORE, "failed to monitor kernel firmware directory '%s'.",
3632
3817
                             KERNEL_FIRMWARE_DIR);
3633
3818
        }
 
3819
 
 
3820
        load_device_factories (manager);
3634
3821
}
3635
3822
 
3636
3823
static void