All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kay Sievers <kay.sievers@vrfy.org>
To: linux-hotplug@vger.kernel.org
Subject: [PATCH] udev - advanced user query options
Date: Sun, 11 Jan 2004 02:33:00 +0000	[thread overview]
Message-ID: <20040111023300.GA4762@vrfy.org> (raw)

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

This patch improves the user options for udev.
It is possible now to query for the name, the symlinks or owner/group.
If asked for the name of the node we are able to prepend the udev_root
with the -r option.

SAMPLE:

  kay@pim:~/src/udev.test$ ./udev -V
  udev, version 012_bk

  kay@pim:~/src/udev.test$ ./udev -h
  Usage: [-qrVh]
    -q <name>  query database for the specified value
    -p <path>  device path used for query
    -r         print udev root
    -V         print udev version
    -h         print this help text

  kay@pim:~/src/udev.test$ ./udev -r
  /udev/

  kay@pim:~/src/udev.test$ ./udev -q name -p /class/video4linux/video0
  video/webcam0

  kay@pim:~/src/udev.test$ ./udev -q symlink -p /class/video4linux/video0
  camera0 kamera0

  kay@pim:~/src/udev.test$ ./udev -q owner -p /class/video4linux/video0
  501

  kay@pim:~/src/udev.test$ ./udev -r -q name -p /class/video4linux/video0
  /udev/video/webcam0


thanks,
Kay

[-- Attachment #2: 02-more-user-options.diff --]
[-- Type: text/plain, Size: 3070 bytes --]

diff -Nru a/udev.c b/udev.c
--- a/udev.c	Sun Jan 11 03:31:19 2004
+++ b/udev.c	Sun Jan 11 03:31:19 2004
@@ -82,13 +82,26 @@
 	return seqnum;
 }
 
+enum query_type {
+	NONE,
+	NAME,
+	SYMLINK,
+	OWNER,
+	GROUP
+};
+
 static inline int udev_user(int argc, char **argv)
 {
-	static const char short_options[] = "q:rVh";
+	static const char short_options[] = "p:q:rVh";
 	int option;
 	int retval = -EINVAL;
 	struct udevice dev;
+	int root = 0;
+	enum query_type query = NONE;
+	char result[NAME_SIZE] = "";
+	char path[NAME_SIZE] = "";
 
+	/* get command line options */
 	while (1) {
 		option = getopt(argc, argv, short_options);
 		if (option == -1)
@@ -96,25 +109,40 @@
 
 		dbg("option '%c'", option);
 		switch (option) {
+		case 'p':
+			dbg("udev path: %s\n", optarg);
+			strfieldcpy(path, optarg);
+			break;
+
 		case 'q':
 			dbg("udev query: %s\n", optarg);
-			retval = udevdb_open_ro();
-			if (retval != 0) {
-				printf("unable to open udev database\n");
-				return -1;
+
+			if (strcmp(optarg, "name") == 0) {
+				query = NAME;
+				break;
+			}
+
+			if (strcmp(optarg, "symlink") == 0) {
+				query = SYMLINK;
+				break;
 			}
-			retval = udevdb_get_dev(optarg, &dev);
-			if (retval == 0) {
-				printf("%s\n", dev.name);
-			} else {
-				printf("device not found in udev database\n");
+
+			if (strcmp(optarg, "owner") == 0) {
+				query = OWNER;
+				break;
+			}
+
+			if (strcmp(optarg, "group") == 0) {
+				query = GROUP;
+				break;
 			}
-			udevdb_exit();
-			return retval;
+
+			printf("unknown query type\n");
+			return -EINVAL;
 
 		case 'r':
-			printf("%s\n", udev_root);
-			return 0;
+			root = 1;
+			break;
 
 		case 'V':
 			printf("udev, version %s\n", UDEV_VERSION);
@@ -128,14 +156,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;
+
+			case GROUP:
+				strfieldcpy(result, dev.group);
+				break;
+
+			case OWNER:
+				strfieldcpy(result, dev.owner);
+				break;
+
+			default:
+				break;
+			}
+			printf("%s\n", result);
+		} else {
+			printf("device not found in udev database\n");
+		}
+		udevdb_exit();
+		return retval;
+	}
+
+	if (root) {
+		printf("%s\n", udev_root);
+		return 0;
+	}
+
 help:
 	printf("Usage: [-qrVh]\n"
-	       "  -q <path>  query database for the name of the created node\n"
+	       "  -q <name>  query database for the specified value\n"
+	       "  -p <path>  device path used for query\n"
 	       "  -r         print udev root\n"
 	       "  -V         print udev version\n"
 	       "  -h         print this help text\n"
 	       "\n");
-
 	return retval;
 }
 

             reply	other threads:[~2004-01-11  2:33 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-11  2:33 Kay Sievers [this message]
2004-01-11  2:37 ` [PATCH] udev - advanced user query options Robert Love
2004-01-11  3:17 ` Kay Sievers
2004-01-12 18:05 ` David Zeuthen
2004-01-12 21:21 ` Greg KH
2004-01-12 21:22 ` Greg KH
2004-01-12 23:56 ` Kay Sievers
2004-01-13  1:04 ` Greg KH
2004-01-13  1:21 ` Kay Sievers
2004-01-13  4:35 ` Kay Sievers
2004-01-13 18:29 ` Greg KH
2004-01-13 19:47 ` Greg KH
2004-01-15 13:54 ` Olaf Hering
2004-01-15 20:56 ` Greg KH
2004-01-15 21:16 ` Kay Sievers
2004-01-15 21:21 ` Greg KH
2004-01-16  9:49 ` Olaf Hering
2004-01-16  9:57 ` Kay Sievers
2004-01-16 21:54 ` Greg KH
2004-01-19  9:11 ` Olaf Hering
2004-01-19 17:47 ` Greg KH
2004-01-19 19:23 ` Olaf Hering
2004-01-20  0:08 ` Greg KH

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=20040111023300.GA4762@vrfy.org \
    --to=kay.sievers@vrfy.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.