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"
46 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
48 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
49 static int strcmp_pattern(const char *p, const char *s)
54 return (p[0] != '\0');
65 while ((p[0] != '\0') && (p[0] != ']')) {
68 if ((s[0] >= p[0]) && (s[0] <= p[2]))
72 match = (p[0] == s[0]);
76 while ((p[0] != '\0') && (p[0] != ']'))
79 return strcmp_pattern(p+1, s+1);
85 if (strcmp_pattern(p, s+1))
86 return strcmp_pattern(p+1, s);
94 if ((p[0] == s[0]) || (p[0] == '?'))
95 return strcmp_pattern(p+1, s+1);
101 /* extract possible {attr} and move str behind it */
102 static char *get_format_attribute(char **str)
107 if (*str[0] == '{') {
108 pos = strchr(*str, '}');
110 dbg("missing closing brace for format");
116 dbg("attribute='%s', str='%s'", attr, *str);
121 /* extract possible format length and move str behind it*/
122 static int get_format_len(char **str)
127 if (isdigit(*str[0])) {
128 num = (int) strtoul(*str, &tail, 10);
131 dbg("format length=%i", num);
134 dbg("format parsing error '%s'", *str);
140 /** Finds the lowest positive N such that <name>N isn't present in
141 * $(udevroot) either as a file or a symlink.
143 * @param name Name to check for
144 * @return 0 if <name> didn't exist and N otherwise.
146 static int find_free_number(struct udevice *udev, const char *name)
148 char devpath[PATH_SIZE];
149 char filename[PATH_SIZE];
152 strlcpy(filename, name, sizeof(filename));
154 dbg("look for existing node '%s'", filename);
155 if (udev_db_search_name(devpath, sizeof(devpath), filename) != 0) {
156 dbg("free num=%d", num);
162 info("find_free_number gone crazy (num=%d), aborted", num);
165 snprintf(filename, sizeof(filename), "%s%d", name, num);
166 filename[sizeof(filename)-1] = '\0';
170 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
171 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
173 char temp[PATH_SIZE];
174 char temp2[PATH_SIZE];
175 char *tail, *pos, *cpos, *attr, *rest;
179 struct sysfs_attribute *tmpattr;
180 unsigned int next_free_number;
181 struct sysfs_class_device *class_dev_parent;
185 pos = strchr(pos, '%');
191 len = get_format_len(&tail);
193 strlcpy(temp, tail+1, sizeof(temp));
195 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
196 attr = get_format_attribute(&tail);
200 strlcat(string, udev->devpath, maxsize);
201 dbg("substitute kernel name '%s'", udev->kernel_name);
204 strlcat(string, udev->bus_id, maxsize);
205 dbg("substitute bus_id '%s'", udev->bus_id);
208 strlcat(string, udev->kernel_name, maxsize);
209 dbg("substitute kernel name '%s'", udev->kernel_name);
212 strlcat(string, udev->kernel_number, maxsize);
213 dbg("substitute kernel number '%s'", udev->kernel_number);
216 sprintf(temp2, "%d", minor(udev->devt));
217 strlcat(string, temp2, maxsize);
218 dbg("substitute minor number '%s'", temp2);
221 sprintf(temp2, "%d", major(udev->devt));
222 strlcat(string, temp2, maxsize);
223 dbg("substitute major number '%s'", temp2);
226 if (udev->program_result[0] == '\0')
228 /* get part part of the result string */
231 i = strtoul(attr, &rest, 10);
233 dbg("request part #%d of result string", i);
234 cpos = udev->program_result;
236 while (cpos[0] != '\0' && !isspace(cpos[0]))
238 while (isspace(cpos[0]))
242 dbg("requested part of result string not found");
245 strlcpy(temp2, cpos, sizeof(temp2));
246 /* %{2+}c copies the whole string from the second part on */
247 if (rest[0] != '+') {
248 cpos = strchr(temp2, ' ');
252 strlcat(string, temp2, maxsize);
253 dbg("substitute part of result string '%s'", temp2);
255 strlcat(string, udev->program_result, maxsize);
256 dbg("substitute result string '%s'", udev->program_result);
263 dbg("missing attribute");
266 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
267 if (tmpattr == NULL) {
268 dbg("sysfa attribute '%s' not found", attr);
271 /* strip trailing whitespace of matching value */
272 if (isspace(tmpattr->value[strlen(tmpattr->value)-1])) {
273 i = len = strlen(tmpattr->value);
274 while (i > 0 && isspace(tmpattr->value[i-1]))
277 tmpattr->value[i] = '\0';
278 dbg("remove %i trailing whitespace chars from '%s'",
279 len - i, tmpattr->value);
282 strlcat(string, tmpattr->value, maxsize);
283 dbg("substitute sysfs value '%s'", tmpattr->value);
286 strlcat(string, "%", maxsize);
290 next_free_number = find_free_number(udev, string);
291 if (next_free_number > 0) {
292 sprintf(temp2, "%d", next_free_number);
293 strlcat(string, temp2, maxsize);
299 class_dev_parent = sysfs_get_classdev_parent(class_dev);
300 if (class_dev_parent != NULL) {
301 struct udevice udev_parent;
303 dbg("found parent '%s', get the node name", class_dev_parent->path);
304 udev_init_device(&udev_parent, NULL, NULL);
305 /* lookup the name in the udev_db with the DEVPATH of the parent */
306 if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
307 strlcat(string, udev_parent.name, maxsize);
308 dbg("substitute parent node name'%s'", udev_parent.name);
310 dbg("parent not found in database");
311 udev_cleanup_device(&udev_parent);
315 if (udev->tmp_node[0] == '\0') {
316 dbg("create temporary device node for callout");
317 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
318 udev_root, major(udev->devt), minor(udev->devt));
319 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
320 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
322 strlcat(string, udev->tmp_node, maxsize);
323 dbg("substitute temporary device node name '%s'", udev->tmp_node);
326 strlcat(string, udev_root, maxsize);
327 dbg("substitute udev_root '%s'", udev_root);
330 dbg("unknown substitution type '%%%c'", c);
333 /* truncate to specified length */
337 strlcat(string, tail, maxsize);
341 static int execute_program(struct udevice *udev, const char *path, char *value, int len)
350 char *argv[(sizeof(arg) / 2) + 1];
353 strlcpy(arg, path, sizeof(arg));
355 if (strchr(path, ' ')) {
357 while (pos != NULL) {
358 if (pos[0] == '\'') {
359 /* don't separate if in apostrophes */
361 argv[i] = strsep(&pos, "\'");
362 while (pos && pos[0] == ' ')
365 argv[i] = strsep(&pos, " ");
367 dbg("arg[%i] '%s'", i, argv[i]);
371 dbg("execute '%s' with parsed arguments", arg);
374 argv[1] = udev->subsystem;
376 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
389 /* dup2 write side of pipe to STDOUT */
390 dup2(fds[1], STDOUT_FILENO);
391 retval = execv(arg, argv);
393 info(KEY_PROGRAM " execution of '%s' failed", path);
399 /* parent reads from fds[0] */
404 count = read(fds[0], value + i, len - i-1);
410 dbg("result len %d too short", len);
417 dbg("read failed with '%s'", strerror(errno));
421 if (i > 0 && value[i-1] == '\n')
424 dbg("result is '%s'", value);
427 waitpid(pid, &status, 0);
429 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
430 dbg("exec program status 0x%x", status);
437 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
439 struct sysfs_attribute *tmpattr = NULL;
442 dbg("look for device attribute '%s'", attr);
443 /* try to find the attribute in the class device directory */
444 tmpattr = sysfs_get_classdev_attr(class_dev, attr);
448 /* look in the class device directory if present */
450 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
458 c = strchr(tmpattr->value, '\n');
462 dbg("found attribute '%s'", tmpattr->path);
466 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct key_pair *pair)
468 struct sysfs_attribute *tmpattr;
472 if ((pair == NULL) || (pair->name[0] == '\0') || (pair->value == '\0'))
475 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->name);
479 /* strip trailing whitespace of value, if not asked to match for it */
480 if (! isspace(pair->value[strlen(pair->value)-1])) {
481 i = len = strlen(tmpattr->value);
482 while (i > 0 && isspace(tmpattr->value[i-1]))
485 tmpattr->value[i] = '\0';
486 dbg("remove %i trailing whitespace chars from '%s'",
487 len - i, tmpattr->value);
491 dbg("compare attribute '%s' value '%s' with '%s'",
492 pair->name, tmpattr->value, pair->value);
493 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
496 dbg("found matching attribute '%s' with value '%s'",
497 pair->name, pair->value);
501 static int match_rule(struct udevice *udev, struct udev_rule *rule,
502 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
504 if (rule->kernel[0] != '\0') {
505 dbg("check for " KEY_KERNEL " rule->kernel='%s' class_dev->name='%s'",
506 rule->kernel, class_dev->name);
507 if (strcmp_pattern(rule->kernel, class_dev->name) != 0) {
508 dbg(KEY_KERNEL " is not matching");
509 if (rule->kernel_operation != KEY_OP_NOMATCH)
512 dbg(KEY_KERNEL " matches");
513 if (rule->kernel_operation == KEY_OP_NOMATCH)
516 dbg(KEY_KERNEL " key is true");
519 if (rule->subsystem[0] != '\0') {
520 dbg("check for " KEY_SUBSYSTEM " rule->subsystem='%s' class_dev->name='%s'",
521 rule->subsystem, class_dev->name);
522 if (strcmp_pattern(rule->subsystem, udev->subsystem) != 0) {
523 dbg(KEY_SUBSYSTEM " is not matching");
524 if (rule->subsystem_operation != KEY_OP_NOMATCH)
527 dbg(KEY_SUBSYSTEM " matches");
528 if (rule->subsystem_operation == KEY_OP_NOMATCH)
531 dbg(KEY_SUBSYSTEM " key is true");
534 if (rule->env_pair_count) {
537 dbg("check for " KEY_ENV " pairs");
538 for (i = 0; i < rule->env_pair_count; i++) {
539 struct key_pair *pair;
542 pair = &rule->env_pair[i];
543 value = getenv(pair->name);
545 dbg(KEY_ENV "{'%s'} is not found", pair->name);
548 if (strcmp_pattern(pair->value, value) != 0) {
549 dbg(KEY_ENV "{'%s'} is not matching", pair->name);
550 if (pair->operation != KEY_OP_NOMATCH)
553 dbg(KEY_ENV "{'%s'} matches", pair->name);
554 if (pair->operation == KEY_OP_NOMATCH)
558 dbg(KEY_ENV " key is true");
561 /* walk up the chain of physical devices and find a match */
563 /* check for matching driver */
564 if (rule->driver[0] != '\0') {
565 if (sysfs_device == NULL) {
566 dbg("device has no sysfs_device");
569 dbg("check for " KEY_DRIVER " rule->driver='%s' sysfs_device->driver_name='%s'",
570 rule->driver, sysfs_device->driver_name);
571 if (strcmp_pattern(rule->driver, sysfs_device->driver_name) != 0) {
572 dbg(KEY_DRIVER " is not matching");
573 if (rule->driver_operation != KEY_OP_NOMATCH)
576 dbg(KEY_DRIVER " matches");
577 if (rule->driver_operation == KEY_OP_NOMATCH)
580 dbg(KEY_DRIVER " key is true");
583 /* check for matching bus value */
584 if (rule->bus[0] != '\0') {
585 if (sysfs_device == NULL) {
586 dbg("device has no sysfs_device");
589 dbg("check for " KEY_BUS " rule->bus='%s' sysfs_device->bus='%s'",
590 rule->bus, sysfs_device->bus);
591 if (strcmp_pattern(rule->bus, sysfs_device->bus) != 0) {
592 dbg(KEY_BUS " is not matching");
593 if (rule->bus_operation != KEY_OP_NOMATCH)
596 dbg(KEY_BUS " matches");
597 if (rule->bus_operation == KEY_OP_NOMATCH)
600 dbg(KEY_BUS " key is true");
603 /* check for matching bus id */
604 if (rule->id[0] != '\0') {
605 if (sysfs_device == NULL) {
606 dbg("device has no sysfs_device");
609 dbg("check " KEY_ID);
610 if (strcmp_pattern(rule->id, sysfs_device->bus_id) != 0) {
611 dbg(KEY_ID " is not matching");
612 if (rule->id_operation != KEY_OP_NOMATCH)
615 dbg(KEY_ID " matches");
616 if (rule->id_operation == KEY_OP_NOMATCH)
619 dbg(KEY_ID " key is true");
622 /* check for matching sysfs pairs */
623 if (rule->sysfs_pair_count) {
626 dbg("check " KEY_SYSFS " pairs");
627 for (i = 0; i < rule->sysfs_pair_count; i++) {
628 struct key_pair *pair;
630 pair = &rule->sysfs_pair[i];
631 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
632 dbg(KEY_SYSFS "{'%s'} is not matching", pair->name);
633 if (pair->operation != KEY_OP_NOMATCH)
636 dbg(KEY_SYSFS "{'%s'} matches", pair->name);
637 if (pair->operation == KEY_OP_NOMATCH)
641 dbg(KEY_SYSFS " keys are true");
644 /* found matching physical device */
647 dbg("try parent sysfs device");
648 sysfs_device = sysfs_get_device_parent(sysfs_device);
649 if (sysfs_device == NULL)
651 dbg("look at sysfs_device->path='%s'", sysfs_device->path);
652 dbg("look at sysfs_device->bus_id='%s'", sysfs_device->bus_id);
655 /* execute external program */
656 if (rule->program[0] != '\0') {
657 char program[PATH_SIZE];
659 dbg("check " KEY_PROGRAM);
660 strlcpy(program, rule->program, sizeof(program));
661 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
662 if (execute_program(udev, program, udev->program_result, sizeof(udev->program_result)) != 0) {
663 dbg(KEY_PROGRAM " returned nonzero");
664 if (rule->program_operation != KEY_OP_NOMATCH)
667 dbg(KEY_PROGRAM " returned successful");
668 if (rule->program_operation == KEY_OP_NOMATCH)
671 dbg(KEY_PROGRAM " key is true");
674 /* check for matching result of external program */
675 if (rule->result[0] != '\0') {
676 dbg("check for " KEY_RESULT " rule->result='%s', udev->program_result='%s'",
677 rule->result, udev->program_result);
678 if (strcmp_pattern(rule->result, udev->program_result) != 0) {
679 dbg(KEY_RESULT " is not matching");
680 if (rule->result_operation != KEY_OP_NOMATCH)
683 dbg(KEY_RESULT " matches");
684 if (rule->result_operation == KEY_OP_NOMATCH)
687 dbg(KEY_RESULT " key is true");
697 int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_dev)
699 struct sysfs_class_device *class_dev_parent;
700 struct sysfs_device *sysfs_device = NULL;
701 struct udev_rule *rule;
703 dbg("class_dev->name='%s'", class_dev->name);
705 /* Figure out where the "device"-symlink is at. For char devices this will
706 * always be in the class_dev->path. On block devices, only the main block
707 * device will have the device symlink in it's path. All partition devices
708 * need to look at the symlink in its parent directory.
710 class_dev_parent = sysfs_get_classdev_parent(class_dev);
711 if (class_dev_parent != NULL) {
712 dbg("given class device has a parent, use this instead");
713 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
715 sysfs_device = sysfs_get_classdev_device(class_dev);
719 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
720 sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
721 strlcpy(udev->bus_id, sysfs_device->bus_id, sizeof(udev->bus_id));
724 dbg("udev->kernel_name='%s'", udev->kernel_name);
726 /* look for a matching rule to apply */
727 list_for_each_entry(rule, &udev_rule_list, node) {
729 if (match_rule(udev, rule, class_dev, sysfs_device) == 0) {
732 if (rule->ignore_device) {
733 info("configured rule in '%s[%i]' applied, '%s' is ignored",
734 rule->config_file, rule->config_line, udev->kernel_name);
737 if (rule->ignore_remove) {
738 udev->ignore_remove = 1;
739 dbg("remove event should be ignored");
741 /* apply all_partitions option only at a main block device */
742 if (rule->partitions && udev->type == DEV_BLOCK && udev->kernel_number[0] == '\0') {
743 udev->partitions = rule->partitions;
744 dbg("creation of partition nodes requested");
747 /* apply permissions */
748 if (rule->mode != 0000) {
749 udev->mode = rule->mode;
750 dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
752 if (rule->owner[0] != '\0') {
753 strlcpy(udev->owner, rule->owner, sizeof(udev->owner));
754 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
755 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
757 if (rule->group[0] != '\0') {
758 strlcpy(udev->group, rule->group, sizeof(udev->group));
759 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
760 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
763 /* collect symlinks */
764 if (rule->symlink[0] != '\0') {
765 char temp[PATH_SIZE];
768 info("configured rule in '%s[%i]' applied, added symlink '%s'",
769 rule->config_file, rule->config_line, rule->symlink);
770 strlcpy(temp, rule->symlink, sizeof(temp));
771 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
773 /* add multiple symlinks separated by spaces */
775 next = strchr(temp, ' ');
778 dbg("add symlink '%s'", pos);
779 name_list_add(&udev->symlink_list, pos, 0);
781 next = strchr(pos, ' ');
783 dbg("add symlink '%s'", pos);
784 name_list_add(&udev->symlink_list, pos, 0);
788 if (rule->name[0] != '\0') {
789 info("configured rule in '%s[%i]' applied, '%s' becomes '%s'",
790 rule->config_file, rule->config_line, udev->kernel_name, rule->name);
792 strlcpy(udev->name, rule->name, sizeof(udev->name));
793 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
794 strlcpy(udev->config_file, rule->config_file, sizeof(udev->config_file));
795 udev->config_line = rule->config_line;
797 if (udev->type != DEV_NET)
798 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
799 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
804 if (rule->last_rule) {
805 dbg("last rule to be applied");
812 if (udev->name[0] == '\0') {
813 /* no rule matched, so we use the kernel name */
814 strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
815 dbg("no rule found, use kernel name '%s'", udev->name);
818 if (udev->tmp_node[0] != '\0') {
819 dbg("removing temporary device node");
820 unlink_secure(udev->tmp_node);
821 udev->tmp_node[0] = '\0';