6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include "libsysfs/sysfs/libsysfs.h"
38 #include "udev_utils.h"
39 #include "udev_version.h"
44 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
46 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
47 static int strcmp_pattern(const char *p, const char *s)
52 return (p[0] != '\0');
63 while ((p[0] != '\0') && (p[0] != ']')) {
66 if ((s[0] >= p[0]) && (s[0] <= p[2]))
70 match = (p[0] == s[0]);
74 while ((p[0] != '\0') && (p[0] != ']'))
77 return strcmp_pattern(p+1, s+1);
83 if (strcmp_pattern(p, s+1))
84 return strcmp_pattern(p+1, s);
92 if ((p[0] == s[0]) || (p[0] == '?'))
93 return strcmp_pattern(p+1, s+1);
99 /* extract possible {attr} and move str behind it */
100 static char *get_format_attribute(char **str)
105 if (*str[0] == '{') {
106 pos = strchr(*str, '}');
108 dbg("missing closing brace for format");
114 dbg("attribute='%s', str='%s'", attr, *str);
119 /* extract possible format length and move str behind it*/
120 static int get_format_len(char **str)
125 if (isdigit(*str[0])) {
126 num = (int) strtoul(*str, &tail, 10);
129 dbg("format length=%i", num);
132 dbg("format parsing error '%s'", *str);
138 /** Finds the lowest positive N such that <name>N isn't present in
139 * $(udevroot) either as a file or a symlink.
141 * @param name Name to check for
142 * @return 0 if <name> didn't exist and N otherwise.
144 static int find_free_number(struct udevice *udev, const char *name)
146 char filename[NAME_SIZE];
148 struct udevice db_udev;
150 strfieldcpy(filename, name);
152 dbg("look for existing node '%s'", filename);
153 memset(&db_udev, 0x00, sizeof(struct udevice));
154 if (udev_db_get_device_byname(&db_udev, 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,
171 struct sysfs_device *sysfs_device)
173 char temp[NAME_SIZE];
174 char temp2[NAME_SIZE];
184 struct sysfs_attribute *tmpattr;
185 unsigned int next_free_number;
186 struct sysfs_class_device *class_dev_parent;
190 pos = strchr(pos, '%');
196 len = get_format_len(&tail);
198 strfieldcpy(temp, tail+1);
200 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
201 attr = get_format_attribute(&tail);
206 if (strlen(udev->devpath) == 0)
208 strfieldcatmax(string, udev->devpath, maxsize);
209 dbg("substitute kernel name '%s'", udev->kernel_name);
212 if (strlen(udev->bus_id) == 0)
214 strfieldcatmax(string, udev->bus_id, maxsize);
215 dbg("substitute bus_id '%s'", udev->bus_id);
218 if (strlen(udev->kernel_name) == 0)
220 strfieldcatmax(string, udev->kernel_name, maxsize);
221 dbg("substitute kernel name '%s'", udev->kernel_name);
224 if (strlen(udev->kernel_number) == 0)
226 strfieldcatmax(string, udev->kernel_number, maxsize);
227 dbg("substitute kernel number '%s'", udev->kernel_number);
230 strintcatmax(string, udev->minor, maxsize);
231 dbg("substitute minor number '%u'", udev->minor);
234 strintcatmax(string, udev->major, maxsize);
235 dbg("substitute major number '%u'", udev->major);
238 if (strlen(udev->program_result) == 0)
240 /* get part part of the result string */
243 i = strtoul(attr, &rest, 10);
245 foreach_strpart(udev->program_result, " \n\r", spos, slen) {
251 dbg("requested part of result string not found");
255 strfieldcpy(temp2, spos);
257 strfieldcpymax(temp2, spos, slen+1);
258 strfieldcatmax(string, temp2, maxsize);
259 dbg("substitute part of result string '%s'", temp2);
261 strfieldcatmax(string, udev->program_result, maxsize);
262 dbg("substitute result string '%s'", udev->program_result);
267 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
268 if (tmpattr == NULL) {
269 dbg("sysfa attribute '%s' not found", attr);
272 /* strip trailing whitespace of matching value */
273 if (isspace(tmpattr->value[strlen(tmpattr->value)-1])) {
274 i = len = strlen(tmpattr->value);
275 while (i > 0 && isspace(tmpattr->value[i-1]))
278 tmpattr->value[i] = '\0';
279 dbg("remove %i trailing whitespace chars from '%s'",
280 len - i, tmpattr->value);
283 strfieldcatmax(string, tmpattr->value, maxsize);
284 dbg("substitute sysfs value '%s'", tmpattr->value);
286 dbg("missing attribute");
290 strfieldcatmax(string, "%", maxsize);
294 next_free_number = find_free_number(udev, string);
295 if (next_free_number > 0) {
296 sprintf(temp2, "%d", next_free_number);
297 strfieldcatmax(string, temp2, maxsize);
301 class_dev_parent = sysfs_get_classdev_parent(class_dev);
302 if (class_dev_parent != NULL) {
303 struct udevice udev_parent;
305 dbg("found parent '%s', get the node name", class_dev_parent->path);
306 memset(&udev_parent, 0x00, sizeof(struct udevice));
307 /* lookup the name in the udev_db with the DEVPATH of the parent */
308 strfieldcpy(udev_parent.devpath, &class_dev_parent->path[strlen(sysfs_path)]);
309 if (udev_db_get_device(&udev_parent) == 0) {
310 strfieldcatmax(string, udev_parent.name, maxsize);
311 dbg("substitute parent node name'%s'", udev_parent.name);
313 dbg("parent not found in database");
317 if (udev->tmp_node[0] == '\0') {
318 dbg("create temporary device node for callout");
319 snprintf(udev->tmp_node, NAME_SIZE-1, "%s/.tmp-%u-%u", udev_root, udev->major, udev->minor);
320 udev_make_node(udev, udev->tmp_node, udev->major, udev->minor, 0600, 0, 0);
322 strfieldcatmax(string, udev->tmp_node, maxsize);
323 dbg("substitute temporary device node name '%s'", udev->tmp_node);
326 strfieldcatmax(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 strfieldcatmax(string, tail, maxsize);
341 static void fix_kernel_name(struct udevice *udev)
343 char *temp = udev->kernel_name;
345 while (*temp != 0x00) {
346 /* Some block devices have a ! in their name,
347 * we need to change that to / */
354 static int execute_program(struct udevice *udev, const char *path, char *value, int len)
362 char arg[PROGRAM_SIZE];
363 char *argv[(PROGRAM_SIZE / 2) + 1];
366 strfieldcpy(arg, path);
368 if (strchr(path, ' ')) {
370 while (pos != NULL) {
371 if (pos[0] == '\'') {
372 /* don't separate if in apostrophes */
374 argv[i] = strsep(&pos, "\'");
375 while (pos && pos[0] == ' ')
378 argv[i] = strsep(&pos, " ");
380 dbg("arg[%i] '%s'", i, argv[i]);
384 dbg("execute '%s' with parsed arguments", arg);
387 argv[1] = udev->subsystem;
389 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
402 /* dup2 write side of pipe to STDOUT */
403 dup2(fds[1], STDOUT_FILENO);
404 retval = execv(arg, argv);
406 info(FIELD_PROGRAM " execution of '%s' failed", path);
412 /* parent reads from fds[0] */
417 count = read(fds[0], value + i, len - i-1);
423 dbg("result len %d too short", len);
430 dbg("read failed with '%s'", strerror(errno));
434 if (i > 0 && value[i-1] == '\n')
437 dbg("result is '%s'", value);
440 waitpid(pid, &status, 0);
442 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
443 dbg("exec program status 0x%x", status);
450 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
452 struct sysfs_attribute *tmpattr = NULL;
455 dbg("look for device attribute '%s'", attr);
456 /* try to find the attribute in the class device directory */
457 tmpattr = sysfs_get_classdev_attr(class_dev, attr);
461 /* look in the class device directory if present */
463 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
471 c = strchr(tmpattr->value, '\n');
475 dbg("found attribute '%s'", tmpattr->path);
479 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
481 struct sysfs_attribute *tmpattr;
485 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
488 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
492 /* strip trailing whitespace of value, if not asked to match for it */
493 if (! isspace(pair->value[strlen(pair->value)-1])) {
494 i = len = strlen(tmpattr->value);
495 while (i > 0 && isspace(tmpattr->value[i-1]))
498 tmpattr->value[i] = '\0';
499 dbg("remove %i trailing whitespace chars from '%s'",
500 len - i, tmpattr->value);
504 dbg("compare attribute '%s' value '%s' with '%s'",
505 pair->file, tmpattr->value, pair->value);
506 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
509 dbg("found matching attribute '%s' with value '%s'",
510 pair->file, pair->value);
514 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
516 struct sysfs_pair *pair;
519 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
520 pair = &dev->sysfs_pair[i];
521 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
523 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
524 dbg("sysfs attribute doesn't match");
532 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
534 char path[SYSFS_PATH_MAX];
537 /* we have to have a sysfs device for ID to work */
541 strfieldcpy(path, sysfs_device->path);
542 temp = strrchr(path, '/');
544 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
545 if (strcmp_pattern(dev->id, temp) != 0)
551 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
553 char path[SYSFS_PATH_MAX];
557 /* we have to have a sysfs device for PLACE to work */
562 strfieldcpy(path, sysfs_device->path);
563 temp = strrchr(path, '/');
564 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
565 if (strstr(temp, dev->place) != NULL) {
569 temp = strrchr(path, '/');
570 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
571 if (strstr(temp, dev->place) != NULL)
575 dbg("place doesn't match");
582 static int match_rule(struct udevice *udev, struct config_device *dev,
583 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
586 /* check for matching kernel name */
587 if (dev->kernel[0] != '\0') {
588 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'",
589 dev->kernel, class_dev->name);
590 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
591 dbg(FIELD_KERNEL " is not matching");
594 dbg(FIELD_KERNEL " matches");
597 /* check for matching subsystem */
598 if (dev->subsystem[0] != '\0') {
599 dbg("check for " FIELD_SUBSYSTEM " dev->subsystem='%s' class_dev->name='%s'",
600 dev->subsystem, class_dev->name);
601 if (strcmp_pattern(dev->subsystem, udev->subsystem) != 0) {
602 dbg(FIELD_SUBSYSTEM " is not matching");
605 dbg(FIELD_SUBSYSTEM " matches");
608 /* check for matching driver */
609 if (dev->driver[0] != '\0') {
610 dbg("check for " FIELD_DRIVER " dev->driver='%s' sysfs_device->driver_name='%s'",
611 dev->driver, sysfs_device->driver_name);
612 if (strcmp_pattern(dev->driver, sysfs_device->driver_name) != 0) {
613 dbg(FIELD_DRIVER " is not matching");
616 dbg(FIELD_DRIVER " matches");
620 /* check for matching bus value */
621 if (dev->bus[0] != '\0') {
622 if (sysfs_device == NULL) {
623 dbg("device has no bus");
626 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'",
627 dev->bus, sysfs_device->bus);
628 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
629 dbg(FIELD_BUS " is not matching");
632 dbg(FIELD_BUS " matches");
635 /* check for matching bus id */
636 if (dev->id[0] != '\0') {
637 dbg("check " FIELD_ID);
638 if (match_id(dev, class_dev, sysfs_device) != 0) {
639 dbg(FIELD_ID " is not matching");
642 dbg(FIELD_ID " matches");
646 /* check for matching place of device */
647 if (dev->place[0] != '\0') {
648 dbg("check " FIELD_PLACE);
649 if (match_place(dev, class_dev, sysfs_device) != 0) {
650 dbg(FIELD_PLACE " is not matching");
653 dbg(FIELD_PLACE " matches");
657 /* check for matching sysfs pairs */
658 if (dev->sysfs_pair[0].file[0] != '\0') {
659 dbg("check " FIELD_SYSFS " pairs");
660 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
661 dbg(FIELD_SYSFS " is not matching");
664 dbg(FIELD_SYSFS " matches");
668 /* execute external program */
669 if (dev->program[0] != '\0') {
670 char program[PROGRAM_SIZE];
672 dbg("check " FIELD_PROGRAM);
673 strfieldcpy(program, dev->program);
674 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
675 if (execute_program(udev, program, udev->program_result, NAME_SIZE) != 0) {
676 dbg(FIELD_PROGRAM " returned nonzero");
679 dbg(FIELD_PROGRAM " returned successful");
683 /* check for matching result of external program */
684 if (dev->result[0] != '\0') {
685 dbg("check for " FIELD_RESULT " dev->result='%s', udev->program_result='%s'",
686 dev->result, udev->program_result);
687 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
688 dbg(FIELD_RESULT " is not matching");
691 dbg(FIELD_RESULT " matches");
699 dbg("try parent sysfs device");
700 sysfs_device = sysfs_get_device_parent(sysfs_device);
701 if (sysfs_device == NULL)
703 dbg("sysfs_device->path='%s'", sysfs_device->path);
704 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
711 int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_dev)
713 struct sysfs_class_device *class_dev_parent;
714 struct sysfs_device *sysfs_device = NULL;
715 struct config_device *dev;
718 dbg("class_dev->name='%s'", class_dev->name);
720 /* Figure out where the "device"-symlink is at. For char devices this will
721 * always be in the class_dev->path. On block devices, only the main block
722 * device will have the device symlink in it's path. All partition devices
723 * need to look at the symlink in its parent directory.
725 class_dev_parent = sysfs_get_classdev_parent(class_dev);
726 if (class_dev_parent != NULL) {
727 dbg("given class device has a parent, use this instead");
728 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
730 sysfs_device = sysfs_get_classdev_device(class_dev);
734 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
735 sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
736 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
739 strfieldcpy(udev->kernel_name, class_dev->name);
740 fix_kernel_name(udev);
741 dbg("udev->kernel_name = '%s'", udev->kernel_name);
743 /* get kernel number */
744 pos = class_dev->name + strlen(class_dev->name);
745 while (isdigit(*(pos-1)))
747 strfieldcpy(udev->kernel_number, pos);
748 dbg("kernel_number='%s'", udev->kernel_number);
750 /* look for a matching rule to apply */
751 list_for_each_entry(dev, &config_device_list, node) {
753 if (match_rule(udev, dev, class_dev, sysfs_device) == 0) {
755 /* empty name, symlink and perms will not create any node */
756 if (dev->name[0] == '\0' && dev->symlink[0] == '\0' &&
757 dev->mode == 0000 && dev->owner[0] == '\0' && dev->group[0] == '\0') {
758 info("configured rule in '%s[%i]' applied, '%s' is ignored",
759 dev->config_file, dev->config_line, udev->kernel_name);
763 /* apply permissions */
764 if (dev->mode != 0000) {
765 udev->mode = dev->mode;
766 dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
768 if (dev->owner[0] != '\0') {
769 strfieldcpy(udev->owner, dev->owner);
770 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
771 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
773 if (dev->group[0] != '\0') {
774 strfieldcpy(udev->group, dev->group);
775 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
776 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
779 /* collect symlinks for this or the final matching rule */
780 if (dev->symlink[0] != '\0') {
781 char temp[NAME_SIZE];
783 info("configured rule in '%s[%i]' applied, added symlink '%s'",
784 dev->config_file, dev->config_line, dev->symlink);
785 strfieldcpy(temp, dev->symlink);
786 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
787 if (udev->symlink[0] != '\0')
788 strfieldcat(udev->symlink, " ");
789 strfieldcat(udev->symlink, temp);
793 if (dev->name[0] != '\0') {
794 /* apply all_partitions flag only at a main block device */
795 if (dev->partitions > 0 &&
796 (udev->type != 'b' || udev->kernel_number[0] != '\0'))
799 info("configured rule in '%s[%i]' applied, '%s' becomes '%s'",
800 dev->config_file, dev->config_line, udev->kernel_name, dev->name);
802 strfieldcpy(udev->name, dev->name);
803 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
804 strfieldcpy(udev->config_file, dev->config_file);
805 udev->config_line = dev->config_line;
806 udev->ignore_remove = dev->ignore_remove;
808 if (udev->type == 'n')
811 udev->partitions = dev->partitions;
813 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
814 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
821 /* no rule matched, so we use the kernel name */
822 strfieldcpy(udev->name, udev->kernel_name);
823 dbg("no rule found, use kernel name '%s'", udev->name);
826 if (udev->tmp_node[0] != '\0') {
827 dbg("removing temporary device node");
828 unlink_secure(udev->tmp_node);
829 udev->tmp_node[0] = '\0';