1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-25 01:34:28 +03:00

[PATCH] udev - advanced user query options

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
This commit is contained in:
kay.sievers@vrfy.org 2004-01-12 21:18:57 -08:00 committed by Greg KH
parent 606143c8d2
commit 5472beeea5

109
udev.c
View File

@ -82,13 +82,26 @@ static inline char *get_seqnum(void)
return seqnum; return seqnum;
} }
enum query_type {
NONE,
NAME,
SYMLINK,
OWNER,
GROUP
};
static inline int udev_user(int argc, char **argv) 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 option;
int retval = -EINVAL; int retval = -EINVAL;
struct udevice dev; 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) { while (1) {
option = getopt(argc, argv, short_options); option = getopt(argc, argv, short_options);
if (option == -1) if (option == -1)
@ -96,25 +109,40 @@ static inline int udev_user(int argc, char **argv)
dbg("option '%c'", option); dbg("option '%c'", option);
switch (option) { switch (option) {
case 'p':
dbg("udev path: %s\n", optarg);
strfieldcpy(path, optarg);
break;
case 'q': case 'q':
dbg("udev query: %s\n", optarg); dbg("udev query: %s\n", optarg);
retval = udevdb_open_ro();
if (retval != 0) { if (strcmp(optarg, "name") == 0) {
printf("unable to open udev database\n"); query = NAME;
return -1; break;
} }
retval = udevdb_get_dev(optarg, &dev);
if (retval == 0) { if (strcmp(optarg, "symlink") == 0) {
printf("%s\n", dev.name); query = SYMLINK;
} else { break;
printf("device not found in udev database\n");
} }
udevdb_exit();
return retval; if (strcmp(optarg, "owner") == 0) {
query = OWNER;
break;
}
if (strcmp(optarg, "group") == 0) {
query = GROUP;
break;
}
printf("unknown query type\n");
return -EINVAL;
case 'r': case 'r':
printf("%s\n", udev_root); root = 1;
return 0; break;
case 'V': case 'V':
printf("udev, version %s\n", UDEV_VERSION); printf("udev, version %s\n", UDEV_VERSION);
@ -128,14 +156,63 @@ static inline int udev_user(int argc, char **argv)
} }
} }
/* 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: help:
printf("Usage: [-qrVh]\n" 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" " -r print udev root\n"
" -V print udev version\n" " -V print udev version\n"
" -h print this help text\n" " -h print this help text\n"
"\n"); "\n");
return retval; return retval;
} }