6 * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (C) 2003-2005 Kay Sievers <kay.sievers@vrfy.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include "udev_libc_wrapper.h"
36 #include "udev_utils.h"
38 #include "udev_rules.h"
41 void udev_rules_iter_init(struct udev_rules *rules)
43 dbg("bufsize=%zi", rules->bufsize);
47 struct udev_rule *udev_rules_iter_next(struct udev_rules *rules)
49 static struct udev_rule *rule;
54 dbg("current=%zi", rules->current);
55 if (rules->current >= rules->bufsize)
59 rule = (struct udev_rule *) (rules->buf + rules->current);
60 rules->current += sizeof(struct udev_rule) + rule->bufsize;
65 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
75 while (isspace(linepos[0]) || linepos[0] == ',')
82 if (linepos[0] == '\0')
84 if (isspace(linepos[0]))
86 if (linepos[0] == '=')
88 if (linepos[0] == '+')
90 if (linepos[0] == '!')
92 if (linepos[0] == ':')
96 /* remember end of key */
99 /* skip whitespace after key */
100 while (isspace(linepos[0]))
103 /* get operation type */
104 if (linepos[0] == '=' && linepos[1] == '=') {
105 *operation = KEY_OP_MATCH;
107 dbg("operator=match");
108 } else if (linepos[0] == '!' && linepos[1] == '=') {
109 *operation = KEY_OP_NOMATCH;
111 dbg("operator=nomatch");
112 } else if (linepos[0] == '+' && linepos[1] == '=') {
113 *operation = KEY_OP_ADD;
116 } else if (linepos[0] == '=') {
117 *operation = KEY_OP_ASSIGN;
119 dbg("operator=assign");
120 } else if (linepos[0] == ':' && linepos[1] == '=') {
121 *operation = KEY_OP_ASSIGN_FINAL;
123 dbg("operator=assign_final");
129 dbg("key='%s'", *key);
131 /* skip whitespace after operator */
132 while (isspace(linepos[0]))
136 if (linepos[0] == '"')
142 temp = strchr(linepos, '"');
147 dbg("value='%s'", *value);
149 /* move line to next key */
155 /* extract possible KEY{attr} */
156 static char *get_key_attribute(char *str)
161 attr = strchr(str, '{');
164 pos = strchr(attr, '}');
166 err("missing closing brace for format");
170 dbg("attribute='%s'", attr);
177 static int add_rule_key(struct udev_rule *rule, struct key *key,
178 enum key_operation operation, const char *value)
180 size_t val_len = strnlen(value, PATH_SIZE);
182 key->operation = operation;
184 key->val_off = rule->bufsize;
185 strlcpy(rule->buf + rule->bufsize, value, val_len+1);
186 rule->bufsize += val_len+1;
191 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
192 enum key_operation operation, const char *key, const char *value)
194 size_t key_len = strnlen(key, PATH_SIZE);
196 if (pairs->count >= PAIRS_MAX) {
197 err("skip, too many keys in a single rule");
201 add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
203 /* add the key-name of the pair */
204 pairs->keys[pairs->count].key_name_off = rule->bufsize;
205 strlcpy(rule->buf + rule->bufsize, key, key_len+1);
206 rule->bufsize += key_len+1;
213 static int add_to_rules(struct udev_rules *rules, char *line)
215 struct udev_rule *rule;
222 /* get all the keys */
223 rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
225 err("malloc failed");
234 enum key_operation operation = KEY_OP_UNSET;
236 retval = get_key(&linepos, &key, &operation, &value);
240 if (strcasecmp(key, "KERNEL") == 0) {
241 add_rule_key(rule, &rule->kernel_name, operation, value);
246 if (strcasecmp(key, "SUBSYSTEM") == 0) {
247 add_rule_key(rule, &rule->subsystem, operation, value);
252 if (strcasecmp(key, "ACTION") == 0) {
253 add_rule_key(rule, &rule->action, operation, value);
258 if (strcasecmp(key, "DEVPATH") == 0) {
259 add_rule_key(rule, &rule->devpath, operation, value);
264 if (strcasecmp(key, "BUS") == 0) {
265 add_rule_key(rule, &rule->bus, operation, value);
270 if (strcasecmp(key, "ID") == 0) {
271 add_rule_key(rule, &rule->id, operation, value);
276 if (strncasecmp(key, "SYSFS", sizeof("SYSFS")-1) == 0) {
277 attr = get_key_attribute(key + sizeof("SYSFS")-1);
279 err("error parsing SYSFS attribute in '%s'", line);
282 add_rule_key_pair(rule, &rule->sysfs, operation, attr, value);
287 if (strncasecmp(key, "ENV", sizeof("ENV")-1) == 0) {
288 attr = get_key_attribute(key + sizeof("ENV")-1);
290 err("error parsing ENV attribute");
293 add_rule_key_pair(rule, &rule->env, operation, attr, value);
298 if (strcasecmp(key, "MODALIAS") == 0) {
299 add_rule_key(rule, &rule->modalias, operation, value);
304 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
305 attr = get_key_attribute(key + sizeof("IMPORT")-1);
306 if (attr && strstr(attr, "program")) {
307 dbg("IMPORT will be executed");
308 rule->import_exec = 1;
309 } else if (attr && strstr(attr, "file")) {
310 dbg("IMPORT will be included as file");
312 /* figure it out if it is executable */
313 char file[PATH_SIZE];
317 strlcpy(file, value, sizeof(file));
318 pos = strchr(file, ' ');
321 dbg("IMPORT auto mode for '%s'", file);
322 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
323 dbg("IMPORT is executable, will be executed");
324 rule->import_exec = 1;
327 add_rule_key(rule, &rule->import, operation, value);
332 if (strcasecmp(key, "DRIVER") == 0) {
333 add_rule_key(rule, &rule->driver, operation, value);
338 if (strcasecmp(key, "RESULT") == 0) {
339 add_rule_key(rule, &rule->result, operation, value);
344 if (strcasecmp(key, "PROGRAM") == 0) {
345 add_rule_key(rule, &rule->program, operation, value);
350 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
351 attr = get_key_attribute(key + sizeof("NAME")-1);
353 if (strstr(attr, "all_partitions") != NULL) {
354 dbg("creation of partition nodes requested");
355 rule->partitions = DEFAULT_PARTITIONS_COUNT;
357 if (strstr(attr, "ignore_remove") != NULL) {
358 dbg("remove event should be ignored");
359 rule->ignore_remove = 1;
362 add_rule_key(rule, &rule->name, operation, value);
366 if (strcasecmp(key, "SYMLINK") == 0) {
367 add_rule_key(rule, &rule->symlink, operation, value);
372 if (strcasecmp(key, "OWNER") == 0) {
374 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
376 strtoul(value, &endptr, 10);
377 if (endptr[0] != '\0') {
379 uid_t uid = lookup_user(value);
380 dbg("replacing username='%s' by id=%i", value, uid);
381 sprintf(owner, "%li", uid);
382 add_rule_key(rule, &rule->owner, operation, owner);
387 add_rule_key(rule, &rule->owner, operation, value);
391 if (strcasecmp(key, "GROUP") == 0) {
393 if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
395 strtoul(value, &endptr, 10);
396 if (endptr[0] != '\0') {
398 gid_t gid = lookup_group(value);
399 dbg("replacing groupname='%s' by id=%i", value, gid);
400 sprintf(group, "%li", gid);
401 add_rule_key(rule, &rule->owner, operation, group);
406 add_rule_key(rule, &rule->group, operation, value);
410 if (strcasecmp(key, "MODE") == 0) {
411 rule->mode = strtol(value, NULL, 8);
412 rule->mode_operation = operation;
417 if (strcasecmp(key, "RUN") == 0) {
418 add_rule_key(rule, &rule->run, operation, value);
423 if (strcasecmp(key, "OPTIONS") == 0) {
424 if (strstr(value, "last_rule") != NULL) {
425 dbg("last rule to be applied");
428 if (strstr(value, "ignore_device") != NULL) {
429 dbg("device should be ignored");
430 rule->ignore_device = 1;
432 if (strstr(value, "ignore_remove") != NULL) {
433 dbg("remove event should be ignored");
434 rule->ignore_remove = 1;
436 if (strstr(value, "all_partitions") != NULL) {
437 dbg("creation of partition nodes requested");
438 rule->partitions = DEFAULT_PARTITIONS_COUNT;
444 err("unknown key '%s', in '%s'", key, line);
447 /* skip line if not any valid key was found */
449 err("invalid rule '%s'", line);
453 /* grow buffer and add rule */
454 rule_size = sizeof(struct udev_rule) + rule->bufsize;
455 rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
457 err("realloc failed");
460 memcpy(rules->buf + rules->bufsize, rule, rule_size);
461 rules->bufsize += rule_size;
467 static int parse_file(struct udev_rules *rules, const char *filename)
469 char line[LINE_SIZE];
478 if (file_map(filename, &buf, &bufsize) != 0) {
479 err("can't open '%s' as rules file", filename);
482 dbg("reading '%s' as rules file", filename);
484 /* loop through the whole file */
487 while (cur < bufsize) {
490 count = buf_get_line(buf, bufsize, cur);
495 if (count >= sizeof(line)) {
496 info("line too long, rule skipped %s, line %d", filename, lineno);
500 /* eat the whitespace */
501 while ((count > 0) && isspace(bufline[0])) {
508 /* see if this is a comment */
509 if (bufline[0] == COMMENT_CHARACTER)
512 /* skip backslash and newline from multi line rules */
513 for (i = j = 0; i < count; i++) {
514 if (bufline[i] == '\\' && bufline[i+1] == '\n')
517 line[j++] = bufline[i];
521 dbg("read '%s'", line);
522 add_to_rules(rules, line);
525 file_unmap(buf, bufsize);
529 static int rules_map(struct udev_rules *rules, const char *filename)
531 if (file_map(filename, &rules->buf, &rules->bufsize)) {
535 if (rules->bufsize == 0) {
536 file_unmap(rules->buf, rules->bufsize);
544 int udev_rules_init(struct udev_rules *rules, int resolve_names)
546 char comp[PATH_SIZE];
550 memset(rules, 0x00, sizeof(struct udev_rules));
551 rules->resolve_names = resolve_names;
553 /* check for precompiled rules */
554 strlcpy(comp, udev_rules_filename, sizeof(comp));
555 strlcat(comp, ".compiled", sizeof(comp));
556 if (stat(comp, &stats) == 0) {
557 dbg("map compiled rules '%s'", comp);
558 if (rules_map(rules, comp) == 0)
562 if (stat(udev_rules_filename, &stats) != 0)
565 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
566 dbg("parse single rules file '%s'", udev_rules_filename);
567 retval = parse_file(rules, udev_rules_filename);
569 struct name_entry *name_loop, *name_tmp;
570 LIST_HEAD(name_list);
572 dbg("parse rules directory '%s'", udev_rules_filename);
573 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
575 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
576 parse_file(rules, name_loop->name);
577 list_del(&name_loop->node);
584 void udev_rules_close(struct udev_rules *rules)
587 file_unmap(rules->buf, rules->bufsize);