chiark / gitweb /
udevtrigger: trigger devices sorted by their dependency
[elogind.git] / udevinfo.c
index d1d77152c3ffed3ad82bc4eb305a8508d84c8a06..2832ea0ee368677ca7325384bb7655108c7fd980 100644 (file)
@@ -26,6 +26,8 @@
 #include <dirent.h>
 #include <errno.h>
 #include <getopt.h>
 #include <dirent.h>
 #include <errno.h>
 #include <getopt.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 
 #include "udev.h"
 
 
 #include "udev.h"
 
@@ -56,10 +58,23 @@ static void print_all_attributes(const char *devpath, const char *key)
        dir = opendir(path);
        if (dir != NULL) {
                for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
        dir = opendir(path);
        if (dir != NULL) {
                for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
+                       struct stat statbuf;
+                       char filename[PATH_SIZE];
                        char *attr_value;
                        char value[NAME_SIZE];
                        size_t len;
 
                        char *attr_value;
                        char value[NAME_SIZE];
                        size_t len;
 
+                       if (dent->d_name[0] == '.')
+                               continue;
+
+                       strlcpy(filename, path, sizeof(filename));
+                       strlcat(filename, "/", sizeof(filename));
+                       strlcat(filename, dent->d_name, sizeof(filename));
+                       if (lstat(filename, &statbuf) != 0)
+                               continue;
+                       if (S_ISLNK(statbuf.st_mode))
+                               continue;
+
                        attr_value = sysfs_attr_get_value(devpath, dent->d_name);
                        if (attr_value == NULL)
                                continue;
                        attr_value = sysfs_attr_get_value(devpath, dent->d_name);
                        if (attr_value == NULL)
                                continue;
@@ -89,6 +104,10 @@ static int print_device_chain(const char *devpath)
 {
        struct sysfs_device *dev;
 
 {
        struct sysfs_device *dev;
 
+       dev = sysfs_device_get(devpath);
+       if (dev == NULL)
+               return -1;
+
        printf("\n"
               "Udevinfo starts with the device specified by the devpath and then\n"
               "walks up the chain of parent devices. It prints for every device\n"
        printf("\n"
               "Udevinfo starts with the device specified by the devpath and then\n"
               "walks up the chain of parent devices. It prints for every device\n"
@@ -97,10 +116,6 @@ static int print_device_chain(const char *devpath)
               "and the attributes from one single parent device.\n"
               "\n");
 
               "and the attributes from one single parent device.\n"
               "\n");
 
-       dev = sysfs_device_get(devpath);
-       if (dev == NULL)
-               return -1;
-
        printf("  looking at device '%s':\n", dev->devpath);
        printf("    KERNEL==\"%s\"\n", dev->kernel);
        printf("    SUBSYSTEM==\"%s\"\n", dev->subsystem);
        printf("  looking at device '%s':\n", dev->devpath);
        printf("    KERNEL==\"%s\"\n", dev->kernel);
        printf("    SUBSYSTEM==\"%s\"\n", dev->subsystem);
@@ -114,7 +129,7 @@ static int print_device_chain(const char *devpath)
                        break;
                printf("  looking at parent device '%s':\n", dev->devpath);
                printf("    KERNELS==\"%s\"\n", dev->kernel);
                        break;
                printf("  looking at parent device '%s':\n", dev->devpath);
                printf("    KERNELS==\"%s\"\n", dev->kernel);
-               printf("    SUBSYTEMS==\"%s\"\n", dev->subsystem);
+               printf("    SUBSYSTEMS==\"%s\"\n", dev->subsystem);
                printf("    DRIVERS==\"%s\"\n", dev->driver);
 
                print_all_attributes(dev->devpath, "ATTRS");
                printf("    DRIVERS==\"%s\"\n", dev->driver);
 
                print_all_attributes(dev->devpath, "ATTRS");
@@ -168,7 +183,7 @@ int main(int argc, char *argv[], char *envp[])
        struct udevice *udev;
        int root = 0;
 
        struct udevice *udev;
        int root = 0;
 
-       struct option options[] = {
+       static const struct option options[] = {
                { "name", 1, NULL, 'n' },
                { "path", 1, NULL, 'p' },
                { "query", 1, NULL, 'q' },
                { "name", 1, NULL, 'n' },
                { "path", 1, NULL, 'p' },
                { "query", 1, NULL, 'q' },
@@ -362,7 +377,11 @@ int main(int argc, char *argv[], char *envp[])
                break;
        case ACTION_ATTRIBUTE_WALK:
                if (path[0] != '\0') {
                break;
        case ACTION_ATTRIBUTE_WALK:
                if (path[0] != '\0') {
-                       print_device_chain(path);
+                       if (print_device_chain(path) != 0) {
+                               fprintf(stderr, "device not found\n");
+                               rc = 4;
+                               goto exit;
+                       }
                } else if (name[0] != '\0') {
                        char devpath[PATH_SIZE];
 
                } else if (name[0] != '\0') {
                        char devpath[PATH_SIZE];
 
@@ -371,7 +390,11 @@ int main(int argc, char *argv[], char *envp[])
                                rc = 4;
                                goto exit;
                        }
                                rc = 4;
                                goto exit;
                        }
-                       print_device_chain(devpath);
+                       if (print_device_chain(devpath) != 0) {
+                               fprintf(stderr, "device not found\n");
+                               rc = 4;
+                               goto exit;
+                       }
                } else {
                        fprintf(stderr, "attribute walk needs --path or node --name specified\n");
                        rc = 5;
                } else {
                        fprintf(stderr, "attribute walk needs --path or node --name specified\n");
                        rc = 5;