2003-12-03 09:38:30 +07:00
|
|
|
/*
|
2005-03-13 04:36:32 +07:00
|
|
|
* udev_rules_parse.c
|
2003-12-03 09:38:30 +07:00
|
|
|
*
|
|
|
|
* Userspace devfs
|
|
|
|
*
|
2004-01-27 10:21:58 +07:00
|
|
|
* Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
|
2005-02-14 12:03:06 +07:00
|
|
|
* Copyright (C) 2003-2005 Kay Sievers <kay.sievers@vrfy.org>
|
2003-12-03 09:38:30 +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 version 2 of the License.
|
|
|
|
*
|
|
|
|
* 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, write to the Free Software Foundation, Inc.,
|
|
|
|
* 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
2005-03-07 10:29:43 +07:00
|
|
|
#include "udev_libc_wrapper.h"
|
2003-12-03 09:38:30 +07:00
|
|
|
#include "udev.h"
|
2004-11-25 08:44:38 +07:00
|
|
|
#include "udev_utils.h"
|
[PATCH] add udev logging to info log
On Thu, Jan 15, 2004 at 05:14:16AM +0100, Kay Sievers wrote:
> On Wed, Jan 14, 2004 at 01:10:43PM -0800, Greg KH wrote:
> > On Wed, Jan 14, 2004 at 02:34:26PM -0600, Clay Haapala wrote:
> > > On Wed, 14 Jan 2004, Chris Friesen spake thusly:
> > > >
> > > > Maybe for ones with a matching rule, you could print something like:
> > > >
> > > >
> > > Is the act of printing/syslogging a rule in an of itself?
> >
> > No, as currently the only way stuff ends up in the syslog is if
> > DEBUG=true is used on the build line.
> >
> > But it's sounding like we might want to change that... :)
>
> How about this in the syslog after connect/disconnect?
>
> Jan 15 05:07:45 pim udev[28007]: configured rule in '/etc/udev/udev.rules' at line 17 applied, 'video*' becomes 'video/webcam%n'
> Jan 15 05:07:45 pim udev[28007]: creating device node '/udev/video/webcam0'
> Jan 15 05:07:47 pim udev[28015]: removing device node '/udev/video/webcam0'
Here is a slightly better version. I've created a logging.h file and
moved the debug macros from udev.h in there.
If you type:
'make' - you will get a binary that prints one or two lines to syslog
if a device node is created or deleted
'make LOG=false' - you get a binary that prints asolutely nothing
'make DEBUG=true' - the same as today, it will print all debug lines
2004-01-16 12:53:20 +07:00
|
|
|
#include "logging.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
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
void udev_rules_iter_init(struct udev_rules *rules)
|
2005-06-24 23:05:32 +07:00
|
|
|
{
|
2005-07-05 20:24:41 +07:00
|
|
|
dbg("bufsize=%zi", rules->bufsize);
|
|
|
|
rules->current = 0;
|
2005-06-24 23:05:32 +07:00
|
|
|
}
|
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
struct udev_rule *udev_rules_iter_next(struct udev_rules *rules)
|
2005-06-24 23:05:32 +07:00
|
|
|
{
|
|
|
|
static struct udev_rule *rule;
|
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (!rules)
|
|
|
|
return NULL;
|
2005-06-24 23:05:32 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
dbg("current=%zi", rules->current);
|
|
|
|
if (rules->current >= rules->bufsize)
|
|
|
|
return NULL;
|
2003-12-03 09:38:30 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
/* get next rule */
|
|
|
|
rule = (struct udev_rule *) (rules->buf + rules->current);
|
|
|
|
rules->current += sizeof(struct udev_rule) + rule->bufsize;
|
|
|
|
|
|
|
|
return rule;
|
2003-12-03 09:38:30 +07:00
|
|
|
}
|
2003-12-04 09:33:58 +07:00
|
|
|
|
2005-03-13 11:46:31 +07:00
|
|
|
static int get_key(char **line, char **key, enum key_operation *operation, char **value)
|
|
|
|
{
|
|
|
|
char *linepos;
|
|
|
|
char *temp;
|
|
|
|
|
|
|
|
linepos = *line;
|
|
|
|
if (!linepos)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* skip whitespace */
|
|
|
|
while (isspace(linepos[0]) || linepos[0] == ',')
|
|
|
|
linepos++;
|
|
|
|
|
|
|
|
/* get the key */
|
|
|
|
*key = linepos;
|
|
|
|
while (1) {
|
|
|
|
linepos++;
|
|
|
|
if (linepos[0] == '\0')
|
|
|
|
return -1;
|
|
|
|
if (isspace(linepos[0]))
|
|
|
|
break;
|
|
|
|
if (linepos[0] == '=')
|
|
|
|
break;
|
|
|
|
if (linepos[0] == '+')
|
|
|
|
break;
|
|
|
|
if (linepos[0] == '!')
|
|
|
|
break;
|
2005-06-05 09:57:03 +07:00
|
|
|
if (linepos[0] == ':')
|
|
|
|
break;
|
2005-03-13 11:46:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* remember end of key */
|
|
|
|
temp = linepos;
|
|
|
|
|
|
|
|
/* skip whitespace after key */
|
|
|
|
while (isspace(linepos[0]))
|
|
|
|
linepos++;
|
|
|
|
|
|
|
|
/* get operation type */
|
|
|
|
if (linepos[0] == '=' && linepos[1] == '=') {
|
|
|
|
*operation = KEY_OP_MATCH;
|
|
|
|
linepos += 2;
|
|
|
|
dbg("operator=match");
|
|
|
|
} else if (linepos[0] == '!' && linepos[1] == '=') {
|
|
|
|
*operation = KEY_OP_NOMATCH;
|
|
|
|
linepos += 2;
|
|
|
|
dbg("operator=nomatch");
|
|
|
|
} else if (linepos[0] == '+' && linepos[1] == '=') {
|
|
|
|
*operation = KEY_OP_ADD;
|
|
|
|
linepos += 2;
|
|
|
|
dbg("operator=add");
|
|
|
|
} else if (linepos[0] == '=') {
|
|
|
|
*operation = KEY_OP_ASSIGN;
|
|
|
|
linepos++;
|
|
|
|
dbg("operator=assign");
|
2005-06-05 09:57:03 +07:00
|
|
|
} else if (linepos[0] == ':' && linepos[1] == '=') {
|
|
|
|
*operation = KEY_OP_ASSIGN_FINAL;
|
|
|
|
linepos += 2;
|
|
|
|
dbg("operator=assign_final");
|
2005-03-13 11:46:31 +07:00
|
|
|
} else
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* terminate key */
|
|
|
|
temp[0] = '\0';
|
|
|
|
dbg("key='%s'", *key);
|
|
|
|
|
|
|
|
/* skip whitespace after operator */
|
|
|
|
while (isspace(linepos[0]))
|
|
|
|
linepos++;
|
|
|
|
|
|
|
|
/* get the value*/
|
|
|
|
if (linepos[0] == '"')
|
|
|
|
linepos++;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
*value = linepos;
|
|
|
|
|
|
|
|
temp = strchr(linepos, '"');
|
|
|
|
if (!temp)
|
|
|
|
return -1;
|
|
|
|
temp[0] = '\0';
|
|
|
|
temp++;
|
|
|
|
dbg("value='%s'", *value);
|
|
|
|
|
|
|
|
/* move line to next key */
|
|
|
|
*line = temp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-13 18:50:44 +07:00
|
|
|
/* extract possible KEY{attr} */
|
2004-02-17 12:39:40 +07:00
|
|
|
static char *get_key_attribute(char *str)
|
|
|
|
{
|
|
|
|
char *pos;
|
|
|
|
char *attr;
|
|
|
|
|
|
|
|
attr = strchr(str, '{');
|
|
|
|
if (attr != NULL) {
|
|
|
|
attr++;
|
|
|
|
pos = strchr(attr, '}');
|
|
|
|
if (pos == NULL) {
|
2005-03-27 06:11:03 +07:00
|
|
|
err("missing closing brace for format");
|
2004-02-17 12:39:40 +07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
pos[0] = '\0';
|
|
|
|
dbg("attribute='%s'", attr);
|
|
|
|
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
|
|
|
{
|
2005-07-05 20:24:41 +07:00
|
|
|
size_t val_len = strnlen(value, PATH_SIZE);
|
|
|
|
|
|
|
|
key->operation = operation;
|
|
|
|
|
|
|
|
key->val_off = rule->bufsize;
|
|
|
|
strlcpy(rule->buf + rule->bufsize, value, val_len+1);
|
|
|
|
rule->bufsize += val_len+1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
|
|
|
|
enum key_operation operation, const char *key, const char *value)
|
|
|
|
{
|
|
|
|
size_t key_len = strnlen(key, PATH_SIZE);
|
|
|
|
|
|
|
|
if (pairs->count >= PAIRS_MAX) {
|
|
|
|
err("skip, too many keys in a single rule");
|
|
|
|
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;
|
|
|
|
strlcpy(rule->buf + rule->bufsize, key, key_len+1);
|
|
|
|
rule->bufsize += key_len+1;
|
|
|
|
|
|
|
|
pairs->count++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_to_rules(struct udev_rules *rules, char *line)
|
|
|
|
{
|
|
|
|
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-05 20:24:41 +07:00
|
|
|
int retval;
|
2003-12-03 09:38:30 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
/* get all the keys */
|
|
|
|
rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
|
|
|
|
if (!rule) {
|
|
|
|
err("malloc failed");
|
2004-03-23 13:22:20 +07:00
|
|
|
return -1;
|
2003-12-03 09:38:30 +07:00
|
|
|
}
|
2005-07-05 20:24:41 +07:00
|
|
|
linepos = line;
|
|
|
|
valid = 0;
|
2003-12-03 09:38:30 +07:00
|
|
|
|
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
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
retval = get_key(&linepos, &key, &operation, &value);
|
|
|
|
if (retval)
|
|
|
|
break;
|
2003-12-03 09:38:30 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "KERNEL") == 0) {
|
|
|
|
add_rule_key(rule, &rule->kernel_name, operation, value);
|
|
|
|
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) {
|
|
|
|
add_rule_key(rule, &rule->subsystem, operation, value);
|
|
|
|
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
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "ACTION") == 0) {
|
|
|
|
add_rule_key(rule, &rule->action, operation, value);
|
|
|
|
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
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "DEVPATH") == 0) {
|
|
|
|
add_rule_key(rule, &rule->devpath, operation, value);
|
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2004-09-11 10:54:04 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "BUS") == 0) {
|
|
|
|
add_rule_key(rule, &rule->bus, operation, value);
|
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-03-13 11:46:31 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "ID") == 0) {
|
|
|
|
add_rule_key(rule, &rule->id, operation, value);
|
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2003-12-08 00:12:07 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strncasecmp(key, "SYSFS", sizeof("SYSFS")-1) == 0) {
|
|
|
|
attr = get_key_attribute(key + sizeof("SYSFS")-1);
|
|
|
|
if (attr == NULL) {
|
|
|
|
err("error parsing SYSFS attribute in '%s'", line);
|
2005-02-21 20:01:23 +07:00
|
|
|
continue;
|
|
|
|
}
|
2005-07-05 20:24:41 +07:00
|
|
|
add_rule_key_pair(rule, &rule->sysfs, operation, attr, value);
|
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-02-21 20:01:23 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strncasecmp(key, "ENV", sizeof("ENV")-1) == 0) {
|
|
|
|
attr = get_key_attribute(key + sizeof("ENV")-1);
|
|
|
|
if (attr == NULL) {
|
|
|
|
err("error parsing ENV attribute");
|
2005-02-21 20:01:23 +07:00
|
|
|
continue;
|
|
|
|
}
|
2005-07-05 20:24:41 +07:00
|
|
|
add_rule_key_pair(rule, &rule->env, operation, attr, value);
|
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-02-21 20:01:23 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "MODALIAS") == 0) {
|
|
|
|
add_rule_key(rule, &rule->modalias, operation, value);
|
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2005-04-02 22:45:35 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
|
|
|
|
attr = get_key_attribute(key + sizeof("IMPORT")-1);
|
|
|
|
if (attr && strstr(attr, "program")) {
|
|
|
|
dbg("IMPORT will be executed");
|
|
|
|
rule->import_exec = 1;
|
|
|
|
} else if (attr && strstr(attr, "file")) {
|
|
|
|
dbg("IMPORT will be included as file");
|
|
|
|
} else {
|
|
|
|
/* figure it out if it is executable */
|
|
|
|
char file[PATH_SIZE];
|
|
|
|
char *pos;
|
|
|
|
struct stat stats;
|
|
|
|
|
|
|
|
strlcpy(file, value, sizeof(file));
|
|
|
|
pos = strchr(file, ' ');
|
|
|
|
if (pos)
|
|
|
|
pos[0] = '\0';
|
|
|
|
dbg("IMPORT auto mode for '%s'", file);
|
|
|
|
if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
|
|
|
|
dbg("IMPORT is executable, will be executed");
|
|
|
|
rule->import_exec = 1;
|
|
|
|
}
|
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
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "DRIVER") == 0) {
|
|
|
|
add_rule_key(rule, &rule->driver, operation, value);
|
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2003-12-17 14:36:19 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "RESULT") == 0) {
|
|
|
|
add_rule_key(rule, &rule->result, operation, value);
|
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2003-12-17 14:36:19 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "PROGRAM") == 0) {
|
|
|
|
add_rule_key(rule, &rule->program, operation, value);
|
|
|
|
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) {
|
|
|
|
attr = get_key_attribute(key + sizeof("NAME")-1);
|
|
|
|
if (attr != NULL) {
|
|
|
|
if (strstr(attr, "all_partitions") != NULL) {
|
|
|
|
dbg("creation of partition nodes requested");
|
|
|
|
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) {
|
|
|
|
dbg("remove event should be ignored");
|
|
|
|
rule->ignore_remove = 1;
|
2005-03-13 17:40:32 +07:00
|
|
|
}
|
|
|
|
}
|
2005-07-05 20:24:41 +07:00
|
|
|
add_rule_key(rule, &rule->name, operation, value);
|
|
|
|
continue;
|
|
|
|
}
|
2005-03-13 17:40:32 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "SYMLINK") == 0) {
|
|
|
|
add_rule_key(rule, &rule->symlink, operation, value);
|
|
|
|
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];
|
|
|
|
uid_t uid = lookup_user(value);
|
|
|
|
dbg("replacing username='%s' by id=%i", value, uid);
|
|
|
|
sprintf(owner, "%li", uid);
|
|
|
|
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];
|
|
|
|
gid_t gid = lookup_group(value);
|
|
|
|
dbg("replacing groupname='%s' by id=%i", value, gid);
|
|
|
|
sprintf(group, "%li", gid);
|
|
|
|
add_rule_key(rule, &rule->owner, operation, group);
|
|
|
|
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) {
|
|
|
|
rule->mode = strtol(value, NULL, 8);
|
|
|
|
rule->mode_operation = operation;
|
|
|
|
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, "RUN") == 0) {
|
|
|
|
add_rule_key(rule, &rule->run, operation, value);
|
|
|
|
valid = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2003-12-17 14:36:19 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strcasecmp(key, "OPTIONS") == 0) {
|
|
|
|
if (strstr(value, "last_rule") != NULL) {
|
|
|
|
dbg("last rule to be applied");
|
|
|
|
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) {
|
|
|
|
dbg("device should be ignored");
|
|
|
|
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) {
|
|
|
|
dbg("remove event should be ignored");
|
|
|
|
rule->ignore_remove = 1;
|
2004-03-11 13:35:37 +07:00
|
|
|
}
|
2005-07-05 20:24:41 +07:00
|
|
|
if (strstr(value, "all_partitions") != NULL) {
|
|
|
|
dbg("creation of partition nodes requested");
|
|
|
|
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
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
err("unknown key '%s', in '%s'", key, line);
|
|
|
|
}
|
2005-04-02 22:45:35 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
/* skip line if not any valid key was found */
|
|
|
|
if (!valid) {
|
|
|
|
err("invalid rule '%s'", line);
|
|
|
|
goto exit;
|
|
|
|
}
|
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;
|
|
|
|
rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
|
|
|
|
if (!rules->buf) {
|
|
|
|
err("realloc failed");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
memcpy(rules->buf + rules->bufsize, rule, rule_size);
|
|
|
|
rules->bufsize += rule_size;
|
|
|
|
exit:
|
|
|
|
free(rule);
|
|
|
|
return 0;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
char line[LINE_SIZE];
|
|
|
|
char *bufline;
|
|
|
|
int lineno;
|
|
|
|
char *buf;
|
|
|
|
size_t bufsize;
|
|
|
|
size_t cur;
|
|
|
|
size_t count;
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
if (file_map(filename, &buf, &bufsize) != 0) {
|
|
|
|
err("can't open '%s' as rules file", filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dbg("reading '%s' as rules file", 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++;
|
|
|
|
|
|
|
|
if (count >= sizeof(line)) {
|
|
|
|
info("line too long, rule skipped %s, line %d", filename, lineno);
|
|
|
|
continue;
|
|
|
|
}
|
2004-09-11 10:54:04 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
/* 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 */
|
|
|
|
if (bufline[0] == COMMENT_CHARACTER)
|
2003-12-17 14:36:19 +07:00
|
|
|
continue;
|
2005-07-05 20:24:41 +07:00
|
|
|
|
|
|
|
/* skip backslash and newline from multi line rules */
|
|
|
|
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';
|
|
|
|
|
|
|
|
dbg("read '%s'", line);
|
|
|
|
add_to_rules(rules, line);
|
2003-12-03 09:38:30 +07:00
|
|
|
}
|
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
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
static int rules_map(struct udev_rules *rules, const char *filename)
|
2005-06-24 23:05:32 +07:00
|
|
|
{
|
2005-07-05 20:24:41 +07:00
|
|
|
if (file_map(filename, &rules->buf, &rules->bufsize)) {
|
|
|
|
rules->buf = NULL;
|
2005-06-24 23:05:32 +07:00
|
|
|
return -1;
|
2005-07-05 20:24:41 +07:00
|
|
|
}
|
|
|
|
if (rules->bufsize == 0) {
|
|
|
|
file_unmap(rules->buf, rules->bufsize);
|
|
|
|
rules->buf = NULL;
|
2005-06-24 23:05:32 +07:00
|
|
|
return -1;
|
2005-07-05 20:24:41 +07:00
|
|
|
}
|
2005-06-24 23:05:32 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
int udev_rules_init(struct udev_rules *rules, int resolve_names)
|
2003-12-03 09:38:30 +07:00
|
|
|
{
|
2005-06-24 23:05:32 +07:00
|
|
|
char comp[PATH_SIZE];
|
2004-12-18 17:34:17 +07:00
|
|
|
struct stat stats;
|
|
|
|
int retval;
|
2003-12-03 09:38:30 +07:00
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
memset(rules, 0x00, sizeof(struct udev_rules));
|
|
|
|
rules->resolve_names = resolve_names;
|
|
|
|
|
|
|
|
/* check for precompiled rules */
|
2005-06-24 23:05:32 +07:00
|
|
|
strlcpy(comp, udev_rules_filename, sizeof(comp));
|
|
|
|
strlcat(comp, ".compiled", sizeof(comp));
|
|
|
|
if (stat(comp, &stats) == 0) {
|
2005-07-05 20:24:41 +07:00
|
|
|
dbg("map compiled rules '%s'", comp);
|
|
|
|
if (rules_map(rules, comp) == 0)
|
|
|
|
return 0;
|
2005-06-24 23:05:32 +07:00
|
|
|
}
|
|
|
|
|
2004-12-18 17:34:17 +07:00
|
|
|
if (stat(udev_rules_filename, &stats) != 0)
|
2004-03-23 13:22:20 +07:00
|
|
|
return -1;
|
2003-12-03 09:38:30 +07:00
|
|
|
|
2005-06-24 23:05:32 +07:00
|
|
|
if ((stats.st_mode & S_IFMT) != S_IFDIR) {
|
|
|
|
dbg("parse single rules file '%s'", udev_rules_filename);
|
2005-07-05 20:24:41 +07:00
|
|
|
retval = parse_file(rules, udev_rules_filename);
|
2005-06-24 23:05:32 +07:00
|
|
|
} else {
|
2005-03-17 15:59:32 +07:00
|
|
|
struct name_entry *name_loop, *name_tmp;
|
|
|
|
LIST_HEAD(name_list);
|
|
|
|
|
2005-06-24 23:05:32 +07:00
|
|
|
dbg("parse rules directory '%s'", udev_rules_filename);
|
2005-03-17 15:59:32 +07:00
|
|
|
retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
|
|
|
|
|
|
|
|
list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
|
2005-07-05 20:24:41 +07:00
|
|
|
parse_file(rules, name_loop->name);
|
2005-03-17 15:59:32 +07:00
|
|
|
list_del(&name_loop->node);
|
|
|
|
}
|
|
|
|
}
|
2004-02-24 10:31:14 +07:00
|
|
|
|
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
|
|
|
|
2005-07-05 20:24:41 +07:00
|
|
|
void udev_rules_close(struct udev_rules *rules)
|
2005-02-09 14:43:18 +07:00
|
|
|
{
|
2005-07-05 20:24:41 +07:00
|
|
|
if (rules->mapped)
|
|
|
|
file_unmap(rules->buf, rules->bufsize);
|
2005-06-24 23:05:32 +07:00
|
|
|
else
|
2005-07-05 20:24:41 +07:00
|
|
|
free(rules->buf);
|
|
|
|
|
|
|
|
rules->buf = NULL;
|
2005-02-09 14:43:18 +07:00
|
|
|
}
|