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"
39 #include "udev_utils.h"
40 #include "udev_version.h"
45 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
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 dbg("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 dbg("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[NAME_SIZE];
148 char filename[NAME_SIZE];
151 strfieldcpy(filename, name);
153 dbg("look for existing node '%s'", filename);
154 if (udev_db_search_name(devpath, DEVPATH_SIZE, filename) != 0) {
155 dbg("free num=%d", num);
161 info("find_free_number gone crazy (num=%d), aborted", num);
164 snprintf(filename, NAME_SIZE, "%s%d", name, num);
165 filename[NAME_SIZE-1] = '\0';
169 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
170 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
172 char temp[NAME_SIZE];
173 char temp2[NAME_SIZE];
183 struct sysfs_attribute *tmpattr;
184 unsigned int next_free_number;
185 struct sysfs_class_device *class_dev_parent;
189 pos = strchr(pos, '%');
195 len = get_format_len(&tail);
197 strfieldcpy(temp, tail+1);
199 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
200 attr = get_format_attribute(&tail);
204 strfieldcatmax(string, udev->devpath, maxsize);
205 dbg("substitute kernel name '%s'", udev->kernel_name);
208 strfieldcatmax(string, udev->bus_id, maxsize);
209 dbg("substitute bus_id '%s'", udev->bus_id);
212 strfieldcatmax(string, udev->kernel_name, maxsize);
213 dbg("substitute kernel name '%s'", udev->kernel_name);
216 strfieldcatmax(string, udev->kernel_number, maxsize);
217 dbg("substitute kernel number '%s'", udev->kernel_number);
220 strintcatmax(string, minor(udev->devt), maxsize);
221 dbg("substitute minor number '%u'", minor(udev->devt));
224 strintcatmax(string, major(udev->devt), maxsize);
225 dbg("substitute major number '%u'", major(udev->devt));
228 if (udev->program_result[0] == '\0')
230 /* get part part of the result string */
233 i = strtoul(attr, &rest, 10);
235 foreach_strpart(udev->program_result, " \n\r", spos, slen) {
241 dbg("requested part of result string not found");
245 strfieldcpy(temp2, spos);
247 strfieldcpymax(temp2, spos, slen+1);
248 strfieldcatmax(string, temp2, maxsize);
249 dbg("substitute part of result string '%s'", temp2);
251 strfieldcatmax(string, udev->program_result, maxsize);
252 dbg("substitute result string '%s'", udev->program_result);
259 dbg("missing attribute");
262 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
263 if (tmpattr == NULL) {
264 dbg("sysfa attribute '%s' not found", attr);
267 /* strip trailing whitespace of matching value */
268 if (isspace(tmpattr->value[strlen(tmpattr->value)-1])) {
269 i = len = strlen(tmpattr->value);
270 while (i > 0 && isspace(tmpattr->value[i-1]))
273 tmpattr->value[i] = '\0';
274 dbg("remove %i trailing whitespace chars from '%s'",
275 len - i, tmpattr->value);
278 strfieldcatmax(string, tmpattr->value, maxsize);
279 dbg("substitute sysfs value '%s'", tmpattr->value);
282 strfieldcatmax(string, "%", maxsize);
286 next_free_number = find_free_number(udev, string);
287 if (next_free_number > 0) {
288 sprintf(temp2, "%d", next_free_number);
289 strfieldcatmax(string, temp2, maxsize);
295 class_dev_parent = sysfs_get_classdev_parent(class_dev);
296 if (class_dev_parent != NULL) {
297 struct udevice udev_parent;
299 dbg("found parent '%s', get the node name", class_dev_parent->path);
300 udev_init_device(&udev_parent, NULL, NULL);
301 /* lookup the name in the udev_db with the DEVPATH of the parent */
302 if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
303 strfieldcatmax(string, udev_parent.name, maxsize);
304 dbg("substitute parent node name'%s'", udev_parent.name);
306 dbg("parent not found in database");
307 udev_cleanup_device(&udev_parent);
311 if (udev->tmp_node[0] == '\0') {
312 dbg("create temporary device node for callout");
313 snprintf(udev->tmp_node, NAME_SIZE, "%s/.tmp-%u-%u", udev_root, major(udev->devt), minor(udev->devt));
314 udev->tmp_node[NAME_SIZE] = '\0';
315 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
317 strfieldcatmax(string, udev->tmp_node, maxsize);
318 dbg("substitute temporary device node name '%s'", udev->tmp_node);
321 strfieldcatmax(string, udev_root, maxsize);
322 dbg("substitute udev_root '%s'", udev_root);
325 dbg("unknown substitution type '%%%c'", c);
328 /* truncate to specified length */
332 strfieldcatmax(string, tail, maxsize);
336 static int execute_program(struct udevice *udev, const char *path, char *value, int len)
344 char arg[PROGRAM_SIZE];
345 char *argv[(PROGRAM_SIZE / 2) + 1];
348 strfieldcpy(arg, path);
350 if (strchr(path, ' ')) {
352 while (pos != NULL) {
353 if (pos[0] == '\'') {
354 /* don't separate if in apostrophes */
356 argv[i] = strsep(&pos, "\'");
357 while (pos && pos[0] == ' ')
360 argv[i] = strsep(&pos, " ");
362 dbg("arg[%i] '%s'", i, argv[i]);
366 dbg("execute '%s' with parsed arguments", arg);
369 argv[1] = udev->subsystem;
371 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
384 /* dup2 write side of pipe to STDOUT */
385 dup2(fds[1], STDOUT_FILENO);
386 retval = execv(arg, argv);
388 info(FIELD_PROGRAM " execution of '%s' failed", path);
394 /* parent reads from fds[0] */
399 count = read(fds[0], value + i, len - i-1);
405 dbg("result len %d too short", len);
412 dbg("read failed with '%s'", strerror(errno));
416 if (i > 0 && value[i-1] == '\n')
419 dbg("result is '%s'", value);
422 waitpid(pid, &status, 0);
424 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
425 dbg("exec program status 0x%x", status);
432 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
434 struct sysfs_attribute *tmpattr = NULL;
437 dbg("look for device attribute '%s'", attr);
438 /* try to find the attribute in the class device directory */
439 tmpattr = sysfs_get_classdev_attr(class_dev, attr);
443 /* look in the class device directory if present */
445 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
453 c = strchr(tmpattr->value, '\n');
457 dbg("found attribute '%s'", tmpattr->path);
461 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
463 struct sysfs_attribute *tmpattr;
467 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
470 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
474 /* strip trailing whitespace of value, if not asked to match for it */
475 if (! isspace(pair->value[strlen(pair->value)-1])) {
476 i = len = strlen(tmpattr->value);
477 while (i > 0 && isspace(tmpattr->value[i-1]))
480 tmpattr->value[i] = '\0';
481 dbg("remove %i trailing whitespace chars from '%s'",
482 len - i, tmpattr->value);
486 dbg("compare attribute '%s' value '%s' with '%s'",
487 pair->file, tmpattr->value, pair->value);
488 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
491 dbg("found matching attribute '%s' with value '%s'",
492 pair->file, pair->value);
496 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
498 struct sysfs_pair *pair;
501 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
502 pair = &dev->sysfs_pair[i];
503 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
505 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
506 dbg("sysfs attribute doesn't match");
514 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
516 char path[SYSFS_PATH_MAX];
519 /* we have to have a sysfs device for ID to work */
523 strfieldcpy(path, sysfs_device->path);
524 temp = strrchr(path, '/');
526 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
527 if (strcmp_pattern(dev->id, temp) != 0)
533 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
535 char path[SYSFS_PATH_MAX];
538 /* we have to have a sysfs device for PLACE to work */
542 strfieldcpy(path, sysfs_device->path);
543 temp = strrchr(path, '/');
544 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
545 if (strstr(temp, dev->place) != NULL)
550 temp = strrchr(path, '/');
551 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
552 if (strstr(temp, dev->place) == NULL)
555 dbg("place doesn't match");
559 static int match_rule(struct udevice *udev, struct config_device *dev,
560 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
562 if (dev->kernel[0] != '\0') {
563 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'",
564 dev->kernel, class_dev->name);
565 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
566 dbg(FIELD_KERNEL " is not matching");
569 dbg(FIELD_KERNEL " matches");
572 if (dev->subsystem[0] != '\0') {
573 dbg("check for " FIELD_SUBSYSTEM " dev->subsystem='%s' class_dev->name='%s'",
574 dev->subsystem, class_dev->name);
575 if (strcmp_pattern(dev->subsystem, udev->subsystem) != 0) {
576 dbg(FIELD_SUBSYSTEM " is not matching");
579 dbg(FIELD_SUBSYSTEM " matches");
582 /* walk up the chain of physical devices and find a match */
584 /* check for matching driver */
585 if (dev->driver[0] != '\0') {
586 dbg("check for " FIELD_DRIVER " dev->driver='%s' sysfs_device->driver_name='%s'",
587 dev->driver, sysfs_device->driver_name);
588 if (strcmp_pattern(dev->driver, sysfs_device->driver_name) != 0) {
589 dbg(FIELD_DRIVER " is not matching");
592 dbg(FIELD_DRIVER " matches");
595 /* check for matching bus value */
596 if (dev->bus[0] != '\0') {
597 if (sysfs_device == NULL) {
598 dbg("device has no bus");
601 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'",
602 dev->bus, sysfs_device->bus);
603 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
604 dbg(FIELD_BUS " is not matching");
607 dbg(FIELD_BUS " matches");
610 /* check for matching bus id */
611 if (dev->id[0] != '\0') {
612 dbg("check " FIELD_ID);
613 if (match_id(dev, class_dev, sysfs_device) != 0) {
614 dbg(FIELD_ID " is not matching");
617 dbg(FIELD_ID " matches");
620 /* check for matching place of device */
621 if (dev->place[0] != '\0') {
622 dbg("check " FIELD_PLACE);
623 if (match_place(dev, class_dev, sysfs_device) != 0) {
624 dbg(FIELD_PLACE " is not matching");
627 dbg(FIELD_PLACE " matches");
630 /* check for matching sysfs pairs */
631 if (dev->sysfs_pair[0].file[0] != '\0') {
632 dbg("check " FIELD_SYSFS " pairs");
633 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
634 dbg(FIELD_SYSFS " is not matching");
637 dbg(FIELD_SYSFS " matches");
640 /* found matching physical device */
643 dbg("try parent sysfs device");
644 sysfs_device = sysfs_get_device_parent(sysfs_device);
645 if (sysfs_device == NULL)
647 dbg("sysfs_device->path='%s'", sysfs_device->path);
648 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
651 /* execute external program */
652 if (dev->program[0] != '\0') {
653 char program[PROGRAM_SIZE];
655 dbg("check " FIELD_PROGRAM);
656 strfieldcpy(program, dev->program);
657 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
658 if (execute_program(udev, program, udev->program_result, NAME_SIZE) != 0) {
659 dbg(FIELD_PROGRAM " returned nonzero");
662 dbg(FIELD_PROGRAM " returned successful");
665 /* check for matching result of external program */
666 if (dev->result[0] != '\0') {
667 dbg("check for " FIELD_RESULT " dev->result='%s', udev->program_result='%s'",
668 dev->result, udev->program_result);
669 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
670 dbg(FIELD_RESULT " is not matching");
673 dbg(FIELD_RESULT " matches");
683 int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_dev)
685 struct sysfs_class_device *class_dev_parent;
686 struct sysfs_device *sysfs_device = NULL;
687 struct config_device *dev;
689 dbg("class_dev->name='%s'", class_dev->name);
691 /* Figure out where the "device"-symlink is at. For char devices this will
692 * always be in the class_dev->path. On block devices, only the main block
693 * device will have the device symlink in it's path. All partition devices
694 * need to look at the symlink in its parent directory.
696 class_dev_parent = sysfs_get_classdev_parent(class_dev);
697 if (class_dev_parent != NULL) {
698 dbg("given class device has a parent, use this instead");
699 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
701 sysfs_device = sysfs_get_classdev_device(class_dev);
705 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
706 sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
707 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
710 dbg("udev->kernel_name='%s'", udev->kernel_name);
712 /* look for a matching rule to apply */
713 list_for_each_entry(dev, &config_device_list, node) {
715 if (match_rule(udev, dev, class_dev, sysfs_device) == 0) {
718 if (dev->ignore_device) {
719 info("configured rule in '%s[%i]' applied, '%s' is ignored",
720 dev->config_file, dev->config_line, udev->kernel_name);
723 if (dev->ignore_remove) {
724 udev->ignore_remove = 1;
725 dbg_parse("remove event should be ignored");
727 /* apply all_partitions option only at a main block device */
728 if (dev->partitions && udev->type == BLOCK && udev->kernel_number[0] == '\0') {
729 udev->partitions = dev->partitions;
730 dbg("creation of partition nodes requested");
733 /* apply permissions */
734 if (dev->mode != 0000) {
735 udev->mode = dev->mode;
736 dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
738 if (dev->owner[0] != '\0') {
739 strfieldcpy(udev->owner, dev->owner);
740 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
741 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
743 if (dev->group[0] != '\0') {
744 strfieldcpy(udev->group, dev->group);
745 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
746 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
749 /* collect symlinks */
750 if (dev->symlink[0] != '\0') {
751 char temp[NAME_SIZE];
754 info("configured rule in '%s[%i]' applied, added symlink '%s'",
755 dev->config_file, dev->config_line, dev->symlink);
756 strfieldcpy(temp, dev->symlink);
757 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
759 /* add multiple symlinks separated by spaces */
761 next = strchr(temp, ' ');
764 dbg("add symlink '%s'", pos);
765 name_list_add(&udev->symlink_list, pos, 0);
767 next = strchr(pos, ' ');
769 dbg("add symlink '%s'", pos);
770 name_list_add(&udev->symlink_list, pos, 0);
774 if (dev->name[0] != '\0') {
775 info("configured rule in '%s[%i]' applied, '%s' becomes '%s'",
776 dev->config_file, dev->config_line, udev->kernel_name, dev->name);
778 strfieldcpy(udev->name, dev->name);
779 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
780 strfieldcpy(udev->config_file, dev->config_file);
781 udev->config_line = dev->config_line;
783 if (udev->type != NET)
784 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
785 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
792 if (udev->name[0] == '\0') {
793 /* no rule matched, so we use the kernel name */
794 strfieldcpy(udev->name, udev->kernel_name);
795 dbg("no rule found, use kernel name '%s'", udev->name);
798 if (udev->tmp_node[0] != '\0') {
799 dbg("removing temporary device node");
800 unlink_secure(udev->tmp_node);
801 udev->tmp_node[0] = '\0';