IMPORT=<file> allow to import a shell-var style config-file

This allows to source-in a file into the udev environment to have
the defined keys available for later processing by udev itself or
the forked helper programs.

Signed-off-by: Kay Sievers <kay.sievers@suse.de>
This commit is contained in:
Kay Sievers 2005-06-25 13:10:16 +02:00
parent 6bf0ffe8fd
commit bd0ed2ffbe
4 changed files with 182 additions and 2 deletions

View File

@ -256,6 +256,16 @@ EOF
exp_name => "M8-m3-n3-b0:0-sIBM" , exp_name => "M8-m3-n3-b0:0-sIBM" ,
rules => <<EOF rules => <<EOF
BUS=="scsi", ID=="0:0:0:0", NAME="M%M-m%m-n%n-b%3b-s%3s{vendor}" BUS=="scsi", ID=="0:0:0:0", NAME="M%M-m%m-n%n-b%3b-s%3s{vendor}"
EOF
},
{
desc => "import of shellvalue file",
subsys => "block",
devpath => "/block/sda",
exp_name => "subdir/sys/node" ,
rules => <<EOF
BUS=="scsi", IMPORT="test.all", NAME="subdir%E{SYSFSDIR}/node"
KERNEL=="ttyUSB0", NAME="visor"
EOF EOF
}, },
{ {
@ -1372,6 +1382,15 @@ EOF
KERNEL=="sda", GROUP:="nobody" KERNEL=="sda", GROUP:="nobody"
SUBSYSTEM=="block", MODE:="640" SUBSYSTEM=="block", MODE:="640"
KERNEL=="sda", GROUP="not-ok", MODE="0666", NAME="ok" KERNEL=="sda", GROUP="not-ok", MODE="0666", NAME="ok"
EOF
},
{
desc => "env substitution",
subsys => "block",
devpath => "/block/sda",
exp_name => "node-add-me",
rules => <<EOF
KERNEL=="sda", MODE="0666", NAME="node-\$env{ACTION}-me"
EOF EOF
}, },
{ {

View File

@ -135,6 +135,129 @@ static int get_format_len(char **str)
return -1; return -1;
} }
static int get_key(char **line, char **key, char **value)
{
char *linepos;
char *temp;
linepos = *line;
if (!linepos)
return -1;
if (strchr(linepos, '\\')) {
dbg("escaped characters are not supported, skip");
return -1;
}
/* skip whitespace */
while (isspace(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;
}
/* terminate key */
linepos[0] = '\0';
linepos++;
/* skip whitespace */
while (isspace(linepos[0]))
linepos++;
/* get the value*/
if (linepos[0] == '\0')
return -1;
if (linepos[0] == '"') {
linepos++;
temp = strchr(linepos, '"');
if (!temp)
return -1;
temp[0] = '\0';
} else if (linepos[0] == '\'') {
linepos++;
temp = strchr(linepos, '\'');
if (!temp)
return -1;
temp[0] = '\0';
} else {
temp = linepos;
while (temp[0] && !isspace(temp[0]))
temp++;
temp[0] = '\0';
}
*value = linepos;
return 0;
}
static int import_file_into_env(const char *filename)
{
char line[LINE_SIZE];
char *bufline;
char *linepos;
char *variable;
char *value;
char *buf;
size_t bufsize;
size_t cur;
size_t count;
int lineno;
int retval = 0;
if (file_map(filename, &buf, &bufsize) != 0) {
err("can't open '%s'", filename);
return -1;
}
/* loop through the whole file */
lineno = 0;
cur = 0;
while (cur < bufsize) {
count = buf_get_line(buf, bufsize, cur);
bufline = &buf[cur];
cur += count+1;
lineno++;
if (count >= sizeof(line)) {
err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
continue;
}
/* eat the whitespace */
while ((count > 0) && isspace(bufline[0])) {
bufline++;
count--;
}
if (count == 0)
continue;
/* see if this is a comment */
if (bufline[0] == COMMENT_CHARACTER)
continue;
strlcpy(line, bufline, count+1);
linepos = line;
if (get_key(&linepos, &variable, &value) == 0) {
dbg("import %s=%s", variable, value);
setenv(variable, value, 0);
}
}
file_unmap(buf, bufsize);
return retval;
}
/** Finds the lowest positive N such that <name>N isn't present in /** Finds the lowest positive N such that <name>N isn't present in
* $(udevroot) either as a file or a symlink. * $(udevroot) either as a file or a symlink.
* *
@ -198,7 +321,7 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize,
{ {
char temp[PATH_SIZE]; char temp[PATH_SIZE];
char temp2[PATH_SIZE]; char temp2[PATH_SIZE];
char *head, *tail, *cpos, *attr, *rest; char *head, *tail, *pos, *cpos, *attr, *rest;
int len; int len;
int i; int i;
unsigned int next_free_number; unsigned int next_free_number;
@ -218,6 +341,7 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize,
SUBST_TEMP_NODE, SUBST_TEMP_NODE,
SUBST_ROOT, SUBST_ROOT,
SUBST_MODALIAS, SUBST_MODALIAS,
SUBST_ENV,
}; };
static const struct subst_map { static const struct subst_map {
char *name; char *name;
@ -237,6 +361,7 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize,
{ .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE }, { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE },
{ .name = "root", .fmt = 'r', .type = SUBST_ROOT }, { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
{ .name = "modalias", .fmt = 'A', .type = SUBST_MODALIAS }, { .name = "modalias", .fmt = 'A', .type = SUBST_MODALIAS },
{ .name = "env", .fmt = 'E', .type = SUBST_ENV },
{} {}
}; };
enum subst_type type; enum subst_type type;
@ -431,6 +556,17 @@ found:
strlcat(string, temp2, maxsize); strlcat(string, temp2, maxsize);
dbg("substitute MODALIAS '%s'", temp2); dbg("substitute MODALIAS '%s'", temp2);
break; break;
case SUBST_ENV:
if (attr == NULL) {
dbg("missing attribute");
break;
}
pos = getenv(attr);
if (pos == NULL)
break;
strlcat(string, pos, maxsize);
dbg("substitute env '%s=%s'", attr, pos);
break;
default: default:
err("unknown substitution type=%i", type); err("unknown substitution type=%i", type);
break; break;
@ -773,13 +909,28 @@ try_parent:
dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id); dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id);
} }
if (rule->import_operation != KEY_OP_UNSET) {
char import[PATH_SIZE];
strlcpy(import, rule->import, sizeof(import));
apply_format(udev, import, sizeof(import), class_dev, sysfs_device);
dbg("check for " KEY_IMPORT " import='%s", import);
if (import_file_into_env(import) == 0) {
dbg(KEY_IMPORT " file '%s' imported", rule->import);
if (rule->import_operation == KEY_OP_NOMATCH)
goto exit;
} else
goto exit;
dbg(KEY_IMPORT " key is true");
}
/* execute external program */ /* execute external program */
if (rule->program_operation != KEY_OP_UNSET) { if (rule->program_operation != KEY_OP_UNSET) {
char program[PATH_SIZE]; char program[PATH_SIZE];
dbg("check " KEY_PROGRAM);
strlcpy(program, rule->program, sizeof(program)); strlcpy(program, rule->program, sizeof(program));
apply_format(udev, program, sizeof(program), class_dev, sysfs_device); apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
dbg("check for " KEY_PROGRAM " program='%s", program);
if (execute_program_pipe(program, udev->subsystem, if (execute_program_pipe(program, udev->subsystem,
udev->program_result, sizeof(udev->program_result)) != 0) { udev->program_result, sizeof(udev->program_result)) != 0) {
dbg(KEY_PROGRAM " returned nonzero"); dbg(KEY_PROGRAM " returned nonzero");

View File

@ -40,6 +40,7 @@
#define KEY_SYSFS "SYSFS" #define KEY_SYSFS "SYSFS"
#define KEY_ENV "ENV" #define KEY_ENV "ENV"
#define KEY_MODALIAS "MODALIAS" #define KEY_MODALIAS "MODALIAS"
#define KEY_IMPORT "IMPORT"
#define KEY_NAME "NAME" #define KEY_NAME "NAME"
#define KEY_SYMLINK "SYMLINK" #define KEY_SYMLINK "SYMLINK"
#define KEY_OWNER "OWNER" #define KEY_OWNER "OWNER"
@ -100,6 +101,8 @@ struct udev_rule {
int env_pair_count; int env_pair_count;
enum key_operation modalias_operation; enum key_operation modalias_operation;
char modalias[PATH_SIZE]; char modalias[PATH_SIZE];
enum key_operation import_operation;
char import[PATH_SIZE];
char name[PATH_SIZE]; char name[PATH_SIZE];
enum key_operation name_operation; enum key_operation name_operation;

View File

@ -375,6 +375,13 @@ static int rules_parse(const char *filename)
continue; continue;
} }
if (strcasecmp(key, KEY_IMPORT) == 0) {
strlcpy(rule.import, value, sizeof(rule.import));
rule.import_operation = operation;
valid = 1;
continue;
}
if (strcasecmp(key, KEY_DRIVER) == 0) { if (strcasecmp(key, KEY_DRIVER) == 0) {
strlcpy(rule.driver, value, sizeof(rule.driver)); strlcpy(rule.driver, value, sizeof(rule.driver));
rule.driver_operation = operation; rule.driver_operation = operation;