Lines Matching defs:rules

58         "/etc/udev/rules.d",
59 "/run/udev/rules.d",
60 UDEVLIBEXECDIR "/rules.d",
69 /* every key in the rules file becomes a token */
86 static char *rules_str(struct udev_rules *rules, unsigned int off) {
87 return rules->strbuf->buf + off;
90 static unsigned int rules_add_string(struct udev_rules *rules, const char *s) {
91 return strbuf_add_string(rules->strbuf, s, strlen(s));
224 struct udev_rules *rules;
326 static void dump_token(struct udev_rules *rules, struct token *token) {
330 const char *value = str(rules, token->key.value_off);
331 const char *attr = &rules->buf[token->key.attr_off];
336 const char *tks_ptr = (char *)rules->tokens;
341 &rules->buf[token->rule.filename_off], token->rule.filename_line,
343 &rules->buf[token->rule.label_off]);
437 static void dump_rules(struct udev_rules *rules) {
441 rules->token_cur,
442 rules->token_cur * sizeof(struct token),
443 rules->buf_count,
444 rules->buf_cur);
445 for (i = 0; i < rules->token_cur; i++)
446 dump_token(rules, &rules->tokens[i]);
449 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
450 static inline void dump_rules(struct udev_rules *rules) {}
453 static int add_token(struct udev_rules *rules, struct token *token) {
455 if (rules->token_cur+1 >= rules->token_max) {
460 add = rules->token_max;
464 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
467 rules->tokens = tokens;
468 rules->token_max += add;
470 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
471 rules->token_cur++;
475 static uid_t add_uid(struct udev_rules *rules, const char *owner) {
482 for (i = 0; i < rules->uids_cur; i++) {
483 off = rules->uids[i].name_off;
484 if (streq(rules_str(rules, off), owner)) {
485 uid = rules->uids[i].uid;
498 if (rules->uids_cur+1 >= rules->uids_max) {
503 add = rules->uids_max;
507 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
510 rules->uids = uids;
511 rules->uids_max += add;
513 rules->uids[rules->uids_cur].uid = uid;
514 off = rules_add_string(rules, owner);
517 rules->uids[rules->uids_cur].name_off = off;
518 rules->uids_cur++;
522 static gid_t add_gid(struct udev_rules *rules, const char *group) {
529 for (i = 0; i < rules->gids_cur; i++) {
530 off = rules->gids[i].name_off;
531 if (streq(rules_str(rules, off), group)) {
532 gid = rules->gids[i].gid;
545 if (rules->gids_cur+1 >= rules->gids_max) {
550 add = rules->gids_max;
554 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
557 rules->gids = gids;
558 rules->gids_max += add;
560 rules->gids[rules->gids_cur].gid = gid;
561 off = rules_add_string(rules, group);
564 rules->gids[rules->gids_cur].name_off = off;
565 rules->gids_cur++;
871 token->key.value_off = rules_add_string(rule_tmp->rules, value);
874 token->key.value_off = rules_add_string(rule_tmp->rules, value);
886 token->key.value_off = rules_add_string(rule_tmp->rules, value);
887 token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
890 token->key.value_off = rules_add_string(rule_tmp->rules, value);
901 token->key.value_off = rules_add_string(rule_tmp->rules, value);
927 /* check if we need to split or call fnmatch() while matching rules */
979 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp) {
1000 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1013 static int add_rule(struct udev_rules *rules, char *line,
1018 .rules = rules,
1033 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
1110 attr = get_key_attribute(rules->udev, key + strlen("ATTR"));
1127 attr = get_key_attribute(rules->udev, key + strlen("SYSCTL"));
1144 attr = get_key_attribute(rules->udev, key + strlen("SECLABEL"));
1190 attr = get_key_attribute(rules->udev, key + strlen("ATTRS"));
1215 attr = get_key_attribute(rules->udev, key + strlen("ENV"));
1282 attr = get_key_attribute(rules->udev, key + strlen("IMPORT"));
1332 attr = get_key_attribute(rules->udev, key + strlen("TEST"));
1343 attr = get_key_attribute(rules->udev, key + strlen("RUN"));
1374 rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
1436 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1437 uid = add_uid(rules, value);
1439 } else if (rules->resolve_names >= 0)
1458 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1459 gid = add_gid(rules, value);
1461 } else if (rules->resolve_names >= 0)
1543 if (add_token(rules, &rule_tmp.rule) != 0)
1547 if (sort_token(rules, &rule_tmp) != 0)
1556 static int parse_file(struct udev_rules *rules, const char *filename) {
1576 log_debug("Reading rules file: %s", filename);
1578 first_token = rules->token_cur;
1579 filename_off = rules_add_string(rules, filename);
1613 add_rule(rules, key, filename, filename_off, line_nr);
1616 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1617 for (i = first_token+1; i < rules->token_cur; i++) {
1618 if (rules->tokens[i].type == TK_A_GOTO) {
1619 char *label = rules_str(rules, rules->tokens[i].key.value_off);
1622 for (j = i+1; j < rules->token_cur; j++) {
1623 if (rules->tokens[j].type != TK_RULE)
1625 if (rules->tokens[j].rule.label_off == 0)
1627 if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
1629 rules->tokens[i].key.rule_goto = j;
1632 if (rules->tokens[i].key.rule_goto == 0)
1640 struct udev_rules *rules;
1646 rules = new0(struct udev_rules, 1);
1647 if (rules == NULL)
1649 rules->udev = udev;
1650 rules->resolve_names = resolve_names;
1654 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1655 if (rules->tokens == NULL)
1656 return udev_rules_unref(rules);
1657 rules->token_max = PREALLOC_TOKEN;
1659 rules->strbuf = strbuf_new();
1660 if (!rules->strbuf)
1661 return udev_rules_unref(rules);
1663 udev_rules_check_timestamp(rules);
1665 r = conf_files_list_strv(&files, ".rules", NULL, rules_dirs);
1667 log_error_errno(r, "failed to enumerate rules files: %m");
1668 return udev_rules_unref(rules);
1672 * The offset value in the rules strct is limited; add all
1673 * rules file names to the beginning of the string buffer.
1676 rules_add_string(rules, *f);
1679 parse_file(rules, *f);
1685 add_token(rules, &end_token);
1686 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1687 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
1691 rules->strbuf->in_count, rules->strbuf->in_len,
1692 rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count);
1693 strbuf_complete(rules->strbuf);
1696 rules->uids = mfree(rules->uids);
1697 rules->uids_cur = 0;
1698 rules->uids_max = 0;
1699 rules->gids = mfree(rules->gids);
1700 rules->gids_cur = 0;
1701 rules->gids_max = 0;
1703 dump_rules(rules);
1704 return rules;
1707 struct udev_rules *udev_rules_unref(struct udev_rules *rules) {
1708 if (rules == NULL)
1710 free(rules->tokens);
1711 strbuf_cleanup(rules->strbuf);
1712 free(rules->uids);
1713 free(rules->gids);
1714 free(rules);
1718 bool udev_rules_check_timestamp(struct udev_rules *rules) {
1719 if (!rules)
1722 return paths_check_timestamp(rules_dirs, &rules->dirs_ts_usec, true);
1725 static int match_key(struct udev_rules *rules, struct token *token, const char *val) {
1726 char *key_value = rules_str(rules, token->key.value_off);
1745 s = rules_str(rules, token->key.value_off);
1769 strscpy(value, sizeof(value), rules_str(rules, token->key.value_off));
1798 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur) {
1805 name = rules_str(rules, cur->key.attr_off);
1831 key_value = rules_str(rules, cur->key.value_off);
1843 return match_key(rules, cur, value);
1852 int udev_rules_apply_to_event(struct udev_rules *rules,
1862 if (rules->tokens == NULL)
1870 cur = &rules->tokens[0];
1873 dump_token(rules, cur);
1878 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1884 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
1888 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
1892 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
1903 if (match_key(rules, cur, devlink) == 0) {
1913 if (match_key(rules, cur, event->name) != 0)
1917 const char *key_name = rules_str(rules, cur->key.attr_off);
1934 if (match_key(rules, cur, value))
1943 if (streq(rules_str(rules, cur->key.value_off), udev_list_entry_get_name(list_entry))) {
1954 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
1958 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
1962 if (match_attr(rules, event->dev, event, cur) != 0)
1970 udev_event_apply_format(event, rules_str(rules, cur->key.attr_off), filename, sizeof(filename));
1978 if (match_key(rules, cur, value) != 0)
2001 dump_token(rules, key);
2004 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
2008 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
2012 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
2016 if (match_attr(rules, event->dev_parent, event, key) != 0)
2020 bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
2048 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
2074 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program));
2077 rules_str(rules, rule->rule.filename_off),
2101 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2110 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2113 rules_str(rules, rule->rule.filename_off),
2129 rules_str(rules, rule->rule.filename_off),
2141 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command));
2144 rules_str(rules, rule->rule.filename_off),
2158 const char *key = rules_str(rules, cur->key.value_off);
2179 const char *key = rules_str(rules, cur->key.value_off);
2211 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
2218 if (match_key(rules, cur, event->program_result) != 0)
2249 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner));
2262 rules_str(rules, rule->rule.filename_off),
2275 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group));
2288 rules_str(rules, rule->rule.filename_off),
2299 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str));
2311 rules_str(rules, rule->rule.filename_off),
2324 rules_str(rules, rule->rule.filename_off),
2336 rules_str(rules, rule->rule.filename_off),
2348 rules_str(rules, rule->rule.filename_off),
2354 name = rules_str(rules, cur->key.attr_off);
2355 label = rules_str(rules, cur->key.value_off);
2361 rules_str(rules, rule->rule.filename_off),
2366 const char *name = rules_str(rules, cur->key.attr_off);
2367 char *value = rules_str(rules, cur->key.value_off);
2396 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag));
2415 const char *name = rules_str(rules, cur->key.value_off);
2434 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2440 rules_str(rules, rule->rule.filename_off),
2460 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp));
2474 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2484 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2491 const char *key_name = rules_str(rules, cur->key.attr_off);
2500 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
2502 rules_str(rules, rule->rule.filename_off),
2519 udev_event_apply_format(event, rules_str(rules, cur->key.attr_off), filename, sizeof(filename));
2521 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
2523 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2536 rules_str(rules, cur->key.value_off),
2537 rules_str(rules, rule->rule.filename_off),
2539 entry = udev_list_entry_add(&event->run_list, rules_str(rules, cur->key.value_off), NULL);
2546 cur = &rules->tokens[cur->key.rule_goto];
2567 int udev_rules_apply_static_dev_perms(struct udev_rules *rules) {
2579 if (rules->tokens == NULL)
2582 cur = &rules->tokens[0];
2590 /* skip rules without a static_node tag */
2609 r = strv_extend(&tags, rules_str(rules, cur->key.value_off));
2625 strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL);
2641 unescaped_filename = xescape(rules_str(rules, cur->key.value_off), "/.");