HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/udev/udev-rules.c

146,142 bytesGit mode 100644Download original fileGPL-2.0-or-later

Git blob: c7443a98981e096d10358ad4f663909ffd0ee64a

Lines 3001–3362 of 3,362

 
                        if (token->op == OP_REMOVE) {
                                r = device_remove_devlink(dev, path);
                                if (r == -ENOMEM)
                                        return log_oom();
                                if (r < 0)
                                        log_event_warning_errno(event, token, r, "Failed to remove device node symlink \"%s\", ignoring: %m", path);
                                else if (r > 0)
                                        log_event_debug(event, token, "Removed device node symlink \"%s\"", path);
                        } else {
                                r = device_add_devlink(dev, path);
                                if (r == -ENOMEM)
                                        return log_oom();
                                if (r < 0)
                                        log_event_warning_errno(event, token, r, "Failed to add device node symlink \"%s\", ignoring: %m", path);
                                else if (r > 0)
                                        log_event_debug(event, token, "Added device node symlink \"%s\".", path);
                        }
                }
                assert_not_reached();
        }
        case TK_A_ATTR: {
                char buf[UDEV_PATH_SIZE], value[UDEV_NAME_SIZE];
                const char *key = token->data;
 
                /* First, try to resolve "[<SUBSYSTEM>/<KERNEL>]<attribute>" format. */
                r = udev_resolve_subsys_kernel(key, buf, sizeof(buf), false);
                if (r == -ENOMEM)
                        return log_oom();
                if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) {
                        log_event_warning_errno(event, token, r, "Failed to resolve sysfs attribute \"%s\", ignoring: %m", key);
                        return true;
                }
                if (r < 0) {
                        /* If not, make the path to sysfs attribute absolute, to make '*' resolvable by attr_subst_subdir(). */
                        const char *syspath;
                        r = sd_device_get_syspath(dev, &syspath);
                        if (r < 0)
                                return log_event_error_errno(event, token, r, "Failed to get syspath: %m");
 
                        bool truncated;
                        strscpyl_full(buf, sizeof(buf), &truncated, syspath, "/", key, NULL);
                        if (truncated) {
                                log_event_warning(event, token,
                                                  "The path to the attribute \"%s/%s\" is too long, refusing to set the attribute.",
                                                  syspath, key);
                                return true;
                        }
 
                        /* Resolve '*' in the path. */
                        r = attr_subst_subdir(buf);
                        if (r < 0) {
                                log_event_error_errno(event, token, r, "Could not find file matches \"%s\", ignoring: %m", buf);
                                return true;
                        }
                }
 
                /* Then, make the path relative again. This also checks if the path being inside of the sysfs. */
                _cleanup_free_ char *resolved = NULL;
                _cleanup_close_ int fd = -EBADF;
                r = device_chase(dev, buf, /* flags= */ 0, &resolved, &fd);
                if (r < 0) {
                        log_event_error_errno(event, token, r, "Could not chase sysfs attribute \"%s\", ignoring: %m", buf);
                        return true;
                }
 
                /* Apply formatting to the value. */
                if (!apply_format_value(event, token, value, sizeof(value), "attribute value"))
                        return true;
 
                if (EVENT_MODE_DESTRUCTIVE(event)) {
                        log_event_debug(event, token, "Writing \"%s\" to sysfs attribute \"%s\".", value, resolved);
                        r = sd_device_set_sysattr_value(dev, resolved, value);
                        if (r < 0)
                                log_event_error_errno(event, token, r, "Failed to write \"%s\" to sysfs attribute \"%s\", ignoring: %m", value, resolved);
                        else {
                                event_cache_written_sysattr(event, resolved, value);
                                log_event_done(event, token);
                        }
                } else {
                        log_event_debug(event, token, "Running in test mode, skipping writing \"%s\" to sysfs attribute \"%s\".", value, resolved);
 
                        /* We assume the attribute is writable if the path points to a regular file, and cache
                         * the value to make it shown by OPTIONS="dump" or obtained by later ATTR match token. */
                        r = fd_verify_regular(fd);
                        if (r < 0 && !ERRNO_IS_NEG_PRIVILEGE(r))
                                log_event_error_errno(event, token, r, "Failed to verify sysfs attribute \"%s\" is a regular file: %m", resolved);
                        else {
                                event_cache_written_sysattr(event, resolved, value);
 
                                _cleanup_free_ char *copied = strdup(value);
                                if (!copied)
                                        return log_oom();
 
                                r = device_cache_sysattr_value(dev, resolved, copied, /* error= */ 0);
                                if (r < 0)
                                        log_event_warning_errno(event, token, r, "Failed to cache sysfs attribute \"%s\", ignoring: %m", resolved);
                                else if (r > 0) {
                                        TAKE_PTR(resolved);
                                        TAKE_PTR(copied);
                                }
                        }
                }
                return true;
        }
        case TK_A_SYSCTL: {
                char buf[UDEV_PATH_SIZE], value[UDEV_NAME_SIZE];
 
                if (!apply_format_attr(event, token, buf, sizeof(buf), "sysctl entry name"))
                        return true;
 
                if (!apply_format_value(event, token, value, sizeof(value), "sysctl value"))
                        return true;
 
                sysctl_normalize(buf);
 
                if (EVENT_MODE_DESTRUCTIVE(event)) {
                        log_event_debug(event, token, "Writing \"%s\" to sysctl entry \"%s\".", value, buf);
                        r = sysctl_write(buf, value);
                        if (r < 0)
                                log_event_error_errno(event, token, r, "Failed to write \"%s\" to sysctl entry \"%s\", ignoring: %m", value, buf);
                        else {
                                event_cache_written_sysctl(event, buf, value);
                                log_event_done(event, token);
                        }
                } else {
                        log_event_debug(event, token, "Running in test mode, skipping writing \"%s\" to sysctl entry \"%s\".", value, buf);
 
                        _cleanup_free_ char *path = path_join("/proc/sys/", buf);
                        if (!path)
                                return log_oom();
 
                        r = verify_regular_at(AT_FDCWD, path, /* follow= */ true);
                        if (r < 0 && !ERRNO_IS_NEG_PRIVILEGE(r))
                                log_event_error_errno(event, token, r, "Failed to verify sysctl entry \"%s\" is a regular file: %m", buf);
                        else
                                event_cache_written_sysctl(event, buf, value);
                }
                return true;
        }
        case TK_A_RUN_BUILTIN:
        case TK_A_RUN_PROGRAM: {
                _cleanup_free_ char *cmd = NULL;
                char buf[UDEV_LINE_SIZE];
 
                if (event->run_final)
                        return log_event_final_set(event, token);
 
                if (token->op == OP_ASSIGN_FINAL)
                        event->run_final = true;
 
                if (IN_SET(token->op, OP_ASSIGN, OP_ASSIGN_FINAL))
                        ordered_hashmap_clear(event->run_list);
 
                if (!apply_format_value(event, token, buf, sizeof(buf), "command"))
                        return true;
 
                cmd = strdup(buf);
                if (!cmd)
                        return log_oom();
 
                r = ordered_hashmap_ensure_put(&event->run_list, &trivial_hash_ops_free, cmd, token->data);
                if (r < 0)
                        return log_event_error_errno(event, token, r, "Failed to store command \"%s\": %m", cmd);
 
                log_event_debug(event, token, "Set command: %s", cmd);
                TAKE_PTR(cmd);
                return true;
        }
        case TK_A_OPTIONS_STATIC_NODE:
                /* do nothing for events. */
                return true;
 
        default:
                assert_not_reached();
        }
 
        assert_not_reached();
}
 
