X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=udevinfo.c;h=a0a17c40b4d1828e31eaa6c4f8318fe577a0b906;hb=34bb5d057c99fa433392e0d5c17f604c8c111381;hp=6b7368119f821ae87d1b949dffc0f1fcd9fd0012;hpb=1aa0c52b4458be4cf6e55b2faf5c602761f5d3a3;p=elogind.git diff --git a/udevinfo.c b/udevinfo.c index 6b7368119..a0a17c40b 100644 --- a/udevinfo.c +++ b/udevinfo.c @@ -45,7 +45,7 @@ void log_message (int priority, const char *format, ...) } #endif -static void print_all_attributes(const char *devpath) +static void print_all_attributes(const char *devpath, const char *key) { char path[PATH_SIZE]; DIR *dir; @@ -80,7 +80,7 @@ static void print_all_attributes(const char *devpath) } replace_untrusted_chars(value); - printf(" SYSFS{%s}==\"%s\"\n", dent->d_name, value); + printf(" %s{%s}==\"%s\"\n", key, dent->d_name, value); } } printf("\n"); @@ -103,10 +103,10 @@ static int print_device_chain(const char *devpath) return -1; printf(" looking at device '%s':\n", dev->devpath); - printf(" KERNEL==\"%s\"\n", dev->kernel_name); + printf(" KERNEL==\"%s\"\n", dev->kernel); printf(" SUBSYSTEM==\"%s\"\n", dev->subsystem); printf(" DRIVER==\"%s\"\n", dev->driver); - print_all_attributes(dev->devpath); + print_all_attributes(dev->devpath, "ATTR"); /* walk up the chain of devices */ while (1) { @@ -114,11 +114,11 @@ static int print_device_chain(const char *devpath) if (dev == NULL) break; printf(" looking at parent device '%s':\n", dev->devpath); - printf(" ID==\"%s\"\n", dev->kernel_name); - printf(" BUS==\"%s\"\n", dev->subsystem); - printf(" DRIVER==\"%s\"\n", dev->driver); + printf(" KERNELS==\"%s\"\n", dev->kernel); + printf(" SUBSYTEMS==\"%s\"\n", dev->subsystem); + printf(" DRIVERS==\"%s\"\n", dev->driver); - print_all_attributes(dev->devpath); + print_all_attributes(dev->devpath, "ATTRS"); } return 0; @@ -163,30 +163,8 @@ static void export_db(void fnct(struct udevice *udev)) { name_list_cleanup(&name_list); } -static void print_help(void) -{ - fprintf(stderr, "Usage: udevinfo [-anpqrVh]\n" - " -q TYPE query database for the specified value:\n" - " 'name' name of device node\n" - " 'symlink' pointing to node\n" - " 'path' sysfs device path\n" - " 'env' the device related imported environment\n" - " 'all' all values\n" - "\n" - " -p PATH sysfs device path used for query or chain\n" - " -n NAME node/symlink name used for query\n" - "\n" - " -r prepend to query result or print udev_root\n" - " -a print all SYSFS_attributes along the device chain\n" - " -e export the content of the udev database\n" - " -V print udev version\n" - " -h print this help text\n" - "\n"); -} - int main(int argc, char *argv[], char *envp[]) { - static const char short_options[] = "aden:p:q:rVh"; int option; struct udevice *udev; int root = 0; @@ -209,13 +187,10 @@ int main(int argc, char *argv[], char *envp[]) char path[PATH_SIZE] = ""; char name[PATH_SIZE] = ""; - char temp[PATH_SIZE]; struct name_entry *name_loop; - char *pos; int rc = 0; logging_init("udevinfo"); - udev_config_init(); sysfs_init(); @@ -226,24 +201,29 @@ int main(int argc, char *argv[], char *envp[]) } /* get command line options */ + opterr = 0; while (1) { - option = getopt(argc, argv, short_options); + option = getopt(argc, argv, ":aden:p:q:rVh"); if (option == -1) break; dbg("option '%c'", option); switch (option) { case 'n': - dbg("udev name: %s\n", optarg); - strlcpy(name, optarg, sizeof(name)); + /* remove /dev if given */ + if (strncmp(optarg, udev_root, strlen(udev_root)) == 0) + strlcpy(name, &optarg[strlen(udev_root)+1], sizeof(name)); + else + strlcpy(name, optarg, sizeof(name)); + dbg("name: %s\n", name); break; case 'p': - dbg("udev path: %s\n", optarg); - /* remove sysfs mountpoint if not given */ + /* remove /sys if given */ if (strncmp(optarg, sysfs_path, strlen(sysfs_path)) == 0) strlcpy(path, &optarg[strlen(sysfs_path)], sizeof(path)); else strlcpy(path, optarg, sizeof(path)); + dbg("path: %s\n", path); break; case 'q': dbg("udev query: %s\n", optarg); @@ -289,9 +269,30 @@ int main(int argc, char *argv[], char *envp[]) printf("udevinfo, version %s\n", UDEV_VERSION); goto exit; case 'h': + printf("Usage: udevinfo [-anpqrVh]\n" + " -q TYPE query database for the specified value:\n" + " 'name' name of device node\n" + " 'symlink' pointing to node\n" + " 'path' sysfs device path\n" + " 'env' the device related imported environment\n" + " 'all' all values\n" + "\n" + " -p PATH sysfs device path used for query or chain\n" + " -n NAME node/symlink name used for query\n" + "\n" + " -r prepend to query result or print udev_root\n" + " -a print all SYSFS_attributes along the device chain\n" + " -e export the content of the udev database\n" + " -V print udev version\n" + " -h print this help text\n" + "\n"); + goto exit; + case ':': + fprintf(stderr, "missing argument for '%c'\n", optopt); + goto exit; case '?': default: - print_help(); + fprintf(stderr, "unrecognized option '%c'\n", optopt); goto exit; } } @@ -299,40 +300,19 @@ int main(int argc, char *argv[], char *envp[]) /* run action */ switch (action) { case ACTION_QUERY: - /* need devpath or node/symlink name for query */ + /* needs devpath or node/symlink name for query */ if (path[0] != '\0') { - /* remove sysfs_path if given */ - if (strncmp(path, sysfs_path, strlen(sysfs_path)) == 0) { - pos = path + strlen(sysfs_path); - } else { - if (path[0] != '/') { - /* prepend '/' if missing */ - strcpy(temp, "/"); - strlcpy(temp, path, sizeof(temp)); - pos = temp; - } else { - pos = path; - } - } - if (udev_db_get_device(udev, pos) != 0) { - fprintf(stderr, "no record for '%s' in database\n", pos); + if (udev_db_get_device(udev, path) != 0) { + fprintf(stderr, "no record for '%s' in database\n", path); rc = 3; goto exit; } } else if (name[0] != '\0') { char devpath[PATH_SIZE]; - int len; - - /* remove udev_root if given */ - len = strlen(udev_root); - if (strncmp(name, udev_root, len) == 0) { - pos = &name[len+1]; - } else - pos = name; - if (udev_db_lookup_name(pos, devpath, sizeof(devpath)) != 0) { - fprintf(stderr, "no record for '%s' in database\n", pos); - rc = 3; + if (udev_db_lookup_name(name, devpath, sizeof(devpath)) != 0) { + fprintf(stderr, "node name not found\n"); + rc = 4; goto exit; } udev_db_get_device(udev, devpath); @@ -371,7 +351,7 @@ int main(int argc, char *argv[], char *envp[]) print_record(udev); break; default: - print_help(); + fprintf(stderr, "unknown query type\n"); break; } break; @@ -397,7 +377,7 @@ int main(int argc, char *argv[], char *envp[]) printf("%s\n", udev_root); break; default: - print_help(); + fprintf(stderr, "missing option\n"); rc = 1; break; }