2008-10-30 Dan Williams <dcbw@redhat.com>
authorDan Williams <dcbw@redhat.com>
Thu, 30 Oct 2008 05:52:20 +0000 (05:52 +0000)
committerDan Williams <dcbw@redhat.com>
Thu, 30 Oct 2008 05:52:20 +0000 (05:52 +0000)
* src/nm-device-ethernet.c
  src/nm-device-wifi.c
  src/nm-device.c
  src/ppp-manager/nm-ppp-manager.c
- Harmonize return checking of ioctl

* system-settings/plugins/ifcfg-fedora/reader.c
- (is_wireless_device): fall back to SIOCGIWNAME (rh #466340)

git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@4236 4912f4e0-d625-0410-9fb7-b9a5a253dbdc

ChangeLog
src/nm-device-ethernet.c
src/nm-device-wifi.c
src/nm-device.c
src/ppp-manager/nm-ppp-manager.c
system-settings/plugins/ifcfg-fedora/reader.c

index f5948af..41a3056 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2008-10-30  Dan Williams  <dcbw@redhat.com>
+
+       * src/nm-device-ethernet.c
+         src/nm-device-wifi.c
+         src/nm-device.c
+         src/ppp-manager/nm-ppp-manager.c
+               - Harmonize return checking of ioctl
+
+       * system-settings/plugins/ifcfg-fedora/reader.c
+               - (is_wireless_device): fall back to SIOCGIWNAME (rh #466340)
+
 2008-10-30  Dan Williams  <dcbw@redhat.com>
 
        * src/ppp-manager/nm-ppp-manager.c
index 7f8fa9a..2cfd021 100644 (file)
@@ -451,7 +451,7 @@ nm_device_ethernet_get_speed (NMDeviceEthernet *self)
        strncpy (ifr.ifr_name, nm_device_get_iface (NM_DEVICE (self)), IFNAMSIZ);
        ifr.ifr_data = (char *) &edata;
 
-       if (ioctl (fd, SIOCETHTOOL, &ifr) == -1)
+       if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
                goto out;
 
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
@@ -474,7 +474,7 @@ real_update_hw_address (NMDevice *dev)
        NMDeviceEthernet *self = NM_DEVICE_ETHERNET (dev);
        NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self);
        struct ifreq req;
-       int ret, fd;
+       int fd;
 
        fd = socket (PF_INET, SOCK_DGRAM, 0);
        if (fd < 0) {
@@ -484,8 +484,7 @@ real_update_hw_address (NMDevice *dev)
 
        memset (&req, 0, sizeof (struct ifreq));
        strncpy (req.ifr_name, nm_device_get_iface (dev), IFNAMSIZ);
-       ret = ioctl (fd, SIOCGIFHWADDR, &req);
-       if (ret) {
+       if (ioctl (fd, SIOCGIFHWADDR, &req) < 0) {
                nm_warning ("%s: (%s) error getting hardware address: %d",
                            __func__, nm_device_get_iface (dev), errno);
                goto out;
@@ -1616,7 +1615,7 @@ supports_ethtool_carrier_detect (NMDeviceEthernet *self)
        edata.cmd = ETHTOOL_GLINK;
        ifr.ifr_data = (char *) &edata;
 
-       if (ioctl (fd, SIOCETHTOOL, &ifr) == -1)
+       if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
                goto out;
 
        supports_ethtool = TRUE;
@@ -1649,7 +1648,7 @@ mdio_read (NMDeviceEthernet *self, int fd, struct ifreq *ifr, int location)
        mii = (struct mii_ioctl_data *) &ifr->ifr_ifru;
        mii->reg_num = location;
 
-       if (ioctl (fd, SIOCGMIIREG, ifr) >= 0)
+       if (ioctl (fd, SIOCGMIIREG, ifr) == 0)
                val = mii->val_out;
 
        return val;
@@ -1658,7 +1657,7 @@ mdio_read (NMDeviceEthernet *self, int fd, struct ifreq *ifr, int location)
 static gboolean
 supports_mii_carrier_detect (NMDeviceEthernet *self)
 {
-       int err, fd, bmsr;
+       int fd, bmsr;
        struct ifreq ifr;
        gboolean supports_mii = FALSE;
 
@@ -1673,8 +1672,7 @@ supports_mii_carrier_detect (NMDeviceEthernet *self)
        memset (&ifr, 0, sizeof (struct ifreq));
        strncpy (ifr.ifr_name, nm_device_get_iface (NM_DEVICE (self)), IFNAMSIZ);
 
-       err = ioctl (fd, SIOCGMIIPHY, &ifr);
-       if (err < 0)
+       if (ioctl (fd, SIOCGMIIPHY, &ifr) < 0)
                goto out;
 
        /* If we can read the BMSR register, we assume that the card supports MII link detection */
index f0f2099..c6d51c5 100644 (file)
@@ -310,7 +310,7 @@ wireless_get_range (NMDeviceWifi *self,
                     struct iw_range *range,
                     guint32 *response_len)
 {
-       int fd, err, i = 26;
+       int fd, i = 26;
        gboolean success = FALSE;
        const char *iface;
        struct iwreq wrq;
@@ -336,8 +336,7 @@ wireless_get_range (NMDeviceWifi *self,
         * see rh bz#362421)
         */
        while (i-- > 0) {
-               err = ioctl (fd, SIOCGIWRANGE, &wrq);
-               if (err == 0) {
+               if (ioctl (fd, SIOCGIWRANGE, &wrq) == 0) {
                        if (response_len)
                                *response_len = wrq.u.data.length;
                        success = TRUE;
@@ -390,8 +389,7 @@ real_get_generic_capabilities (NMDevice *dev)
        strncpy (wrq.ifr_name, iface, IFNAMSIZ);
        err = ioctl (fd, SIOCSIWSCAN, &wrq);
        close (fd);
-
-       if ((err == -1) && (errno == EOPNOTSUPP))
+       if ((err < 0) && (errno == EOPNOTSUPP))
                caps = NM_DEVICE_CAP_NONE;
        else
                caps |= NM_DEVICE_CAP_NM_SUPPORTED;
@@ -1239,14 +1237,13 @@ nm_device_wifi_set_mode (NMDeviceWifi *self, const NM80211Mode mode)
        iface = nm_device_get_iface (NM_DEVICE (self));
        strncpy (wrq.ifr_name, iface, IFNAMSIZ);
 
-       if (ioctl (fd, SIOCSIWMODE, &wrq) == 0)
-               success = TRUE;
-       else {
+       if (ioctl (fd, SIOCSIWMODE, &wrq) < 0) {
                if (errno != ENODEV) {
                        nm_warning ("error setting card %s to mode %d: %s",
                                    iface, mode, strerror (errno));
                }
-       }
+       } else
+               success = TRUE;
        close (fd);
 
 out:
@@ -1263,7 +1260,7 @@ out:
 static guint32
 nm_device_wifi_get_frequency (NMDeviceWifi *self)
 {
-       int err, fd;
+       int fd;
        guint32 freq = 0;
        const char *iface;
        struct iwreq wrq;
@@ -1278,11 +1275,10 @@ nm_device_wifi_get_frequency (NMDeviceWifi *self)
        iface = nm_device_get_iface (NM_DEVICE (self));
        strncpy (wrq.ifr_name, iface, IFNAMSIZ);
 
-       err = ioctl (fd, SIOCGIWFREQ, &wrq);
-       if (err >= 0)
-               freq = iw_freq_to_uint32 (&wrq.u.freq);
-       else if (err == -1)
+       if (ioctl (fd, SIOCGIWFREQ, &wrq) < 0)
                nm_warning ("(%s): error getting frequency: %s", iface, strerror (errno));
+       else
+               freq = iw_freq_to_uint32 (&wrq.u.freq);
 
        close (fd);
        return freq;
@@ -1555,7 +1551,7 @@ nm_device_wifi_get_bitrate (NMDeviceWifi *self)
        err = ioctl (fd, SIOCGIWRATE, &wrq);
        close (fd);
 
-       return ((err >= 0) ? wrq.u.bitrate.value / 1000 : 0);
+       return ((err == 0) ? wrq.u.bitrate.value / 1000 : 0);
 }
 
 /*
@@ -1584,7 +1580,7 @@ nm_device_wifi_get_bssid (NMDeviceWifi *self,
 
        memset (&wrq, 0, sizeof (wrq));
        strncpy (wrq.ifr_name, nm_device_get_iface (NM_DEVICE (self)), IFNAMSIZ);
-       if (ioctl (fd, SIOCGIWAP, &wrq) >= 0)
+       if (ioctl (fd, SIOCGIWAP, &wrq) == 0)
                memcpy (bssid->ether_addr_octet, &(wrq.u.ap_addr.sa_data), ETH_ALEN);
 
        close (fd);
@@ -1619,7 +1615,7 @@ nm_device_wifi_disable_encryption (NMDeviceWifi *self)
        iface = nm_device_get_iface (NM_DEVICE (self));
        strncpy (wrq.ifr_name, iface, IFNAMSIZ);
 
-       if (ioctl (fd, SIOCSIWENCODE, &wrq) == -1) {
+       if (ioctl (fd, SIOCSIWENCODE, &wrq) < 0) {
                if (errno != ENODEV) {
                        nm_warning ("error setting key for device %s: %s",
                                    iface, strerror (errno));
@@ -2714,7 +2710,7 @@ real_update_hw_address (NMDevice *dev)
        NMDeviceWifi *self = NM_DEVICE_WIFI (dev);
        NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self);
        struct ifreq req;
-       int ret, fd;
+       int fd;
 
        fd = socket (PF_INET, SOCK_DGRAM, 0);
        if (fd < 0) {
@@ -2724,8 +2720,7 @@ real_update_hw_address (NMDevice *dev)
 
        memset (&req, 0, sizeof (struct ifreq));
        strncpy (req.ifr_name, nm_device_get_iface (dev), IFNAMSIZ);
-       ret = ioctl (fd, SIOCGIFHWADDR, &req);
-       if (ret) {
+       if (ioctl (fd, SIOCGIFHWADDR, &req) < 0) {
                nm_warning ("%s: (%s) error getting hardware address: %d",
                            __func__, nm_device_get_iface (dev), errno);
                goto out;
index 7fa140b..9794ab7 100644 (file)
@@ -1970,7 +1970,7 @@ nm_device_update_ip4_address (NMDevice *self)
 {
        struct ifreq req;
        guint32 new_address;
-       int fd, err;
+       int fd;
        
        g_return_if_fail (self  != NULL);
 
@@ -1982,15 +1982,12 @@ nm_device_update_ip4_address (NMDevice *self)
 
        memset (&req, 0, sizeof (struct ifreq));
        strncpy (req.ifr_name, nm_device_get_ip_iface (self), IFNAMSIZ);
-       err = ioctl (fd, SIOCGIFADDR, &req);
+       if (ioctl (fd, SIOCGIFADDR, &req) == 0) {
+               new_address = ((struct sockaddr_in *)(&req.ifr_addr))->sin_addr.s_addr;
+               if (new_address != nm_device_get_ip4_address (self))
+                       self->priv->ip4_address = new_address;
+       }
        close (fd);
-
-       if (err != 0)
-               return;
-
-       new_address = ((struct sockaddr_in *)(&req.ifr_addr))->sin_addr.s_addr;
-       if (new_address != nm_device_get_ip4_address (self))
-               self->priv->ip4_address = new_address;
 }
 
 static gboolean
index 90b8b63..3962f6f 100644 (file)
@@ -306,7 +306,7 @@ monitor_cb (gpointer user_data)
        req.stats_ptr = (caddr_t) &req.stats;
 
        strncpy (req.ifr__name, priv->ip_iface, sizeof (req.ifr__name));
-       if (!ioctl (priv->monitor_fd, SIOCGPPPSTATS, &req) < 0)
+       if (ioctl (priv->monitor_fd, SIOCGPPPSTATS, &req) < 0)
                nm_warning ("Could not read ppp stats: %s", strerror (errno));
        else
                g_signal_emit (manager, signals[STATS], 0, 
index 5e1f52f..b44cd1a 100644 (file)
@@ -880,17 +880,14 @@ wired_connection_from_ifcfg (const char *file,
 }
 
 static gboolean
-is_wireless_device (const char *iface, gboolean *is_wireless)
+is_wireless_device (const char *iface)
 {
        int fd;
        struct iw_range range;
        struct iwreq wrq;
-       gboolean success = FALSE;
+       gboolean is_wireless = FALSE;
 
        g_return_val_if_fail (iface != NULL, FALSE);
-       g_return_val_if_fail (is_wireless != NULL, FALSE);
-
-       *is_wireless = FALSE;
 
        fd = socket(AF_INET, SOCK_DGRAM, 0);
        if (!fd)
@@ -902,18 +899,25 @@ is_wireless_device (const char *iface, gboolean *is_wireless)
        wrq.u.data.pointer = (caddr_t) &range;
        wrq.u.data.length = sizeof (struct iw_range);
 
-       if (ioctl (fd, SIOCGIWRANGE, &wrq) < 0) {
+       if (ioctl (fd, SIOCGIWRANGE, &wrq) == 0)
+               is_wireless = TRUE;
+       else {
                if (errno == EOPNOTSUPP)
-                       success = TRUE;
-               goto out;
+                       is_wireless = FALSE;
+               else {
+                       /* Sigh... some wired devices (kvm/qemu) return EINVAL when the
+                        * device is down even though it's not a wireless device.  So try
+                        * IWNAME as a fallback.
+                        */
+                       memset (&wrq, 0, sizeof (struct iwreq));
+                       strncpy (wrq.ifr_name, iface, IFNAMSIZ);
+                       if (ioctl (fd, SIOCGIWNAME, &wrq) == 0)
+                               is_wireless = TRUE;
+               }
        }
 
-       *is_wireless = TRUE;
-       success = TRUE;
-
-out:
        close (fd);
-       return success;
+       return is_wireless;
 }
 
 NMConnection *
@@ -953,7 +957,6 @@ connection_from_file (const char *filename,
        type = svGetValue (parsed, "TYPE");
        if (!type) {
                char *device;
-               gboolean is_wireless = FALSE;
 
                /* If no type, if the device has wireless extensions, it's wifi,
                 * otherwise it's ethernet.
@@ -973,15 +976,7 @@ connection_from_file (const char *filename,
                }
 
                /* Test wireless extensions */
-               if (!is_wireless_device (device, &is_wireless)) {
-                       g_set_error (error, ifcfg_plugin_error_quark (), 0,
-                                    "File '%s' specified device '%s', but the device's "
-                                    "type could not be determined.", filename, device);
-                       g_free (device);
-                       goto done;
-               }
-
-               if (is_wireless)
+               if (is_wireless_device (device))
                        type = g_strdup (TYPE_WIRELESS);
                else
                        type = g_strdup (TYPE_ETHERNET);