static int udev_rule_apply_parent_token_to_event(UdevRuleToken *head_token, UdevEvent *event) {
        int r;
 
        assert(head_token);
        assert(token_is_for_parents(head_token));
        assert(event);
 
        UdevRuleLine *line = head_token->rule_line;
        if (event->trace) {
                _cleanup_free_ char *joined = NULL;
 
                LIST_FOREACH(tokens, token, head_token)
                        if (token_is_for_parents(token))
                                (void) strextend_with_separator(&joined, ", ", token->token_str);
                        else
                                break;
 
                log_event_line(event, line, "Checking conditions for parent devices (including self): %s", strna(joined));
        }
 
        event->dev_parent = ASSERT_PTR(event->dev);
 
        for (;;) {
                LIST_FOREACH(tokens, token, head_token) {
                        if (!token_is_for_parents(token)) {
                                r = 1; /* avoid false maybe-uninitialized warning */
                                break; /* All parent tokens match. */
                        }
 
                        r = udev_rule_apply_token_to_event(token, event->dev_parent, event);
                        if (r < 0)
                                return r;
                        if (r == 0)
                                break;
                }
                if (r > 0) {
                        if (event->trace) {
                                const char *s = NULL;
                                (void) sd_device_get_syspath(event->dev_parent, &s);
                                log_event_line(event, line, "Parent device \"%s\" passed all parent conditions.", strna(s));
                        }
                        return true;
                }
 
                if (sd_device_get_parent(event->dev_parent, &event->dev_parent) < 0) {
                        event->dev_parent = NULL;
                        log_event_line(event, line, "No parent device passed parent conditions.");
                        return false;
                }
        }
}
 
