linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [udev] allow multiple symlinks
@ 2003-12-09  4:49 Kay Sievers
  0 siblings, 0 replies; 2+ messages in thread
From: Kay Sievers @ 2003-12-09  4:49 UTC (permalink / raw)
  To: linux-hotplug

[-- Attachment #1: Type: text/plain, Size: 852 bytes --]

Here is a patch to allow the creation of multiple symlinks.
The names must be separated by a space character.


REPLACE, KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"

results in:

Dec  9 05:28:51 pim udev[12019]: create_node: mknod(udev-root/visor, 020666, 188, 0)
Dec  9 05:28:51 pim udev[12019]: create_node: symlink 'udev-root/first-0' to node 'visor' requested
Dec  9 05:28:51 pim udev[12019]: create_node: symlink(./visor, udev-root/first-0)
Dec  9 05:28:51 pim udev[12019]: create_node: symlink 'udev-root/second-0' to node 'visor' requested
Dec  9 05:28:51 pim udev[12019]: create_node: symlink(./visor, udev-root/second-0)
Dec  9 05:28:51 pim udev[12019]: create_node: symlink 'udev-root/third-0' to node 'visor' requested
Dec  9 05:28:51 pim udev[12019]: create_node: symlink(./visor, udev-root/third-0)


thanks,
Kay

[-- Attachment #2: 02-allow-multiple-symlinks.diff --]
[-- Type: text/plain, Size: 4056 bytes --]

diff -Nru a/test/udev-test.pl b/test/udev-test.pl
--- a/test/udev-test.pl	Tue Dec  9 05:30:28 2003
+++ b/test/udev-test.pl	Tue Dec  9 05:30:28 2003
@@ -217,6 +217,15 @@
 LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="1/2/a/b/node", SYMLINK="1/2/c/d/symlink"
 EOF
 	},
+	{
+		desc     => "multiple symlinks",
+		subsys   => "tty",
+		devpath  => "class/tty/ttyUSB0",
+		expected => "second-0" ,
+		conf     => <<EOF
+REPLACE, KERNEL="ttyUSB0", NAME="visor", SYMLINK="first-%n second-%n third-%n"
+EOF
+	},
 );
 
 # set env
diff -Nru a/udev-add.c b/udev-add.c
--- a/udev-add.c	Tue Dec  9 05:30:28 2003
+++ b/udev-add.c	Tue Dec  9 05:30:28 2003
@@ -102,6 +102,8 @@
 {
 	char filename[255];
 	char linktarget[255];
+	char *linkname;
+	char *symlinks;
 	int retval = 0;
 	uid_t uid = 0;
 	gid_t gid = 0;
@@ -186,39 +188,45 @@
 			    filename, uid, gid, strerror(errno));
 	}
 
-
 	/* create symlink if requested */
 	if (*dev->symlink) {
-		strncpy(filename, udev_root, sizeof(filename));
-		strncat(filename, dev->symlink, sizeof(filename));
-		dbg("symlink '%s' to node '%s' requested", filename, dev->name);
-		if (strrchr(dev->symlink, '/'))
-			create_path(filename);
-
-		/* optimize relative link */
-		linktarget[0] = '\0';
-		i = 0;
-		tail = 0;
-		while ((dev->name[i] == dev->symlink[i]) && dev->name[i]) {
-			if (dev->name[i] == '/')
-				tail = i+1;
-			i++;
-		}
-		while (dev->symlink[i]) {
-			if (dev->symlink[i] == '/')
-				strcat(linktarget, "../");
-			i++;
+		symlinks = dev->symlink;
+		while (1) {
+			linkname = strsep(&symlinks, " ");
+			if (linkname == NULL)
+				break;
+
+			strncpy(filename, udev_root, sizeof(filename));
+			strncat(filename, linkname, sizeof(filename));
+			dbg("symlink '%s' to node '%s' requested", filename, dev->name);
+			if (strrchr(linkname, '/'))
+				create_path(filename);
+
+			/* optimize relative link */
+			linktarget[0] = '\0';
+			i = 0;
+			tail = 0;
+			while ((dev->name[i] == linkname[i]) && dev->name[i]) {
+				if (dev->name[i] == '/')
+					tail = i+1;
+				i++;
+			}
+			while (linkname[i]) {
+				if (linkname[i] == '/')
+					strcat(linktarget, "../");
+				i++;
+			}
+
+			if (*linktarget == '\0')
+				strcpy(linktarget, "./");
+			strcat(linktarget, &dev->name[tail]);
+
+			dbg("symlink(%s, %s)", linktarget, filename);
+			retval = symlink(linktarget, filename);
+			if (retval)
+				dbg("symlink(%s, %s) failed with error '%s'",
+				    linktarget, filename, strerror(errno));
 		}
-
-		if (*linktarget == '\0')
-			strcpy(linktarget, "./");
-		strcat(linktarget, &dev->name[tail]);
-
-		dbg("symlink(%s, %s)", linktarget, filename);
-		retval = symlink(linktarget, filename);
-		if (retval)
-			dbg("symlink(%s, %s) failed with error '%s'",
-			    linktarget, filename, strerror(errno));
 	}
 
 	return retval;
diff -Nru a/udev-remove.c b/udev-remove.c
--- a/udev-remove.c	Tue Dec  9 05:30:28 2003
+++ b/udev-remove.c	Tue Dec  9 05:30:28 2003
@@ -66,6 +66,8 @@
 static int delete_node(struct udevice *dev)
 {
 	char filename[255];
+	char *symlinks;
+	char *linkname;
 	int retval;
 
 	strncpy(filename, udev_root, sizeof(filename));
@@ -84,17 +86,25 @@
 		delete_path(filename);
 
 	if (*dev->symlink) {
-		strncpy(filename, udev_root, sizeof(filename));
-		strncat(filename, dev->symlink, sizeof(filename));
-		dbg("unlinking symlink '%s'", filename);
-		retval = unlink(filename);
-		if (retval) {
-			dbg("unlink(%s) failed with error '%s'",
-				filename, strerror(errno));
-			return retval;
-		}
-		if (strchr(dev->symlink, '/')) {
-			delete_path(filename);
+		symlinks = dev->symlink;
+		while (1) {
+			linkname = strsep(&symlinks, " ");
+			if (linkname == NULL)
+				break;
+
+			strncpy(filename, udev_root, sizeof(filename));
+			strncat(filename, linkname, sizeof(filename));
+
+			dbg("unlinking symlink '%s'", filename);
+			retval = unlink(filename);
+			if (retval) {
+				dbg("unlink(%s) failed with error '%s'",
+					filename, strerror(errno));
+				return retval;
+			}
+			if (strchr(dev->symlink, '/')) {
+				delete_path(filename);
+			}
 		}
 	}
 

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [udev] allow multiple symlinks
@ 2003-12-10  0:47 Greg KH
  0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2003-12-10  0:47 UTC (permalink / raw)
  To: linux-hotplug

On Tue, Dec 09, 2003 at 05:49:43AM +0100, Kay Sievers wrote:
> Here is a patch to allow the creation of multiple symlinks.
> The names must be separated by a space character.

Nice, that wasn't that much extra code at all to add this support.  I've
applied this patch, thanks.

greg k-h


-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills.  Sign up for IBM's
Free Linux Tutorials.  Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id\x1278&alloc_id371&op=click
_______________________________________________
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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-12-10  0:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-12-10  0:47 [udev] allow multiple symlinks Greg KH
  -- strict thread matches above, loose matches on Subject: below --
2003-12-09  4:49 Kay Sievers

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).