HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/udev/udevadm-info.c

46,355 bytesGit mode 100644Download original fileGPL-2.0-or-later

Git blob: a4fd2ccd7ee1eeb159420433458b5b59826999b0

Lines 1001–1265 of 1,265

                           properties. */
                        if (isempty(opts.arg) && !arg_properties) {
                                arg_properties = new0(char*, 1);
                                if (!arg_properties)
                                        return log_oom();
                        } else {
                                r = strv_split_and_extend(&arg_properties, opts.arg, ",", true);
                                if (r < 0)
                                        return log_oom();
                        }
                        break;
 
                OPTION_LONG("value", NULL, "Print only values"):
                        arg_value = true;
                        break;
 
                OPTION('p', "path", "SYSPATH", "sysfs device path used for query or attribute walk"): {} /* fall through */
                OPTION('n', "name", "NAME", "Node or symlink name used for query or attribute walk"): {
                        const char *prefix = opts.opt->short_code == 'n' ? "/dev/" : "/sys/";
                        char *path;
 
                        path = path_join(path_startswith(opts.arg, prefix) ? NULL : prefix, opts.arg);
                        if (!path)
                                return log_oom();
 
                        r = strv_consume(&arg_devices, path);
                        if (r < 0)
                                return log_oom();
                        break;
                }
 
                OPTION('r', "root", NULL, "Prepend dev directory to path names"):
                        arg_root = true;
                        break;
 
                OPTION('a', "attribute-walk", NULL,
                       "Print all key matches walking along the chain of parent devices"):
                        arg_action_type = ACTION_ATTRIBUTE_WALK;
                        break;
 
                OPTION('t', "tree", NULL, "Show tree of devices"):
                        arg_action_type = ACTION_TREE;
                        break;
 
                OPTION('d', "device-id-of-file", "FILE",
                       "Print major:minor of device containing this file"):
                        arg_action_type = ACTION_DEVICE_ID_FILE;
                        r = free_and_strdup(&arg_name, opts.arg);
                        if (r < 0)
                                return log_oom();
                        break;
 
                OPTION('x', "export", NULL, "Export key/value pairs"):
                        arg_export = true;
                        break;
 
                OPTION('P', "export-prefix", "NAME", "Export the key name with a prefix"):
                        arg_export = true;
                        arg_export_prefix = opts.arg;
                        break;
 
                OPTION('e', "export-db", NULL, "Export the content of the udev database"):
                        arg_action_type = ACTION_EXPORT;
                        break;
 
                OPTION('c', "cleanup-db", NULL, "Clean up the udev database"):
                        arg_action_type = ACTION_CLEANUP_DB;
                        break;
 
                OPTION_FULL(OPTION_OPTIONAL_ARG, 'w', "wait-for-initialization", "SECS",
                            "Wait for device to be initialized"):
                        if (opts.arg) {
                                r = parse_sec(opts.arg, &arg_wait_for_initialization_timeout);
                                if (r < 0)
                                        return log_error_errno(r, "Failed to parse timeout value: %m");
                        } else
                                arg_wait_for_initialization_timeout = USEC_INFINITY;
                        break;
 
                OPTION_COMMON_NO_PAGER:
                        arg_pager_flags |= PAGER_DISABLE;
                        break;
 
                OPTION_COMMON_JSON:
                        r = parse_json_argument(opts.arg, &arg_json_format_flags);
                        if (r <= 0)
                                return r;
                        break;
 
                OPTION_LONG("subsystem-match", "SUBSYSTEM",
                            "Query devices matching a subsystem"):
                        r = strv_extend(&arg_subsystem_match, opts.arg);
                        if (r < 0)
                                return log_oom();
                        break;
 
                OPTION_LONG("subsystem-nomatch", "SUBSYSTEM",
                            "Query devices not matching a subsystem"):
                        r = strv_extend(&arg_subsystem_nomatch, opts.arg);
                        if (r < 0)
                                return log_oom();
                        break;
 
                OPTION_LONG("attr-match", "FILE[=VALUE]",
                            "Query devices that match an attribute"):
                        r = strv_extend(&arg_attr_match, opts.arg);
                        if (r < 0)
                                return log_oom();
                        break;
 
                OPTION_LONG("attr-nomatch", "FILE[=VALUE]",
                            "Query devices that do not match an attribute"):
                        r = strv_extend(&arg_attr_nomatch, opts.arg);
                        if (r < 0)
                                return log_oom();
                        break;
 
                OPTION_LONG("property-match", "KEY=VALUE",
                            "Query devices with matching properties"):
                        if (!strchr(opts.arg, '='))
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                "Expected <PROPERTY>=<value> instead of '%s'", opts.arg);
 
                        r = strv_extend(&arg_property_match, opts.arg);
                        if (r < 0)
                                return log_oom();
                        break;
 
                OPTION_LONG("tag-match", "TAG", "Query devices with a matching tag"):
                        r = strv_extend(&arg_tag_match, opts.arg);
                        if (r < 0)
                                return log_oom();
                        break;
 
                OPTION_LONG("sysname-match", "NAME", "Query devices with this /sys path"):
                        r = strv_extend(&arg_sysname_match, opts.arg);
                        if (r < 0)
                                return log_oom();
                        break;
 
                OPTION_LONG("name-match", "NAME", "Query devices with this /dev name"):
                        r = strv_extend(&arg_name_match, opts.arg);
                        if (r < 0)
                                return log_oom();
                        break;
 
                OPTION_LONG("parent-match", "NAME", "Query devices with this parent device"):
                        r = strv_extend(&arg_parent_match, opts.arg);
                        if (r < 0)
                                return log_oom();
                        break;
 
                OPTION_LONG("initialized-match", NULL,
                            "Query devices that are already initialized"):
                        arg_initialized_match = MATCH_INITIALIZED_YES;
                        break;
 
                OPTION_LONG("initialized-nomatch", NULL,
                            "Query devices that are not initialized yet"):
                        arg_initialized_match = MATCH_INITIALIZED_NO;
                        break;
 
                OPTION_COMMON_INTROSPECT_CLI:
                        return introspect_cli(arg_json_format_flags);
                }
 
        r = strv_extend_strv(&arg_devices, option_parser_get_args(&opts), /* filter_duplicates= */ false);
        if (r < 0)
                return log_error_errno(r, "Failed to build argument list: %m");
 
        if (IN_SET(arg_action_type, ACTION_DEVICE_ID_FILE, ACTION_CLEANUP_DB) && !strv_isempty(arg_devices))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "Devices are not allowed with -d/--device-id-of-file and -c/--cleanup-db.");
 
        if (!IN_SET(arg_action_type, ACTION_DEVICE_ID_FILE, ACTION_CLEANUP_DB, ACTION_EXPORT, ACTION_TREE) &&
            strv_isempty(arg_devices))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "A device name or path is required");
 
        if (IN_SET(arg_action_type, ACTION_ATTRIBUTE_WALK, ACTION_TREE) && strv_length(arg_devices) > 1)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "Only one device may be specified with -a/--attribute-walk and -t/--tree");
 
        if (arg_export && arg_value)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "-x/--export or -P/--export-prefix cannot be used with --value");
 
        return 1;
}
 
