linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Scott James Remnant <scott@ubuntu.com>
To: linux-hotplug@vger.kernel.org
Subject: Re: swapping interface names (again)
Date: Mon, 26 Jun 2006 07:46:52 +0000	[thread overview]
Message-ID: <1151308012.15881.4.camel@quest.netsplit.com> (raw)
In-Reply-To: <449C9C3D.30601@bham.ac.uk>


[-- Attachment #1.1.1: Type: text/plain, Size: 701 bytes --]

On Sun, 2006-06-25 at 10:35 +0200, Marco d'Itri wrote:

> On Jun 25, "Alexander E. Patrakov" <patrakov@ums.usu.ru> wrote:
> 
> > Don't look at the Debian package. It is very likely to contain this problem, 
> > because for my single-card setup, Debian udev 0.093-1 writes:
> It's not, because there is an Ubuntu-derived patch which prevents this.
> I have been shipping it since february with no issues, so maybe it's
> time to consider merging it.
> 
That patch is normally sent out with my occasional "Ubuntu Collection"
mails (one is due soon as we're getting back up to date with upstream
post-release), here it is applied to 093.

Scott
-- 
Scott James Remnant
scott@ubuntu.com

[-- Attachment #1.1.2: Type: text/x-patch, Size: 3800 bytes --]

diff -ruNp udev-093~/udev_device.c udev-093/udev_device.c
--- udev-093~/udev_device.c	2006-05-29 10:18:16.000000000 +0100
+++ udev-093/udev_device.c	2006-06-07 14:08:43.000000000 +0100
@@ -30,6 +30,8 @@
 #include <sys/socket.h>
 #include <net/if.h>
 #include <linux/sockios.h>
+#include <errno.h>
+#include <ctype.h>
 
 #include "udev.h"
 #include "udev_rules.h"
@@ -102,10 +104,97 @@ static int rename_net_if(struct udevice 
 	strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ);
 
 	retval = ioctl(sk, SIOCSIFNAME, &ifr);
-	if (retval != 0)
-		err("error changing net interface name: %s", strerror(errno));
+	if (retval != 0) {
+		if (errno != EEXIST) {
+			err("error changing net interface name: %s", strerror(errno));
+			goto error;
+		}
+
+		/* Destination interface already exits.
+		 * First rename our interface to something temporary in case
+		 * we're trying to swap with that one. */
+		strlcpy(ifr.ifr_newname, udev->dev->kernel_name, IFNAMSIZ);
+		strlcat(ifr.ifr_newname, "_ifrename", IFNAMSIZ);
+
+		retval = ioctl(sk, SIOCSIFNAME, &ifr);
+		if (retval != 0) {
+			err("error changing net interface name: %s", strerror(errno));
+			goto error;
+		}
+
+		/* Now we loop until our target interface goes away. */
+		strlcpy(ifr.ifr_name, ifr.ifr_newname, IFNAMSIZ);
+		strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ);
+		while ((retval = ioctl(sk, SIOCSIFNAME, &ifr)) != 0) {
+			if (errno != EEXIST) {
+				err("error changing net interface name: %s", strerror(errno));
+				break;
+			}
+
+			usleep(50000);
+		}
+	}
+
+error:
 	close(sk);
+	return retval;
+}
+
+static int rename_net_if_increment(struct udevice *udev)
+{
+	struct ifreq ifr;
+	char *ptr, *end;
+	int dev = 0, sk, retval;
+
+	info("changing net interface name from '%s' to first matching '%s'", udev->dev->kernel_name, udev->name);
+	if (udev->test_run)
+		return 0;
+
+
+	/* Split the name up */
+	ptr = udev->name;
+	while (*ptr && *ptr != '*')
+		ptr++;
+
+	end = ptr + 1;
+	if (isdigit(*end))
+		dev = atoi(end);
 
+	while (isdigit(*end))
+		end++;
+
+
+	sk = socket(PF_INET, SOCK_DGRAM, 0);
+	if (sk < 0) {
+		err("error opening socket: %s", strerror(errno));
+		return -1;
+	}
+
+	memset(&ifr, 0x00, sizeof(struct ifreq));
+	strlcpy(ifr.ifr_name, udev->dev->kernel_name, IFNAMSIZ);
+
+	do {
+		strncpy(ifr.ifr_newname, udev->name, ptr - udev->name);
+		sprintf(ifr.ifr_newname + (ptr - udev->name), "%d%s",
+			dev, end);
+
+		retval = ioctl(sk, SIOCSIFNAME, &ifr);
+		if (retval == 0) {
+			strlcpy(udev->name, ifr.ifr_newname, sizeof(udev->name));
+			break;
+		}
+
+		if (errno != EEXIST) {
+			err("error changing net interface name: %s", strerror(errno));
+			goto error;
+		}
+
+		/* Try the next one */
+		dev++;
+	} while (1);
+
+error:
+	close(sk);
 	return retval;
 }
 
@@ -159,7 +248,11 @@ int udev_device_event(struct udev_rules 
 		if (strcmp(udev->name, udev->dev->kernel_name) != 0) {
 			char *pos;
 
-			retval = rename_net_if(udev);
+			if (strchr(udev->name, '*')) {
+				retval = rename_net_if_increment(udev);
+			} else {
+				retval = rename_net_if(udev);
+			}
 			if (retval != 0)
 				goto exit;
 			info("renamed netif to '%s'", udev->name);
diff -ruNp udev-093~/udev_utils_string.c udev-093/udev_utils_string.c
--- udev-093~/udev_utils_string.c	2006-06-07 14:01:18.000000000 +0100
+++ udev-093/udev_utils_string.c	2006-06-07 14:06:24.000000000 +0100
@@ -241,7 +241,7 @@ int replace_untrusted_chars(char *str)
 		if ((str[i] >= '0' && str[i] <= '9') ||
 		    (str[i] >= 'A' && str[i] <= 'Z') ||
 		    (str[i] >= 'a' && str[i] <= 'z') ||
-		    strchr("#$%+-./:=?@_,", str[i])) {
+		    strchr("#$%+-./:=?*@_,", str[i])) {
 			i++;
 			continue;
 		}

[-- Attachment #1.2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 191 bytes --]

[-- Attachment #2: Type: text/plain, Size: 300 bytes --]

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

[-- Attachment #3: Type: text/plain, Size: 226 bytes --]

_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

      parent reply	other threads:[~2006-06-26  7:46 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-24  1:58 swapping interface names (again) MS Colclough
2006-06-24  7:59 ` Marco d'Itri
2006-06-24 20:02 ` juuso.alasuutari
2006-06-25  3:07 ` Alexander E. Patrakov
2006-06-25  8:35 ` Marco d'Itri
2006-06-25 12:18 ` juuso.alasuutari
2006-06-25 12:21 ` Marco d'Itri
2006-06-25 14:39 ` MS Colclough
2006-06-26  7:46 ` Scott James Remnant [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1151308012.15881.4.camel@quest.netsplit.com \
    --to=scott@ubuntu.com \
    --cc=linux-hotplug@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).