6 * Copyright (C) 2003 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.
36 #include "libsysfs/sysfs/libsysfs.h"
38 #include "udev_libc_wrapper.h"
40 #include "udev_utils.h"
41 #include "udev_version.h"
43 #include "udev_rules.h"
47 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
48 static int strcmp_pattern(const char *p, const char *s)
53 return (p[0] != '\0');
64 while ((p[0] != '\0') && (p[0] != ']')) {
67 if ((s[0] >= p[0]) && (s[0] <= p[2]))
71 match = (p[0] == s[0]);
75 while ((p[0] != '\0') && (p[0] != ']'))
78 return strcmp_pattern(p+1, s+1);
84 if (strcmp_pattern(p, s+1))
85 return strcmp_pattern(p+1, s);
93 if ((p[0] == s[0]) || (p[0] == '?'))
94 return strcmp_pattern(p+1, s+1);
100 /* extract possible {attr} and move str behind it */
101 static char *get_format_attribute(char **str)
106 if (*str[0] == '{') {
107 pos = strchr(*str, '}');
109 err("missing closing brace for format");
115 dbg("attribute='%s', str='%s'", attr, *str);
120 /* extract possible format length and move str behind it*/
121 static int get_format_len(char **str)
126 if (isdigit(*str[0])) {
127 num = (int) strtoul(*str, &tail, 10);
130 dbg("format length=%i", num);
133 err("format parsing error '%s'", *str);
139 /** Finds the lowest positive N such that <name>N isn't present in
140 * $(udevroot) either as a file or a symlink.
142 * @param name Name to check for
143 * @return 0 if <name> didn't exist and N otherwise.
145 static int find_free_number(struct udevice *udev, const char *name)
147 char devpath[PATH_SIZE];
148 char filename[PATH_SIZE];
151 strlcpy(filename, name, sizeof(filename));
153 dbg("look for existing node '%s'", filename);
154 if (udev_db_search_name(devpath, sizeof(devpath), filename) != 0) {
155 dbg("free num=%d", num);
161 info("find_free_number gone crazy (num=%d), aborted", num);
164 snprintf(filename, sizeof(filename), "%s%d", name, num);
165 filename[sizeof(filename)-1] = '\0';
169 static int find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device,
170 const char *name, char *value, size_t len)
172 struct sysfs_attribute *tmpattr;
174 dbg("look for device attribute '%s'", name);
176 tmpattr = sysfs_get_classdev_attr(class_dev, name);
181 tmpattr = sysfs_get_device_attr(sysfs_device, name);
189 strlcpy(value, tmpattr->value, len);
190 remove_trailing_char(value, '\n');
192 dbg("found attribute '%s'", tmpattr->path);
196 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
197 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
199 char temp[PATH_SIZE];
200 char temp2[PATH_SIZE];
201 char *tail, *pos, *cpos, *attr, *rest;
205 unsigned int next_free_number;
206 struct sysfs_class_device *class_dev_parent;
210 pos = strchr(pos, '%');
216 len = get_format_len(&tail);
218 strlcpy(temp, tail+1, sizeof(temp));
220 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
221 attr = get_format_attribute(&tail);
225 strlcat(string, udev->devpath, maxsize);
226 dbg("substitute kernel name '%s'", udev->kernel_name);
229 strlcat(string, udev->bus_id, maxsize);
230 dbg("substitute bus_id '%s'", udev->bus_id);
233 strlcat(string, udev->kernel_name, maxsize);
234 dbg("substitute kernel name '%s'", udev->kernel_name);
237 strlcat(string, udev->kernel_number, maxsize);
238 dbg("substitute kernel number '%s'", udev->kernel_number);
241 sprintf(temp2, "%d", minor(udev->devt));
242 strlcat(string, temp2, maxsize);
243 dbg("substitute minor number '%s'", temp2);
246 sprintf(temp2, "%d", major(udev->devt));
247 strlcat(string, temp2, maxsize);
248 dbg("substitute major number '%s'", temp2);
251 if (udev->program_result[0] == '\0')
253 /* get part part of the result string */
256 i = strtoul(attr, &rest, 10);
258 dbg("request part #%d of result string", i);
259 cpos = udev->program_result;
261 while (cpos[0] != '\0' && !isspace(cpos[0]))
263 while (isspace(cpos[0]))
267 err("requested part of result string not found");
270 strlcpy(temp2, cpos, sizeof(temp2));
271 /* %{2+}c copies the whole string from the second part on */
272 if (rest[0] != '+') {
273 cpos = strchr(temp2, ' ');
277 strlcat(string, temp2, maxsize);
278 dbg("substitute part of result string '%s'", temp2);
280 strlcat(string, udev->program_result, maxsize);
281 dbg("substitute result string '%s'", udev->program_result);
286 dbg("missing attribute");
289 if (find_sysfs_attribute(class_dev, sysfs_device, attr, temp2, sizeof(temp2)) != 0) {
290 dbg("sysfs attribute '%s' not found", attr);
293 /* strip trailing whitespace of sysfs value */
295 while (i > 0 && isspace(temp2[i-1]))
297 replace_untrusted_chars(temp2);
298 strlcat(string, temp2, maxsize);
299 dbg("substitute sysfs value '%s'", temp2);
302 strlcat(string, "%", maxsize);
306 next_free_number = find_free_number(udev, string);
307 if (next_free_number > 0) {
308 sprintf(temp2, "%d", next_free_number);
309 strlcat(string, temp2, maxsize);
315 class_dev_parent = sysfs_get_classdev_parent(class_dev);
316 if (class_dev_parent != NULL) {
317 struct udevice udev_parent;
319 dbg("found parent '%s', get the node name", class_dev_parent->path);
320 udev_init_device(&udev_parent, NULL, NULL);
321 /* lookup the name in the udev_db with the DEVPATH of the parent */
322 if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
323 strlcat(string, udev_parent.name, maxsize);
324 dbg("substitute parent node name'%s'", udev_parent.name);
326 dbg("parent not found in database");
327 udev_cleanup_device(&udev_parent);
331 if (udev->tmp_node[0] == '\0') {
332 dbg("create temporary device node for callout");
333 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
334 udev_root, major(udev->devt), minor(udev->devt));
335 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
336 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
338 strlcat(string, udev->tmp_node, maxsize);
339 dbg("substitute temporary device node name '%s'", udev->tmp_node);
342 strlcat(string, udev_root, maxsize);
343 dbg("substitute udev_root '%s'", udev_root);
346 err("unknown substitution type '%%%c'", c);
349 /* truncate to specified length */
353 strlcat(string, tail, maxsize);
357 static int execute_program(struct udevice *udev, const char *path, char *value, int len)
366 char *argv[(sizeof(arg) / 2) + 1];
369 strlcpy(arg, path, sizeof(arg));
371 if (strchr(path, ' ')) {
373 while (pos != NULL) {
374 if (pos[0] == '\'') {
375 /* don't separate if in apostrophes */
377 argv[i] = strsep(&pos, "\'");
378 while (pos && pos[0] == ' ')
381 argv[i] = strsep(&pos, " ");
383 dbg("arg[%i] '%s'", i, argv[i]);
387 dbg("execute '%s' with parsed arguments", arg);
390 argv[1] = udev->subsystem;
392 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
404 /* child dup2 write side of pipe to STDOUT */
405 dup2(fds[1], STDOUT_FILENO);
406 retval = execv(arg, argv);
408 info(KEY_PROGRAM " execution of '%s' failed", path);
411 err("fork of '%s' failed", path);
414 /* parent reads from fds[0] */
419 count = read(fds[0], value + i, len - i-1);
421 err("read failed with '%s'", strerror(errno));
430 err("result len %d too short", len);
438 waitpid(pid, &status, 0);
440 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
441 dbg("exec program status 0x%x", status);
447 remove_trailing_char(value, '\n');
448 dbg("result is '%s'", value);
449 replace_untrusted_chars(value);
456 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct key_pair *pair)
458 char value[VALUE_SIZE];
461 if (find_sysfs_attribute(class_dev, sysfs_device, pair->name, value, sizeof(value)) != 0)
464 /* strip trailing whitespace of value, if not asked to match for it */
465 if (!isspace(pair->value[strlen(pair->value)-1])) {
467 while (i > 0 && isspace(value[i-1]))
469 dbg("removed %i trailing whitespace chars from '%s'", strlen(value)-i, value);
472 dbg("compare attribute '%s' value '%s' with '%s'", pair->name, value, pair->value);
473 if (strcmp_pattern(pair->value, value) != 0)
476 dbg("found matching attribute '%s' with value '%s'", pair->name, pair->value);
480 static int match_rule(struct udevice *udev, struct udev_rule *rule,
481 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
483 struct sysfs_device *parent_device = sysfs_device;
485 if (rule->kernel[0] != '\0') {
486 dbg("check for " KEY_KERNEL " rule->kernel='%s' class_dev->name='%s'",
487 rule->kernel, class_dev->name);
488 if (strcmp_pattern(rule->kernel, class_dev->name) != 0) {
489 dbg(KEY_KERNEL " is not matching");
490 if (rule->kernel_operation != KEY_OP_NOMATCH)
493 dbg(KEY_KERNEL " matches");
494 if (rule->kernel_operation == KEY_OP_NOMATCH)
497 dbg(KEY_KERNEL " key is true");
500 if (rule->subsystem[0] != '\0') {
501 dbg("check for " KEY_SUBSYSTEM " rule->subsystem='%s' class_dev->name='%s'",
502 rule->subsystem, class_dev->name);
503 if (strcmp_pattern(rule->subsystem, udev->subsystem) != 0) {
504 dbg(KEY_SUBSYSTEM " is not matching");
505 if (rule->subsystem_operation != KEY_OP_NOMATCH)
508 dbg(KEY_SUBSYSTEM " matches");
509 if (rule->subsystem_operation == KEY_OP_NOMATCH)
512 dbg(KEY_SUBSYSTEM " key is true");
515 if (rule->env_pair_count) {
518 dbg("check for " KEY_ENV " pairs");
519 for (i = 0; i < rule->env_pair_count; i++) {
520 struct key_pair *pair;
523 pair = &rule->env_pair[i];
524 value = getenv(pair->name);
526 dbg(KEY_ENV "{'%s'} is not found", pair->name);
529 if (strcmp_pattern(pair->value, value) != 0) {
530 dbg(KEY_ENV "{'%s'} is not matching", pair->name);
531 if (pair->operation != KEY_OP_NOMATCH)
534 dbg(KEY_ENV "{'%s'} matches", pair->name);
535 if (pair->operation == KEY_OP_NOMATCH)
539 dbg(KEY_ENV " key is true");
542 /* walk up the chain of physical devices and find a match */
544 /* check for matching driver */
545 if (rule->driver[0] != '\0') {
546 if (parent_device == NULL) {
547 dbg("device has no sysfs_device");
550 dbg("check for " KEY_DRIVER " rule->driver='%s' sysfs_device->driver_name='%s'",
551 rule->driver, parent_device->driver_name);
552 if (strcmp_pattern(rule->driver, parent_device->driver_name) != 0) {
553 dbg(KEY_DRIVER " is not matching");
554 if (rule->driver_operation != KEY_OP_NOMATCH)
557 dbg(KEY_DRIVER " matches");
558 if (rule->driver_operation == KEY_OP_NOMATCH)
561 dbg(KEY_DRIVER " key is true");
564 /* check for matching bus value */
565 if (rule->bus[0] != '\0') {
566 if (parent_device == NULL) {
567 dbg("device has no sysfs_device");
570 dbg("check for " KEY_BUS " rule->bus='%s' sysfs_device->bus='%s'",
571 rule->bus, parent_device->bus);
572 if (strcmp_pattern(rule->bus, parent_device->bus) != 0) {
573 dbg(KEY_BUS " is not matching");
574 if (rule->bus_operation != KEY_OP_NOMATCH)
577 dbg(KEY_BUS " matches");
578 if (rule->bus_operation == KEY_OP_NOMATCH)
581 dbg(KEY_BUS " key is true");
584 /* check for matching bus id */
585 if (rule->id[0] != '\0') {
586 if (parent_device == NULL) {
587 dbg("device has no sysfs_device");
590 dbg("check " KEY_ID);
591 if (strcmp_pattern(rule->id, parent_device->bus_id) != 0) {
592 dbg(KEY_ID " is not matching");
593 if (rule->id_operation != KEY_OP_NOMATCH)
596 dbg(KEY_ID " matches");
597 if (rule->id_operation == KEY_OP_NOMATCH)
600 dbg(KEY_ID " key is true");
603 /* check for matching sysfs pairs */
604 if (rule->sysfs_pair_count) {
607 dbg("check " KEY_SYSFS " pairs");
608 for (i = 0; i < rule->sysfs_pair_count; i++) {
609 struct key_pair *pair;
611 pair = &rule->sysfs_pair[i];
612 if (compare_sysfs_attribute(class_dev, parent_device, pair) != 0) {
613 dbg(KEY_SYSFS "{'%s'} is not matching", pair->name);
614 if (pair->operation != KEY_OP_NOMATCH)
617 dbg(KEY_SYSFS "{'%s'} matches", pair->name);
618 if (pair->operation == KEY_OP_NOMATCH)
622 dbg(KEY_SYSFS " keys are true");
625 /* found matching physical device */
628 dbg("try parent sysfs device");
629 parent_device = sysfs_get_device_parent(parent_device);
630 if (parent_device == NULL)
632 dbg("look at sysfs_device->path='%s'", parent_device->path);
633 dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id);
636 /* execute external program */
637 if (rule->program[0] != '\0') {
638 char program[PATH_SIZE];
640 dbg("check " KEY_PROGRAM);
641 strlcpy(program, rule->program, sizeof(program));
642 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
643 if (execute_program(udev, program, udev->program_result, sizeof(udev->program_result)) != 0) {
644 dbg(KEY_PROGRAM " returned nonzero");
645 if (rule->program_operation != KEY_OP_NOMATCH)
648 dbg(KEY_PROGRAM " returned successful");
649 if (rule->program_operation == KEY_OP_NOMATCH)
652 dbg(KEY_PROGRAM " key is true");
655 /* check for matching result of external program */
656 if (rule->result[0] != '\0') {
657 dbg("check for " KEY_RESULT " rule->result='%s', udev->program_result='%s'",
658 rule->result, udev->program_result);
659 if (strcmp_pattern(rule->result, udev->program_result) != 0) {
660 dbg(KEY_RESULT " is not matching");
661 if (rule->result_operation != KEY_OP_NOMATCH)
664 dbg(KEY_RESULT " matches");
665 if (rule->result_operation == KEY_OP_NOMATCH)
668 dbg(KEY_RESULT " key is true");
678 int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_dev)
680 struct sysfs_class_device *class_dev_parent;
681 struct sysfs_device *sysfs_device = NULL;
682 struct udev_rule *rule;
684 dbg("class_dev->name='%s'", class_dev->name);
686 /* Figure out where the "device"-symlink is at. For char devices this will
687 * always be in the class_dev->path. On block devices, only the main block
688 * device will have the device symlink in it's path. All partition devices
689 * need to look at the symlink in its parent directory.
691 class_dev_parent = sysfs_get_classdev_parent(class_dev);
692 if (class_dev_parent != NULL) {
693 dbg("given class device has a parent, use this instead");
694 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
696 sysfs_device = sysfs_get_classdev_device(class_dev);
700 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
701 sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
702 strlcpy(udev->bus_id, sysfs_device->bus_id, sizeof(udev->bus_id));
705 dbg("udev->kernel_name='%s'", udev->kernel_name);
707 /* look for a matching rule to apply */
708 list_for_each_entry(rule, &udev_rule_list, node) {
710 if (match_rule(udev, rule, class_dev, sysfs_device) == 0) {
713 if (rule->ignore_device) {
714 info("configured rule in '%s[%i]' applied, '%s' is ignored",
715 rule->config_file, rule->config_line, udev->kernel_name);
718 if (rule->ignore_remove) {
719 udev->ignore_remove = 1;
720 dbg("remove event should be ignored");
722 /* apply all_partitions option only at a main block device */
723 if (rule->partitions && udev->type == DEV_BLOCK && udev->kernel_number[0] == '\0') {
724 udev->partitions = rule->partitions;
725 dbg("creation of partition nodes requested");
728 /* apply permissions */
729 if (rule->mode != 0000) {
730 udev->mode = rule->mode;
731 dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
733 if (rule->owner[0] != '\0') {
734 strlcpy(udev->owner, rule->owner, sizeof(udev->owner));
735 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
736 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
738 if (rule->group[0] != '\0') {
739 strlcpy(udev->group, rule->group, sizeof(udev->group));
740 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
741 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
744 /* collect symlinks */
745 if (rule->symlink[0] != '\0') {
746 char temp[PATH_SIZE];
749 info("configured rule in '%s[%i]' applied, added symlink '%s'",
750 rule->config_file, rule->config_line, rule->symlink);
751 strlcpy(temp, rule->symlink, sizeof(temp));
752 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
754 /* add multiple symlinks separated by spaces */
756 next = strchr(temp, ' ');
759 info("add symlink '%s'", pos);
760 name_list_add(&udev->symlink_list, pos, 0);
762 next = strchr(pos, ' ');
764 info("add symlink '%s'", pos);
765 name_list_add(&udev->symlink_list, pos, 0);
769 if (rule->name[0] != '\0') {
770 info("configured rule in '%s[%i]' applied, '%s' becomes '%s'",
771 rule->config_file, rule->config_line, udev->kernel_name, rule->name);
773 strlcpy(udev->name, rule->name, sizeof(udev->name));
774 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
775 strlcpy(udev->config_file, rule->config_file, sizeof(udev->config_file));
776 udev->config_line = rule->config_line;
778 if (udev->type != DEV_NET)
779 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
780 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
785 if (rule->last_rule) {
786 dbg("last rule to be applied");
793 if (udev->name[0] == '\0') {
794 /* no rule matched, so we use the kernel name */
795 strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
796 info("no rule found, use kernel name '%s'", udev->name);
799 if (udev->tmp_node[0] != '\0') {
800 dbg("removing temporary device node");
801 unlink_secure(udev->tmp_node);
802 udev->tmp_node[0] = '\0';