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;
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);
205 if (strlen(udev->devpath) == 0)
207 strfieldcatmax(string, udev->devpath, maxsize);
208 dbg("substitute kernel name '%s'", udev->kernel_name);
211 if (strlen(udev->bus_id) == 0)
213 strfieldcatmax(string, udev->bus_id, maxsize);
214 dbg("substitute bus_id '%s'", udev->bus_id);
217 if (strlen(udev->kernel_name) == 0)
219 strfieldcatmax(string, udev->kernel_name, maxsize);
220 dbg("substitute kernel name '%s'", udev->kernel_name);
223 if (strlen(udev->kernel_number) == 0)
225 strfieldcatmax(string, udev->kernel_number, maxsize);
226 dbg("substitute kernel number '%s'", udev->kernel_number);
229 strintcatmax(string, udev->minor, maxsize);
230 dbg("substitute minor number '%u'", udev->minor);
233 strintcatmax(string, udev->major, maxsize);
234 dbg("substitute major number '%u'", udev->major);
237 if (strlen(udev->program_result) == 0)
239 /* get part part of the result string */
242 i = strtoul(attr, &rest, 10);
244 foreach_strpart(udev->program_result, " \n\r", spos, slen) {
250 dbg("requested part of result string not found");
254 strfieldcpy(temp2, spos);
256 strfieldcpymax(temp2, spos, slen+1);
257 strfieldcatmax(string, temp2, maxsize);
258 dbg("substitute part of result string '%s'", temp2);
260 strfieldcatmax(string, udev->program_result, maxsize);
261 dbg("substitute result string '%s'", udev->program_result);
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 strfieldcatmax(string, tmpattr->value, maxsize);
283 dbg("substitute sysfs value '%s'", tmpattr->value);
285 dbg("missing attribute");
289 strfieldcatmax(string, "%", maxsize);
293 next_free_number = find_free_number(udev, string);
294 if (next_free_number > 0) {
295 sprintf(temp2, "%d", next_free_number);
296 strfieldcatmax(string, temp2, maxsize);
300 if (udev->tmp_node[0] == '\0') {
301 dbg("create temporary device node for callout");
302 snprintf(udev->tmp_node, NAME_SIZE-1, "%s/.tmp-%u-%u", udev_root, udev->major, udev->minor);
303 udev_make_node(udev, udev->tmp_node, udev->major, udev->minor, 0600, 0, 0);
305 strfieldcatmax(string, udev->tmp_node, maxsize);
306 dbg("substitute temporary device node name '%s'", udev->tmp_node);
309 dbg("unknown substitution type '%%%c'", c);
312 /* truncate to specified length */
316 strfieldcatmax(string, tail, maxsize);
320 static void fix_kernel_name(struct udevice *udev)
322 char *temp = udev->kernel_name;
324 while (*temp != 0x00) {
325 /* Some block devices have a ! in their name,
326 * we need to change that to / */
333 static int execute_program(struct udevice *udev, const char *path, char *value, int len)
341 char arg[PROGRAM_SIZE];
342 char *argv[(PROGRAM_SIZE / 2) + 1];
345 strfieldcpy(arg, path);
347 if (strchr(path, ' ')) {
349 while (pos != NULL) {
350 if (pos[0] == '\'') {
351 /* don't separate if in apostrophes */
353 argv[i] = strsep(&pos, "\'");
354 while (pos && pos[0] == ' ')
357 argv[i] = strsep(&pos, " ");
359 dbg("arg[%i] '%s'", i, argv[i]);
363 dbg("execute '%s' with parsed arguments", arg);
366 argv[1] = udev->subsystem;
368 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
381 /* dup2 write side of pipe to STDOUT */
382 dup2(fds[1], STDOUT_FILENO);
383 retval = execv(arg, argv);
385 info(FIELD_PROGRAM " execution of '%s' failed", path);
391 /* parent reads from fds[0] */
396 count = read(fds[0], value + i, len - i-1);
402 dbg("result len %d too short", len);
409 dbg("read failed with '%s'", strerror(errno));
413 if (i > 0 && value[i-1] == '\n')
416 dbg("result is '%s'", value);
419 waitpid(pid, &status, 0);
421 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
422 dbg("exec program status 0x%x", status);
429 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
431 struct sysfs_attribute *tmpattr = NULL;
434 dbg("look for device attribute '%s'", attr);
435 /* try to find the attribute in the class device directory */
436 tmpattr = sysfs_get_classdev_attr(class_dev, attr);
440 /* look in the class device directory if present */
442 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
450 c = strchr(tmpattr->value, '\n');
454 dbg("found attribute '%s'", tmpattr->path);
458 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
460 struct sysfs_attribute *tmpattr;
464 if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
467 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
471 /* strip trailing whitespace of value, if not asked to match for it */
472 if (! isspace(pair->value[strlen(pair->value)-1])) {
473 i = len = strlen(tmpattr->value);
474 while (i > 0 && isspace(tmpattr->value[i-1]))
477 tmpattr->value[i] = '\0';
478 dbg("remove %i trailing whitespace chars from '%s'",
479 len - i, tmpattr->value);
483 dbg("compare attribute '%s' value '%s' with '%s'",
484 pair->file, tmpattr->value, pair->value);
485 if (strcmp_pattern(pair->value, tmpattr->value) != 0)
488 dbg("found matching attribute '%s' with value '%s'",
489 pair->file, pair->value);
493 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
495 struct sysfs_pair *pair;
498 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
499 pair = &dev->sysfs_pair[i];
500 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
502 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
503 dbg("sysfs attribute doesn't match");
511 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
513 char path[SYSFS_PATH_MAX];
516 /* we have to have a sysfs device for ID to work */
520 strfieldcpy(path, sysfs_device->path);
521 temp = strrchr(path, '/');
523 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
524 if (strcmp_pattern(dev->id, temp) != 0)
530 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
532 char path[SYSFS_PATH_MAX];
536 /* we have to have a sysfs device for PLACE to work */
541 strfieldcpy(path, sysfs_device->path);
542 temp = strrchr(path, '/');
543 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
544 if (strstr(temp, dev->place) != NULL) {
548 temp = strrchr(path, '/');
549 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
550 if (strstr(temp, dev->place) != NULL)
554 dbg("place doesn't match");
561 static int match_rule(struct udevice *udev, struct config_device *dev,
562 struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
565 /* check for matching bus value */
566 if (dev->bus[0] != '\0') {
567 if (sysfs_device == NULL) {
568 dbg("device has no bus");
571 dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'",
572 dev->bus, sysfs_device->bus);
573 if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
574 dbg(FIELD_BUS " is not matching");
577 dbg(FIELD_BUS " matches");
581 /* check for matching kernel name */
582 if (dev->kernel[0] != '\0') {
583 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'",
584 dev->kernel, class_dev->name);
585 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
586 dbg(FIELD_KERNEL " is not matching");
589 dbg(FIELD_KERNEL " matches");
593 /* check for matching subsystem */
594 if (dev->subsystem[0] != '\0') {
595 dbg("check for " FIELD_SUBSYSTEM " dev->subsystem='%s' class_dev->name='%s'",
596 dev->subsystem, class_dev->name);
597 if (strcmp_pattern(dev->subsystem, udev->subsystem) != 0) {
598 dbg(FIELD_SUBSYSTEM " is not matching");
601 dbg(FIELD_SUBSYSTEM " matches");
605 /* check for matching driver */
606 if (dev->driver[0] != '\0') {
607 dbg("check for " FIELD_DRIVER " dev->driver='%s' sysfs_device->driver_name='%s'",
608 dev->driver, sysfs_device->driver_name);
609 if (strcmp_pattern(dev->driver, sysfs_device->driver_name) != 0) {
610 dbg(FIELD_DRIVER " is not matching");
613 dbg(FIELD_DRIVER " matches");
617 /* check for matching bus id */
618 if (dev->id[0] != '\0') {
619 dbg("check " FIELD_ID);
620 if (match_id(dev, class_dev, sysfs_device) != 0) {
621 dbg(FIELD_ID " is not matching");
624 dbg(FIELD_ID " matches");
628 /* check for matching place of device */
629 if (dev->place[0] != '\0') {
630 dbg("check " FIELD_PLACE);
631 if (match_place(dev, class_dev, sysfs_device) != 0) {
632 dbg(FIELD_PLACE " is not matching");
635 dbg(FIELD_PLACE " matches");
639 /* check for matching sysfs pairs */
640 if (dev->sysfs_pair[0].file[0] != '\0') {
641 dbg("check " FIELD_SYSFS " pairs");
642 if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
643 dbg(FIELD_SYSFS " is not matching");
646 dbg(FIELD_SYSFS " matches");
650 /* execute external program */
651 if (dev->program[0] != '\0') {
652 char program[PROGRAM_SIZE];
654 dbg("check " FIELD_PROGRAM);
655 strfieldcpy(program, dev->program);
656 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
657 if (execute_program(udev, program, udev->program_result, NAME_SIZE) != 0) {
658 dbg(FIELD_PROGRAM " returned nonzero");
661 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");
677 /* Yeah, we matched! */
681 dbg("try parent sysfs device");
682 sysfs_device = sysfs_get_device_parent(sysfs_device);
683 if (sysfs_device == NULL)
685 dbg("sysfs_device->path='%s'", sysfs_device->path);
686 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
690 int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_dev)
692 struct sysfs_class_device *class_dev_parent;
693 struct sysfs_device *sysfs_device = NULL;
694 struct config_device *dev;
697 dbg("class_dev->name='%s'", class_dev->name);
699 /* Figure out where the "device"-symlink is at. For char devices this will
700 * always be in the class_dev->path. On block devices, only the main block
701 * device will have the device symlink in it's path. All partition devices
702 * need to look at the symlink in its parent directory.
704 class_dev_parent = sysfs_get_classdev_parent(class_dev);
705 if (class_dev_parent != NULL) {
706 dbg("given class device has a parent, use this instead");
707 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
709 sysfs_device = sysfs_get_classdev_device(class_dev);
713 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
714 sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
715 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
718 strfieldcpy(udev->kernel_name, class_dev->name);
719 fix_kernel_name(udev);
720 dbg("udev->kernel_name = '%s'", udev->kernel_name);
722 /* get kernel number */
723 pos = class_dev->name + strlen(class_dev->name);
724 while (isdigit(*(pos-1)))
726 strfieldcpy(udev->kernel_number, pos);
727 dbg("kernel_number='%s'", udev->kernel_number);
729 /* look for a matching rule to apply */
730 list_for_each_entry(dev, &config_device_list, node) {
732 if (match_rule(udev, dev, class_dev, sysfs_device) == 0) {
734 /* empty name, symlink and perms will not create any node */
735 if (dev->name[0] == '\0' && dev->symlink[0] == '\0' &&
736 dev->mode == 0000 && dev->owner[0] == '\0' && dev->group[0] == '\0') {
737 info("configured rule in '%s[%i]' applied, '%s' is ignored",
738 dev->config_file, dev->config_line, udev->kernel_name);
742 /* apply permissions */
743 if (dev->mode != 0000) {
744 udev->mode = dev->mode;
745 dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
747 if (dev->owner[0] != '\0') {
748 strfieldcpy(udev->owner, dev->owner);
749 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
750 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
752 if (dev->group[0] != '\0') {
753 strfieldcpy(udev->group, dev->group);
754 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
755 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
758 /* collect symlinks for this or the final matching rule */
759 if (dev->symlink[0] != '\0') {
760 char temp[NAME_SIZE];
762 info("configured rule in '%s[%i]' applied, added symlink '%s'",
763 dev->config_file, dev->config_line, dev->symlink);
764 strfieldcpy(temp, dev->symlink);
765 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
766 if (udev->symlink[0] != '\0')
767 strfieldcat(udev->symlink, " ");
768 strfieldcat(udev->symlink, temp);
772 if (dev->name[0] != '\0') {
773 /* apply all_partitions flag only at a main block device */
774 if (dev->partitions > 0 &&
775 (udev->type != 'b' || udev->kernel_number[0] != '\0'))
778 info("configured rule in '%s[%i]' applied, '%s' becomes '%s'",
779 dev->config_file, dev->config_line, udev->kernel_name, dev->name);
781 strfieldcpy(udev->name, dev->name);
782 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
783 strfieldcpy(udev->config_file, dev->config_file);
784 udev->config_line = dev->config_line;
785 udev->ignore_remove = dev->ignore_remove;
787 if (udev->type == 'n')
790 udev->partitions = dev->partitions;
792 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
793 udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
800 /* no rule matched, so we use the kernel name */
801 strfieldcpy(udev->name, udev->kernel_name);
802 dbg("no rule found, use kernel name '%s'", udev->name);
805 if (udev->tmp_node[0] != '\0') {
806 dbg("removing temporary device node");
807 unlink_secure(udev->tmp_node);
808 udev->tmp_node[0] = '\0';