Skip to contentSkip to conversation

udevadm-info: add query for properties with quoted output

PATCH In review [email protected] 3 participants 3 messages Dec 9, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 udevadm-info: add query for properties with quoted output

From: Harald Hoyer <[email protected]>

$ udevadm info --query=shproperty --name=/dev/sda
UDEV_LOG='3'
DEVPATH='/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda'
MAJOR='8'
MINOR='0'
DEVNAME='/dev/sda'
DEVTYPE='disk'
...
DEVLINKS='/dev/block/8:0 /dev/disk/by-id/ata-APPLE_SSD_TS128B_60CS105MT4RZ /dev/disk/by-id/scsi-SATA_APPLE_SSD_TS128_60CS105MT4RZ /dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0'

This enables the use of "eval" in shell scripts:

$ eval $(udevadm info --query=shenv --name=/dev/sda)
$ echo $DEVLINKS
/dev/block/8:0 /dev/disk/by-id/ata-APPLE_SSD_TS128B_60CS105MT4RZ /dev/disk/by-id/scsi-SATA_APPLE_SSD_TS128_60CS105MT4RZ /dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0
---
 libudev/libudev-private.h |    1 +
 libudev/libudev-util.c    |   25 +++++++++++++++++++++++++
 udev/udevadm-info.c       |   19 +++++++++++++++++++
 3 files changed, 45 insertions(+), 0 deletions(-)
libudev/libudev-private.h +1 -0
index c9ed462..277a8bf 100644
@@ -197,6 +197,7 @@ ssize_t util_get_sys_subsystem(struct udev *udev, const char *syspath, char *sub
 ssize_t util_get_sys_driver(struct udev *udev, const char *syspath, char *driver, size_t size);
 int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size);
 int util_log_priority(const char *priority);
+size_t util_shell_encode(const char *src, char *dest, size_t size);
 size_t util_path_encode(const char *src, char *dest, size_t size);
 size_t util_path_decode(char *s);
 void util_remove_trailing_chars(char *path, char c);
libudev/libudev-util.c +25 -0
index 030b78c..12b2b3d 100644
@@ -98,6 +98,31 @@ int util_log_priority(const char *priority)
 	return 0;
 }
 
+size_t util_shell_encode(const char *src, char *dest, size_t size)
+{
+	size_t i, j;
+
+	for (i = 0, j = 0; src[i] != '\0'; i++) {
+		if (src[i] = '\'') {
+			if (j+4 >= size) {
+				j = 0;
+				break;
+			}
+			memcpy(&dest[j], "'\\''", 4);
+			j += 4;
+		} else {
+			if (j+1 >= size) {
+				j = 0;
+				break;
+			}
+			dest[j] = src[i];
+			j++;
+		}
+	}
+	dest[j] = '\0';
+	return j;
+}
+
 size_t util_path_encode(const char *src, char *dest, size_t size)
 {
 	size_t i, j;
udev/udevadm-info.c +19 -0
index 9bd60c7..6e0feea 100644
@@ -235,6 +235,7 @@ int udevadm_info(struct udev *udev, int argc, char *argv[])
 		QUERY_PATH,
 		QUERY_SYMLINK,
 		QUERY_PROPERTY,
+		QUERY_PROPERTY_QUOTED,
 		QUERY_ALL,
 	} query = QUERY_NONE;
 
@@ -307,6 +308,8 @@ int udevadm_info(struct udev *udev, int argc, char *argv[])
 			action = ACTION_QUERY;
 			if (strcmp(optarg, "property") = 0 || strcmp(optarg, "env") = 0) {
 				query = QUERY_PROPERTY;
+			} else if (strcmp(optarg, "shproperty") = 0 || strcmp(optarg, "shenv") = 0) {
+				query = QUERY_PROPERTY_QUOTED;
 			} else if (strcmp(optarg, "name") = 0) {
 				query = QUERY_NAME;
 			} else if (strcmp(optarg, "symlink") = 0) {
@@ -352,6 +355,7 @@ int udevadm_info(struct udev *udev, int argc, char *argv[])
 			       "      symlink                  pointing to node\n"
 			       "      path                     sys device path\n"
 			       "      property                 the device properties\n"
+			       "      shproperty               the device properties in shell style key='value'\n"
 			       "      all                      all values\n"
 			       "  --path=<syspath>           sys device path used for query or attribute walk\n"
 			       "  --name=<name>              node or symlink name used for query or attribute walk\n"
@@ -421,6 +425,21 @@ int udevadm_info(struct udev *udev, int argc, char *argv[])
 				list_entry = udev_list_entry_get_next(list_entry);
 			}
 			break;