static int udev_rule_apply_line_to_event(
                UdevRuleLine *line,
                UdevEvent *event,
                UdevRuleLine **next_line) {
 
        UdevRuleLineType mask = LINE_HAS_GOTO | LINE_UPDATE_SOMETHING;
        bool parents_done = false;
        sd_device_action_t action;
        int r;
 
        assert(line);
        assert(event);
        assert(next_line);
 
        r = sd_device_get_action(event->dev, &action);
        if (r < 0)
                return r;
 
        if (action != SD_DEVICE_REMOVE) {
                if (sd_device_get_devnum(event->dev, NULL) >= 0)
                        mask |= LINE_HAS_DEVLINK;
 
                if (sd_device_get_ifindex(event->dev, NULL) >= 0)
                        mask |= LINE_HAS_NAME;
        }
 
        if ((line->type & mask) == 0)
                return 0;
 
        event->esc = ESCAPE_UNSET;
 
        DEVICE_TRACE_POINT(rules_apply_line, event->dev, line->rule_file->filename, line->line_number);
 
        LIST_FOREACH(tokens, token, line->tokens) {
                if (token_is_for_parents(token)) {
                        if (parents_done)
                                continue;
 
                        r = udev_rule_apply_parent_token_to_event(token, event);
                        if (r <= 0)
                                return r;
 
                        parents_done = true;
                        continue;
                }
 
                r = udev_rule_apply_token_to_event(token, event->dev, event);
                if (r <= 0)
                        return r;
        }
 
        if (line->goto_line) {
                log_event_line(event, line, "GOTO=%s", strna(line->goto_label));
                *next_line = line->goto_line; /* update next_line only when the line has GOTO token. */
        }
 
        return 0;
}
 
int udev_rules_apply_to_event(UdevRules *rules, UdevEvent *event) {
        int r;
 
        assert(rules);
        assert(event);
 
        LIST_FOREACH(rule_files, file, rules->rule_files)
                LIST_FOREACH_WITH_NEXT(rule_lines, line, next_line, file->rule_lines) {
                        r = udev_rule_apply_line_to_event(line, event, &next_line);
                        if (r < 0)
                                return r;
                }
 
        return 0;
}
 
static int udev_rule_line_apply_static_dev_perms(UdevRuleLine *rule_line) {
        _cleanup_strv_free_ char **tags = NULL;
        uid_t uid = UID_INVALID;
        gid_t gid = GID_INVALID;
        mode_t mode = MODE_INVALID;
        int r;
 
        assert(rule_line);
 
        if (!FLAGS_SET(rule_line->type, LINE_HAS_STATIC_NODE))
                return 0;
 
        LIST_FOREACH(tokens, token, rule_line->tokens)
                if (token->type == TK_A_OWNER_ID)
                        uid = PTR_TO_UID(token->data);
                else if (token->type == TK_A_GROUP_ID)
                        gid = PTR_TO_GID(token->data);
                else if (token->type == TK_A_MODE_ID)
                        mode = PTR_TO_MODE(token->data);
                else if (token->type == TK_A_TAG) {
                        r = strv_extend(&tags, token->value);
                        if (r < 0)
                                return log_oom();
                } else if (token->type == TK_A_OPTIONS_STATIC_NODE) {
                        r = static_node_apply_permissions(token->value, mode, uid, gid, tags);
                        if (r < 0)
                                return r;
                }
 
        return 0;
}
 
int udev_rules_apply_static_dev_perms(UdevRules *rules) {
        int r;
 
        assert(rules);
 
        LIST_FOREACH(rule_files, file, rules->rule_files)
                LIST_FOREACH(rule_lines, line, file->rule_lines) {
                        r = udev_rule_line_apply_static_dev_perms(line);
                        if (r < 0)
                                return r;
                }
 
        return 0;
}
 
static const char* const resolve_name_timing_table[_RESOLVE_NAME_TIMING_MAX] = {
        [RESOLVE_NAME_NEVER] = "never",
        [RESOLVE_NAME_LATE]  = "late",
        [RESOLVE_NAME_EARLY] = "early",
};
 
DEFINE_STRING_TABLE_LOOKUP(resolve_name_timing, ResolveNameTiming);