From fc30b1a0b0e98541e7d317810a53f69abc382865 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 7 Feb 2025 10:04:41 +0900 Subject: [PATCH] udev-dump: show more information This also reorders and renames entries. --- src/udev/udev-dump.c | 107 ++++++++++++++++++++++++++++++------------- 1 file changed, 74 insertions(+), 33 deletions(-) diff --git a/src/udev/udev-dump.c b/src/udev/udev-dump.c index 2882ad1001a..b13779e5f79 100644 --- a/src/udev/udev-dump.c +++ b/src/udev/udev-dump.c @@ -3,6 +3,7 @@ #include "ansi-color.h" #include "device-private.h" #include "device-util.h" +#include "devnum-util.h" #include "format-util.h" #include "fs-util.h" #include "udev-builtin.h" @@ -30,54 +31,51 @@ void event_cache_written_sysctl(UdevEvent *event, const char *attr, const char * void dump_event(UdevEvent *event, FILE *f) { sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev); + const char *subsys = NULL, *str; if (!f) f = stdout; - if (!hashmap_isempty(event->written_sysattrs)) { - const char *key, *value; + if (sd_device_get_devpath(dev, &str) >= 0) + fprintf(f, "%sDevice path:%s\n %s\n", ansi_highlight(), ansi_normal(), str); - fprintf(f, "%sWritten sysfs attributes:%s\n", ansi_highlight(), ansi_normal()); - HASHMAP_FOREACH_KEY(value, key, event->written_sysattrs) - fprintf(f, " %s : %s\n", key, value); - } + if (sd_device_get_sysname(dev, &str) >= 0) + fprintf(f, "%sDevice name:%s\n %s\n", ansi_highlight(), ansi_normal(), str); - if (!hashmap_isempty(event->written_sysctls)) { - const char *key, *value; + if (sd_device_get_sysnum(dev, &str) >= 0) + fprintf(f, "%sDevice number:%s\n %s\n", ansi_highlight(), ansi_normal(), str); - fprintf(f, "%sWritten sysctl entries:%s\n", ansi_highlight(), ansi_normal()); - HASHMAP_FOREACH_KEY(value, key, event->written_sysctls) - fprintf(f, " %s : %s\n", key, value); - } + if (sd_device_get_device_id(dev, &str) >= 0) + fprintf(f, "%sDevice ID:%s\n %s\n", ansi_highlight(), ansi_normal(), str); - fprintf(f, "%sProperties:%s\n", ansi_highlight(), ansi_normal()); - FOREACH_DEVICE_PROPERTY(dev, key, value) - fprintf(f, " %s=%s\n", key, value); - - if (sd_device_get_tag_first(dev)) { - fprintf(f, "%sTags:%s\n", ansi_highlight(), ansi_normal()); - FOREACH_DEVICE_TAG(dev, tag) - fprintf(f, " %s\n", tag); + if (sd_device_get_subsystem(dev, &subsys) >= 0) { + const char *driver_subsys = NULL; + (void) sd_device_get_driver_subsystem(dev, &driver_subsys); + fprintf(f, "%sSubsystem:%s\n %s%s%s%s\n", ansi_highlight(), ansi_normal(), subsys, + driver_subsys ? " (" : "", + strempty(driver_subsys), + driver_subsys ? ")" : ""); } - if (sd_device_get_devnum(dev, NULL) >= 0) { + if (sd_device_get_devtype(dev, &str) >= 0) + fprintf(f, "%sDevice type:%s\n %s\n", ansi_highlight(), ansi_normal(), str); - if (sd_device_get_devlink_first(dev)) { - int prio = 0; - (void) device_get_devlink_priority(dev, &prio); - fprintf(f, "%sDevice node symlinks:%s (priority=%i)\n", ansi_highlight(), ansi_normal(), prio); - FOREACH_DEVICE_DEVLINK(dev, devlink) - fprintf(f, " %s\n", devlink); - } + if (sd_device_get_driver(dev, &str) >= 0) + fprintf(f, "%sDevice driver:%s\n %s\n", ansi_highlight(), ansi_normal(), str); - fprintf(f, "%sInotify watch:%s\n %s\n", ansi_highlight(), ansi_normal(), enabled_disabled(event->inotify_watch)); + if (sd_device_get_devname(dev, &str) >= 0) { + dev_t devnum; + if (sd_device_get_devnum(dev, &devnum) >= 0) + fprintf(f, "%sDevice node:%s\n %s (%s "DEVNUM_FORMAT_STR")\n", ansi_highlight(), ansi_normal(), str, + streq_ptr(subsys, "block") ? "block" : "char", + DEVNUM_FORMAT_VAL(devnum)); uid_t uid = event->uid; if (!uid_is_valid(uid)) (void) device_get_devnode_uid(dev, &uid); if (uid_is_valid(uid)) { _cleanup_free_ char *user = uid_to_name(uid); - fprintf(f, "%sDevice node owner:%s\n %s (uid="UID_FMT")\n", ansi_highlight(), ansi_normal(), strna(user), uid); + fprintf(f, "%sDevice node owner user:%s\n %s (uid="UID_FMT")\n", ansi_highlight(), ansi_normal(), strna(user), uid); } gid_t gid = event->gid; @@ -85,7 +83,7 @@ void dump_event(UdevEvent *event, FILE *f) { (void) device_get_devnode_gid(dev, &gid); if (gid_is_valid(gid)) { _cleanup_free_ char *group = gid_to_name(gid); - fprintf(f, "%sDevice node group:%s\n %s (gid="GID_FMT")\n", ansi_highlight(), ansi_normal(), strna(group), gid); + fprintf(f, "%sDevice node owner group:%s\n %s (gid="GID_FMT")\n", ansi_highlight(), ansi_normal(), strna(group), gid); } mode_t mode = event->mode; @@ -94,26 +92,69 @@ void dump_event(UdevEvent *event, FILE *f) { if (mode != MODE_INVALID) fprintf(f, "%sDevice node permission:%s\n %04o\n", ansi_highlight(), ansi_normal(), mode); + if (sd_device_get_devlink_first(dev)) { + int prio = 0; + (void) device_get_devlink_priority(dev, &prio); + fprintf(f, "%sDevice node symlinks:%s (priority=%i)\n", ansi_highlight(), ansi_normal(), prio); + FOREACH_DEVICE_DEVLINK(dev, devlink) + fprintf(f, " %s\n", devlink); + } + if (!ordered_hashmap_isempty(event->seclabel_list)) { const char *name, *label; fprintf(f, "%sDevice node security label:%s\n", ansi_highlight(), ansi_normal()); ORDERED_HASHMAP_FOREACH_KEY(label, name, event->seclabel_list) fprintf(f, " %s : %s\n", name, label); } + + fprintf(f, "%sInotify watch:%s\n %s\n", ansi_highlight(), ansi_normal(), enabled_disabled(event->inotify_watch)); } - if (sd_device_get_ifindex(dev, NULL) >= 0) { + int ifindex; + if (sd_device_get_ifindex(dev, &ifindex) >= 0) { + fprintf(f, "%sNetwork interface index:%s\n %i\n", ansi_highlight(), ansi_normal(), ifindex); + if (!isempty(event->name)) fprintf(f, "%sNetwork interface name:%s\n %s\n", ansi_highlight(), ansi_normal(), event->name); if (!strv_isempty(event->altnames)) { bool space = true; fprintf(f, "%sAlternative interface names:%s", ansi_highlight(), ansi_normal()); - fputstrv(f, event->altnames, "\n ", &space); + fputstrv(f, strv_sort(event->altnames), "\n ", &space); fputs("\n", f); } } + if (!hashmap_isempty(event->written_sysattrs)) { + const char *key, *value; + + fprintf(f, "%sWritten sysfs attributes:%s\n", ansi_highlight(), ansi_normal()); + HASHMAP_FOREACH_KEY(value, key, event->written_sysattrs) + fprintf(f, " %s : %s\n", key, value); + } + + if (!hashmap_isempty(event->written_sysctls)) { + const char *key, *value; + + fprintf(f, "%sWritten sysctl entries:%s\n", ansi_highlight(), ansi_normal()); + HASHMAP_FOREACH_KEY(value, key, event->written_sysctls) + fprintf(f, " %s : %s\n", key, value); + } + + if (sd_device_get_tag_first(dev)) { + fprintf(f, "%sTags:%s\n", ansi_highlight(), ansi_normal()); + FOREACH_DEVICE_TAG(dev, tag) + fprintf(f, " %s\n", tag); + } + + char **properties; + if (device_get_properties_strv(dev, &properties) >= 0 && !strv_isempty(properties)) { + bool space = true; + fprintf(f, "%sProperties:%s", ansi_highlight(), ansi_normal()); + fputstrv(f, strv_sort(properties), "\n ", &space); + fputs("\n", f); + } + if (!ordered_hashmap_isempty(event->run_list)) { void *val; const char *command; -- 2.47.3