int verb_info_main(int argc, char *argv[], uintptr_t _data, void *userdata) {
        int r;
 
        r = parse_argv(argc, argv);
        if (r <= 0)
                return r;
 
        if (arg_action_type == ACTION_CLEANUP_DB)
                return cleanup_db();
 
        if (arg_action_type == ACTION_DEVICE_ID_FILE)
                return stat_device();
 
        PagerFlags pager_flags = arg_pager_flags;
        if (arg_action_type == ACTION_QUERY && arg_query == QUERY_SYMLINK && !arg_value)
                pager_flags |= PAGER_DISABLE;
 
        pager_open(pager_flags);
 
        if (arg_action_type == ACTION_EXPORT)
                return export_devices();
 
        if (strv_isempty(arg_devices)) {
                assert(arg_action_type == ACTION_TREE);
                return print_tree(NULL);
        }
 
        int ret = 0;
        STRV_FOREACH(p, arg_devices) {
                _cleanup_(sd_device_unrefp) sd_device *device = NULL;
 
                r = find_device(*p, /* prefix= */ NULL, &device);
                if (r < 0) {
                        if (r == -EINVAL)
                                log_error_errno(r, "Bad argument \"%s\", expected an absolute path in /dev/ or /sys/, device ID, or a unit name: %m", *p);
                        else
                                log_error_errno(r, "Unknown device \"%s\": %m",  *p);
 
                        RET_GATHER(ret, r);
                        continue;
                }
 
                if (arg_wait_for_initialization_timeout > 0) {
                        sd_device *d;
 
                        r = device_wait_for_initialization(
                                        device,
                                        NULL,
                                        arg_wait_for_initialization_timeout,
                                        &d);
                        if (r < 0)
                                return r;
 
                        sd_device_unref(device);
                        device = d;
                }
 
                if (arg_action_type == ACTION_QUERY)
                        r = query_device(arg_query, device);
                else if (arg_action_type == ACTION_ATTRIBUTE_WALK) {
                        if (sd_json_format_enabled(arg_json_format_flags))
                                r = print_device_chain_in_json(device);
                        else
                                r = print_device_chain(device);
                } else if (arg_action_type == ACTION_TREE)
                        r = print_tree(device);
                else
                        assert_not_reached();
                if (r < 0)
                        return r;
        }
 
        return ret;
}