* [PATCH] udevadm-info: add query for properties with quoted output
@ 2010-12-09 14:26 harald
2010-12-09 18:03 ` David Zeuthen
2010-12-09 18:17 ` Kay Sievers
0 siblings, 2 replies; 3+ messages in thread
From: harald @ 2010-12-09 14:26 UTC (permalink / raw)
To: linux-hotplug
From: Harald Hoyer <harald@redhat.com>
$ 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(-)
diff --git a/libudev/libudev-private.h b/libudev/libudev-private.h
index c9ed462..277a8bf 100644
--- a/libudev/libudev-private.h
+++ b/libudev/libudev-private.h
@@ -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);
diff --git a/libudev/libudev-util.c b/libudev/libudev-util.c
index 030b78c..12b2b3d 100644
--- a/libudev/libudev-util.c
+++ b/libudev/libudev-util.c
@@ -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;
diff --git a/udev/udevadm-info.c b/udev/udevadm-info.c
index 9bd60c7..6e0feea 100644
--- a/udev/udevadm-info.c
+++ b/udev/udevadm-info.c
@@ -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;
--
1.7.3.2
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] udevadm-info: add query for properties with quoted output
2010-12-09 14:26 [PATCH] udevadm-info: add query for properties with quoted output harald
@ 2010-12-09 18:03 ` David Zeuthen
2010-12-09 18:17 ` Kay Sievers
1 sibling, 0 replies; 3+ messages in thread
From: David Zeuthen @ 2010-12-09 18:03 UTC (permalink / raw)
To: linux-hotplug
On Thu, Dec 9, 2010 at 9:26 AM, <harald@redhat.com> wrote:
> From: Harald Hoyer <harald@redhat.com>
>
> $ 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
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] udevadm-info: add query for properties with quoted output
2010-12-09 14:26 [PATCH] udevadm-info: add query for properties with quoted output harald
2010-12-09 18:03 ` David Zeuthen
@ 2010-12-09 18:17 ` Kay Sievers
1 sibling, 0 replies; 3+ messages in thread
From: Kay Sievers @ 2010-12-09 18:17 UTC (permalink / raw)
To: linux-hotplug
On Thu, Dec 9, 2010 at 19:03, David Zeuthen <zeuthen@gmail.com> wrote:
> On Thu, Dec 9, 2010 at 9:26 AM, <harald@redhat.com> wrote:
>> From: Harald Hoyer <harald@redhat.com>
>>
>> $ 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
>
> 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.
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2010-12-09 18:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-09 14:26 [PATCH] udevadm-info: add query for properties with quoted output harald
2010-12-09 18:03 ` David Zeuthen
2010-12-09 18:17 ` Kay Sievers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox