udev-rules.c revision 8e18485284a8718d4a9111b7c1ed91cb2f9634b0
3988N/A/*
3988N/A * Copyright (C) 2003-2012 Kay Sievers <kay@vrfy.org>
3988N/A *
3988N/A * This program is free software: you can redistribute it and/or modify
3988N/A * it under the terms of the GNU General Public License as published by
3988N/A * the Free Software Foundation, either version 2 of the License, or
3988N/A * (at your option) any later version.
3988N/A *
3988N/A * This program is distributed in the hope that it will be useful,
3988N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
3988N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3988N/A * GNU General Public License for more details.
3988N/A *
3988N/A * You should have received a copy of the GNU General Public License
3988N/A * along with this program. If not, see <http://www.gnu.org/licenses/>.
3988N/A */
3988N/A
3988N/A#include <stddef.h>
3988N/A#include <limits.h>
3988N/A#include <stdlib.h>
3988N/A#include <stdbool.h>
3988N/A#include <string.h>
3988N/A#include <stdio.h>
3988N/A#include <fcntl.h>
4988N/A#include <ctype.h>
6300N/A#include <unistd.h>
3988N/A#include <errno.h>
3988N/A#include <dirent.h>
3988N/A#include <fnmatch.h>
3988N/A#include <time.h>
3988N/A
3988N/A#include "udev.h"
3988N/A#include "path-util.h"
4011N/A#include "conf-files.h"
6345N/A#include "strbuf.h"
6153N/A#include "strv.h"
6756N/A#include "util.h"
6756N/A
3988N/A#define PREALLOC_TOKEN 2048
3988N/A
3988N/Astruct uid_gid {
3988N/A unsigned int name_off;
3988N/A union {
3988N/A uid_t uid;
3988N/A gid_t gid;
6153N/A };
3988N/A};
6153N/A
6153N/Astruct udev_rules {
3988N/A struct udev *udev;
6339N/A char **dirs;
6339N/A usec_t *dirs_ts_usec;
6339N/A int resolve_names;
6153N/A
4011N/A /* every key in the rules file becomes a token */
3988N/A struct token *tokens;
6153N/A unsigned int token_cur;
6756N/A unsigned int token_max;
6596N/A
6596N/A /* all key strings are copied and de-duplicated in a single continuous string buffer */
6596N/A struct strbuf *strbuf;
6153N/A
3988N/A /* during rule parsing, uid/gid lookup results are cached */
3988N/A struct uid_gid *uids;
3988N/A unsigned int uids_cur;
3988N/A unsigned int uids_max;
3988N/A struct uid_gid *gids;
3988N/A unsigned int gids_cur;
3988N/A unsigned int gids_max;
6596N/A};
6345N/A
3988N/Astatic char *rules_str(struct udev_rules *rules, unsigned int off) {
3988N/A return rules->strbuf->buf + off;
6756N/A}
6756N/A
6756N/Astatic unsigned int rules_add_string(struct udev_rules *rules, const char *s) {
6756N/A return strbuf_add_string(rules->strbuf, s, strlen(s));
6756N/A}
6756N/A
6756N/A/* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
6756N/Aenum operation_type {
6756N/A OP_UNSET,
6756N/A
6756N/A OP_MATCH,
6756N/A OP_NOMATCH,
6756N/A OP_MATCH_MAX,
6756N/A
6756N/A OP_ADD,
6756N/A OP_ASSIGN,
6756N/A OP_ASSIGN_FINAL,
6756N/A};
6756N/A
6756N/Aenum string_glob_type {
6756N/A GL_UNSET,
6756N/A GL_PLAIN, /* no special chars */
6756N/A GL_GLOB, /* shell globs ?,*,[] */
6756N/A GL_SPLIT, /* multi-value A|B */
6756N/A GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
6756N/A GL_SOMETHING, /* commonly used "?*" */
6756N/A};
6756N/A
6756N/Aenum string_subst_type {
6756N/A SB_UNSET,
6756N/A SB_NONE,
6756N/A SB_FORMAT,
6756N/A SB_SUBSYS,
6756N/A};
6756N/A
6345N/A/* tokens of a rule are sorted/handled in this order */
6345N/Aenum token_type {
6345N/A TK_UNSET,
6345N/A TK_RULE,
6345N/A
6345N/A TK_M_ACTION, /* val */
6345N/A TK_M_DEVPATH, /* val */
3988N/A TK_M_KERNEL, /* val */
6345N/A TK_M_DEVLINK, /* val */
6345N/A TK_M_NAME, /* val */
3988N/A TK_M_ENV, /* val, attr */
4134N/A TK_M_TAG, /* val */
4134N/A TK_M_SUBSYSTEM, /* val */
4134N/A TK_M_DRIVER, /* val */
3988N/A TK_M_WAITFOR, /* val */
3988N/A TK_M_ATTR, /* val, attr */
3988N/A
3988N/A TK_M_PARENTS_MIN,
6345N/A TK_M_KERNELS, /* val */
3988N/A TK_M_SUBSYSTEMS, /* val */
3988N/A TK_M_DRIVERS, /* val */
3988N/A TK_M_ATTRS, /* val, attr */
3988N/A TK_M_TAGS, /* val */
3988N/A TK_M_PARENTS_MAX,
3988N/A
3988N/A TK_M_TEST, /* val, mode_t */
3988N/A TK_M_EVENT_TIMEOUT, /* int */
4011N/A TK_M_PROGRAM, /* val */
3988N/A TK_M_IMPORT_FILE, /* val */
6345N/A TK_M_IMPORT_PROG, /* val */
3988N/A TK_M_IMPORT_BUILTIN, /* val */
3988N/A TK_M_IMPORT_DB, /* val */
3988N/A TK_M_IMPORT_CMDLINE, /* val */
3988N/A TK_M_IMPORT_PARENT, /* val */
3988N/A TK_M_RESULT, /* val */
3988N/A TK_M_MAX,
3988N/A
3988N/A TK_A_STRING_ESCAPE_NONE,
3988N/A TK_A_STRING_ESCAPE_REPLACE,
3988N/A TK_A_DB_PERSIST,
3988N/A TK_A_INOTIFY_WATCH, /* int */
3988N/A TK_A_DEVLINK_PRIO, /* int */
3988N/A TK_A_OWNER, /* val */
3988N/A TK_A_GROUP, /* val */
3988N/A TK_A_MODE, /* val */
3988N/A TK_A_OWNER_ID, /* uid_t */
3988N/A TK_A_GROUP_ID, /* gid_t */
3988N/A TK_A_MODE_ID, /* mode_t */
3988N/A TK_A_TAG, /* val */
3988N/A TK_A_STATIC_NODE, /* val */
6596N/A TK_A_SECLABEL, /* val, attr */
3988N/A TK_A_ENV, /* val, attr */
3988N/A TK_A_NAME, /* val */
3988N/A TK_A_DEVLINK, /* val */
3988N/A TK_A_ATTR, /* val, attr */
3988N/A TK_A_RUN_BUILTIN, /* val, bool */
3988N/A TK_A_RUN_PROGRAM, /* val, bool */
3988N/A TK_A_GOTO, /* size_t */
3988N/A
3988N/A TK_END,
3988N/A};
3988N/A
3988N/A/* we try to pack stuff in a way that we take only 12 bytes per token */
3988N/Astruct token {
3988N/A union {
3988N/A unsigned char type; /* same in rule and key */
3988N/A struct {
3988N/A enum token_type type:8;
3988N/A bool can_set_name:1;
4988N/A bool has_static_node:1;
3988N/A unsigned int unused:6;
3988N/A unsigned short token_count;
3988N/A unsigned int label_off;
3988N/A unsigned short filename_off;
3988N/A unsigned short filename_line;
3988N/A } rule;
4988N/A struct {
3988N/A enum token_type type:8;
3988N/A enum operation_type op:8;
3988N/A enum string_glob_type glob:8;
3988N/A enum string_subst_type subst:4;
3988N/A enum string_subst_type attrsubst:4;
3988N/A unsigned int value_off;
3988N/A union {
3988N/A unsigned int attr_off;
3988N/A unsigned int rule_goto;
3988N/A mode_t mode;
3988N/A uid_t uid;
3988N/A gid_t gid;
3988N/A int devlink_prio;
3988N/A int event_timeout;
3988N/A int watch;
3988N/A enum udev_builtin_cmd builtin_cmd;
3988N/A };
3988N/A } key;
3988N/A };
3988N/A};
3988N/A
3988N/A#define MAX_TK 64
3988N/Astruct rule_tmp {
3988N/A struct udev_rules *rules;
3988N/A struct token rule;
3988N/A struct token token[MAX_TK];
3988N/A unsigned int token_cur;
3988N/A};
3988N/A
3988N/A#ifdef DEBUG
3988N/Astatic const char *operation_str(enum operation_type type)
4641N/A{
4641N/A static const char *operation_strs[] = {
4641N/A [OP_UNSET] = "UNSET",
4641N/A [OP_MATCH] = "match",
3988N/A [OP_NOMATCH] = "nomatch",
3988N/A [OP_MATCH_MAX] = "MATCH_MAX",
3988N/A
3988N/A [OP_ADD] = "add",
3988N/A [OP_ASSIGN] = "assign",
3988N/A [OP_ASSIGN_FINAL] = "assign-final",
3988N/A} ;
3988N/A
3988N/A return operation_strs[type];
3988N/A}
3988N/A
3988N/Astatic const char *string_glob_str(enum string_glob_type type)
3988N/A{
3988N/A static const char *string_glob_strs[] = {
3988N/A [GL_UNSET] = "UNSET",
3988N/A [GL_PLAIN] = "plain",
3988N/A [GL_GLOB] = "glob",
3988N/A [GL_SPLIT] = "split",
3988N/A [GL_SPLIT_GLOB] = "split-glob",
3988N/A [GL_SOMETHING] = "split-glob",
3988N/A };
3988N/A
3988N/A return string_glob_strs[type];
3988N/A}
3988N/A
3988N/Astatic const char *token_str(enum token_type type)
3988N/A{
3988N/A static const char *token_strs[] = {
3988N/A [TK_UNSET] = "UNSET",
3988N/A [TK_RULE] = "RULE",
3988N/A
3988N/A [TK_M_ACTION] = "M ACTION",
3988N/A [TK_M_DEVPATH] = "M DEVPATH",
3988N/A [TK_M_KERNEL] = "M KERNEL",
3988N/A [TK_M_DEVLINK] = "M DEVLINK",
3988N/A [TK_M_NAME] = "M NAME",
3988N/A [TK_M_ENV] = "M ENV",
3988N/A [TK_M_TAG] = "M TAG",
3988N/A [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
3988N/A [TK_M_DRIVER] = "M DRIVER",
3988N/A [TK_M_WAITFOR] = "M WAITFOR",
4641N/A [TK_M_ATTR] = "M ATTR",
4641N/A
3988N/A [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
3988N/A [TK_M_KERNELS] = "M KERNELS",
3988N/A [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
3988N/A [TK_M_DRIVERS] = "M DRIVERS",
3988N/A [TK_M_ATTRS] = "M ATTRS",
3988N/A [TK_M_TAGS] = "M TAGS",
3988N/A [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
3988N/A
3988N/A [TK_M_TEST] = "M TEST",
3988N/A [TK_M_EVENT_TIMEOUT] = "M EVENT_TIMEOUT",
3988N/A [TK_M_PROGRAM] = "M PROGRAM",
3988N/A [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
3988N/A [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
4011N/A [TK_M_IMPORT_BUILTIN] = "M IMPORT_BUILTIN",
4011N/A [TK_M_IMPORT_DB] = "M IMPORT_DB",
4011N/A [TK_M_IMPORT_CMDLINE] = "M IMPORT_CMDLINE",
4011N/A [TK_M_IMPORT_PARENT] = "M IMPORT_PARENT",
3988N/A [TK_M_RESULT] = "M RESULT",
3988N/A [TK_M_MAX] = "M MAX",
3988N/A
3988N/A [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
3988N/A [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
3988N/A [TK_A_DB_PERSIST] = "A DB_PERSIST",
6339N/A [TK_A_INOTIFY_WATCH] = "A INOTIFY_WATCH",
3988N/A [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
3988N/A [TK_A_OWNER] = "A OWNER",
4134N/A [TK_A_GROUP] = "A GROUP",
4802N/A [TK_A_MODE] = "A MODE",
6678N/A [TK_A_OWNER_ID] = "A OWNER_ID",
6678N/A [TK_A_GROUP_ID] = "A GROUP_ID",
6678N/A [TK_A_STATIC_NODE] = "A STATIC_NODE",
3988N/A [TK_A_SECLABEL] = "A SECLABEL",
3988N/A [TK_A_MODE_ID] = "A MODE_ID",
4134N/A [TK_A_ENV] = "A ENV",
3988N/A [TK_A_TAG] = "A ENV",
3988N/A [TK_A_NAME] = "A NAME",
6755N/A [TK_A_DEVLINK] = "A DEVLINK",
3988N/A [TK_A_ATTR] = "A ATTR",
6678N/A [TK_A_RUN_BUILTIN] = "A RUN_BUILTIN",
3988N/A [TK_A_RUN_PROGRAM] = "A RUN_PROGRAM",
6678N/A [TK_A_GOTO] = "A GOTO",
3988N/A
6678N/A [TK_END] = "END",
3988N/A };
3988N/A
6678N/A return token_strs[type];
3988N/A}
6755N/A
6678N/Astatic void dump_token(struct udev_rules *rules, struct token *token)
6678N/A{
6678N/A enum token_type type = token->type;
3988N/A enum operation_type op = token->key.op;
3988N/A enum string_glob_type glob = token->key.glob;
6678N/A const char *value = str(rules, token->key.value_off);
4988N/A const char *attr = &rules->buf[token->key.attr_off];
4011N/A
6678N/A switch (type) {
6678N/A case TK_RULE:
6678N/A {
6678N/A const char *tks_ptr = (char *)rules->tokens;
6678N/A const char *tk_ptr = (char *)token;
6678N/A unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
4988N/A
4988N/A log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'\n",
3988N/A &rules->buf[token->rule.filename_off], token->rule.filename_line,
3988N/A idx, token->rule.token_count,
4011N/A &rules->buf[token->rule.label_off]);
3988N/A break;
3988N/A }
4988N/A case TK_M_ACTION:
3988N/A case TK_M_DEVPATH:
3988N/A case TK_M_KERNEL:
3988N/A case TK_M_SUBSYSTEM:
3988N/A case TK_M_DRIVER:
3988N/A case TK_M_WAITFOR:
4988N/A case TK_M_DEVLINK:
3988N/A case TK_M_NAME:
3988N/A case TK_M_KERNELS:
3988N/A case TK_M_SUBSYSTEMS:
4988N/A case TK_M_DRIVERS:
4988N/A case TK_M_TAGS:
3988N/A case TK_M_PROGRAM:
4011N/A case TK_M_IMPORT_FILE:
3988N/A case TK_M_IMPORT_PROG:
3988N/A case TK_M_IMPORT_DB:
3988N/A case TK_M_IMPORT_CMDLINE:
3988N/A case TK_M_IMPORT_PARENT:
3988N/A case TK_M_RESULT:
3988N/A case TK_A_NAME:
4988N/A case TK_A_DEVLINK:
4988N/A case TK_A_OWNER:
4988N/A case TK_A_GROUP:
4988N/A case TK_A_MODE:
4988N/A case TK_A_RUN_BUILTIN:
4988N/A case TK_A_RUN_PROGRAM:
3988N/A log_debug("%s %s '%s'(%s)\n",
3988N/A token_str(type), operation_str(op), value, string_glob_str(glob));
3988N/A break;
3988N/A case TK_M_IMPORT_BUILTIN:
3988N/A log_debug("%s %i '%s'\n", token_str(type), token->key.builtin_cmd, value);
3988N/A break;
3988N/A case TK_M_ATTR:
6678N/A case TK_M_ATTRS:
4011N/A case TK_M_ENV:
3988N/A case TK_A_ATTR:
3988N/A case TK_A_ENV:
3988N/A log_debug("%s %s '%s' '%s'(%s)\n",
3988N/A token_str(type), operation_str(op), attr, value, string_glob_str(glob));
3988N/A break;
3988N/A case TK_M_TAG:
3988N/A case TK_A_TAG:
3988N/A log_debug("%s %s '%s'\n", token_str(type), operation_str(op), value);
3988N/A break;
3988N/A case TK_A_STRING_ESCAPE_NONE:
3988N/A case TK_A_STRING_ESCAPE_REPLACE:
3988N/A case TK_A_DB_PERSIST:
3988N/A log_debug("%s\n", token_str(type));
3988N/A break;
3988N/A case TK_M_TEST:
3988N/A log_debug("%s %s '%s'(%s) %#o\n",
3988N/A token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
3988N/A break;
3988N/A case TK_A_INOTIFY_WATCH:
3988N/A log_debug("%s %u\n", token_str(type), token->key.watch);
3988N/A break;
3988N/A case TK_A_DEVLINK_PRIO:
3988N/A log_debug("%s %u\n", token_str(type), token->key.devlink_prio);
3988N/A break;
3988N/A case TK_A_OWNER_ID:
3988N/A log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.uid);
3988N/A break;
3988N/A case TK_A_GROUP_ID:
3988N/A log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.gid);
3988N/A break;
3988N/A case TK_A_MODE_ID:
3988N/A log_debug("%s %s %#o\n", token_str(type), operation_str(op), token->key.mode);
3988N/A break;
3988N/A case TK_A_STATIC_NODE:
5589N/A log_debug("%s '%s'\n", token_str(type), value);
3988N/A break;
5589N/A case TK_A_SECLABEL:
3988N/A log_debug("%s %s '%s' '%s'\n", token_str(type), operation_str(op), attr, value);
3988N/A break;
3988N/A case TK_M_EVENT_TIMEOUT:
3988N/A log_debug("%s %u\n", token_str(type), token->key.event_timeout);
3988N/A break;
3988N/A case TK_A_GOTO:
3988N/A log_debug("%s '%s' %u\n", token_str(type), value, token->key.rule_goto);
3988N/A break;
3988N/A case TK_END:
3988N/A log_debug("* %s\n", token_str(type));
3988N/A break;
3988N/A case TK_M_PARENTS_MIN:
3988N/A case TK_M_PARENTS_MAX:
3988N/A case TK_M_MAX:
3988N/A case TK_UNSET:
3988N/A log_debug("unknown type %u\n", type);
3988N/A break;
3988N/A }
3988N/A}
3988N/A
3988N/Astatic void dump_rules(struct udev_rules *rules)
3988N/A{
3988N/A unsigned int i;
3988N/A
3988N/A log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
3988N/A rules->token_cur,
3988N/A rules->token_cur * sizeof(struct token),
3988N/A rules->buf_count,
3988N/A rules->buf_cur);
3988N/A for(i = 0; i < rules->token_cur; i++)
3988N/A dump_token(rules, &rules->tokens[i]);
3988N/A}
3988N/A#else
3988N/Astatic inline const char *operation_str(enum operation_type type) { return NULL; }
3988N/Astatic inline const char *token_str(enum token_type type) { return NULL; }
3988N/Astatic inline void dump_token(struct udev_rules *rules, struct token *token) {}
3988N/Astatic inline void dump_rules(struct udev_rules *rules) {}
3988N/A#endif /* DEBUG */
3988N/A
3988N/Astatic int add_token(struct udev_rules *rules, struct token *token)
3988N/A{
3988N/A /* grow buffer if needed */
5339N/A if (rules->token_cur+1 >= rules->token_max) {
3988N/A struct token *tokens;
5339N/A unsigned int add;
3988N/A
3988N/A /* double the buffer size */
3988N/A add = rules->token_max;
3988N/A if (add < 8)
3988N/A add = 8;
3988N/A
3988N/A tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
3988N/A if (tokens == NULL)
3988N/A return -1;
3988N/A rules->tokens = tokens;
3988N/A rules->token_max += add;
3988N/A }
3988N/A memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
3988N/A rules->token_cur++;
3988N/A return 0;
3988N/A}
3988N/A
3988N/Astatic uid_t add_uid(struct udev_rules *rules, const char *owner)
3988N/A{
3988N/A unsigned int i;
3988N/A uid_t uid;
6755N/A unsigned int off;
3988N/A
3988N/A /* lookup, if we know it already */
3988N/A for (i = 0; i < rules->uids_cur; i++) {
3988N/A off = rules->uids[i].name_off;
3988N/A if (streq(rules_str(rules, off), owner)) {
3988N/A uid = rules->uids[i].uid;
3988N/A return uid;
3988N/A }
6755N/A }
3988N/A uid = util_lookup_user(rules->udev, owner);
3988N/A
3988N/A /* grow buffer if needed */
3988N/A if (rules->uids_cur+1 >= rules->uids_max) {
3988N/A struct uid_gid *uids;
3988N/A unsigned int add;
3988N/A
3988N/A /* double the buffer size */
4812N/A add = rules->uids_max;
4812N/A if (add < 1)
4812N/A add = 8;
4812N/A
4812N/A uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
4812N/A if (uids == NULL)
4812N/A return uid;
4812N/A rules->uids = uids;
3988N/A rules->uids_max += add;
3988N/A }
3988N/A rules->uids[rules->uids_cur].uid = uid;
3988N/A off = rules_add_string(rules, owner);
4988N/A if (off <= 0)
4988N/A return uid;
4988N/A rules->uids[rules->uids_cur].name_off = off;
4988N/A rules->uids_cur++;
4988N/A return uid;
4988N/A}
4988N/A
3988N/Astatic gid_t add_gid(struct udev_rules *rules, const char *group)
3988N/A{
3988N/A unsigned int i;
3988N/A gid_t gid;
3988N/A unsigned int off;
3988N/A
3988N/A /* lookup, if we know it already */
3988N/A for (i = 0; i < rules->gids_cur; i++) {
3988N/A off = rules->gids[i].name_off;
5031N/A if (streq(rules_str(rules, off), group)) {
3988N/A gid = rules->gids[i].gid;
3988N/A return gid;
3988N/A }
3988N/A }
3988N/A gid = util_lookup_group(rules->udev, group);
3988N/A
3988N/A /* grow buffer if needed */
4812N/A if (rules->gids_cur+1 >= rules->gids_max) {
4812N/A struct uid_gid *gids;
3988N/A unsigned int add;
3988N/A
3988N/A /* double the buffer size */
3988N/A add = rules->gids_max;
3988N/A if (add < 1)
3988N/A add = 8;
3988N/A
3988N/A gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
3988N/A if (gids == NULL)
3988N/A return gid;
3988N/A rules->gids = gids;
3988N/A rules->gids_max += add;
3988N/A }
3988N/A rules->gids[rules->gids_cur].gid = gid;
3988N/A off = rules_add_string(rules, group);
3988N/A if (off <= 0)
3988N/A return gid;
3988N/A rules->gids[rules->gids_cur].name_off = off;
3988N/A rules->gids_cur++;
3988N/A return gid;
3988N/A}
3988N/A
3988N/Astatic int import_property_from_string(struct udev_device *dev, char *line)
3988N/A{
3988N/A char *key;
3988N/A char *val;
3988N/A size_t len;
3988N/A
3988N/A /* find key */
3988N/A key = line;
3988N/A while (isspace(key[0]))
3988N/A key++;
3988N/A
3988N/A /* comment or empty line */
3988N/A if (key[0] == '#' || key[0] == '\0')
4988N/A return -1;
6678N/A
3988N/A /* split key/value */
3988N/A val = strchr(key, '=');
5339N/A if (val == NULL)
3988N/A return -1;
3988N/A val[0] = '\0';
3988N/A val++;
3988N/A
3988N/A /* find value */
3988N/A while (isspace(val[0]))
3988N/A val++;
3988N/A
3988N/A /* terminate key */
3988N/A len = strlen(key);
3988N/A if (len == 0)
3988N/A return -1;
3988N/A while (isspace(key[len-1]))
3988N/A len--;
3988N/A key[len] = '\0';
3988N/A
3988N/A /* terminate value */
3988N/A len = strlen(val);
3988N/A if (len == 0)
3988N/A return -1;
3988N/A while (isspace(val[len-1]))
3988N/A len--;
3988N/A val[len] = '\0';
3988N/A
4011N/A if (len == 0)
4011N/A return -1;
4011N/A
4011N/A /* unquote */
4011N/A if (val[0] == '"' || val[0] == '\'') {
4011N/A if (val[len-1] != val[0]) {
4011N/A log_debug("inconsistent quoting: '%s', skip\n", line);
4011N/A return -1;
4011N/A }
4011N/A val[len-1] = '\0';
4011N/A val++;
4011N/A }
4011N/A
4011N/A /* handle device, renamed by external tool, returning new path */
4011N/A if (streq(key, "DEVPATH")) {
3988N/A char syspath[UTIL_PATH_SIZE];
3988N/A
3988N/A log_debug("updating devpath from '%s' to '%s'\n",
4988N/A udev_device_get_devpath(dev), val);
3988N/A strscpyl(syspath, sizeof(syspath), "/sys", val, NULL);
3988N/A udev_device_set_syspath(dev, syspath);
4011N/A } else {
4011N/A struct udev_list_entry *entry;
4011N/A
4011N/A entry = udev_device_add_property(dev, key, val);
4011N/A /* store in db, skip private keys */
4011N/A if (key[0] != '.')
4011N/A udev_list_entry_set_num(entry, true);
4011N/A }
4011N/A return 0;
6755N/A}
4011N/A
4011N/Astatic int import_file_into_properties(struct udev_device *dev, const char *filename)
4011N/A{
4011N/A FILE *f;
4011N/A char line[UTIL_LINE_SIZE];
4011N/A
4011N/A f = fopen(filename, "re");
4011N/A if (f == NULL)
4011N/A return -1;
4011N/A while (fgets(line, sizeof(line), f) != NULL)
4011N/A import_property_from_string(dev, line);
6339N/A fclose(f);
4011N/A return 0;
4011N/A}
4011N/A
4011N/Astatic int import_program_into_properties(struct udev_event *event, const char *program, const sigset_t *sigmask)
4011N/A{
4011N/A struct udev_device *dev = event->dev;
4011N/A char **envp;
4011N/A char result[UTIL_LINE_SIZE];
4011N/A char *line;
4011N/A int err;
4011N/A
4011N/A envp = udev_device_get_properties_envp(dev);
3988N/A err = udev_event_spawn(event, program, envp, sigmask, result, sizeof(result));
3988N/A if (err < 0)
3988N/A return err;
3988N/A
3988N/A line = result;
3988N/A while (line != NULL) {
3988N/A char *pos;
3988N/A
3988N/A pos = strchr(line, '\n');
3988N/A if (pos != NULL) {
3988N/A pos[0] = '\0';
3988N/A pos = &pos[1];
3988N/A }
3988N/A import_property_from_string(dev, line);
3988N/A line = pos;
3988N/A }
3988N/A return 0;
3988N/A}
3988N/A
3988N/Astatic int import_parent_into_properties(struct udev_device *dev, const char *filter)
3988N/A{
3988N/A struct udev_device *dev_parent;
3988N/A struct udev_list_entry *list_entry;
3988N/A
3988N/A dev_parent = udev_device_get_parent(dev);
3988N/A if (dev_parent == NULL)
3988N/A return -1;
3988N/A
3988N/A udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
3988N/A const char *key = udev_list_entry_get_name(list_entry);
3988N/A const char *val = udev_list_entry_get_value(list_entry);
3988N/A
3988N/A if (fnmatch(filter, key, 0) == 0) {
3988N/A struct udev_list_entry *entry;
3988N/A
3988N/A entry = udev_device_add_property(dev, key, val);
3988N/A /* store in db, skip private keys */
3988N/A if (key[0] != '.')
3988N/A udev_list_entry_set_num(entry, true);
4988N/A }
4988N/A }
3988N/A return 0;
3988N/A}
3988N/A
3988N/A#define WAIT_LOOP_PER_SECOND 50
3988N/Astatic int wait_for_file(struct udev_device *dev, const char *file, int timeout)
3988N/A{
3988N/A char filepath[UTIL_PATH_SIZE];
3988N/A char devicepath[UTIL_PATH_SIZE];
3988N/A struct stat stats;
3988N/A int loop = timeout * WAIT_LOOP_PER_SECOND;
3988N/A
3988N/A /* a relative path is a device attribute */
3988N/A devicepath[0] = '\0';
3988N/A if (file[0] != '/') {
3988N/A strscpyl(devicepath, sizeof(devicepath), udev_device_get_syspath(dev), NULL);
3988N/A strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
3988N/A file = filepath;
6678N/A }
3988N/A
3988N/A while (--loop) {
3988N/A const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
3988N/A
3988N/A /* lookup file */
3988N/A if (stat(file, &stats) == 0) {
3988N/A log_debug("file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
3988N/A return 0;
3988N/A }
3988N/A /* make sure, the device did not disappear in the meantime */
3988N/A if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
3988N/A log_debug("device disappeared while waiting for '%s'\n", file);
3988N/A return -2;
3988N/A }
3988N/A log_debug("wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
3988N/A nanosleep(&duration, NULL);
5339N/A }
4988N/A log_debug("waiting for '%s' failed\n", file);
6755N/A return -1;
4988N/A}
4988N/A
4988N/Astatic int attr_subst_subdir(char *attr, size_t len)
4988N/A{
4988N/A bool found = false;
3988N/A
3988N/A if (strstr(attr, "/*/")) {
3988N/A char *pos;
3988N/A char dirname[UTIL_PATH_SIZE];
4890N/A const char *tail;
3988N/A DIR *dir;
3988N/A
3988N/A strscpy(dirname, sizeof(dirname), attr);
3988N/A pos = strstr(dirname, "/*/");
3988N/A if (pos == NULL)
3988N/A return -1;
3988N/A pos[0] = '\0';
3988N/A tail = &pos[2];
3988N/A dir = opendir(dirname);
3988N/A if (dir != NULL) {
3988N/A struct dirent *dent;
3988N/A
3988N/A for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
3988N/A struct stat stats;
3988N/A
4802N/A if (dent->d_name[0] == '.')
4802N/A continue;
4802N/A strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
6678N/A if (stat(attr, &stats) == 0) {
3988N/A found = true;
3988N/A break;
3988N/A }
3988N/A }
3988N/A closedir(dir);
3988N/A }
3988N/A }
3988N/A
3988N/A return found;
3988N/A}
3988N/A
3988N/Astatic int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
3988N/A{
3988N/A char *linepos;
4802N/A char *temp;
4802N/A
4802N/A linepos = *line;
4802N/A if (linepos == NULL || linepos[0] == '\0')
6678N/A return -1;
3988N/A
3988N/A /* skip whitespace */
3988N/A while (isspace(linepos[0]) || linepos[0] == ',')
3988N/A linepos++;
3988N/A
3988N/A /* get the key */
3988N/A if (linepos[0] == '\0')
4134N/A return -1;
3988N/A *key = linepos;
3988N/A
3988N/A for (;;) {
3988N/A linepos++;
3988N/A if (linepos[0] == '\0')
3988N/A return -1;
3988N/A if (isspace(linepos[0]))
3988N/A break;
3988N/A if (linepos[0] == '=')
3988N/A break;
3988N/A if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
3988N/A if (linepos[1] == '=')
3988N/A break;
3988N/A }
3988N/A
3988N/A /* remember end of key */
3988N/A temp = linepos;
3988N/A
3988N/A /* skip whitespace after key */
3988N/A while (isspace(linepos[0]))
3988N/A linepos++;
3988N/A if (linepos[0] == '\0')
3988N/A return -1;
3988N/A
3988N/A /* get operation type */
3988N/A if (linepos[0] == '=' && linepos[1] == '=') {
4802N/A *op = OP_MATCH;
4802N/A linepos += 2;
6678N/A } else if (linepos[0] == '!' && linepos[1] == '=') {
3988N/A *op = OP_NOMATCH;
3988N/A linepos += 2;
3988N/A } else if (linepos[0] == '+' && linepos[1] == '=') {
3988N/A *op = OP_ADD;
3988N/A linepos += 2;
3988N/A } else if (linepos[0] == '=') {
3988N/A *op = OP_ASSIGN;
3988N/A linepos++;
3988N/A } else if (linepos[0] == ':' && linepos[1] == '=') {
3988N/A *op = OP_ASSIGN_FINAL;
3988N/A linepos += 2;
3988N/A } else
4802N/A return -1;
4802N/A
4802N/A /* terminate key */
6678N/A temp[0] = '\0';
3988N/A
3988N/A /* skip whitespace after operator */
3988N/A while (isspace(linepos[0]))
3988N/A linepos++;
3988N/A if (linepos[0] == '\0')
3988N/A return -1;
3988N/A
3988N/A /* get the value */
3988N/A if (linepos[0] == '"')
3988N/A linepos++;
3988N/A else
3988N/A return -1;
3988N/A *value = linepos;
3988N/A
3988N/A /* terminate */
3988N/A temp = strchr(linepos, '"');
3988N/A if (!temp)
3988N/A return -1;
3988N/A temp[0] = '\0';
3988N/A temp++;
3988N/A
3988N/A /* move line to next key */
3988N/A *line = temp;
3988N/A return 0;
3988N/A}
3988N/A
3988N/A/* extract possible KEY{attr} */
3988N/Astatic const char *get_key_attribute(struct udev *udev, char *str)
3988N/A{
3988N/A char *pos;
3988N/A char *attr;
3988N/A
3988N/A attr = strchr(str, '{');
4011N/A if (attr != NULL) {
4011N/A attr++;
4011N/A pos = strchr(attr, '}');
4011N/A if (pos == NULL) {
4011N/A log_error("missing closing brace for format\n");
4011N/A return NULL;
4011N/A }
4011N/A pos[0] = '\0';
4011N/A return attr;
4011N/A }
4011N/A return NULL;
4011N/A}
4011N/A
3988N/Astatic int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
3988N/A enum operation_type op,
4011N/A const char *value, const void *data)
4011N/A{
3988N/A struct token *token = &rule_tmp->token[rule_tmp->token_cur];
4011N/A const char *attr = NULL;
4011N/A
3988N/A memset(token, 0x00, sizeof(struct token));
3988N/A
3988N/A switch (type) {
4988N/A case TK_M_ACTION:
3988N/A case TK_M_DEVPATH:
3988N/A case TK_M_KERNEL:
3988N/A case TK_M_SUBSYSTEM:
3988N/A case TK_M_DRIVER:
4011N/A case TK_M_WAITFOR:
4988N/A case TK_M_DEVLINK:
4988N/A case TK_M_NAME:
4988N/A case TK_M_KERNELS:
3988N/A case TK_M_SUBSYSTEMS:
3988N/A case TK_M_DRIVERS:
6339N/A case TK_M_TAGS:
3988N/A case TK_M_PROGRAM:
4988N/A case TK_M_IMPORT_FILE:
4988N/A case TK_M_IMPORT_PROG:
4988N/A case TK_M_IMPORT_DB:
3988N/A case TK_M_IMPORT_CMDLINE:
4988N/A case TK_M_IMPORT_PARENT:
4988N/A case TK_M_RESULT:
3988N/A case TK_A_OWNER:
4988N/A case TK_A_GROUP:
4988N/A case TK_A_MODE:
4988N/A case TK_A_DEVLINK:
4988N/A case TK_A_NAME:
4988N/A case TK_A_GOTO:
4988N/A case TK_M_TAG:
4988N/A case TK_A_TAG:
4988N/A token->key.value_off = rules_add_string(rule_tmp->rules, value);
4988N/A break;
4988N/A case TK_M_IMPORT_BUILTIN:
4988N/A token->key.value_off = rules_add_string(rule_tmp->rules, value);
4988N/A token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
4988N/A break;
4988N/A case TK_M_ENV:
4988N/A case TK_M_ATTR:
4988N/A case TK_M_ATTRS:
3988N/A case TK_A_ATTR:
3988N/A case TK_A_ENV:
4988N/A case TK_A_SECLABEL:
4988N/A attr = data;
4988N/A token->key.value_off = rules_add_string(rule_tmp->rules, value);
3988N/A token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
4988N/A break;
4011N/A case TK_M_TEST:
4011N/A token->key.value_off = rules_add_string(rule_tmp->rules, value);
4011N/A if (data != NULL)
4011N/A token->key.mode = *(mode_t *)data;
4011N/A break;
4011N/A case TK_A_STRING_ESCAPE_NONE:
4011N/A case TK_A_STRING_ESCAPE_REPLACE:
4011N/A case TK_A_DB_PERSIST:
4011N/A break;
4011N/A case TK_A_RUN_BUILTIN:
3988N/A case TK_A_RUN_PROGRAM:
4011N/A token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
4011N/A token->key.value_off = rules_add_string(rule_tmp->rules, value);
6756N/A break;
6756N/A case TK_A_INOTIFY_WATCH:
6756N/A case TK_A_DEVLINK_PRIO:
6756N/A token->key.devlink_prio = *(int *)data;
6756N/A break;
6756N/A case TK_A_OWNER_ID:
4011N/A token->key.uid = *(uid_t *)data;
4011N/A break;
4011N/A case TK_A_GROUP_ID:
4011N/A token->key.gid = *(gid_t *)data;
3988N/A break;
4011N/A case TK_A_MODE_ID:
4011N/A token->key.mode = *(mode_t *)data;
4011N/A break;
6756N/A case TK_A_STATIC_NODE:
6756N/A token->key.value_off = rules_add_string(rule_tmp->rules, value);
6756N/A break;
4011N/A case TK_M_EVENT_TIMEOUT:
3988N/A token->key.event_timeout = *(int *)data;
3988N/A break;
4988N/A case TK_RULE:
3988N/A case TK_M_PARENTS_MIN:
3988N/A case TK_M_PARENTS_MAX:
3988N/A case TK_M_MAX:
6756N/A case TK_END:
6756N/A case TK_UNSET:
6756N/A log_error("wrong type %u\n", type);
6756N/A return -1;
6756N/A }
6756N/A
6756N/A if (value != NULL && type < TK_M_MAX) {
6756N/A /* check if we need to split or call fnmatch() while matching rules */
6756N/A enum string_glob_type glob;
6756N/A int has_split;
6756N/A int has_glob;
6756N/A
6756N/A has_split = (strchr(value, '|') != NULL);
6756N/A has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL || strchr(value, '[') != NULL);
6756N/A if (has_split && has_glob) {
6756N/A glob = GL_SPLIT_GLOB;
6756N/A } else if (has_split) {
6756N/A glob = GL_SPLIT;
6756N/A } else if (has_glob) {
6756N/A if (streq(value, "?*"))
6756N/A glob = GL_SOMETHING;
6756N/A else
6756N/A glob = GL_GLOB;
6756N/A } else {
3988N/A glob = GL_PLAIN;
3988N/A }
3988N/A token->key.glob = glob;
4011N/A }
4011N/A
3988N/A if (value != NULL && type > TK_M_MAX) {
4011N/A /* check if assigned value has substitution chars */
4011N/A if (value[0] == '[')
3988N/A token->key.subst = SB_SUBSYS;
3988N/A else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
3988N/A token->key.subst = SB_FORMAT;
6339N/A else
3988N/A token->key.subst = SB_NONE;
3988N/A }
3988N/A
4011N/A if (attr != NULL) {
4988N/A /* check if property/attribut name has substitution chars */
4988N/A if (attr[0] == '[')
4988N/A token->key.attrsubst = SB_SUBSYS;
3988N/A else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
3988N/A token->key.attrsubst = SB_FORMAT;
6339N/A else
4134N/A token->key.attrsubst = SB_NONE;
4988N/A }
4988N/A
4988N/A token->key.type = type;
4988N/A token->key.op = op;
4988N/A rule_tmp->token_cur++;
4988N/A if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
4988N/A log_error("temporary rule array too small\n");
4988N/A return -1;
4988N/A }
4988N/A return 0;
4988N/A}
4988N/A
3988N/Astatic int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
3988N/A{
4988N/A unsigned int i;
4988N/A unsigned int start = 0;
4988N/A unsigned int end = rule_tmp->token_cur;
4988N/A
4988N/A for (i = 0; i < rule_tmp->token_cur; i++) {
4988N/A enum token_type next_val = TK_UNSET;
4988N/A unsigned int next_idx = 0;
3988N/A unsigned int j;
4988N/A
4988N/A /* find smallest value */
4988N/A for (j = start; j < end; j++) {
4988N/A if (rule_tmp->token[j].type == TK_UNSET)
4988N/A continue;
5339N/A if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
4988N/A next_val = rule_tmp->token[j].type;
3988N/A next_idx = j;
4011N/A }
4988N/A }
4988N/A
4011N/A /* add token and mark done */
4011N/A if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
4011N/A return -1;
4011N/A rule_tmp->token[next_idx].type = TK_UNSET;
4011N/A
4011N/A /* shrink range */
4011N/A if (next_idx == start)
4011N/A start++;
4011N/A if (next_idx+1 == end)
3988N/A end--;
4011N/A }
4011N/A return 0;
6756N/A}
6756N/A
6756N/Astatic int add_rule(struct udev_rules *rules, char *line,
6756N/A const char *filename, unsigned int filename_off, unsigned int lineno)
6756N/A{
6756N/A char *linepos;
6756N/A const char *attr;
4011N/A struct rule_tmp rule_tmp;
4011N/A
4011N/A memset(&rule_tmp, 0x00, sizeof(struct rule_tmp));
4011N/A rule_tmp.rules = rules;
3988N/A rule_tmp.rule.type = TK_RULE;
4011N/A /* the offset in the rule is limited to unsigned short */
4011N/A if (filename_off < USHRT_MAX)
4011N/A rule_tmp.rule.rule.filename_off = filename_off;
6756N/A rule_tmp.rule.rule.filename_line = lineno;
6756N/A
6756N/A linepos = line;
4011N/A for (;;) {
3988N/A char *key;
3988N/A char *value;
4988N/A enum operation_type op;
3988N/A
3988N/A if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
3988N/A /* Avoid erroring on trailing whitespace. This is probably rare
3988N/A * so save the work for the error case instead of always trying
4890N/A * to strip the trailing whitespace with strstrip(). */
3988N/A while (isblank(*linepos))
3988N/A linepos++;
3988N/A
3988N/A /* If we aren't at the end of the line, this is a parsing error.
3988N/A * Make a best effort to describe where the problem is. */
3988N/A if (*linepos != '\n') {
3988N/A char buf[2] = {linepos[1]};
3988N/A _cleanup_free_ char *tmp;
3988N/A
3988N/A tmp = cescape(buf);
4011N/A log_error("invalid key/value pair in file %s on line %u,"
4988N/A "starting at character %tu ('%s')\n",
3988N/A filename, lineno, linepos - line + 1, tmp);
3988N/A if (linepos[1] == '#')
3988N/A log_info("hint: comments can only start at beginning of line");
3988N/A }
3988N/A break;
3988N/A }
3988N/A
3988N/A if (streq(key, "ACTION")) {
3988N/A if (op > OP_MATCH_MAX) {
3988N/A log_error("invalid ACTION operation\n");
3988N/A goto invalid;
3988N/A }
4066N/A rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
3988N/A continue;
3988N/A }
3988N/A
3988N/A if (streq(key, "DEVPATH")) {
3988N/A if (op > OP_MATCH_MAX) {
3988N/A log_error("invalid DEVPATH operation\n");
4988N/A goto invalid;
3988N/A }
3988N/A rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
3988N/A continue;
3988N/A }
3988N/A
3988N/A if (streq(key, "KERNEL")) {
3988N/A if (op > OP_MATCH_MAX) {
3988N/A log_error("invalid KERNEL operation\n");
3988N/A goto invalid;
3988N/A }
3988N/A rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
3988N/A continue;
3988N/A }
3988N/A
3988N/A if (streq(key, "SUBSYSTEM")) {
3988N/A if (op > OP_MATCH_MAX) {
3988N/A log_error("invalid SUBSYSTEM operation\n");
3988N/A goto invalid;
3988N/A }
3988N/A /* bus, class, subsystem events should all be the same */
3988N/A if (streq(value, "subsystem") ||
3988N/A streq(value, "bus") ||
3988N/A streq(value, "class")) {
3988N/A if (streq(value, "bus") || streq(value, "class"))
3988N/A log_error("'%s' must be specified as 'subsystem' \n"
3988N/A "please fix it in %s:%u", value, filename, lineno);
3988N/A rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
3988N/A } else
3988N/A rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
3988N/A continue;
3988N/A }
3988N/A
3988N/A if (streq(key, "DRIVER")) {
3988N/A if (op > OP_MATCH_MAX) {
3988N/A log_error("invalid DRIVER operation\n");
4988N/A goto invalid;
3988N/A }
4988N/A rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
3988N/A continue;
3988N/A }
3988N/A
3988N/A if (startswith(key, "ATTR{")) {
3988N/A attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
6755N/A if (attr == NULL) {
3988N/A log_error("error parsing ATTR attribute\n");
3988N/A goto invalid;
3988N/A }
3988N/A if (op < OP_MATCH_MAX) {
3988N/A rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
3988N/A } else {
3988N/A rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
3988N/A }
3988N/A continue;
3988N/A }
3988N/A
3988N/A if (startswith(key, "SECLABEL{")) {
3988N/A attr = get_key_attribute(rules->udev, key + sizeof("SECLABEL")-1);
3988N/A if (!attr) {
3988N/A log_error("error parsing SECLABEL attribute\n");
3988N/A goto invalid;
4011N/A }
4988N/A
3988N/A rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr);
3988N/A continue;
3988N/A }
3988N/A
3988N/A if (streq(key, "KERNELS")) {
3988N/A if (op > OP_MATCH_MAX) {
3988N/A log_error("invalid KERNELS operation\n");
3988N/A goto invalid;
3988N/A }
3988N/A rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
3988N/A continue;
3988N/A }
3988N/A
3988N/A if (streq(key, "SUBSYSTEMS")) {
3988N/A if (op > OP_MATCH_MAX) {
3988N/A log_error("invalid SUBSYSTEMS operation\n");
3988N/A goto invalid;
3988N/A }
3988N/A rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
3988N/A continue;
3988N/A }
3988N/A
3988N/A if (streq(key, "DRIVERS")) {
3988N/A if (op > OP_MATCH_MAX) {
4011N/A log_error("invalid DRIVERS operation\n");
6756N/A goto invalid;
6756N/A }
6756N/A rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
6756N/A continue;
6756N/A }
6756N/A
3988N/A if (startswith(key, "ATTRS{")) {
3988N/A if (op > OP_MATCH_MAX) {
4988N/A log_error("invalid ATTRS operation\n");
3988N/A goto invalid;
3988N/A }
3988N/A attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
3988N/A if (attr == NULL) {
3988N/A log_error("error parsing ATTRS attribute\n");
3988N/A goto invalid;
3988N/A }
3988N/A if (startswith(attr, "device/"))
3988N/A log_error("the 'device' link may not be available in a future kernel, "
3988N/A "please fix it in %s:%u", filename, lineno);
3988N/A else if (strstr(attr, "../") != NULL)
3988N/A log_error("do not reference parent sysfs directories directly, "
3988N/A "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
3988N/A rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
3988N/A continue;
3988N/A }
4011N/A
4988N/A if (streq(key, "TAGS")) {
3988N/A if (op > OP_MATCH_MAX) {
3988N/A log_error("invalid TAGS operation\n");
3988N/A goto invalid;
3988N/A }
3988N/A rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
3988N/A continue;
3988N/A }
3988N/A
3988N/A if (startswith(key, "ENV{")) {
3988N/A attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
3988N/A if (attr == NULL) {
3988N/A log_error("error parsing ENV attribute\n");
3988N/A goto invalid;
3988N/A }
3988N/A if (op < OP_MATCH_MAX) {
3988N/A if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
3988N/A goto invalid;
3988N/A } else {
3988N/A static const char *blacklist[] = {
3988N/A "ACTION",
3988N/A "SUBSYSTEM",
3988N/A "DEVTYPE",
3988N/A "MAJOR",
3988N/A "MINOR",
4011N/A "DRIVER",
6756N/A "IFINDEX",
6756N/A "DEVNAME",
6756N/A "DEVLINKS",
6756N/A "DEVPATH",
6756N/A "TAGS",
6756N/A };
3988N/A unsigned int i;
3988N/A
4988N/A for (i = 0; i < ELEMENTSOF(blacklist); i++) {
3988N/A if (!streq(attr, blacklist[i]))
3988N/A continue;
3988N/A log_error("invalid ENV attribute, '%s' can not be set %s:%u\n", attr, filename, lineno);
3988N/A goto invalid;
4011N/A }
4011N/A if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
4011N/A goto invalid;
4066N/A }
4011N/A continue;
4011N/A }
4011N/A
4011N/A if (streq(key, "TAG")) {
4011N/A if (op < OP_MATCH_MAX)
4011N/A rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
4011N/A else
4011N/A rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
4011N/A continue;
4988N/A }
4011N/A
4011N/A if (streq(key, "PROGRAM")) {
4011N/A rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
4011N/A continue;
4011N/A }
4011N/A
4011N/A if (streq(key, "RESULT")) {
4011N/A if (op > OP_MATCH_MAX) {
4011N/A log_error("invalid RESULT operation\n");
4011N/A goto invalid;
4011N/A }
4011N/A rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
4011N/A continue;
4011N/A }
4011N/A
4011N/A if (startswith(key, "IMPORT")) {
4011N/A attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
4011N/A if (attr == NULL) {
4011N/A log_error("IMPORT{} type missing, ignoring IMPORT %s:%u\n", filename, lineno);
4011N/A continue;
6339N/A }
4011N/A if (streq(attr, "program")) {
4011N/A /* find known built-in command */
4011N/A if (value[0] != '/') {
4011N/A enum udev_builtin_cmd cmd;
4011N/A
4011N/A cmd = udev_builtin_lookup(value);
4011N/A if (cmd < UDEV_BUILTIN_MAX) {
4011N/A log_debug("IMPORT found builtin '%s', replacing %s:%u\n",
4011N/A value, filename, lineno);
4011N/A rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
4011N/A continue;
4011N/A }
4053N/A }
4053N/A rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
6756N/A } else if (streq(attr, "builtin")) {
6756N/A enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
6756N/A
6756N/A if (cmd < UDEV_BUILTIN_MAX)
6756N/A rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
6756N/A else
4011N/A log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
4011N/A } else if (streq(attr, "file")) {
4011N/A rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
4011N/A } else if (streq(attr, "db")) {
4011N/A rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
4011N/A } else if (streq(attr, "cmdline")) {
4011N/A rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
4011N/A } else if (streq(attr, "parent")) {
4011N/A rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
4011N/A } else
4011N/A log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u\n", filename, lineno);
4011N/A continue;
4011N/A }
4011N/A
4011N/A if (startswith(key, "TEST")) {
4011N/A mode_t mode = 0;
4011N/A
4011N/A if (op > OP_MATCH_MAX) {
4011N/A log_error("invalid TEST operation\n");
4011N/A goto invalid;
4011N/A }
4011N/A attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1);
4011N/A if (attr != NULL) {
4812N/A mode = strtol(attr, NULL, 8);
4011N/A rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
4812N/A } else {
4011N/A rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
4011N/A }
4988N/A continue;
4011N/A }
4011N/A
4011N/A if (startswith(key, "RUN")) {
4011N/A attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1);
4011N/A if (attr == NULL)
4011N/A attr = "program";
4011N/A
4066N/A if (streq(attr, "builtin")) {
4011N/A enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
4011N/A
4011N/A if (cmd < UDEV_BUILTIN_MAX)
4011N/A rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
4011N/A else
4011N/A log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno);
4011N/A } else if (streq(attr, "program")) {
4011N/A enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
4011N/A
4988N/A rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
4011N/A } else {
4011N/A log_error("RUN{} unknown type, ignoring RUN %s:%u\n", filename, lineno);
4011N/A }
4011N/A
4011N/A continue;
4011N/A }
4011N/A
4011N/A if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) {
4011N/A rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
4011N/A continue;
4011N/A }
4011N/A
4011N/A if (streq(key, "LABEL")) {
4011N/A rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
4011N/A continue;
4011N/A }
6339N/A
4011N/A if (streq(key, "GOTO")) {
4011N/A rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
4011N/A continue;
4011N/A }
4011N/A
4011N/A if (startswith(key, "NAME")) {
4011N/A if (op < OP_MATCH_MAX) {
4011N/A rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
4011N/A } else {
4011N/A if (streq(value, "%k")) {
4011N/A log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
4011N/A "please remove it from %s:%u\n", filename, lineno);
4011N/A continue;
4988N/A }
4011N/A if (value[0] == '\0') {
4011N/A log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
4011N/A "please remove it from %s:%u\n", filename, lineno);
4011N/A continue;
3988N/A }
3988N/A rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
3988N/A }
4066N/A rule_tmp.rule.rule.can_set_name = true;
3988N/A continue;
3988N/A }
3988N/A
3988N/A if (streq(key, "SYMLINK")) {
3988N/A if (op < OP_MATCH_MAX)
3988N/A rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
3988N/A else
3988N/A rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
3988N/A rule_tmp.rule.rule.can_set_name = true;
4011N/A continue;
4988N/A }
3988N/A
3988N/A if (streq(key, "OWNER")) {
3988N/A uid_t uid;
3988N/A char *endptr;
3988N/A
3988N/A uid = strtoul(value, &endptr, 10);
3988N/A if (endptr[0] == '\0') {
3988N/A rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
3988N/A } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
3988N/A uid = add_uid(rules, value);
3988N/A rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
3988N/A } else if (rules->resolve_names >= 0) {
3988N/A rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
3988N/A }
3988N/A rule_tmp.rule.rule.can_set_name = true;
3988N/A continue;
3988N/A }
3988N/A
3988N/A if (streq(key, "GROUP")) {
3988N/A gid_t gid;
3988N/A char *endptr;
3988N/A
3988N/A gid = strtoul(value, &endptr, 10);
3988N/A if (endptr[0] == '\0') {
3988N/A rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
3988N/A } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
3988N/A gid = add_gid(rules, value);
4011N/A rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
6756N/A } else if (rules->resolve_names >= 0) {
6756N/A rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
6756N/A }
6756N/A rule_tmp.rule.rule.can_set_name = true;
6756N/A continue;
6756N/A }
3988N/A
3988N/A if (streq(key, "MODE")) {
3988N/A mode_t mode;
3988N/A char *endptr;
3988N/A
3988N/A mode = strtol(value, &endptr, 8);
3988N/A if (endptr[0] == '\0')
3988N/A rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
3988N/A else
3988N/A rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
3988N/A rule_tmp.rule.rule.can_set_name = true;
3988N/A continue;
3988N/A }
3988N/A
3988N/A if (streq(key, "OPTIONS")) {
3988N/A const char *pos;
4011N/A
6756N/A pos = strstr(value, "link_priority=");
6756N/A if (pos != NULL) {
6756N/A int prio = atoi(&pos[strlen("link_priority=")]);
6756N/A
6756N/A rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
6756N/A }
3988N/A
3988N/A pos = strstr(value, "event_timeout=");
3988N/A if (pos != NULL) {
3988N/A int tout = atoi(&pos[strlen("event_timeout=")]);
3988N/A
3988N/A rule_add_key(&rule_tmp, TK_M_EVENT_TIMEOUT, op, NULL, &tout);
3988N/A }
3988N/A
3988N/A pos = strstr(value, "string_escape=");
3988N/A if (pos != NULL) {
3988N/A pos = &pos[strlen("string_escape=")];
3988N/A if (startswith(pos, "none"))
3988N/A rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
3988N/A else if (startswith(pos, "replace"))
3988N/A rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
4011N/A }
6756N/A
6756N/A pos = strstr(value, "db_persist");
6756N/A if (pos != NULL)
6756N/A rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
6756N/A
6756N/A pos = strstr(value, "nowatch");
6756N/A if (pos != NULL) {
3988N/A const int off = 0;
3988N/A
3988N/A rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
4988N/A } else {
3988N/A pos = strstr(value, "watch");
3988N/A if (pos != NULL) {
3988N/A const int on = 1;
3988N/A
3988N/A rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
3988N/A }
3988N/A }
4066N/A
3988N/A pos = strstr(value, "static_node=");
3988N/A if (pos != NULL) {
3988N/A rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
3988N/A rule_tmp.rule.rule.has_static_node = true;
3988N/A }
3988N/A
3988N/A continue;
3988N/A }
4011N/A
4988N/A log_error("unknown key '%s' in %s:%u\n", key, filename, lineno);
3988N/A goto invalid;
3988N/A }
3988N/A
3988N/A /* add rule token */
3988N/A rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
3988N/A if (add_token(rules, &rule_tmp.rule) != 0)
3988N/A goto invalid;
3988N/A
3988N/A /* add tokens to list, sorted by type */
3988N/A if (sort_token(rules, &rule_tmp) != 0)
3988N/A goto invalid;
3988N/A
3988N/A return 0;
3988N/Ainvalid:
3988N/A log_error("invalid rule '%s:%u'\n", filename, lineno);
3988N/A return -1;
3988N/A}
3988N/A
3988N/Astatic int parse_file(struct udev_rules *rules, const char *filename)
3988N/A{
3988N/A FILE *f;
3988N/A unsigned int first_token;
3988N/A unsigned int filename_off;
3988N/A char line[UTIL_LINE_SIZE];
3988N/A int line_nr = 0;
3988N/A unsigned int i;
3988N/A
4011N/A if (null_or_empty_path(filename)) {
6756N/A log_debug("skip empty file: %s\n", filename);
6756N/A return 0;
6756N/A }
6756N/A log_debug("read rules file: %s\n", filename);
6756N/A
6756N/A f = fopen(filename, "re");
6756N/A if (f == NULL)
4134N/A return -1;
3988N/A
3988N/A first_token = rules->token_cur;
3988N/A filename_off = rules_add_string(rules, filename);
3988N/A
3988N/A while (fgets(line, sizeof(line), f) != NULL) {
3988N/A char *key;
3988N/A size_t len;
3988N/A
3988N/A /* skip whitespace */
3988N/A line_nr++;
3988N/A key = line;
3988N/A while (isspace(key[0]))
3988N/A key++;
3988N/A
3988N/A /* comment */
3988N/A if (key[0] == '#')
3988N/A continue;
4011N/A
6756N/A len = strlen(line);
6756N/A if (len < 3)
6756N/A continue;
6756N/A
6756N/A /* continue reading if backslash+newline is found */
6756N/A while (line[len-2] == '\\') {
6756N/A if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
6756N/A break;
6756N/A if (strlen(&line[len-2]) < 2)
3988N/A break;
3988N/A line_nr++;
3988N/A len = strlen(line);
3988N/A }
3988N/A
3988N/A if (len+1 >= sizeof(line)) {
3988N/A log_error("line too long '%s':%u, ignored\n", filename, line_nr);
3988N/A continue;
3988N/A }
3988N/A add_rule(rules, key, filename, filename_off, line_nr);
3988N/A }
3988N/A fclose(f);
3988N/A
3988N/A /* link GOTOs to LABEL rules in this file to be able to fast-forward */
3988N/A for (i = first_token+1; i < rules->token_cur; i++) {
4011N/A if (rules->tokens[i].type == TK_A_GOTO) {
6756N/A char *label = rules_str(rules, rules->tokens[i].key.value_off);
6756N/A unsigned int j;
6756N/A
6756N/A for (j = i+1; j < rules->token_cur; j++) {
6756N/A if (rules->tokens[j].type != TK_RULE)
6756N/A continue;
6756N/A if (rules->tokens[j].rule.label_off == 0)
6756N/A continue;
6756N/A if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
3988N/A continue;
3988N/A rules->tokens[i].key.rule_goto = j;
4988N/A break;
3988N/A }
3988N/A if (rules->tokens[i].key.rule_goto == 0)
3988N/A log_error("GOTO '%s' has no matching label in: '%s'\n", label, filename);
3988N/A }
3988N/A }
3988N/A return 0;
3988N/A}
3988N/A
3988N/Astruct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
3988N/A{
3988N/A struct udev_rules *rules;
3988N/A struct udev_list file_list;
3988N/A struct token end_token;
3988N/A char **files, **f;
4988N/A int r;
3988N/A
3988N/A rules = calloc(1, sizeof(struct udev_rules));
3988N/A if (rules == NULL)
3988N/A return NULL;
3988N/A rules->udev = udev;
3988N/A rules->resolve_names = resolve_names;
3988N/A udev_list_init(udev, &file_list, true);
3988N/A
3988N/A /* init token array and string buffer */
3988N/A rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
6755N/A if (rules->tokens == NULL)
3988N/A return udev_rules_unref(rules);
3988N/A rules->token_max = PREALLOC_TOKEN;
6678N/A
3988N/A rules->strbuf = strbuf_new();
6755N/A if (!rules->strbuf)
3988N/A return udev_rules_unref(rules);
3988N/A
3988N/A rules->dirs = strv_new("/etc/udev/rules.d",
3988N/A "/run/udev/rules.d",
3988N/A UDEVLIBEXECDIR "/rules.d",
3988N/A NULL);
3988N/A if (!rules->dirs) {
3988N/A log_error("failed to build config directory array");
6300N/A return udev_rules_unref(rules);
4199N/A }
3988N/A if (!path_strv_canonicalize(rules->dirs)) {
3988N/A log_error("failed to canonicalize config directories\n");
3988N/A return udev_rules_unref(rules);
3988N/A }
3988N/A strv_uniq(rules->dirs);
3988N/A
3988N/A rules->dirs_ts_usec = calloc(strv_length(rules->dirs), sizeof(usec_t));
3988N/A if(!rules->dirs_ts_usec)
3988N/A return udev_rules_unref(rules);
3988N/A udev_rules_check_timestamp(rules);
3988N/A
3988N/A r = conf_files_list_strv(&files, ".rules", NULL, (const char **)rules->dirs);
3988N/A if (r < 0) {
3988N/A log_error("failed to enumerate rules files: %s\n", strerror(-r));
6339N/A return udev_rules_unref(rules);
3988N/A }
3988N/A
3988N/A /*
3988N/A * The offset value in the rules strct is limited; add all
3988N/A * rules file names to the beginning of the string buffer.
3988N/A */
3988N/A STRV_FOREACH(f, files)
3988N/A rules_add_string(rules, *f);
3988N/A
3988N/A STRV_FOREACH(f, files)
3988N/A parse_file(rules, *f);
3988N/A
3988N/A strv_free(files);
3988N/A
4802N/A memset(&end_token, 0x00, sizeof(struct token));
3988N/A end_token.type = TK_END;
3988N/A add_token(rules, &end_token);
3988N/A log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings\n",
3988N/A rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
3988N/A
6339N/A /* cleanup temporary strbuf data */
6339N/A log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used\n",
3988N/A rules->strbuf->in_count, rules->strbuf->in_len,
3988N/A rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count);
6339N/A strbuf_complete(rules->strbuf);
4802N/A
3988N/A /* cleanup uid/gid cache */
6339N/A free(rules->uids);
3988N/A rules->uids = NULL;
3988N/A rules->uids_cur = 0;
3988N/A rules->uids_max = 0;
3988N/A free(rules->gids);
3988N/A rules->gids = NULL;
3988N/A rules->gids_cur = 0;
3988N/A rules->gids_max = 0;
3988N/A
6153N/A dump_rules(rules);
6153N/A return rules;
4988N/A}
4988N/A
4988N/Astruct udev_rules *udev_rules_unref(struct udev_rules *rules)
4988N/A{
4988N/A if (rules == NULL)
4988N/A return NULL;
4988N/A free(rules->tokens);
4988N/A strbuf_cleanup(rules->strbuf);
5339N/A free(rules->uids);
5339N/A free(rules->gids);
4988N/A strv_free(rules->dirs);
4988N/A free(rules->dirs_ts_usec);
3988N/A free(rules);
3988N/A return NULL;
}
bool udev_rules_check_timestamp(struct udev_rules *rules)
{
return paths_check_timestamp(rules->dirs, rules->dirs_ts_usec, true);
}
static int match_key(struct udev_rules *rules, struct token *token, const char *val)
{
char *key_value = rules_str(rules, token->key.value_off);
char *pos;
bool match = false;
if (val == NULL)
val = "";
switch (token->key.glob) {
case GL_PLAIN:
match = (streq(key_value, val));
break;
case GL_GLOB:
match = (fnmatch(key_value, val, 0) == 0);
break;
case GL_SPLIT:
{
const char *s;
size_t len;
s = rules_str(rules, token->key.value_off);
len = strlen(val);
for (;;) {
const char *next;
next = strchr(s, '|');
if (next != NULL) {
size_t matchlen = (size_t)(next - s);
match = (matchlen == len && strneq(s, val, matchlen));
if (match)
break;
} else {
match = (streq(s, val));
break;
}
s = &next[1];
}
break;
}
case GL_SPLIT_GLOB:
{
char value[UTIL_PATH_SIZE];
strscpy(value, sizeof(value), rules_str(rules, token->key.value_off));
key_value = value;
while (key_value != NULL) {
pos = strchr(key_value, '|');
if (pos != NULL) {
pos[0] = '\0';
pos = &pos[1];
}
match = (fnmatch(key_value, val, 0) == 0);
if (match)
break;
key_value = pos;
}
break;
}
case GL_SOMETHING:
match = (val[0] != '\0');
break;
case GL_UNSET:
return -1;
}
if (match && (token->key.op == OP_MATCH))
return 0;
if (!match && (token->key.op == OP_NOMATCH))
return 0;
return -1;
}
static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
{
const char *name;
char nbuf[UTIL_NAME_SIZE];
const char *value;
char vbuf[UTIL_NAME_SIZE];
size_t len;
name = rules_str(rules, cur->key.attr_off);
switch (cur->key.attrsubst) {
case SB_FORMAT:
udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
name = nbuf;
/* fall through */
case SB_NONE:
value = udev_device_get_sysattr_value(dev, name);
if (value == NULL)
return -1;
break;
case SB_SUBSYS:
if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
return -1;
value = vbuf;
break;
default:
return -1;
}
/* remove trailing whitespace, if not asked to match for it */
len = strlen(value);
if (len > 0 && isspace(value[len-1])) {
const char *key_value;
size_t klen;
key_value = rules_str(rules, cur->key.value_off);
klen = strlen(key_value);
if (klen > 0 && !isspace(key_value[klen-1])) {
if (value != vbuf) {
strscpy(vbuf, sizeof(vbuf), value);
value = vbuf;
}
while (len > 0 && isspace(vbuf[--len]))
vbuf[len] = '\0';
}
}
return match_key(rules, cur, value);
}
enum escape_type {
ESCAPE_UNSET,
ESCAPE_NONE,
ESCAPE_REPLACE,
};
int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event, const sigset_t *sigmask)
{
struct token *cur;
struct token *rule;
enum escape_type esc = ESCAPE_UNSET;
bool can_set_name;
if (rules->tokens == NULL)
return -1;
can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) &&
(major(udev_device_get_devnum(event->dev)) > 0 ||
udev_device_get_ifindex(event->dev) > 0));
/* loop through token list, match, run actions or forward to next rule */
cur = &rules->tokens[0];
rule = cur;
for (;;) {
dump_token(rules, cur);
switch (cur->type) {
case TK_RULE:
/* current rule */
rule = cur;
/* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
if (!can_set_name && rule->rule.can_set_name)
goto nomatch;
esc = ESCAPE_UNSET;
break;
case TK_M_ACTION:
if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
goto nomatch;
break;
case TK_M_DEVPATH:
if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
goto nomatch;
break;
case TK_M_KERNEL:
if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
goto nomatch;
break;
case TK_M_DEVLINK: {
struct udev_list_entry *list_entry;
bool match = false;
udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
const char *devlink;
devlink = udev_list_entry_get_name(list_entry) + strlen("/dev/");
if (match_key(rules, cur, devlink) == 0) {
match = true;
break;
}
}
if (!match)
goto nomatch;
break;
}
case TK_M_NAME:
if (match_key(rules, cur, event->name) != 0)
goto nomatch;
break;
case TK_M_ENV: {
const char *key_name = rules_str(rules, cur->key.attr_off);
const char *value;
value = udev_device_get_property_value(event->dev, key_name);
if (value == NULL)
value = "";
if (match_key(rules, cur, value))
goto nomatch;
break;
}
case TK_M_TAG: {
struct udev_list_entry *list_entry;
bool match = false;
udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
if (streq(rules_str(rules, cur->key.value_off), udev_list_entry_get_name(list_entry))) {
match = true;
break;
}
}
if (!match && (cur->key.op != OP_NOMATCH))
goto nomatch;
break;
}
case TK_M_SUBSYSTEM:
if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
goto nomatch;
break;
case TK_M_DRIVER:
if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
goto nomatch;
break;
case TK_M_WAITFOR: {
char filename[UTIL_PATH_SIZE];
int found;
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
found = (wait_for_file(event->dev, filename, 10) == 0);
if (!found && (cur->key.op != OP_NOMATCH))
goto nomatch;
break;
}
case TK_M_ATTR:
if (match_attr(rules, event->dev, event, cur) != 0)
goto nomatch;
break;
case TK_M_KERNELS:
case TK_M_SUBSYSTEMS:
case TK_M_DRIVERS:
case TK_M_ATTRS:
case TK_M_TAGS: {
struct token *next;
/* get whole sequence of parent matches */
next = cur;
while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
next++;
/* loop over parents */
event->dev_parent = event->dev;
for (;;) {
struct token *key;
/* loop over sequence of parent match keys */
for (key = cur; key < next; key++ ) {
dump_token(rules, key);
switch(key->type) {
case TK_M_KERNELS:
if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
goto try_parent;
break;
case TK_M_SUBSYSTEMS:
if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
goto try_parent;
break;
case TK_M_DRIVERS:
if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
goto try_parent;
break;
case TK_M_ATTRS:
if (match_attr(rules, event->dev_parent, event, key) != 0)
goto try_parent;
break;
case TK_M_TAGS: {
bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
if (match && key->key.op == OP_NOMATCH)
goto try_parent;
if (!match && key->key.op == OP_MATCH)
goto try_parent;
break;
}
default:
goto nomatch;
}
}
break;
try_parent:
event->dev_parent = udev_device_get_parent(event->dev_parent);
if (event->dev_parent == NULL)
goto nomatch;
}
/* move behind our sequence of parent match keys */
cur = next;
continue;
}
case TK_M_TEST: {
char filename[UTIL_PATH_SIZE];
struct stat statbuf;
int match;
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
if (filename[0] != '/') {
char tmp[UTIL_PATH_SIZE];
strscpy(tmp, sizeof(tmp), filename);
strscpyl(filename, sizeof(filename),
udev_device_get_syspath(event->dev), "/", tmp, NULL);
}
}
attr_subst_subdir(filename, sizeof(filename));
match = (stat(filename, &statbuf) == 0);
if (match && cur->key.mode > 0)
match = ((statbuf.st_mode & cur->key.mode) > 0);
if (match && cur->key.op == OP_NOMATCH)
goto nomatch;
if (!match && cur->key.op == OP_MATCH)
goto nomatch;
break;
}
case TK_M_EVENT_TIMEOUT:
log_debug("OPTIONS event_timeout=%u\n", cur->key.event_timeout);
event->timeout_usec = cur->key.event_timeout * 1000 * 1000;
break;
case TK_M_PROGRAM: {
char program[UTIL_PATH_SIZE];
char **envp;
char result[UTIL_PATH_SIZE];
free(event->program_result);
event->program_result = NULL;
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program));
envp = udev_device_get_properties_envp(event->dev);
log_debug("PROGRAM '%s' %s:%u\n",
program,
rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line);
if (udev_event_spawn(event, program, envp, sigmask, result, sizeof(result)) < 0) {
if (cur->key.op != OP_NOMATCH)
goto nomatch;
} else {
int count;
util_remove_trailing_chars(result, '\n');
if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
if (count > 0)
log_debug("%i character(s) replaced\n" , count);
}
event->program_result = strdup(result);
if (cur->key.op == OP_NOMATCH)
goto nomatch;
}
break;
}
case TK_M_IMPORT_FILE: {
char import[UTIL_PATH_SIZE];
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
if (import_file_into_properties(event->dev, import) != 0)
if (cur->key.op != OP_NOMATCH)
goto nomatch;
break;
}
case TK_M_IMPORT_PROG: {
char import[UTIL_PATH_SIZE];
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
log_debug("IMPORT '%s' %s:%u\n",
import,
rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line);
if (import_program_into_properties(event, import, sigmask) != 0)
if (cur->key.op != OP_NOMATCH)
goto nomatch;
break;
}
case TK_M_IMPORT_BUILTIN: {
char command[UTIL_PATH_SIZE];
if (udev_builtin_run_once(cur->key.builtin_cmd)) {
/* check if we ran already */
if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
log_debug("IMPORT builtin skip '%s' %s:%u\n",
udev_builtin_name(cur->key.builtin_cmd),
rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line);
/* return the result from earlier run */
if (event->builtin_ret & (1 << cur->key.builtin_cmd))
if (cur->key.op != OP_NOMATCH)
goto nomatch;
break;
}
/* mark as ran */
event->builtin_run |= (1 << cur->key.builtin_cmd);
}
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command));
log_debug("IMPORT builtin '%s' %s:%u\n",
udev_builtin_name(cur->key.builtin_cmd),
rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line);
if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
/* remember failure */
log_debug("IMPORT builtin '%s' returned non-zero\n",
udev_builtin_name(cur->key.builtin_cmd));
event->builtin_ret |= (1 << cur->key.builtin_cmd);
if (cur->key.op != OP_NOMATCH)
goto nomatch;
}
break;
}
case TK_M_IMPORT_DB: {
const char *key = rules_str(rules, cur->key.value_off);
const char *value;
value = udev_device_get_property_value(event->dev_db, key);
if (value != NULL) {
struct udev_list_entry *entry;
entry = udev_device_add_property(event->dev, key, value);
udev_list_entry_set_num(entry, true);
} else {
if (cur->key.op != OP_NOMATCH)
goto nomatch;
}
break;
}
case TK_M_IMPORT_CMDLINE: {
FILE *f;
bool imported = false;
f = fopen("/proc/cmdline", "re");
if (f != NULL) {
char cmdline[4096];
if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
const char *key = rules_str(rules, cur->key.value_off);
char *pos;
pos = strstr(cmdline, key);
if (pos != NULL) {
struct udev_list_entry *entry;
pos += strlen(key);
if (pos[0] == '\0' || isspace(pos[0])) {
/* we import simple flags as 'FLAG=1' */
entry = udev_device_add_property(event->dev, key, "1");
udev_list_entry_set_num(entry, true);
imported = true;
} else if (pos[0] == '=') {
const char *value;
pos++;
value = pos;
while (pos[0] != '\0' && !isspace(pos[0]))
pos++;
pos[0] = '\0';
entry = udev_device_add_property(event->dev, key, value);
udev_list_entry_set_num(entry, true);
imported = true;
}
}
}
fclose(f);
}
if (!imported && cur->key.op != OP_NOMATCH)
goto nomatch;
break;
}
case TK_M_IMPORT_PARENT: {
char import[UTIL_PATH_SIZE];
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
if (import_parent_into_properties(event->dev, import) != 0)
if (cur->key.op != OP_NOMATCH)
goto nomatch;
break;
}
case TK_M_RESULT:
if (match_key(rules, cur, event->program_result) != 0)
goto nomatch;
break;
case TK_A_STRING_ESCAPE_NONE:
esc = ESCAPE_NONE;
break;
case TK_A_STRING_ESCAPE_REPLACE:
esc = ESCAPE_REPLACE;
break;
case TK_A_DB_PERSIST:
udev_device_set_db_persist(event->dev);
break;
case TK_A_INOTIFY_WATCH:
if (event->inotify_watch_final)
break;
if (cur->key.op == OP_ASSIGN_FINAL)
event->inotify_watch_final = true;
event->inotify_watch = cur->key.watch;
break;
case TK_A_DEVLINK_PRIO:
udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
break;
case TK_A_OWNER: {
char owner[UTIL_NAME_SIZE];
if (event->owner_final)
break;
if (cur->key.op == OP_ASSIGN_FINAL)
event->owner_final = true;
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner));
event->owner_set = true;
event->uid = util_lookup_user(event->udev, owner);
log_debug("OWNER %u %s:%u\n",
event->uid,
rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line);
break;
}
case TK_A_GROUP: {
char group[UTIL_NAME_SIZE];
if (event->group_final)
break;
if (cur->key.op == OP_ASSIGN_FINAL)
event->group_final = true;
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group));
event->group_set = true;
event->gid = util_lookup_group(event->udev, group);
log_debug("GROUP %u %s:%u\n",
event->gid,
rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line);
break;
}
case TK_A_MODE: {
char mode_str[UTIL_NAME_SIZE];
mode_t mode;
char *endptr;
if (event->mode_final)
break;
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str));
mode = strtol(mode_str, &endptr, 8);
if (endptr[0] != '\0') {
log_error("ignoring invalid mode '%s'\n", mode_str);
break;
}
if (cur->key.op == OP_ASSIGN_FINAL)
event->mode_final = true;
event->mode_set = true;
event->mode = mode;
log_debug("MODE %#o %s:%u\n",
event->mode,
rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line);
break;
}
case TK_A_OWNER_ID:
if (event->owner_final)
break;
if (cur->key.op == OP_ASSIGN_FINAL)
event->owner_final = true;
event->owner_set = true;
event->uid = cur->key.uid;
log_debug("OWNER %u %s:%u\n",
event->uid,
rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line);
break;
case TK_A_GROUP_ID:
if (event->group_final)
break;
if (cur->key.op == OP_ASSIGN_FINAL)
event->group_final = true;
event->group_set = true;
event->gid = cur->key.gid;
log_debug("GROUP %u %s:%u\n",
event->gid,
rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line);
break;
case TK_A_MODE_ID:
if (event->mode_final)
break;
if (cur->key.op == OP_ASSIGN_FINAL)
event->mode_final = true;
event->mode_set = true;
event->mode = cur->key.mode;
log_debug("MODE %#o %s:%u\n",
event->mode,
rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line);
break;
case TK_A_SECLABEL: {
const char *name, *label;
name = rules_str(rules, cur->key.attr_off);
label = rules_str(rules, cur->key.value_off);
if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
udev_list_cleanup(&event->seclabel_list);
udev_list_entry_add(&event->seclabel_list, name, label);
log_debug("SECLABEL{%s}='%s' %s:%u\n",
name, label,
rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line);
break;
}
case TK_A_ENV: {
const char *name = rules_str(rules, cur->key.attr_off);
char *value = rules_str(rules, cur->key.value_off);
char value_new[UTIL_NAME_SIZE];
const char *value_old = NULL;
struct udev_list_entry *entry;
if (value[0] == '\0') {
if (cur->key.op == OP_ADD)
break;
udev_device_add_property(event->dev, name, NULL);
break;
}
if (cur->key.op == OP_ADD)
value_old = udev_device_get_property_value(event->dev, name);
if (value_old) {
char temp[UTIL_NAME_SIZE];
/* append value separated by space */
udev_event_apply_format(event, value, temp, sizeof(temp));
strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL);
} else
udev_event_apply_format(event, value, value_new, sizeof(value_new));
entry = udev_device_add_property(event->dev, name, value_new);
/* store in db, skip private keys */
if (name[0] != '.')
udev_list_entry_set_num(entry, true);
break;
}
case TK_A_TAG: {
char tag[UTIL_PATH_SIZE];
const char *p;
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag));
if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
udev_device_cleanup_tags_list(event->dev);
for (p = tag; *p != '\0'; p++) {
if ((*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z') ||
(*p >= '0' && *p <= '9') ||
*p == '-' || *p == '_')
continue;
log_error("ignoring invalid tag name '%s'\n", tag);
break;
}
udev_device_add_tag(event->dev, tag);
break;
}
case TK_A_NAME: {
const char *name = rules_str(rules, cur->key.value_off);
char name_str[UTIL_PATH_SIZE];
int count;
if (event->name_final)
break;
if (cur->key.op == OP_ASSIGN_FINAL)
event->name_final = true;
udev_event_apply_format(event, name, name_str, sizeof(name_str));
if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
count = util_replace_chars(name_str, "/");
if (count > 0)
log_debug("%i character(s) replaced\n", count);
}
if (major(udev_device_get_devnum(event->dev)) &&
(!streq(name_str, udev_device_get_devnode(event->dev) + strlen("/dev/")))) {
log_error("NAME=\"%s\" ignored, kernel device nodes "
"can not be renamed; please fix it in %s:%u\n", name,
rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
break;
}
free(event->name);
event->name = strdup(name_str);
log_debug("NAME '%s' %s:%u\n",
event->name,
rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line);
break;
}
case TK_A_DEVLINK: {
char temp[UTIL_PATH_SIZE];
char filename[UTIL_PATH_SIZE];
char *pos, *next;
int count = 0;
if (event->devlink_final)
break;
if (major(udev_device_get_devnum(event->dev)) == 0)
break;
if (cur->key.op == OP_ASSIGN_FINAL)
event->devlink_final = true;
if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
udev_device_cleanup_devlinks_list(event->dev);
/* allow multiple symlinks separated by spaces */
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp));
if (esc == ESCAPE_UNSET)
count = util_replace_chars(temp, "/ ");
else if (esc == ESCAPE_REPLACE)
count = util_replace_chars(temp, "/");
if (count > 0)
log_debug("%i character(s) replaced\n" , count);
pos = temp;
while (isspace(pos[0]))
pos++;
next = strchr(pos, ' ');
while (next != NULL) {
next[0] = '\0';
log_debug("LINK '%s' %s:%u\n", pos,
rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
udev_device_add_devlink(event->dev, filename);
while (isspace(next[1]))
next++;
pos = &next[1];
next = strchr(pos, ' ');
}
if (pos[0] != '\0') {
log_debug("LINK '%s' %s:%u\n", pos,
rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
udev_device_add_devlink(event->dev, filename);
}
break;
}
case TK_A_ATTR: {
const char *key_name = rules_str(rules, cur->key.attr_off);
char attr[UTIL_PATH_SIZE];
char value[UTIL_NAME_SIZE];
FILE *f;
if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
attr_subst_subdir(attr, sizeof(attr));
udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
log_debug("ATTR '%s' writing '%s' %s:%u\n", attr, value,
rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line);
f = fopen(attr, "we");
if (f != NULL) {
if (fprintf(f, "%s", value) <= 0)
log_error("error writing ATTR{%s}: %m\n", attr);
fclose(f);
} else {
log_error("error opening ATTR{%s} for writing: %m\n", attr);
}
break;
}
case TK_A_RUN_BUILTIN:
case TK_A_RUN_PROGRAM: {
struct udev_list_entry *entry;
if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
udev_list_cleanup(&event->run_list);
log_debug("RUN '%s' %s:%u\n",
rules_str(rules, cur->key.value_off),
rules_str(rules, rule->rule.filename_off),
rule->rule.filename_line);
entry = udev_list_entry_add(&event->run_list, rules_str(rules, cur->key.value_off), NULL);
udev_list_entry_set_num(entry, cur->key.builtin_cmd);
break;
}
case TK_A_GOTO:
if (cur->key.rule_goto == 0)
break;
cur = &rules->tokens[cur->key.rule_goto];
continue;
case TK_END:
return 0;
case TK_M_PARENTS_MIN:
case TK_M_PARENTS_MAX:
case TK_M_MAX:
case TK_UNSET:
log_error("wrong type %u\n", cur->type);
goto nomatch;
}
cur++;
continue;
nomatch:
/* fast-forward to next rule */
cur = rule + rule->rule.token_count;
}
}
int udev_rules_apply_static_dev_perms(struct udev_rules *rules)
{
struct token *cur;
struct token *rule;
uid_t uid = 0;
gid_t gid = 0;
mode_t mode = 0;
_cleanup_strv_free_ char **tags = NULL;
char **t;
FILE *f = NULL;
_cleanup_free_ char *path = NULL;
int r = 0;
if (rules->tokens == NULL)
return 0;
cur = &rules->tokens[0];
rule = cur;
for (;;) {
switch (cur->type) {
case TK_RULE:
/* current rule */
rule = cur;
/* skip rules without a static_node tag */
if (!rule->rule.has_static_node)
goto next;
uid = 0;
gid = 0;
mode = 0;
strv_free(tags);
tags = NULL;
break;
case TK_A_OWNER_ID:
uid = cur->key.uid;
break;
case TK_A_GROUP_ID:
gid = cur->key.gid;
break;
case TK_A_MODE_ID:
mode = cur->key.mode;
break;
case TK_A_TAG:
r = strv_extend(&tags, rules_str(rules, cur->key.value_off));
if (r < 0)
goto finish;
break;
case TK_A_STATIC_NODE: {
char device_node[UTIL_PATH_SIZE];
char tags_dir[UTIL_PATH_SIZE];
char tag_symlink[UTIL_PATH_SIZE];
struct stat stats;
/* we assure, that the permissions tokens are sorted before the static token */
if (mode == 0 && uid == 0 && gid == 0 && tags == NULL)
goto next;
strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL);
if (stat(device_node, &stats) != 0)
goto next;
if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
goto next;
if (tags) {
/* Export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
STRV_FOREACH(t, tags) {
_cleanup_free_ char *unescaped_filename = NULL;
strscpyl(tags_dir, sizeof(tags_dir), "/run/udev/static_node-tags/", *t, "/", NULL);
r = mkdir_p(tags_dir, 0755);
if (r < 0) {
log_error("failed to create %s: %s\n", tags_dir, strerror(-r));
return r;
}
unescaped_filename = xescape(rules_str(rules, cur->key.value_off), "/.");
strscpyl(tag_symlink, sizeof(tag_symlink), tags_dir, unescaped_filename, NULL);
r = symlink(device_node, tag_symlink);
if (r < 0 && errno != EEXIST) {
log_error("failed to create symlink %s -> %s: %s\n", tag_symlink, device_node, strerror(errno));
return -errno;
} else
r = 0;
}
}
/* don't touch the permissions if only the tags were set */
if (mode == 0 && uid == 0 && gid == 0)
goto next;
if (mode == 0) {
if (gid > 0)
mode = 0660;
else
mode = 0600;
}
if (mode != (stats.st_mode & 01777)) {
r = chmod(device_node, mode);
if (r < 0) {
log_error("failed to chmod '%s' %#o\n", device_node, mode);
return -errno;
} else
log_debug("chmod '%s' %#o\n", device_node, mode);
}
if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
r = chown(device_node, uid, gid);
if (r < 0) {
log_error("failed to chown '%s' %u %u \n", device_node, uid, gid);
return -errno;
} else
log_debug("chown '%s' %u %u\n", device_node, uid, gid);
}
utimensat(AT_FDCWD, device_node, NULL, 0);
break;
}
case TK_END:
goto finish;
}
cur++;
continue;
next:
/* fast-forward to next rule */
cur = rule + rule->rule.token_count;
continue;
}
finish:
if (f) {
fflush(f);
fchmod(fileno(f), 0644);
if (ferror(f) || rename(path, "/run/udev/static_node-tags") < 0) {
r = -errno;
unlink("/run/udev/static_node-tags");
unlink(path);
}
fclose(f);
}
return r;
}