]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udevadm-test: prettify test results
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 29 Mar 2024 04:03:49 +0000 (13:03 +0900)
committerLuca Boccassi <luca.boccassi@gmail.com>
Fri, 29 Mar 2024 12:44:11 +0000 (12:44 +0000)
src/udev/udevadm-test.c

index e2adc866060a0697e3dc7599f765b614cf3ece8c..b575c0de83c490c58ad51922a4fc9d76399c7cdc 100644 (file)
 
 #include "device-private.h"
 #include "device-util.h"
+#include "format-util.h"
 #include "path-util.h"
 #include "string-util.h"
+#include "strv.h"
 #include "strxcpyx.h"
+#include "terminal-util.h"
 #include "udev-builtin.h"
 #include "udev-event.h"
 #include "udev-format.h"
 #include "udevadm-util.h"
 #include "udevadm.h"
+#include "user-util.h"
 
 static sd_device_action_t arg_action = SD_DEVICE_ADD;
 static ResolveNameTiming arg_resolve_name_timing = RESOLVE_NAME_EARLY;
@@ -89,9 +93,7 @@ int test_main(int argc, char *argv[], void *userdata) {
         _cleanup_(udev_rules_freep) UdevRules *rules = NULL;
         _cleanup_(udev_event_freep) UdevEvent *event = NULL;
         _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
-        const char *cmd;
         sigset_t mask, sigmask_orig;
-        void *val;
         int r;
 
         log_set_max_level(LOG_DEBUG);
@@ -132,17 +134,75 @@ int test_main(int argc, char *argv[], void *userdata) {
 
         udev_event_execute_rules(event, rules);
 
+        printf("%sProperties:%s\n", ansi_highlight(), ansi_normal());
         FOREACH_DEVICE_PROPERTY(dev, key, value)
-                printf("%s=%s\n", key, value);
+                printf("  %s=%s\n", key, value);
 
-        ORDERED_HASHMAP_FOREACH_KEY(val, cmd, event->run_list) {
-                char program[UDEV_PATH_SIZE];
-                bool truncated;
+        if (sd_device_get_tag_first(dev)) {
+                printf("%sTags:%s\n", ansi_highlight(), ansi_normal());
+                FOREACH_DEVICE_TAG(dev, tag)
+                        printf("  %s\n", tag);
+        }
+
+        if (sd_device_get_devnum(dev, NULL) >= 0) {
+
+                if (sd_device_get_devlink_first(dev)) {
+                        int prio;
+                        device_get_devlink_priority(dev, &prio);
+                        printf("%sDevice node symlinks:%s (priority=%i)\n", ansi_highlight(), ansi_normal(), prio);
+                        FOREACH_DEVICE_DEVLINK(dev, devlink)
+                                printf("  %s\n", devlink);
+                }
+
+                printf("%sInotify watch:%s\n  %s\n", ansi_highlight(), ansi_normal(), enabled_disabled(event->inotify_watch));
+
+                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);
+                        printf("%sDevice node owner:%s\n  %s (uid="UID_FMT")\n", ansi_highlight(), ansi_normal(), strna(user), uid);
+                }
+
+                gid_t gid = event->gid;
+                if (!gid_is_valid(uid))
+                        (void) device_get_devnode_gid(dev, &gid);
+                if (gid_is_valid(gid)) {
+                        _cleanup_free_ char *group = gid_to_name(gid);
+                        printf("%sDevice node group:%s\n  %s (gid="GID_FMT")\n", ansi_highlight(), ansi_normal(), strna(group), gid);
+                }
+
+                mode_t mode = event->mode;
+                if (mode == MODE_INVALID)
+                        (void) device_get_devnode_mode(dev, &mode);
+                if (mode != MODE_INVALID)
+                        printf("%sDevice node permission:%s\n  %04o\n", ansi_highlight(), ansi_normal(), mode);
+        }
+
+        if (sd_device_get_ifindex(dev, NULL) >= 0) {
+                if (!isempty(event->name))
+                        printf("%sNetwork interface name:%s  %s\n", ansi_highlight(), ansi_normal(), event->name);
 
-                (void) udev_event_apply_format(event, cmd, program, sizeof(program), false, &truncated);
-                if (truncated)
-                        log_warning("The command '%s' is truncated while substituting into '%s'.", program, cmd);
-                printf("run: '%s'\n", program);
+                if (!strv_isempty(event->altnames)) {
+                        bool space = true;
+                        printf("%sAlternative interface names:%s", ansi_highlight(), ansi_normal());
+                        fputstrv(stdout, event->altnames, "\n  ", &space);
+                        puts("");
+                }
+        }
+
+        if (!ordered_hashmap_isempty(event->run_list)) {
+                void *val;
+                const char *command;
+                printf("%sQueued commands:%s\n", ansi_highlight(), ansi_normal());
+                ORDERED_HASHMAP_FOREACH_KEY(val, command, event->run_list) {
+                        UdevBuiltinCommand builtin_cmd = PTR_TO_UDEV_BUILTIN_CMD(val);
+
+                        if (builtin_cmd != _UDEV_BUILTIN_INVALID)
+                                printf("  RUN{builtin} : %s\n", command);
+                        else
+                                printf("  RUN{program} : %s\n", command);
+                }
         }
 
         r = 0;