+		case QUERY_PROPERTY_QUOTED:
+			list_entry = udev_device_get_properties_list_entry(device);
+			while (list_entry != NULL) {
+				char *val = NULL;
+				const char *oval = udev_list_entry_get_value(list_entry);
+				size_t len = strlen(oval)*4 + 1;
+				val = malloc(len);
+				if (val) {
+					util_shell_encode(oval, val, len);
+					printf("%s='%s'\n", udev_list_entry_get_name(list_entry), val);
+					list_entry = udev_list_entry_get_next(list_entry);
+					free(val);
+				}
+			}
+			break;
 		case QUERY_ALL:
 			print_record(device);
 			break;
Signature · 1 line
1.7.3.2
#1 lore raw
Quoted 12 lines · On Thu, Dec 9, 2010 at 9:26 AM, <[email protected]> wrote:

From: Harald Hoyer <[email protected]>

$ udevadm info --query=shproperty --name=/dev/sda
UDEV_LOG='3'
DEVPATH='/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda'
MAJOR='8'
MINOR='0'
DEVNAME='/dev/sda'
DEVTYPE='disk'
...
DEVLINKS='/dev/block/8:0 /dev/disk/by-id/ata-APPLE_SSD_TS128B_60CS105MT4RZ /dev/disk/by-id/scsi-SATA_APPLE_SSD_TS128_60CS105MT4RZ /dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0'
Quoted 4 lines

This enables the use of "eval" in shell scripts:

$ eval $(udevadm info --query=shenv --name=/dev/sda)
$ echo $DEVLINKS
/dev/block/8:0 /dev/disk/by-id/ata-APPLE_SSD_TS128B_60CS105MT4RZ /dev/disk/by-id/scsi-SATA_APPLE_SSD_TS128_60CS105MT4RZ /dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0

Would be nice if you could prefix the resulting environment variables e.g.

  $ eval $(udevadm info --query=shenv --shprefix=MY_NAMESPACE --name=/dev/sda)
  $ echo $MY_NAMESPACE_DEVLINKS
  /dev/block/8:0 /dev/disk/by-id/ata-APPLE_SSD_TS128B_60CS105MT4RZ
/dev/disk/by-id/scsi-SATA_APPLE_SSD_TS128_60CS105MT4RZ
/dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0

to ensure that no existing variables gets overwritten. You probably also want to patch the man pages.

Thanks,
David

#2 lore raw
In reply to [email protected]
Quoted 20 lines · David Zeuthen
Quoted 12 lines · On Thu, Dec 9, 2010 at 9:26 AM,  <[email protected]> wrote:

From: Harald Hoyer <[email protected]>

$ udevadm info --query=shproperty --name=/dev/sda
UDEV_LOG='3'
DEVPATH='/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda'
MAJOR='8'
MINOR='0'
DEVNAME='/dev/sda'
DEVTYPE='disk'
...
DEVLINKS='/dev/block/8:0 /dev/disk/by-id/ata-APPLE_SSD_TS128B_60CS105MT4RZ /dev/disk/by-id/scsi-SATA_APPLE_SSD_TS128_60CS105MT4RZ /dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0'
Quoted 4 lines

This enables the use of "eval" in shell scripts:

$ eval $(udevadm info --query=shenv --name=/dev/sda)
$ echo $DEVLINKS
/dev/block/8:0 /dev/disk/by-id/ata-APPLE_SSD_TS128B_60CS105MT4RZ /dev/disk/by-id/scsi-SATA_APPLE_SSD_TS128_60CS105MT4RZ /dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0

Would be nice if you could prefix the resulting environment variables e.g.

Quoted 8 lines
 $ eval $(udevadm info --query=shenv --shprefix=MY_NAMESPACE --name=/dev/sda)
 $ echo $MY_NAMESPACE_DEVLINKS
 /dev/block/8:0 /dev/disk/by-id/ata-APPLE_SSD_TS128B_60CS105MT4RZ
/dev/disk/by-id/scsi-SATA_APPLE_SSD_TS128_60CS105MT4RZ
/dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0

to ensure that no existing variables gets overwritten. You probably also want to patch the man pages.

There is already --export, and --export-prefix for 'udevadm info'. That can probably be re-used.

I think, the quotes we can just always unconditionally add, when we have whitespace in the value.

Kay