2003-12-03 09:38:30 +07:00
|
|
|
/*
|
2004-01-27 10:21:58 +07:00
|
|
|
* Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
|
2008-09-10 07:40:42 +07:00
|
|
|
* Copyright (C) 2003-2008 Kay Sievers <kay.sievers@vrfy.org>
|
2003-12-03 09:38:30 +07:00
|
|
|
*
|
2008-09-10 07:40:42 +07:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2003-12-03 09:38:30 +07:00
|
|
|
*
|
2008-09-10 07:40:42 +07:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2003-12-03 09:38:30 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
2004-02-24 10:31:14 +07:00
|
|
|
#include <sys/stat.h>
|
2003-12-03 09:38:30 +07:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "udev.h"
|
2005-03-13 04:36:32 +07:00
|
|
|
#include "udev_rules.h"
|
2003-12-03 09:38:30 +07:00
|
|
|
|
2004-03-11 13:35:37 +07:00
|
|
|
|
2008-09-09 05:09:49 +07:00
|
|
|
void udev_rules_iter_init(struct udev_rules_iter *iter, struct udev_rules *rules)
|
2005-06-24 23:05:32 +07:00
|
|
|
{
|
2008-09-09 05:09:49 +07:00
|
|
|
dbg(iter->rules->udev, "bufsize=%zi\n", rules->bufsize);
|
|
|
|
iter->rules = rules;
|
|
|
|
iter->current = 0;
|
2005-06-24 23:05:32 +07:00
|
|
|
}
|
|
|
|
|
2008-09-09 05:09:49 +07:00
|
|
|
struct udev_rule *udev_rules_iter_next(struct udev_rules_iter *iter)
|
2005-06-24 23:05:32 +07:00
|
|
|
{
|
2008-09-09 05:09:49 +07:00
|
|
|
struct udev_rules *rules;
|
2008-09-02 00:46:19 +07:00
|
|
|
struct udev_rule *rule;
|
2005-06-24 23:05:32 +07:00
|
|
|
|
2008-09-09 05:09:49 +07:00
|
|
|
rules = iter->rules;
|
2005-07-05 20:24:41 +07:00
|
|
|
if (!rules)
|
|
|
|
return NULL;
|
2005-06-24 23:05:32 +07:00
|
|
|
|
2008-09-28 18:01:38 +07:00
|
|
|
dbg(rules->udev, "current=%zi\n", iter->current);
|
2008-09-09 05:09:49 +07:00
|
|
|
if (iter->current >= rules->bufsize) {
|
2008-09-28 18:01:38 +07:00
|
|
|
dbg(rules->udev, "no more rules\n");
|
2005-07-05 20:24:41 +07:00
|
|
|
return NULL;
|
2005-07-16 12:46:31 +07:00
|
|
|
}
|
2003-12-03 09:38:30 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
/* get next rule */
|
2008-09-09 05:09:49 +07:00
|
|
|
rule = (struct udev_rule *) (rules->buf + iter->current);
|
|
|
|
iter->current += sizeof(struct udev_rule) + rule->bufsize;
|
2005-07-05 20:24:41 +07:00
|
|
|
|
|
|
|
return rule;
|
2003-12-03 09:38:30 +07:00
|
|
|
}
|
2003-12-04 09:33:58 +07:00
|
|
|
|
2008-09-28 18:02:44 +07:00
|
|
|
struct udev_rule *udev_rules_iter_goto(struct udev_rules_iter *iter, size_t rule_off)
|
|
|
|
{
|
|
|
|
struct udev_rules *rules = iter->rules;
|
|
|
|
struct udev_rule *rule;
|
|
|
|
|
|
|
|
dbg(rules->udev "current=%zi\n", iter->current);
|
|
|
|
iter->current = rule_off;
|
|
|
|
rule = (struct udev_rule *) (rules->buf + iter->current);
|
|
|
|
|
|
|
|
return rule;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t find_label(const struct udev_rules_iter *iter, const char *label)
|
2005-07-16 12:46:31 +07:00
|
|
|
{
|
2008-09-02 00:46:19 +07:00
|
|
|
struct udev_rule *rule;
|
2008-09-09 05:09:49 +07:00
|
|
|
struct udev_rules *rules = iter->rules;
|
2008-09-28 18:02:44 +07:00
|
|
|
size_t current = iter->current;
|
2005-07-16 12:46:31 +07:00
|
|
|
|
|
|
|
next:
|
2008-09-28 18:02:44 +07:00
|
|
|
dbg(rules->udev, "current=%zi\n", current);
|
|
|
|
if (current >= rules->bufsize) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "LABEL='%s' not found, GOTO will be ignored\n", label);
|
2008-09-28 18:02:44 +07:00
|
|
|
return iter->current;
|
2005-07-16 12:46:31 +07:00
|
|
|
}
|
2008-09-28 18:02:44 +07:00
|
|
|
rule = (struct udev_rule *) (rules->buf + current);
|
2005-07-16 12:46:31 +07:00
|
|
|
|
|
|
|
if (strcmp(&rule->buf[rule->label.val_off], label) != 0) {
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "moving forward, looking for label '%s'\n", label);
|
2008-09-28 18:02:44 +07:00
|
|
|
current += sizeof(struct udev_rule) + rule->bufsize;
|
2005-07-16 12:46:31 +07:00
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "found label '%s'\n", label);
|
2008-09-28 18:02:44 +07:00
|
|
|
return current;
|
2005-07-16 12:46:31 +07:00
|
|
|
}
|
|
|
|
|
2008-09-06 20:45:31 +07:00
|
|
|
static int get_key(struct udev_rules *rules, char **line, char **key, enum key_operation *operation, char **value)
|
2005-03-13 11:46:31 +07:00
|
|
|
{
|
|
|
|
char *linepos;
|
|
|
|
char *temp;
|
|
|
|
|
|
|
|
linepos = *line;
|
2005-08-08 07:21:55 +07:00
|
|
|
if (linepos == NULL && linepos[0] == '\0')
|
2005-03-13 11:46:31 +07:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* skip whitespace */
|
|
|
|
while (isspace(linepos[0]) || linepos[0] == ',')
|
|
|
|
linepos++;
|
|
|
|
|
|
|
|
/* get the key */
|
2005-08-08 07:21:55 +07:00
|
|
|
if (linepos[0] == '\0')
|
|
|
|
return -1;
|
2005-03-13 11:46:31 +07:00
|
|
|
*key = linepos;
|
2005-08-08 07:21:55 +07:00
|
|
|
|
2005-03-13 11:46:31 +07:00
|
|
|
while (1) {
|
|
|
|
linepos++;
|
|
|
|
if (linepos[0] == '\0')
|
|
|
|
return -1;
|
|
|
|
if (isspace(linepos[0]))
|
|
|
|
break;
|
|
|
|
if (linepos[0] == '=')
|
|
|
|
break;
|
2007-02-25 07:04:23 +07:00
|
|
|
if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
|
|
|
|
if (linepos[1] == '=')
|
|
|
|
break;
|
2005-03-13 11:46:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* remember end of key */
|
|
|
|
temp = linepos;
|
|
|
|
|
|
|
|
/* skip whitespace after key */
|
|
|
|
while (isspace(linepos[0]))
|
|
|
|
linepos++;
|
2005-08-08 07:21:55 +07:00
|
|
|
if (linepos[0] == '\0')
|
|
|
|
return -1;
|
2005-03-13 11:46:31 +07:00
|
|
|
|
|
|
|
/* get operation type */
|
|
|
|
if (linepos[0] == '=' && linepos[1] == '=') {
|
|
|
|
*operation = KEY_OP_MATCH;
|
|
|
|
linepos += 2;
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "operator=match\n");
|
2005-03-13 11:46:31 +07:00
|
|
|
} else if (linepos[0] == '!' && linepos[1] == '=') {
|
|
|
|
*operation = KEY_OP_NOMATCH;
|
|
|
|
linepos += 2;
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "operator=nomatch\n");
|
2005-03-13 11:46:31 +07:00
|
|
|
} else if (linepos[0] == '+' && linepos[1] == '=') {
|
|
|
|
*operation = KEY_OP_ADD;
|
|
|
|
linepos += 2;
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "operator=add\n");
|
2005-03-13 11:46:31 +07:00
|
|
|
} else if (linepos[0] == '=') {
|
|
|
|
*operation = KEY_OP_ASSIGN;
|
|
|
|
linepos++;
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "operator=assign\n");
|
2005-06-05 09:57:03 +07:00
|
|
|
} else if (linepos[0] == ':' && linepos[1] == '=') {
|
|
|
|
*operation = KEY_OP_ASSIGN_FINAL;
|
|
|
|
linepos += 2;
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "operator=assign_final\n");
|
2005-03-13 11:46:31 +07:00
|
|
|
} else
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* terminate key */
|
|
|
|
temp[0] = '\0';
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "key='%s'\n", *key);
|
2005-03-13 11:46:31 +07:00
|
|
|
|
|
|
|
/* skip whitespace after operator */
|
|
|
|
while (isspace(linepos[0]))
|
|
|
|
linepos++;
|
2005-08-08 07:21:55 +07:00
|
|
|
if (linepos[0] == '\0')
|
|
|
|
return -1;
|
2005-03-13 11:46:31 +07:00
|
|
|
|
|
|
|
/* get the value*/
|
|
|
|
if (linepos[0] == '"')
|
|
|
|
linepos++;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
*value = linepos;
|
|
|
|
|
|
|
|
temp = strchr(linepos, '"');
|
|
|
|
if (!temp)
|
|
|
|
return -1;
|
|
|
|
temp[0] = '\0';
|
|
|
|
temp++;
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "value='%s'\n", *value);
|
2005-03-13 11:46:31 +07:00
|
|
|
|
|
|
|
/* move line to next key */
|
|
|
|
*line = temp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-13 18:50:44 +07:00
|
|
|
/* extract possible KEY{attr} */
|
2008-09-06 20:45:31 +07:00
|
|
|
static char *get_key_attribute(struct udev_rules *rules, char *str)
|
2004-02-17 12:39:40 +07:00
|
|
|
{
|
|
|
|
char *pos;
|
|
|
|
char *attr;
|
|
|
|
|
|
|
|
attr = strchr(str, '{');
|
|
|
|
if (attr != NULL) {
|
|
|
|
attr++;
|
|
|
|
pos = strchr(attr, '}');
|
|
|
|
if (pos == NULL) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "missing closing brace for format\n");
|
2004-02-17 12:39:40 +07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
pos[0] = '\0';
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "attribute='%s'\n", attr);
|
2004-02-17 12:39:40 +07:00
|
|
|
return attr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
static int add_rule_key(struct udev_rule *rule, struct key *key,
|
|
|
|
enum key_operation operation, const char *value)
|
2003-12-03 09:38:30 +07:00
|
|
|
{
|
2008-09-11 02:50:21 +07:00
|
|
|
size_t val_len = strnlen(value, UTIL_PATH_SIZE);
|
2005-07-05 20:24:41 +07:00
|
|
|
|
|
|
|
key->operation = operation;
|
|
|
|
|
|
|
|
key->val_off = rule->bufsize;
|
2008-09-10 23:59:42 +07:00
|
|
|
util_strlcpy(rule->buf + rule->bufsize, value, val_len+1);
|
2005-07-05 20:24:41 +07:00
|
|
|
rule->bufsize += val_len+1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-06 20:45:31 +07:00
|
|
|
static int add_rule_key_pair(struct udev_rules *rules, struct udev_rule *rule, struct key_pairs *pairs,
|
2005-07-05 20:24:41 +07:00
|
|
|
enum key_operation operation, const char *key, const char *value)
|
|
|
|
{
|
2008-09-11 02:50:21 +07:00
|
|
|
size_t key_len = strnlen(key, UTIL_PATH_SIZE);
|
2005-07-05 20:24:41 +07:00
|
|
|
|
|
|
|
if (pairs->count >= PAIRS_MAX) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "skip, too many keys of the same type in a single rule\n");
|
2005-07-05 20:24:41 +07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
|
|
|
|
|
|
|
|
/* add the key-name of the pair */
|
|
|
|
pairs->keys[pairs->count].key_name_off = rule->bufsize;
|
2008-09-10 23:59:42 +07:00
|
|
|
util_strlcpy(rule->buf + rule->bufsize, key, key_len+1);
|
2005-07-05 20:24:41 +07:00
|
|
|
rule->bufsize += key_len+1;
|
|
|
|
|
|
|
|
pairs->count++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-25 00:25:55 +07:00
|
|
|
static int add_to_rules(struct udev_rules *rules, char *line, const char *filename, unsigned int lineno)
|
2005-07-05 20:24:41 +07:00
|
|
|
{
|
2008-09-11 02:50:21 +07:00
|
|
|
char buf[sizeof(struct udev_rule) + UTIL_LINE_SIZE];
|
2005-07-05 20:24:41 +07:00
|
|
|
struct udev_rule *rule;
|
|
|
|
size_t rule_size;
|
|
|
|
int valid;
|
2005-03-13 11:46:31 +07:00
|
|
|
char *linepos;
|
2004-02-17 12:39:40 +07:00
|
|
|
char *attr;
|
2005-07-17 20:40:29 +07:00
|
|
|
size_t padding;
|
2006-09-19 03:14:18 +07:00
|
|
|
int physdev = 0;
|
2005-07-05 20:24:41 +07:00
|
|
|
int retval;
|
2003-12-03 09:38:30 +07:00
|
|
|
|
2007-06-22 06:27:02 +07:00
|
|
|
memset(buf, 0x00, sizeof(buf));
|
|
|
|
rule = (struct udev_rule *) buf;
|
2008-04-22 00:00:54 +07:00
|
|
|
rule->event_timeout = -1;
|
2005-07-05 20:24:41 +07:00
|
|
|
linepos = line;
|
|
|
|
valid = 0;
|
2003-12-03 09:38:30 +07:00
|
|
|
|
2007-06-22 06:27:02 +07:00
|
|
|
/* get all the keys */
|
2005-07-05 20:24:41 +07:00
|
|
|
while (1) {
|
|
|
|
char *key;
|
|
|
|
char *value;
|
|
|
|
enum key_operation operation = KEY_OP_UNSET;
|
2004-12-20 13:38:33 +07:00
|
|
|
|
2008-09-06 20:45:31 +07:00
|
|
|
retval = get_key(rules, &linepos, &key, &operation, &value);
|
2005-07-05 20:24:41 +07:00
|
|
|
if (retval)
|
|
|
|
break;
|
2003-12-03 09:38:30 +07:00
|
|
|
|
2006-08-19 21:06:25 +07:00
|
|
|
if (strcasecmp(key, "ACTION") == 0) {
|
|
|
|
if (operation != KEY_OP_MATCH &&
|
|
|
|
operation != KEY_OP_NOMATCH) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "invalid ACTION operation\n");
|
2006-08-19 21:06:25 +07:00
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
add_rule_key(rule, &rule->action, operation, value);
|
2005-07-16 12:46:31 +07:00
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-08-19 21:06:25 +07:00
|
|
|
if (strcasecmp(key, "DEVPATH") == 0) {
|
|
|
|
if (operation != KEY_OP_MATCH &&
|
|
|
|
operation != KEY_OP_NOMATCH) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "invalid DEVPATH operation\n");
|
2006-08-19 21:06:25 +07:00
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
add_rule_key(rule, &rule->devpath, operation, value);
|
2005-07-16 12:46:31 +07:00
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "KERNEL") == 0) {
|
2006-04-25 00:25:55 +07:00
|
|
|
if (operation != KEY_OP_MATCH &&
|
|
|
|
operation != KEY_OP_NOMATCH) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "invalid KERNEL operation\n");
|
2006-04-25 00:25:55 +07:00
|
|
|
goto invalid;
|
|
|
|
}
|
2006-08-19 21:06:25 +07:00
|
|
|
add_rule_key(rule, &rule->kernel, operation, value);
|
2005-07-05 20:24:41 +07:00
|
|
|
valid = 1;
|
2004-09-11 10:54:04 +07:00
|
|
|
continue;
|
|
|
|
}
|
2003-12-18 09:24:05 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "SUBSYSTEM") == 0) {
|
2006-04-25 00:25:55 +07:00
|
|
|
if (operation != KEY_OP_MATCH &&
|
|
|
|
operation != KEY_OP_NOMATCH) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "invalid SUBSYSTEM operation\n");
|
2006-04-25 00:25:55 +07:00
|
|
|
goto invalid;
|
|
|
|
}
|
2007-06-02 15:02:32 +07:00
|
|
|
/* bus, class, subsystem events should all be the same */
|
|
|
|
if (strcmp(value, "subsystem") == 0 ||
|
|
|
|
strcmp(value, "bus") == 0 ||
|
|
|
|
strcmp(value, "class") == 0) {
|
|
|
|
if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0)
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "'%s' must be specified as 'subsystem' \n"
|
2007-06-02 15:02:32 +07:00
|
|
|
"please fix it in %s:%u", value, filename, lineno);
|
|
|
|
add_rule_key(rule, &rule->subsystem, operation, "subsystem|class|bus");
|
|
|
|
} else
|
|
|
|
add_rule_key(rule, &rule->subsystem, operation, value);
|
2005-07-05 20:24:41 +07:00
|
|
|
valid = 1;
|
2004-09-15 07:45:48 +07:00
|
|
|
continue;
|
2005-07-05 20:24:41 +07:00
|
|
|
}
|
2004-09-11 10:54:04 +07:00
|
|
|
|
2006-08-19 21:06:25 +07:00
|
|
|
if (strcasecmp(key, "DRIVER") == 0) {
|
2007-02-25 05:43:04 +07:00
|
|
|
if (operation != KEY_OP_MATCH &&
|
|
|
|
operation != KEY_OP_NOMATCH) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "invalid DRIVER operation\n");
|
2007-02-25 05:43:04 +07:00
|
|
|
goto invalid;
|
|
|
|
}
|
2007-02-02 02:23:41 +07:00
|
|
|
add_rule_key(rule, &rule->driver, operation, value);
|
2005-07-05 20:24:41 +07:00
|
|
|
valid = 1;
|
2003-12-03 09:38:30 +07:00
|
|
|
continue;
|
2004-12-20 13:38:33 +07:00
|
|
|
}
|
2003-12-03 09:38:30 +07:00
|
|
|
|
2006-08-21 00:08:37 +07:00
|
|
|
if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
|
2008-09-06 20:45:31 +07:00
|
|
|
attr = get_key_attribute(rules, key + sizeof("ATTR")-1);
|
2006-08-19 21:06:25 +07:00
|
|
|
if (attr == NULL) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "error parsing ATTR attribute\n");
|
2006-08-20 23:15:29 +07:00
|
|
|
goto invalid;
|
2006-08-19 21:06:25 +07:00
|
|
|
}
|
2008-09-06 20:45:31 +07:00
|
|
|
if (add_rule_key_pair(rules, rule, &rule->attr, operation, attr, value) != 0)
|
2006-08-20 23:15:29 +07:00
|
|
|
goto invalid;
|
2005-07-05 20:24:41 +07:00
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2004-09-11 10:54:04 +07:00
|
|
|
|
2006-08-19 21:06:25 +07:00
|
|
|
if (strcasecmp(key, "KERNELS") == 0 ||
|
|
|
|
strcasecmp(key, "ID") == 0) {
|
2006-04-25 00:25:55 +07:00
|
|
|
if (operation != KEY_OP_MATCH &&
|
|
|
|
operation != KEY_OP_NOMATCH) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "invalid KERNELS operation\n");
|
2006-04-25 00:25:55 +07:00
|
|
|
goto invalid;
|
|
|
|
}
|
2006-08-19 21:06:25 +07:00
|
|
|
add_rule_key(rule, &rule->kernels, operation, value);
|
2005-07-05 20:24:41 +07:00
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-03-13 11:46:31 +07:00
|
|
|
|
2006-08-20 23:20:55 +07:00
|
|
|
if (strcasecmp(key, "SUBSYSTEMS") == 0 ||
|
2006-08-19 21:06:25 +07:00
|
|
|
strcasecmp(key, "BUS") == 0) {
|
2006-04-25 00:25:55 +07:00
|
|
|
if (operation != KEY_OP_MATCH &&
|
|
|
|
operation != KEY_OP_NOMATCH) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "invalid SUBSYSTEMS operation\n");
|
2006-04-25 00:25:55 +07:00
|
|
|
goto invalid;
|
|
|
|
}
|
2006-08-19 21:06:25 +07:00
|
|
|
add_rule_key(rule, &rule->subsystems, operation, value);
|
2005-07-05 20:24:41 +07:00
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2003-12-08 00:12:07 +07:00
|
|
|
|
2006-08-19 21:06:25 +07:00
|
|
|
if (strcasecmp(key, "DRIVERS") == 0) {
|
2006-05-01 06:45:26 +07:00
|
|
|
if (operation != KEY_OP_MATCH &&
|
|
|
|
operation != KEY_OP_NOMATCH) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "invalid DRIVERS operation\n");
|
2006-05-01 06:45:26 +07:00
|
|
|
goto invalid;
|
|
|
|
}
|
2006-08-19 21:06:25 +07:00
|
|
|
add_rule_key(rule, &rule->drivers, operation, value);
|
2005-07-05 20:24:41 +07:00
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-02-21 20:01:23 +07:00
|
|
|
|
2006-08-21 00:08:37 +07:00
|
|
|
if (strncasecmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
|
|
|
|
strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
|
2007-02-02 02:23:41 +07:00
|
|
|
if (operation != KEY_OP_MATCH &&
|
|
|
|
operation != KEY_OP_NOMATCH) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "invalid ATTRS operation\n");
|
2007-02-02 02:23:41 +07:00
|
|
|
goto invalid;
|
|
|
|
}
|
2008-09-06 20:45:31 +07:00
|
|
|
attr = get_key_attribute(rules, key + sizeof("ATTRS")-1);
|
2006-08-19 21:06:25 +07:00
|
|
|
if (attr == NULL) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "error parsing ATTRS attribute\n");
|
2006-08-20 23:15:29 +07:00
|
|
|
goto invalid;
|
2006-08-19 21:06:25 +07:00
|
|
|
}
|
2006-09-18 07:01:29 +07:00
|
|
|
if (strncmp(attr, "device/", 7) == 0)
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "the 'device' link is deprecated and will be removed from a future kernel, \n"
|
2006-09-18 07:01:29 +07:00
|
|
|
"please fix it in %s:%u", filename, lineno);
|
2007-06-01 21:37:43 +07:00
|
|
|
else if (strstr(attr, "../") != NULL)
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "do not reference parent sysfs directories directly, that may break with a future kernel, \n"
|
2006-09-18 07:01:29 +07:00
|
|
|
"please fix it in %s:%u", filename, lineno);
|
2008-09-06 20:45:31 +07:00
|
|
|
if (add_rule_key_pair(rules, rule, &rule->attrs, operation, attr, value) != 0)
|
2006-08-20 23:15:29 +07:00
|
|
|
goto invalid;
|
2005-07-08 01:05:51 +07:00
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-08-21 00:08:37 +07:00
|
|
|
if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
|
2008-09-06 20:45:31 +07:00
|
|
|
attr = get_key_attribute(rules, key + sizeof("ENV")-1);
|
2005-07-05 20:24:41 +07:00
|
|
|
if (attr == NULL) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "error parsing ENV attribute\n");
|
2006-08-20 23:15:29 +07:00
|
|
|
goto invalid;
|
2005-02-21 20:01:23 +07:00
|
|
|
}
|
2006-09-18 07:01:29 +07:00
|
|
|
if (strncmp(attr, "PHYSDEV", 7) == 0)
|
2006-09-19 03:14:18 +07:00
|
|
|
physdev = 1;
|
2008-09-06 20:45:31 +07:00
|
|
|
if (add_rule_key_pair(rules, rule, &rule->env, operation, attr, value) != 0)
|
2006-08-20 23:15:29 +07:00
|
|
|
goto invalid;
|
2005-07-05 20:24:41 +07:00
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-02-21 20:01:23 +07:00
|
|
|
|
2006-08-19 21:06:25 +07:00
|
|
|
if (strcasecmp(key, "PROGRAM") == 0) {
|
|
|
|
add_rule_key(rule, &rule->program, operation, value);
|
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcasecmp(key, "RESULT") == 0) {
|
|
|
|
if (operation != KEY_OP_MATCH &&
|
|
|
|
operation != KEY_OP_NOMATCH) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "invalid RESULT operation\n");
|
2006-08-19 21:06:25 +07:00
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
add_rule_key(rule, &rule->result, operation, value);
|
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
|
2008-09-06 20:45:31 +07:00
|
|
|
attr = get_key_attribute(rules, key + sizeof("IMPORT")-1);
|
2007-07-15 01:44:19 +07:00
|
|
|
if (attr != NULL && strstr(attr, "program")) {
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "IMPORT will be executed\n");
|
2005-07-12 19:46:36 +07:00
|
|
|
rule->import_type = IMPORT_PROGRAM;
|
2007-07-15 01:44:19 +07:00
|
|
|
} else if (attr != NULL && strstr(attr, "file")) {
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "IMPORT will be included as file\n");
|
2005-07-12 19:46:36 +07:00
|
|
|
rule->import_type = IMPORT_FILE;
|
2007-07-15 01:44:19 +07:00
|
|
|
} else if (attr != NULL && strstr(attr, "parent")) {
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "IMPORT will include the parent values\n");
|
2005-07-12 19:46:36 +07:00
|
|
|
rule->import_type = IMPORT_PARENT;
|
2005-07-05 20:24:41 +07:00
|
|
|
} else {
|
|
|
|
/* figure it out if it is executable */
|
2008-09-11 02:50:21 +07:00
|
|
|
char file[UTIL_PATH_SIZE];
|
2005-07-05 20:24:41 +07:00
|
|
|
char *pos;
|
2007-07-16 00:10:06 +07:00
|
|
|
struct stat statbuf;
|
2005-07-05 20:24:41 +07:00
|
|
|
|
2008-09-10 23:59:42 +07:00
|
|
|
util_strlcpy(file, value, sizeof(file));
|
2005-07-05 20:24:41 +07:00
|
|
|
pos = strchr(file, ' ');
|
|
|
|
if (pos)
|
|
|
|
pos[0] = '\0';
|
2006-01-26 09:59:13 +07:00
|
|
|
|
|
|
|
/* allow programs in /lib/udev called without the path */
|
|
|
|
if (strchr(file, '/') == NULL) {
|
2008-09-10 23:59:42 +07:00
|
|
|
util_strlcpy(file, UDEV_PREFIX "/lib/udev/", sizeof(file));
|
|
|
|
util_strlcat(file, value, sizeof(file));
|
2006-01-26 09:59:13 +07:00
|
|
|
pos = strchr(file, ' ');
|
|
|
|
if (pos)
|
|
|
|
pos[0] = '\0';
|
|
|
|
}
|
|
|
|
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "IMPORT auto mode for '%s'\n", file);
|
2007-07-16 00:10:06 +07:00
|
|
|
if (!lstat(file, &statbuf) && (statbuf.st_mode & S_IXUSR)) {
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "IMPORT is executable, will be executed (autotype)\n");
|
2005-07-12 19:46:36 +07:00
|
|
|
rule->import_type = IMPORT_PROGRAM;
|
|
|
|
} else {
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "IMPORT is not executable, will be included as file (autotype)\n");
|
2005-07-12 19:46:36 +07:00
|
|
|
rule->import_type = IMPORT_FILE;
|
2005-07-05 20:24:41 +07:00
|
|
|
}
|
2005-06-20 05:29:38 +07:00
|
|
|
}
|
2005-07-05 20:24:41 +07:00
|
|
|
add_rule_key(rule, &rule->import, operation, value);
|
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-06-20 05:29:38 +07:00
|
|
|
|
2007-06-03 05:01:46 +07:00
|
|
|
if (strncasecmp(key, "TEST", sizeof("TEST")-1) == 0) {
|
2007-08-30 03:19:28 +07:00
|
|
|
if (operation != KEY_OP_MATCH &&
|
|
|
|
operation != KEY_OP_NOMATCH) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "invalid TEST operation\n");
|
2007-08-30 03:19:28 +07:00
|
|
|
goto invalid;
|
|
|
|
}
|
2008-09-06 20:45:31 +07:00
|
|
|
attr = get_key_attribute(rules, key + sizeof("TEST")-1);
|
2007-06-03 05:01:46 +07:00
|
|
|
if (attr != NULL)
|
|
|
|
rule->test_mode_mask = strtol(attr, NULL, 8);
|
|
|
|
add_rule_key(rule, &rule->test, operation, value);
|
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-07-15 01:43:01 +07:00
|
|
|
if (strncasecmp(key, "RUN", sizeof("RUN")-1) == 0) {
|
2008-09-06 20:45:31 +07:00
|
|
|
attr = get_key_attribute(rules, key + sizeof("RUN")-1);
|
2007-07-15 01:44:19 +07:00
|
|
|
if (attr != NULL) {
|
|
|
|
if (strstr(attr, "ignore_error"))
|
|
|
|
rule->run_ignore_error = 1;
|
|
|
|
}
|
2006-08-19 21:06:25 +07:00
|
|
|
add_rule_key(rule, &rule->run, operation, value);
|
2005-07-05 20:24:41 +07:00
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2003-12-17 14:36:19 +07:00
|
|
|
|
2008-05-20 20:02:17 +07:00
|
|
|
if (strcasecmp(key, "WAIT_FOR") == 0 || strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
|
|
|
|
add_rule_key(rule, &rule->wait_for, operation, value);
|
2005-07-05 20:24:41 +07:00
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2003-12-17 14:36:19 +07:00
|
|
|
|
2006-08-19 21:06:25 +07:00
|
|
|
if (strcasecmp(key, "LABEL") == 0) {
|
|
|
|
add_rule_key(rule, &rule->label, operation, value);
|
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcasecmp(key, "GOTO") == 0) {
|
|
|
|
add_rule_key(rule, &rule->goto_label, operation, value);
|
2005-07-05 20:24:41 +07:00
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-03-13 14:15:10 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
|
2008-09-06 20:45:31 +07:00
|
|
|
attr = get_key_attribute(rules, key + sizeof("NAME")-1);
|
2005-07-05 20:24:41 +07:00
|
|
|
if (attr != NULL) {
|
|
|
|
if (strstr(attr, "all_partitions") != NULL) {
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "creation of partition nodes requested\n");
|
2005-07-05 20:24:41 +07:00
|
|
|
rule->partitions = DEFAULT_PARTITIONS_COUNT;
|
2003-12-23 13:31:35 +07:00
|
|
|
}
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strstr(attr, "ignore_remove") != NULL) {
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "remove event should be ignored\n");
|
2005-07-05 20:24:41 +07:00
|
|
|
rule->ignore_remove = 1;
|
2005-03-13 17:40:32 +07:00
|
|
|
}
|
|
|
|
}
|
2005-11-07 22:00:41 +07:00
|
|
|
if (value[0] == '\0')
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "name empty, node creation supressed\n");
|
2005-11-07 22:00:41 +07:00
|
|
|
add_rule_key(rule, &rule->name, operation, value);
|
2005-07-05 20:24:41 +07:00
|
|
|
continue;
|
|
|
|
}
|
2005-03-13 17:40:32 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "SYMLINK") == 0) {
|
2007-07-16 18:29:28 +07:00
|
|
|
if (operation == KEY_OP_MATCH ||
|
|
|
|
operation == KEY_OP_NOMATCH)
|
|
|
|
add_rule_key(rule, &rule->symlink_match, operation, value);
|
|
|
|
else
|
|
|
|
add_rule_key(rule, &rule->symlink, operation, value);
|
2005-07-05 20:24:41 +07:00
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-03-13 17:40:32 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "OWNER") == 0) {
|
|
|
|
valid = 1;
|
2005-07-06 07:01:16 +07:00
|
|
|
if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
|
2005-07-05 20:24:41 +07:00
|
|
|
char *endptr;
|
|
|
|
strtoul(value, &endptr, 10);
|
|
|
|
if (endptr[0] != '\0') {
|
|
|
|
char owner[32];
|
2008-09-06 20:45:31 +07:00
|
|
|
uid_t uid = lookup_user(rules->udev, value);
|
|
|
|
dbg(rules->udev, "replacing username='%s' by id=%i\n", value, uid);
|
2005-10-28 01:32:07 +07:00
|
|
|
sprintf(owner, "%u", (unsigned int) uid);
|
2005-07-05 20:24:41 +07:00
|
|
|
add_rule_key(rule, &rule->owner, operation, owner);
|
2005-03-13 14:15:10 +07:00
|
|
|
continue;
|
2003-12-23 13:31:35 +07:00
|
|
|
}
|
2003-12-17 14:36:19 +07:00
|
|
|
}
|
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
add_rule_key(rule, &rule->owner, operation, value);
|
|
|
|
continue;
|
|
|
|
}
|
2005-06-20 05:29:38 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "GROUP") == 0) {
|
|
|
|
valid = 1;
|
2005-07-06 07:01:16 +07:00
|
|
|
if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
|
2005-07-05 20:24:41 +07:00
|
|
|
char *endptr;
|
|
|
|
strtoul(value, &endptr, 10);
|
|
|
|
if (endptr[0] != '\0') {
|
|
|
|
char group[32];
|
2008-09-06 20:45:31 +07:00
|
|
|
gid_t gid = lookup_group(rules->udev, value);
|
|
|
|
dbg(rules->udev, "replacing groupname='%s' by id=%i\n", value, gid);
|
2005-10-28 01:32:07 +07:00
|
|
|
sprintf(group, "%u", (unsigned int) gid);
|
2005-07-08 03:40:09 +07:00
|
|
|
add_rule_key(rule, &rule->group, operation, group);
|
2005-07-05 20:24:41 +07:00
|
|
|
continue;
|
2005-06-25 23:58:49 +07:00
|
|
|
}
|
2005-06-25 18:10:16 +07:00
|
|
|
}
|
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
add_rule_key(rule, &rule->group, operation, value);
|
|
|
|
continue;
|
|
|
|
}
|
2004-11-13 11:21:12 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "MODE") == 0) {
|
2008-04-19 02:07:29 +07:00
|
|
|
add_rule_key(rule, &rule->mode, operation, value);
|
2005-07-05 20:24:41 +07:00
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
[PATCH] udev - drop all methods :)
> Hi,
> as promised yesterday, here is a patch that drops the explicit methods
> given in the udev config and implement only one type of rule.
>
> A rule now consists only of a number of keys to match. All known keys
> are valid in any combination. The former configs should work with a few
> changes:
>
> o the "<METHOD>, " at the beginning of the line should be removed
>
> o the result of the externel program is matched with RESULT= instead if ID=
> the PROGRAM= key is only valid if the program exits with zero
> (just exit with nozero in a script if the rule should not match)
>
> o rules are processed in order they appear in the file, no priority
>
> o if NAME="" is given, udev is instructed to ignore this device,
> no node will be created
>
>
> EXAMPLE:
>
> # combined BUS, SYSFS and KERNEL
> BUS="usb", KERNEL="video*", SYSFS_model="Creative Labs WebCam*", NAME="test/webcam%n"
>
> # exec script only for the first ide drive (hda), all other will be skipped
> BUS="ide", KERNEL="hda*", PROGRAM="/home/kay/src/udev.kay/extras/ide-devfs.sh %k %b %n", RESULT="hd*", NAME="%1c", SYMLINK="%2c %3c"
>
>
> The udev-test.pl and test.block works fine here.
> Please adapt your config and give it a try.
>
Here is a slightly better version of the patch.
After a conversation with Patrick, we are now able to execute the PROGRAM
and also match in all following rules with the RESULT value from this exec.
EXAMPLE:
We have 7 rules with RESULT and 2 with PROGRAM.
Only the 5th rule matches with the callout result from the exec in the 4th rule.
RULES:
PROGRAM="/bin/echo abc", RESULT="no_match", NAME="web-no-2"
KERNEL="video*", RESULT="123", NAME="web-no-3"
KERNEL="video*", RESULT="123", NAME="web-no-4"
PROGRAM="/bin/echo 123", RESULT="no_match", NAME="web-no-5"
KERNEL="video*", RESULT="123", NAME="web-yes"
RESULT:
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: process rule
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: check PROGRAM
Jan 11 23:36:52 pim udev[26050]: execute_program: executing '/bin/echo abc'
Jan 11 23:36:52 pim udev[26050]: execute_program: result is 'abc'
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: PROGRAM returned successful
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: check for RESULT dev->result='no_match', udev->program_result='abc'
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: RESULT is not matching
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: process rule
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: check for KERNEL dev->kernel='video*' class_dev->name='video0'
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: KERNEL matches
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: check for RESULT dev->result='123', udev->program_result='abc'
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: RESULT is not matching
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: process rule
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: check for KERNEL dev->kernel='video*' class_dev->name='video0'
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: KERNEL matches
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: check for RESULT dev->result='123', udev->program_result='abc'
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: RESULT is not matching
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: process rule
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: check PROGRAM
Jan 11 23:36:52 pim udev[26050]: execute_program: executing '/bin/echo 123'
Jan 11 23:36:52 pim udev[26050]: execute_program: result is '123'
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: PROGRAM returned successful
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: check for RESULT dev->result='no_match', udev->program_result='123'
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: RESULT is not matching
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: process rule
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: check for KERNEL dev->kernel='video*' class_dev->name='video0'
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: KERNEL matches
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: check for RESULT dev->result='123', udev->program_result='123'
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: RESULT matches
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: found matching rule, 'video*' becomes ''
Jan 11 23:36:52 pim udev[26050]: namedev_name_device: name, 'web-yes' is going to have owner='', group='', mode = 0600
2004-01-13 12:39:05 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "OPTIONS") == 0) {
|
2007-03-16 21:16:08 +07:00
|
|
|
const char *pos;
|
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strstr(value, "last_rule") != NULL) {
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "last rule to be applied\n");
|
2005-07-05 20:24:41 +07:00
|
|
|
rule->last_rule = 1;
|
2003-12-17 14:36:19 +07:00
|
|
|
}
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strstr(value, "ignore_device") != NULL) {
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "device should be ignored\n");
|
2005-07-05 20:24:41 +07:00
|
|
|
rule->ignore_device = 1;
|
2003-12-17 14:36:19 +07:00
|
|
|
}
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strstr(value, "ignore_remove") != NULL) {
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "remove event should be ignored\n");
|
2005-07-05 20:24:41 +07:00
|
|
|
rule->ignore_remove = 1;
|
2004-03-11 13:35:37 +07:00
|
|
|
}
|
2007-03-16 21:16:08 +07:00
|
|
|
pos = strstr(value, "link_priority=");
|
|
|
|
if (pos != NULL) {
|
|
|
|
rule->link_priority = atoi(&pos[strlen("link_priority=")]);
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "link priority=%i\n", rule->link_priority);
|
2007-03-16 21:16:08 +07:00
|
|
|
}
|
2008-04-22 00:00:54 +07:00
|
|
|
pos = strstr(value, "event_timeout=");
|
|
|
|
if (pos != NULL) {
|
|
|
|
rule->event_timeout = atoi(&pos[strlen("event_timeout=")]);
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "event timout=%i\n", rule->event_timeout);
|
2008-04-22 00:00:54 +07:00
|
|
|
}
|
2007-06-22 06:27:02 +07:00
|
|
|
pos = strstr(value, "string_escape=");
|
|
|
|
if (pos != NULL) {
|
|
|
|
pos = &pos[strlen("string_escape=")];
|
|
|
|
if (strncmp(pos, "none", strlen("none")) == 0)
|
|
|
|
rule->string_escape = ESCAPE_NONE;
|
|
|
|
else if (strncmp(pos, "replace", strlen("replace")) == 0)
|
|
|
|
rule->string_escape = ESCAPE_REPLACE;
|
|
|
|
}
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strstr(value, "all_partitions") != NULL) {
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "creation of partition nodes requested\n");
|
2005-07-05 20:24:41 +07:00
|
|
|
rule->partitions = DEFAULT_PARTITIONS_COUNT;
|
2004-03-11 13:35:37 +07:00
|
|
|
}
|
2005-07-05 20:24:41 +07:00
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2004-03-11 13:35:37 +07:00
|
|
|
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "unknown key '%s' in %s:%u\n", key, filename, lineno);
|
2005-07-05 20:24:41 +07:00
|
|
|
}
|
2005-04-02 22:45:35 +07:00
|
|
|
|
2008-05-20 20:02:17 +07:00
|
|
|
if (physdev && rule->wait_for.operation == KEY_OP_UNSET)
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "PHYSDEV* values are deprecated and will be removed from a future kernel, \n"
|
2006-09-19 03:14:18 +07:00
|
|
|
"please fix it in %s:%u", filename, lineno);
|
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
/* skip line if not any valid key was found */
|
2006-04-25 00:25:55 +07:00
|
|
|
if (!valid)
|
|
|
|
goto invalid;
|
2004-03-11 13:35:37 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
/* grow buffer and add rule */
|
|
|
|
rule_size = sizeof(struct udev_rule) + rule->bufsize;
|
2005-07-17 20:40:29 +07:00
|
|
|
padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "add %zi padding bytes\n", padding);
|
2005-07-17 20:40:29 +07:00
|
|
|
rule_size += padding;
|
|
|
|
rule->bufsize += padding;
|
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
|
|
|
|
if (!rules->buf) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "realloc failed\n");
|
2005-07-05 20:24:41 +07:00
|
|
|
goto exit;
|
|
|
|
}
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "adding rule to offset %zi\n", rules->bufsize);
|
2005-07-05 20:24:41 +07:00
|
|
|
memcpy(rules->buf + rules->bufsize, rule, rule_size);
|
|
|
|
rules->bufsize += rule_size;
|
|
|
|
exit:
|
|
|
|
return 0;
|
2006-04-25 00:25:55 +07:00
|
|
|
|
|
|
|
invalid:
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "invalid rule '%s:%u'\n", filename, lineno);
|
2006-04-25 00:25:55 +07:00
|
|
|
return -1;
|
2005-07-05 20:24:41 +07:00
|
|
|
}
|
2005-02-14 12:03:06 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
static int parse_file(struct udev_rules *rules, const char *filename)
|
|
|
|
{
|
2008-09-11 02:50:21 +07:00
|
|
|
char line[UTIL_LINE_SIZE];
|
2005-07-05 20:24:41 +07:00
|
|
|
char *bufline;
|
2006-04-25 00:25:55 +07:00
|
|
|
unsigned int lineno;
|
2005-07-05 20:24:41 +07:00
|
|
|
char *buf;
|
|
|
|
size_t bufsize;
|
|
|
|
size_t cur;
|
|
|
|
size_t count;
|
|
|
|
int retval = 0;
|
2008-09-28 18:02:44 +07:00
|
|
|
size_t start;
|
|
|
|
struct udev_rule *rule;
|
|
|
|
struct udev_rules_iter iter;
|
|
|
|
|
|
|
|
start = rules->bufsize;
|
2005-07-05 20:24:41 +07:00
|
|
|
|
|
|
|
if (file_map(filename, &buf, &bufsize) != 0) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "can't open '%s' as rules file: %s\n", filename, strerror(errno));
|
2005-07-05 20:24:41 +07:00
|
|
|
return -1;
|
|
|
|
}
|
2008-09-06 20:45:31 +07:00
|
|
|
info(rules->udev, "reading '%s' as rules file\n", filename);
|
2003-12-17 14:36:19 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
/* loop through the whole file */
|
|
|
|
cur = 0;
|
|
|
|
lineno = 0;
|
|
|
|
while (cur < bufsize) {
|
|
|
|
unsigned int i, j;
|
|
|
|
|
|
|
|
count = buf_get_line(buf, bufsize, cur);
|
|
|
|
bufline = &buf[cur];
|
|
|
|
cur += count+1;
|
|
|
|
lineno++;
|
|
|
|
|
|
|
|
/* eat the whitespace */
|
|
|
|
while ((count > 0) && isspace(bufline[0])) {
|
|
|
|
bufline++;
|
|
|
|
count--;
|
2003-12-03 09:38:30 +07:00
|
|
|
}
|
2005-07-05 20:24:41 +07:00
|
|
|
if (count == 0)
|
|
|
|
continue;
|
2003-12-03 09:38:30 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
/* see if this is a comment */
|
2008-09-11 02:50:21 +07:00
|
|
|
if (bufline[0] == '#')
|
2003-12-17 14:36:19 +07:00
|
|
|
continue;
|
2005-07-05 20:24:41 +07:00
|
|
|
|
2006-08-13 09:23:16 +07:00
|
|
|
if (count >= sizeof(line)) {
|
2008-09-06 20:45:31 +07:00
|
|
|
err(rules->udev, "line too long, rule skipped '%s:%u'\n", filename, lineno);
|
2006-08-13 09:23:16 +07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip backslash and newline from multiline rules */
|
2005-07-05 20:24:41 +07:00
|
|
|
for (i = j = 0; i < count; i++) {
|
|
|
|
if (bufline[i] == '\\' && bufline[i+1] == '\n')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
line[j++] = bufline[i];
|
2003-12-03 09:38:30 +07:00
|
|
|
}
|
2005-07-05 20:24:41 +07:00
|
|
|
line[j] = '\0';
|
|
|
|
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(rules->udev, "read '%s'\n", line);
|
2006-04-25 00:25:55 +07:00
|
|
|
add_to_rules(rules, line, filename, lineno);
|
2003-12-03 09:38:30 +07:00
|
|
|
}
|
2004-03-23 13:22:20 +07:00
|
|
|
|
2008-09-28 18:02:44 +07:00
|
|
|
/* Compute all goto targets within this file */
|
|
|
|
udev_rules_iter_init(&iter, rules);
|
|
|
|
udev_rules_iter_goto(&iter, start);
|
|
|
|
while((rule = udev_rules_iter_next(&iter))) {
|
|
|
|
if (rule->goto_label.operation != KEY_OP_UNSET) {
|
|
|
|
char *goto_label = &rule->buf[rule->goto_label.val_off];
|
|
|
|
|
|
|
|
dbg(rules->udev, "resolving goto label '%s'", goto_label);
|
|
|
|
rule->goto_rule_off = find_label(&iter, goto_label);
|
|
|
|
if (rule->goto_rule_off == iter.current) {
|
|
|
|
err(rules->udev, "goto nonexistent label '%s' in '%s'",
|
|
|
|
goto_label, filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-23 13:22:20 +07:00
|
|
|
file_unmap(buf, bufsize);
|
2003-12-03 09:38:30 +07:00
|
|
|
return retval;
|
2003-12-17 14:36:19 +07:00
|
|
|
}
|
2003-12-03 09:38:30 +07:00
|
|
|
|
2008-09-06 20:45:31 +07:00
|
|
|
int udev_rules_init(struct udev *udev, struct udev_rules *rules, int resolve_names)
|
2005-06-24 23:05:32 +07:00
|
|
|
{
|
2007-07-16 00:10:06 +07:00
|
|
|
struct stat statbuf;
|
|
|
|
char filename[PATH_MAX];
|
|
|
|
LIST_HEAD(name_list);
|
2008-07-18 20:56:03 +07:00
|
|
|
LIST_HEAD(sort_list);
|
2007-07-16 00:10:06 +07:00
|
|
|
struct name_entry *name_loop, *name_tmp;
|
2008-07-18 20:56:03 +07:00
|
|
|
struct name_entry *sort_loop, *sort_tmp;
|
2007-07-16 00:10:06 +07:00
|
|
|
int retval = 0;
|
2003-12-03 09:38:30 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
memset(rules, 0x00, sizeof(struct udev_rules));
|
2008-09-06 20:45:31 +07:00
|
|
|
rules->udev = udev;
|
2005-07-05 20:24:41 +07:00
|
|
|
rules->resolve_names = resolve_names;
|
|
|
|
|
2008-09-06 20:45:31 +07:00
|
|
|
if (udev_get_rules_path(udev) != NULL) {
|
2008-07-18 20:56:03 +07:00
|
|
|
/* custom rules location for testing */
|
2008-09-06 20:45:31 +07:00
|
|
|
add_matching_files(udev, &name_list, udev_get_rules_path(udev), ".rules");
|
2005-06-24 23:05:32 +07:00
|
|
|
} else {
|
2008-07-18 20:56:03 +07:00
|
|
|
/* read user/custom rules */
|
2008-09-06 20:45:31 +07:00
|
|
|
add_matching_files(udev, &name_list, SYSCONFDIR "/udev/rules.d", ".rules");
|
2008-07-18 20:56:03 +07:00
|
|
|
|
|
|
|
/* read dynamic/temporary rules */
|
2008-09-10 23:59:42 +07:00
|
|
|
util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
|
|
|
|
util_strlcat(filename, "/.udev/rules.d", sizeof(filename));
|
2008-07-18 20:56:03 +07:00
|
|
|
if (stat(filename, &statbuf) != 0) {
|
2008-09-06 20:45:31 +07:00
|
|
|
create_path(udev, filename);
|
2008-09-10 05:46:17 +07:00
|
|
|
udev_selinux_setfscreatecon(udev, filename, S_IFDIR|0755);
|
2008-07-18 20:56:03 +07:00
|
|
|
mkdir(filename, 0755);
|
2008-09-10 05:46:17 +07:00
|
|
|
udev_selinux_resetfscreatecon(udev);
|
2008-07-18 20:56:03 +07:00
|
|
|
}
|
2008-09-06 20:45:31 +07:00
|
|
|
add_matching_files(udev, &sort_list, filename, ".rules");
|
2007-07-16 00:10:06 +07:00
|
|
|
|
2008-08-09 19:05:01 +07:00
|
|
|
/* read default rules */
|
2008-09-06 20:45:31 +07:00
|
|
|
add_matching_files(udev, &sort_list, UDEV_PREFIX "/lib/udev/rules.d", ".rules");
|
2008-08-09 19:05:01 +07:00
|
|
|
|
2008-07-18 20:56:03 +07:00
|
|
|
/* sort all rules files by basename into list of files */
|
|
|
|
list_for_each_entry_safe(sort_loop, sort_tmp, &sort_list, node) {
|
|
|
|
const char *sort_base = strrchr(sort_loop->name, '/');
|
2007-07-16 00:10:06 +07:00
|
|
|
|
2008-07-18 20:56:03 +07:00
|
|
|
if (sort_base == NULL)
|
|
|
|
continue;
|
2005-03-17 15:59:32 +07:00
|
|
|
|
2008-07-18 20:56:03 +07:00
|
|
|
list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
|
|
|
|
const char *name_base = strrchr(name_loop->name, '/');
|
2007-07-16 00:10:06 +07:00
|
|
|
|
2008-07-18 20:56:03 +07:00
|
|
|
if (name_base == NULL)
|
|
|
|
continue;
|
2007-07-16 00:10:06 +07:00
|
|
|
|
2008-08-09 19:05:01 +07:00
|
|
|
if (strcmp(name_base, sort_base) == 0) {
|
2008-09-06 20:45:31 +07:00
|
|
|
info(udev, "rule file '%s' already added, ignoring '%s'\n",
|
2008-08-09 19:05:01 +07:00
|
|
|
name_loop->name, sort_loop->name);
|
|
|
|
list_del(&sort_loop->node);
|
|
|
|
free(sort_loop);
|
|
|
|
sort_loop = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-07-18 20:56:03 +07:00
|
|
|
if (strcmp(name_base, sort_base) > 0)
|
|
|
|
break;
|
|
|
|
}
|
2008-08-09 19:05:01 +07:00
|
|
|
if (sort_loop != NULL)
|
|
|
|
list_move_tail(&sort_loop->node, &name_loop->node);
|
2005-03-17 15:59:32 +07:00
|
|
|
}
|
|
|
|
}
|
2004-02-24 10:31:14 +07:00
|
|
|
|
2007-07-16 00:10:06 +07:00
|
|
|
/* parse list of files */
|
|
|
|
list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
|
|
|
|
if (stat(name_loop->name, &statbuf) == 0) {
|
|
|
|
if (statbuf.st_size)
|
|
|
|
parse_file(rules, name_loop->name);
|
|
|
|
else
|
2008-09-06 20:45:31 +07:00
|
|
|
dbg(udev, "empty rules file '%s'\n", name_loop->name);
|
2007-07-16 00:10:06 +07:00
|
|
|
} else
|
2008-09-06 20:45:31 +07:00
|
|
|
err(udev, "could not read '%s': %s\n", name_loop->name, strerror(errno));
|
2007-07-16 00:10:06 +07:00
|
|
|
list_del(&name_loop->node);
|
|
|
|
free(name_loop);
|
|
|
|
}
|
|
|
|
|
2004-12-18 17:34:17 +07:00
|
|
|
return retval;
|
2004-02-24 10:31:14 +07:00
|
|
|
}
|
2005-02-09 14:43:18 +07:00
|
|
|
|
2006-01-10 03:18:00 +07:00
|
|
|
void udev_rules_cleanup(struct udev_rules *rules)
|
2005-02-09 14:43:18 +07:00
|
|
|
{
|
2005-08-08 10:07:36 +07:00
|
|
|
if (rules->buf) {
|
2005-11-06 02:00:31 +07:00
|
|
|
free(rules->buf);
|
2005-08-08 10:07:36 +07:00
|
|
|
rules->buf = NULL;
|
|
|
|
}
|
2005-02-09 14:43:18 +07:00
|
|
|
}
|
2007-08-17 01:16:47 +07:00
|
|
|
|