chiark / gitweb /
udevadm: info - use "udev_device"
[elogind.git] / udev / udevadm-info.c
index c2e6cdf84f972ed96efcf3bd4f3d26c8aac8d523..3be5f3154bff3e2a6a7954a3af89d25924671d08 100644 (file)
@@ -1,19 +1,18 @@
 /*
- * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
  *
- *     This program is free software; you can redistribute it and/or modify it
- *     under the terms of the GNU General Public License as published by the
- *     Free Software Foundation version 2 of the License.
- * 
- *     This program is distributed in the hope that it will be useful, but
- *     WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- *     General Public License for more details.
- * 
- *     You should have received a copy of the GNU General Public License along
- *     with this program; if not, write to the Free Software Foundation, Inc.,
- *     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
  *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "config.h"
 
 static void print_all_attributes(struct udev *udev, const char *devpath, const char *key)
 {
-       char path[PATH_SIZE];
+       char path[UTIL_PATH_SIZE];
        DIR *dir;
        struct dirent *dent;
 
-       strlcpy(path, udev_get_sys_path(udev), sizeof(path));
-       strlcat(path, devpath, sizeof(path));
+       util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
+       util_strlcat(path, devpath, sizeof(path));
 
        dir = opendir(path);
        if (dir != NULL) {
                for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
                        struct stat statbuf;
-                       char filename[PATH_SIZE];
+                       char filename[UTIL_PATH_SIZE];
                        char *attr_value;
-                       char value[NAME_SIZE];
+                       char value[UTIL_NAME_SIZE];
                        size_t len;
 
                        if (dent->d_name[0] == '.')
@@ -59,9 +58,9 @@ static void print_all_attributes(struct udev *udev, const char *devpath, const c
                        if (strcmp(dent->d_name, "dev") == 0)
                                continue;
 
-                       strlcpy(filename, path, sizeof(filename));
-                       strlcat(filename, "/", sizeof(filename));
-                       strlcat(filename, dent->d_name, sizeof(filename));
+                       util_strlcpy(filename, path, sizeof(filename));
+                       util_strlcat(filename, "/", sizeof(filename));
+                       util_strlcat(filename, dent->d_name, sizeof(filename));
                        if (lstat(filename, &statbuf) != 0)
                                continue;
                        if (S_ISLNK(statbuf.st_mode))
@@ -70,7 +69,7 @@ static void print_all_attributes(struct udev *udev, const char *devpath, const c
                        attr_value = sysfs_attr_get_value(udev, devpath, dent->d_name);
                        if (attr_value == NULL)
                                continue;
-                       len = strlcpy(value, attr_value, sizeof(value));
+                       len = util_strlcpy(value, attr_value, sizeof(value));
                        if(len >= sizeof(value))
                                len = sizeof(value) - 1;
                        dbg(udev, "attr '%s'='%s'(%zi)\n", dent->d_name, value, len);
@@ -85,16 +84,18 @@ static void print_all_attributes(struct udev *udev, const char *devpath, const c
 
                        printf("    %s{%s}==\"%s\"\n", key, dent->d_name, value);
                }
+               closedir(dir);
        }
        printf("\n");
 }
 
 static int print_device_chain(struct udev *udev, const char *devpath)
 {
-       struct sysfs_device *dev;
+       struct udev_device *device;
+       const char *str;
 
-       dev = sysfs_device_get(udev, devpath);
-       if (dev == NULL)
+       device = udev_device_new_from_devpath(udev, devpath);
+       if (device == NULL)
                return -1;
 
        printf("\n"
@@ -105,23 +106,37 @@ static int print_device_chain(struct udev *udev, const char *devpath)
               "and the attributes from one single parent device.\n"
               "\n");
 
-       printf("  looking at device '%s':\n", dev->devpath);
-       printf("    KERNEL==\"%s\"\n", dev->kernel);
-       printf("    SUBSYSTEM==\"%s\"\n", dev->subsystem);
-       printf("    DRIVER==\"%s\"\n", dev->driver);
-       print_all_attributes(udev, dev->devpath, "ATTR");
-
-       /* walk up the chain of devices */
-       while (1) {
-               dev = sysfs_device_get_parent(udev, dev);
-               if (dev == NULL)
+       printf("  looking at device '%s':\n", udev_device_get_devpath(device));
+       printf("    KERNEL==\"%s\"\n", udev_device_get_sysname(device));
+       str = udev_device_get_subsystem(device);
+       if (str == NULL)
+               str = "";
+       printf("    SUBSYSTEM==\"%s\"\n", str);
+       str = udev_device_get_driver(device);
+       if (str == NULL)
+               str = "";
+       printf("    DRIVER==\"%s\"\n", str);
+       print_all_attributes(udev, udev_device_get_devpath(device), "ATTR");
+
+       while (device != NULL) {
+               struct udev_device *device_parent;
+
+               device_parent = udev_device_new_from_parent(device);
+               udev_device_unref(device);
+               if (device_parent == NULL)
                        break;
-               printf("  looking at parent device '%s':\n", dev->devpath);
-               printf("    KERNELS==\"%s\"\n", dev->kernel);
-               printf("    SUBSYSTEMS==\"%s\"\n", dev->subsystem);
-               printf("    DRIVERS==\"%s\"\n", dev->driver);
-
-               print_all_attributes(udev, dev->devpath, "ATTRS");
+               device = device_parent;
+               printf("  looking at parent device '%s':\n", udev_device_get_devpath(device));
+               printf("    KERNELS==\"%s\"\n", udev_device_get_sysname(device));
+               str = udev_device_get_subsystem(device);
+               if (str == NULL)
+                       str = "";
+               printf("    SUBSYSTEMS==\"%s\"\n", str);
+               str = udev_device_get_driver(device);
+               if (str == NULL)
+                       str = "";
+               printf("    DRIVERS==\"%s\"\n", str);
+               print_all_attributes(udev, udev_device_get_devpath(device), "ATTRS");
        }
 
        return 0;
@@ -181,7 +196,7 @@ static int lookup_device_by_name(struct udev *udev, struct udevice **udevice, co
        /* select the device that seems to match */
        list_for_each_entry(device, &name_list, node) {
                struct udevice *udevice_loop;
-               char filename[PATH_SIZE];
+               char filename[UTIL_PATH_SIZE];
                struct stat statbuf;
 
                udevice_loop = udev_device_init(udev);
@@ -192,9 +207,9 @@ static int lookup_device_by_name(struct udev *udev, struct udevice **udevice, co
                info(udev, "found db entry '%s'\n", device->name);
 
                /* make sure, we don't get a link of a different device */
-               strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
-               strlcat(filename, "/", sizeof(filename));
-               strlcat(filename, name, sizeof(filename));
+               util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
+               util_strlcat(filename, "/", sizeof(filename));
+               util_strlcat(filename, name, sizeof(filename));
                if (stat(filename, &statbuf) != 0)
                        goto next;
                if (major(udevice_loop->devt) > 0 && udevice_loop->devt != statbuf.st_rdev) {
@@ -270,8 +285,8 @@ int udevadm_info(struct udev *udev, int argc, char *argv[])
                QUERY_ALL,
        } query = QUERY_NONE;
 
-       char path[PATH_SIZE] = "";
-       char name[PATH_SIZE] = "";
+       char path[UTIL_PATH_SIZE] = "";
+       char name[UTIL_PATH_SIZE] = "";
        struct name_entry *name_loop;
        int rc = 0;
 
@@ -287,36 +302,36 @@ int udevadm_info(struct udev *udev, int argc, char *argv[])
                case 'n':
                        /* remove /dev if given */
                        if (strncmp(optarg, udev_get_dev_path(udev), strlen(udev_get_dev_path(udev))) == 0)
-                               strlcpy(name, &optarg[strlen(udev_get_dev_path(udev))+1], sizeof(name));
+                               util_strlcpy(name, &optarg[strlen(udev_get_dev_path(udev))+1], sizeof(name));
                        else
-                               strlcpy(name, optarg, sizeof(name));
-                       remove_trailing_chars(name, '/');
+                               util_strlcpy(name, optarg, sizeof(name));
+                       util_remove_trailing_chars(name, '/');
                        dbg(udev, "name: %s\n", name);
                        break;
                case 'p':
                        /* remove /sys if given */
                        if (strncmp(optarg, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) == 0)
-                               strlcpy(path, &optarg[strlen(udev_get_sys_path(udev))], sizeof(path));
+                               util_strlcpy(path, &optarg[strlen(udev_get_sys_path(udev))], sizeof(path));
                        else
-                               strlcpy(path, optarg, sizeof(path));
-                       remove_trailing_chars(path, '/');
+                               util_strlcpy(path, optarg, sizeof(path));
+                       util_remove_trailing_chars(path, '/');
 
                        /* possibly resolve to real devpath */
-                       if (sysfs_resolve_link(udev, path, sizeof(path)) != 0) {
-                               char temp[PATH_SIZE];
+                       if (util_resolve_sys_link(udev, path, sizeof(path)) != 0) {
+                               char temp[UTIL_PATH_SIZE];
                                char *pos;
 
                                /* also check if the parent is a link */
-                               strlcpy(temp, path, sizeof(temp));
+                               util_strlcpy(temp, path, sizeof(temp));
                                pos = strrchr(temp, '/');
                                if (pos != 0) {
-                                       char tail[PATH_SIZE];
+                                       char tail[UTIL_PATH_SIZE];
 
-                                       strlcpy(tail, pos, sizeof(tail));
+                                       util_strlcpy(tail, pos, sizeof(tail));
                                        pos[0] = '\0';
-                                       if (sysfs_resolve_link(udev, temp, sizeof(temp)) == 0) {
-                                               strlcpy(path, temp, sizeof(path));
-                                               strlcat(path, tail, sizeof(path));
+                                       if (util_resolve_sys_link(udev, temp, sizeof(temp)) == 0) {
+                                               util_strlcpy(path, temp, sizeof(path));
+                                               util_strlcat(path, tail, sizeof(path));
                                        }
                                }
                        }
@@ -354,7 +369,7 @@ int udevadm_info(struct udev *udev, int argc, char *argv[])
                        break;
                case 'd':
                        action = ACTION_DEVICE_ID_FILE;
-                       strlcpy(name, optarg, sizeof(name));
+                       util_strlcpy(name, optarg, sizeof(name));
                        break;
                case 'a':
                        action = ACTION_ATTRIBUTE_WALK;