Skip to contentSkip to conversation

udev - reverse user query options

PATCH Applied Kay Sievers 3 participants 6 messages Jan 16, 2004 – Jan 19, 2004

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 udev - reverse user query options

Here we get the ability to query with the name of the node instead of the device path. It uses a linear search over the whole database.

  kay@pim:~/src/udev.kay$ ./udev -q path -n video/webcam0
  /class/video4linux/video0

thanks,
Kay

udev.c +66 -32
diff -Nru a/udev.c b/udev.c
@@ -83,7 +83,8 @@
 	return seqnum;
 }
 
-static void print_record(char *path, struct udevice *dev)
+/* callback for database dump */
+static int print_record(char *path, struct udevice *dev)
 {
 	printf("P: %s\n", path);
 	printf("N: %s\n", dev->name);
@@ -91,11 +92,13 @@
 	printf("O: %s\n", dev->owner);
 	printf("G: %s\n", dev->group);
 	printf("\n");
+	return 0;
 }
 
 enum query_type {
 	NONE,
 	NAME,
+	PATH,
 	SYMLINK,
 	OWNER,
 	GROUP
@@ -103,7 +106,7 @@
 
 static inline int udev_user(int argc, char **argv)
 {
-	static const char short_options[] = "dp:q:rVh";
+	static const char short_options[] = "dn:p:q:rVh";
 	int option;
 	int retval = -EINVAL;
 	struct udevice dev;
@@ -120,6 +123,11 @@
 
 		dbg("option '%c'", option);
 		switch (option) {
+		case 'n':
+			dbg("udev name: %s\n", optarg);
+			strfieldcpy(dev.name, optarg);
+			break;
+
 		case 'p':
 			dbg("udev path: %s\n", optarg);
 			strfieldcpy(path, optarg);
@@ -148,6 +156,11 @@
 				break;
 			}
 
+			if (strcmp(optarg, "path") == 0) {
+				query = PATH;
+				break;
+			}
+
 			printf("unknown query type\n");
 			return -EINVAL;
 
@@ -161,7 +174,7 @@
 				printf("unable to open udev database\n");
 				return -EACCES;
 			}
-			retval = udevdb_dump(print_record);
+			retval = udevdb_call_foreach(print_record);
 			udevdb_exit();
 			return retval;
 
@@ -179,44 +192,63 @@
 
 	/* process options */
 	if (query != NONE) {
-		if (path[0] == '\0') {
-			printf("query needs device path specified\n");
-			return -EINVAL;
-		}
-
 		retval = udevdb_open_ro();
 		if (retval != 0) {
 			printf("unable to open udev database\n");
 			return -EACCES;
 		}
-		retval = udevdb_get_dev(path, &dev);
-		if (retval == 0) {
-			switch(query) {
-			case NAME:
-				if (root)
-				strfieldcpy(result, udev_root);
-				strncat(result, dev.name, sizeof(result));
-				break;
 
-			case SYMLINK:
-				strfieldcpy(result, dev.symlink);
-				break;
+		if (path[0] != '\0') {
+			retval = udevdb_get_dev(path, &dev);
+			if (retval != 0) {
+				printf("device not found in database\n");
+				goto exit;
+			}
+			goto print;
+		}
 
-			case GROUP:
-				strfieldcpy(result, dev.group);
-				break;
+		if (dev.name[0] != '\0') {
+			retval = udevdb_get_dev_byname(dev.name, path, &dev);
+			if (retval != 0) {
+				printf("device not found in database\n");
+				goto exit;
+			}
+			goto print;
+		}
 
-			case OWNER:
-				strfieldcpy(result, dev.owner);
-				break;
+		printf("query needs device path(-p) or node name(-n) specified\n");
+		goto exit;
 
-			default:
-				break;
-			}
-			printf("%s\n", result);
-		} else {
-			printf("device not found in udev database\n");
+print:
+		switch(query) {
+		case NAME:
+			if (root)
+			strfieldcpy(result, udev_root);
+			strncat(result, dev.name, sizeof(result));
+			break;
+
+		case SYMLINK:
+			strfieldcpy(result, dev.symlink);
+			break;
+
+		case GROUP:
+			strfieldcpy(result, dev.group);
+			break;
+
+		case OWNER:
+			strfieldcpy(result, dev.owner);
+			break;
+
+		case PATH:
+			strfieldcpy(result, path);
+			break;
+
+		default:
+			goto exit;
 		}
+		printf("%s\n", result);
+
+exit:
 		udevdb_exit();
 		return retval;
 	}
@@ -227,13 +259,15 @@
 	}
 
 help:
-	printf("Usage: [-pqrdVh]\n"
+	printf("Usage: [-npqrdVh]\n"
 	       "  -q TYPE  query database for the specified value:\n"
 	       "             'name'    name of device node\n"
 	       "             'symlink' pointing to node\n"
 	       "             'owner'   of node\n"
 	       "             'group'   of node\n"
 	       "  -p PATH  sysfs device path used for query\n"
+	       "  -n NAME  node name used for query\n"
+	       "\n"
 	       "  -r       print udev root\n"
 	       "  -d       dump whole database\n"
 	       "  -V       print udev version\n"
udevdb.c +36 -6

61 lines not on this page · raw message

udevdb.h +2 -1
diff -Nru a/udevdb.h b/udevdb.h
@@ -12,10 +12,11 @@
 extern void udevdb_exit(void);
 extern int udevdb_init(int init_flag);
 extern int udevdb_open_ro(void);
-extern int udevdb_dump(void (*user_record_handler) (char *path, struct udevice *dev));
+extern int udevdb_call_foreach(int (*user_record_handler) (char *path, struct udevice *dev));
 
 extern int udevdb_add_dev(const char *path, const struct udevice *dev);
 extern int udevdb_get_dev(const char *path, struct udevice *dev);
 extern int udevdb_delete_dev(const char *path);
+extern int udevdb_get_dev_byname(const char *name, char *path, struct udevice *dev);
 
 #endif /* _UDEVDB_H_ */
#1 lore raw
Quoted 5 lines · Kay Sievers

Here we get the ability to query with the name of the node instead of the device path. It uses a linear search over the whole database.

  kay@pim:~/src/udev.kay$ ./udev -q path -n video/webcam0
  /class/video4linux/video0

Nice, but I think it still needs some tweaking:

$ ./udev -q path -n tty42
/class/tty/tty53
$ ./udev -q path -n tty53
/class/tty/tty53
$ ./udev -q path -n tty40
/class/tty/tty53
$ ./udev -q path -n ttyS1
/class/tty/tty53
$ ./udev -q path -n tty
/class/tty/tty53
$ ./udev -q path -n tt
/class/tty/tty53
$ ./udev -q path -n t
/class/tty/tty53
$ ./udev -q path -n foo
/block/fd0
$ ./udev -q path -n fd0
/block/fd0

I'll hold off applying this one for now :)

thanks,

greg k-h

-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#2 lore raw
In reply to Kay Sievers
Quoted 11 lines · Greg KH
Quoted 5 lines · Kay Sievers

Here we get the ability to query with the name of the node instead of the device path. It uses a linear search over the whole database.

  kay@pim:~/src/udev.kay$ ./udev -q path -n video/webcam0
  /class/video4linux/video0

Nice, but I think it still needs some tweaking:

$ ./udev -q path -n foo
/block/fd0

So here we go to compare more than the first char :) The man page update is also included.

thanks,
Kay

udev.8 +5 -2
diff -Nru a/udev.8 b/udev.8
@@ -50,10 +50,13 @@
 .BI -q " query_type"
 Query the database for specified value of a created device node.
 Valid types are:
-.BR name ", " symlink ", " owner " or " group .
+.BR name ", " symlink ", " owner " , " group " or " path.
 .TP
 .BI -p " sysfs_path"
-Specify the sysfs path needed for the query.
+Specify the sysfs path of the device to query.
+.TP
+.BI -n " name"
+Specify the name of the node for the device to query.
 .TP
 .B -d
 Dump the whole database.
udev.c +67 -32

168 lines not on this page · raw message

udevdb.c +36 -6

61 lines not on this page · raw message

udevdb.h +2 -1
diff -Nru a/udevdb.h b/udevdb.h
@@ -12,10 +12,11 @@
 extern void udevdb_exit(void);
 extern int udevdb_init(int init_flag);
 extern int udevdb_open_ro(void);
-extern int udevdb_dump(void (*user_record_handler) (char *path, struct udevice *dev));
+extern int udevdb_call_foreach(int (*user_record_handler) (char *path, struct udevice *dev));
 
 extern int udevdb_add_dev(const char *path, const struct udevice *dev);
 extern int udevdb_get_dev(const char *path, struct udevice *dev);
 extern int udevdb_delete_dev(const char *path);
+extern int udevdb_get_dev_byname(const char *name, char *path, struct udevice *dev);
 
 #endif /* _UDEVDB_H_ */
#3 lore raw
In reply to Kay Sievers
Quoted 8 lines · Kay Sievers
Quoted 7 lines · Greg KH
Quoted 5 lines · Kay Sievers

Here we get the ability to query with the name of the node instead of the device path. It uses a linear search over the whole database.

  kay@pim:~/src/udev.kay$ ./udev -q path -n video/webcam0
  /class/video4linux/video0
Quoted 7 lines
Quoted 4 lines

Nice, but I think it still needs some tweaking:

$ ./udev -q path -n foo
/block/fd0

So here we go to compare more than the first char :) The man page update is also included.

New version, with better function return codes for error handling.

Kay

udev.8 +5 -2
diff -Nru a/udev.8 b/udev.8
@@ -50,10 +50,13 @@
 .BI -q " query_type"
 Query the database for specified value of a created device node.
 Valid types are:
-.BR name ", " symlink ", " owner " or " group .
+.BR name ", " symlink ", " owner " , " group " or " path.
 .TP
 .BI -p " sysfs_path"
-Specify the sysfs path needed for the query.
+Specify the sysfs path of the device to query.
+.TP
+.BI -n " name"
+Specify the name of the node for the device to query.
 .TP
 .B -d
 Dump the whole database.
udev.c +67 -31

174 lines not on this page · raw message

udevdb.c +43 -6

68 lines not on this page · raw message

udevdb.h +2 -1
diff -Nru a/udevdb.h b/udevdb.h
@@ -12,10 +12,11 @@
 extern void udevdb_exit(void);
 extern int udevdb_init(int init_flag);
 extern int udevdb_open_ro(void);
-extern int udevdb_dump(void (*user_record_handler) (char *path, struct udevice *dev));
+extern int udevdb_call_foreach(int (*user_record_handler) (char *path, struct udevice *dev));
 
 extern int udevdb_add_dev(const char *path, const struct udevice *dev);
 extern int udevdb_get_dev(const char *path, struct udevice *dev);
 extern int udevdb_delete_dev(const char *path);
+extern int udevdb_get_dev_byname(const char *name, char *path, struct udevice *dev);
 
 #endif /* _UDEVDB_H_ */
Applied
#4 lore raw
In reply to Kay Sievers
Quoted 8 lines · Kay Sievers
Quoted 7 lines · Greg KH
Quoted 5 lines · Kay Sievers

Here we get the ability to query with the name of the node instead of the device path. It uses a linear search over the whole database.

  kay@pim:~/src/udev.kay$ ./udev -q path -n video/webcam0
  /class/video4linux/video0
Quoted 7 lines
Quoted 4 lines

Nice, but I think it still needs some tweaking:

$ ./udev -q path -n foo
/block/fd0

So here we go to compare more than the first char :) The man page update is also included.

Thanks, this now works :)

Applied.

greg k-h

-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

Applied
#5 lore raw
In reply to Kay Sievers
Quoted 11 lines · Kay Sievers
Quoted 10 lines · Kay Sievers
Quoted 9 lines · Greg KH
Quoted 5 lines · Kay Sievers

Here we get the ability to query with the name of the node instead of the device path. It uses a linear search over the whole database.

  kay@pim:~/src/udev.kay$ ./udev -q path -n video/webcam0
  /class/video4linux/video0

Nice, but I think it still needs some tweaking:

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
$ ./udev -q path -n foo
/block/fd0

So here we go to compare more than the first char :) The man page update is also included.

New version, with better function return codes for error handling.

Thanks, this works :)

Applied.

greg k-h

-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel